File indexing completed on 2024-05-05 05:28:19
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 <QDBusInterface> 0011 #include <QDBusConnection> 0012 #include <QDBusReply> 0013 #include <QDebug> 0014 0015 WakeupManager::WakeupManager(QObject *parent) : QObject(parent), m_wakeup_backend {new SolidWakeupBackend {this}}, m_cookie {-1}, m_active {m_wakeup_backend->isValid()} 0016 { 0017 m_callback_info = QVariantMap({ 0018 {QStringLiteral("dbus-service"), QStringLiteral("org.kde.calindac")}, 0019 {QStringLiteral("dbus-path"), QStringLiteral("/wakeupmanager")} 0020 }); 0021 0022 new PowerManagementAdaptor(this); 0023 0024 QDBusConnection dbus = QDBusConnection::sessionBus(); 0025 dbus.registerObject(m_callback_info[QStringLiteral("dbus-path")].toString(), this); 0026 0027 connect(m_wakeup_backend, &SolidWakeupBackend::backendChanged, this, &WakeupManager::setActive); 0028 } 0029 0030 void WakeupManager::scheduleWakeup(const QDateTime wakeupAt) 0031 { 0032 if (wakeupAt <= QDateTime::currentDateTime()) { 0033 qDebug() << "WakeupManager:" << "Requested to schedule wake up at" << wakeupAt.toString(QStringLiteral("dd.MM.yyyy hh:mm:ss")) << "Can't chedule a wakeup in the past"; 0034 return; 0035 } 0036 0037 auto scheduledCookie = m_wakeup_backend->scheduleWakeup(m_callback_info, wakeupAt.toSecsSinceEpoch()).toInt(); 0038 0039 if (scheduledCookie > 0) { 0040 qDebug() << "WakeupManager: wake up has been scheduled, wakeup time:" << wakeupAt.toString(QStringLiteral("dd.MM.yyyy hh:mm:ss")) << "Received cookie" << scheduledCookie; 0041 0042 if (m_cookie > 0 && m_cookie != scheduledCookie) { 0043 removeWakeup(m_cookie); 0044 } 0045 0046 m_cookie = scheduledCookie; 0047 } 0048 } 0049 0050 void WakeupManager::wakeupCallback(int cookie) 0051 { 0052 qDebug() << "WakeupManager: awaken by cookie" << cookie; 0053 0054 if (m_cookie == cookie) { 0055 m_cookie = -1; 0056 Q_EMIT wakeupAlarmClient(); 0057 } else { 0058 qDebug() << "WakeupManager: the cookie is invalid"; 0059 } 0060 } 0061 0062 void WakeupManager::removeWakeup(int cookie) 0063 { 0064 qDebug() << "WakeupManager: clearing cookie" << cookie; 0065 0066 m_wakeup_backend->clearWakeup(cookie); 0067 m_cookie = -1; 0068 } 0069 0070 void WakeupManager::removeWakeup() 0071 { 0072 if (m_cookie > 0) { 0073 removeWakeup(m_cookie); 0074 } 0075 } 0076 0077 bool WakeupManager::hasWakeupFeatures() 0078 { 0079 if (m_wakeup_backend->isWakeupBackend()) { 0080 qDebug() << "WakeupManager: the backend is active"; 0081 return true; 0082 } else { 0083 qDebug() << "WakeupManager: the backend does not offer wake up features"; 0084 return false; 0085 } 0086 } 0087 0088 bool WakeupManager::active() const 0089 { 0090 return m_active; 0091 } 0092 0093 void WakeupManager::setActive(const bool activeBackend) 0094 { 0095 if (activeBackend != m_active) { 0096 m_active = activeBackend; 0097 Q_EMIT activeChanged(activeBackend); 0098 } 0099 }