File indexing completed on 2024-04-21 05:50:56

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Dimitris Kardarakos <dimkard@posteo.net>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "wakeupmanager.h"
0008 #include "solidwakeupbackend.h"
0009 #include "powermanagementadaptor.h"
0010 #include <QDBusConnection>
0011 #include <QDebug>
0012 
0013 WakeupManager::WakeupManager(QObject *parent) : QObject {parent}, m_cookie {-1}
0014 {
0015     m_callback_info = QVariantMap {
0016         {
0017             {"dbus-service", QString { "org.kde.kongressac"} },
0018             {"dbus-path", QString {"/wakeupmanager"} }
0019         }
0020     };
0021 
0022     new PowerManagementAdaptor {this};
0023     m_wakeup_backend = new SolidWakeupBackend {this};
0024 
0025     auto dbus = QDBusConnection::sessionBus();
0026     dbus.registerObject(m_callback_info["dbus-path"].toString(), this);
0027 }
0028 
0029 void WakeupManager::scheduleWakeup(const QDateTime wakeupAt)
0030 {
0031     if (wakeupAt <= QDateTime::currentDateTime()) {
0032         qDebug() << "WakeupManager:" << "Requested to schedule wake up at" << wakeupAt.toString("dd.MM.yyyy hh:mm:ss") << "Can't chedule a wakeup in the past";
0033         return;
0034     }
0035 
0036     auto scheduledCookie = m_wakeup_backend->scheduleWakeup(m_callback_info, wakeupAt.toSecsSinceEpoch()).toInt();
0037 
0038     if (scheduledCookie > 0) {
0039         qDebug() << "WakeupManager: wake up has been scheduled, wakeup time:" << wakeupAt.toString("dd.MM.yyyy hh:mm:ss") << "Received cookie" << scheduledCookie;
0040 
0041         if (m_cookie > 0) {
0042             removeWakeup(m_cookie);
0043         }
0044 
0045         m_cookie = scheduledCookie;
0046     }
0047 
0048 }
0049 
0050 void WakeupManager::wakeupCallback(const int cookie)
0051 {
0052     qDebug() << "WakeupManager: awaken by cookie" << cookie;
0053 
0054     if (m_cookie == cookie) {
0055         m_cookie = -1;
0056 
0057         Q_EMIT wakeupAlarmClient();
0058     } else {
0059         qDebug() << "WakeupManager: the cookie is invalid";
0060     }
0061 }
0062 
0063 void WakeupManager::removeWakeup(const int cookie)
0064 {
0065     qDebug() << "WakeupManager: clearing cookie" << cookie;
0066 
0067     m_wakeup_backend->clearWakeup(cookie);
0068     m_cookie = -1;
0069 }
0070 
0071 bool WakeupManager::active() const
0072 {
0073     return m_wakeup_backend->isWakeupBackend();
0074 }
0075 
0076 #include "moc_wakeupmanager.cpp"