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 "solidwakeupbackend.h"
0008 #include <QDBusInterface>
0009 #include <QDBusConnection>
0010 #include <QDBusReply>
0011 #include <QDBusServiceWatcher>
0012 #include <QDebug>
0013 #include <QDateTime>
0014 
0015 SolidWakeupBackend::SolidWakeupBackend(QObject *parent) : WakeupBackend(parent)
0016 {
0017     auto svcName = QStringLiteral("org.kde.Solid.PowerManagement");
0018 
0019     m_interface = new QDBusInterface {svcName,  QStringLiteral("/org/kde/Solid/PowerManagement"), QStringLiteral("org.kde.Solid.PowerManagement"), QDBusConnection::sessionBus(), this};
0020     m_watcher = new QDBusServiceWatcher {svcName, QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration};
0021 
0022     connect(m_watcher, &QDBusServiceWatcher::serviceRegistered, [this]() {
0023         Q_EMIT backendChanged(true);
0024     });
0025     connect(m_watcher, &QDBusServiceWatcher::serviceUnregistered, [this]() {
0026         Q_EMIT backendChanged(false);
0027     });
0028 }
0029 
0030 void SolidWakeupBackend::clearWakeup(const QVariant &scheduledWakeup)
0031 {
0032     m_interface->call(QStringLiteral("clearWakeup"), scheduledWakeup.toInt());
0033 }
0034 
0035 QVariant SolidWakeupBackend::scheduleWakeup(const QVariantMap &callbackInfo, const quint64 wakeupAt)
0036 {
0037     auto scheduledAt = QDateTime::fromSecsSinceEpoch(wakeupAt);
0038 
0039     qDebug() << "SolidWakeupBackend::scheduleWakeup at" << scheduledAt.toString(QStringLiteral("dd.MM.yyyy hh:mm:ss")) << "tz " << scheduledAt.timeZoneAbbreviation() << " epoch" << wakeupAt;
0040 
0041     QDBusReply<uint> reply = m_interface->call(QStringLiteral("scheduleWakeup"), callbackInfo[QStringLiteral("dbus-service")].toString(), QDBusObjectPath(callbackInfo[QStringLiteral("dbus-path")].toString()), wakeupAt);
0042     if (reply.isValid()) {
0043         return reply.value();
0044     }
0045 
0046     return 0;
0047 }
0048 
0049 bool SolidWakeupBackend::isValid()
0050 {
0051     if (m_interface != nullptr) {
0052         return m_interface->isValid();
0053     }
0054 
0055     return false;
0056 }
0057 
0058 bool SolidWakeupBackend::isWakeupBackend()
0059 {
0060     auto callMessage = QDBusMessage::createMethodCall(m_interface->service(), m_interface->path(), QStringLiteral("org.freedesktop.DBus.Introspectable"), QStringLiteral("Introspect"));
0061     QDBusReply<QString> result = QDBusConnection::sessionBus().call(callMessage);
0062 
0063     if (result.isValid() && result.value().indexOf(QStringLiteral("scheduleWakeup")) >= 0) {
0064         return true;
0065     }
0066 
0067     return false;
0068 
0069 }