File indexing completed on 2024-05-12 05:38:48

0001 /*
0002     SPDX-FileCopyrightText: 2016 Sebastian Kügler <sebas@kde.org>
0003     SPDX-FileCopyrightText: 2022 David Redondo <kde@david-redondo.de>
0004     SPDX-FileCopyrightText: 2023 Natalie Clarius <natalie.clarius@kde.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "osdmanager.h"
0010 #include "osd.h"
0011 #include "osdaction.h"
0012 #include "powerprofileosdserviceadaptor.h"
0013 
0014 #include <QDBusConnection>
0015 #include <QDBusMessage>
0016 
0017 #include <QQmlEngine>
0018 
0019 namespace PowerDevil
0020 {
0021 OsdManager::OsdManager(QObject *parent)
0022     : QObject(parent)
0023 {
0024     qmlRegisterUncreatableMetaObject(PowerDevil::OsdAction::staticMetaObject,
0025                                      "org.kde.powerdevil",
0026                                      1,
0027                                      0,
0028                                      "OsdAction",
0029                                      QStringLiteral("Can't create OsdAction"));
0030     new PowerProfileOsdServiceAdaptor(this);
0031 
0032     // free up memory when the osd hasn't been used for more than 1 minute
0033     m_cleanupTimer->setInterval(60000);
0034     m_cleanupTimer->setSingleShot(true);
0035     connect(m_cleanupTimer, &QTimer::timeout, this, [this]() {
0036         quit();
0037     });
0038     QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/kde/powerdevil/powerProfileOsdService"), this, QDBusConnection::ExportAdaptors);
0039     QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.powerdevil.powerProfileOsdService"));
0040 }
0041 
0042 void OsdManager::hideOsd() const
0043 {
0044     // Let QML engine finish execution of signal handlers, if any.
0045     QTimer::singleShot(0, this, &OsdManager::quit);
0046 }
0047 
0048 void OsdManager::quit()
0049 {
0050     qApp->quit();
0051 }
0052 
0053 void OsdManager::showOsd()
0054 {
0055     QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.Solid.PowerManagement"),
0056                                                           QStringLiteral("/org/kde/Solid/PowerManagement/Actions/PowerProfile"),
0057                                                           QStringLiteral("org.kde.Solid.PowerManagement.Actions.PowerProfile"),
0058                                                           QStringLiteral("currentProfile"));
0059     auto reply = QDBusConnection::sessionBus().call(message);
0060     if (reply.type() == QDBusMessage::ErrorMessage) {
0061         return;
0062     }
0063     QString currentProfile = reply.arguments().first().toString();
0064 
0065     auto osd = new PowerDevil::Osd(this);
0066     osd->showActionSelector(currentProfile);
0067 
0068     connect(osd, &Osd::osdActionSelected, this, [this](QString profile) {
0069         applyProfile(profile);
0070         hideOsd();
0071     });
0072 }
0073 
0074 void OsdManager::applyProfile(const QString &profile)
0075 {
0076     if (profile.isEmpty()) {
0077         return;
0078     }
0079     QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.Solid.PowerManagement"),
0080                                                           QStringLiteral("/org/kde/Solid/PowerManagement/Actions/PowerProfile"),
0081                                                           QStringLiteral("org.kde.Solid.PowerManagement.Actions.PowerProfile"),
0082                                                           QStringLiteral("setProfile"));
0083     message.setArguments({profile});
0084     QDBusConnection::sessionBus().call(message);
0085 }
0086 
0087 } // namespace PowerDevil
0088 
0089 #include "moc_osdmanager.cpp"