Plasma5Support

powermanagementjob.cpp
1/*
2 SPDX-FileCopyrightText: 2011 Sebastian Kügler <sebas@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-only
5*/
6
7#include <QCoreApplication>
8#include <QDBusInterface>
9#include <QDBusMessage>
10#include <QDBusReply>
11
12#include "powermanagementjob.h"
13
14PowerManagementJob::PowerManagementJob(const QString &operation, QMap<QString, QVariant> &parameters, QObject *parent)
15 : ServiceJob(parent->objectName(), operation, parameters, parent)
16{
17}
18
19PowerManagementJob::~PowerManagementJob()
20{
21}
22
23static void callWhenFinished(const QDBusPendingCall &pending, std::function<void(bool)> func, QObject *parent)
24{
25 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pending, parent);
27 watcher->deleteLater();
28 func(!watcher->isError());
29 });
30}
31
32void PowerManagementJob::start()
33{
34 const QString operation = operationName();
35 // qDebug() << "starting operation ... " << operation;
36 if (operation == QLatin1String("beginSuppressingSleep")) {
37 if (m_sleepInhibitionCookie != -1) { // an inhibition request is already active; don't trigger another one
38 setResult(true);
39 return;
40 }
41 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
42 QStringLiteral("/org/freedesktop/PowerManagement/Inhibit"),
43 QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
44 QStringLiteral("Inhibit"));
45 msg << QCoreApplication::applicationName() << parameters().value(QStringLiteral("reason")).toString();
47 m_sleepInhibitionCookie = reply.isValid() ? reply.value() : -1;
48 setResult(reply.isValid());
49 if (reply.isValid() && !parameters().value(QStringLiteral("silent")).toBool()) {
50 QDBusMessage osdMsg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.plasmashell"),
51 QStringLiteral("/org/kde/osdService"),
52 QStringLiteral("org.kde.osdService"),
53 QStringLiteral("powerManagementInhibitedChanged"));
54 osdMsg << true;
56 }
57 return;
58 } else if (operation == QLatin1String("stopSuppressingSleep")) {
59 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
60 QStringLiteral("/org/freedesktop/PowerManagement/Inhibit"),
61 QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
62 QStringLiteral("UnInhibit"));
63 msg << m_sleepInhibitionCookie;
65 m_sleepInhibitionCookie = reply.isValid() ? -1 : m_sleepInhibitionCookie; // reset cookie if the stop request was successful
66 setResult(reply.isValid());
67 if (reply.isValid() && !parameters().value(QStringLiteral("silent")).toBool()) {
68 QDBusMessage osdMsg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.plasmashell"),
69 QStringLiteral("/org/kde/osdService"),
70 QStringLiteral("org.kde.osdService"),
71 QStringLiteral("powerManagementInhibitedChanged"));
72 osdMsg << false;
74 }
75 return;
76 } else if (operation == QLatin1String("beginSuppressingScreenPowerManagement")) {
77 if (m_lockInhibitionCookie != -1) { // an inhibition request is already active; don't trigger another one
78 setResult(true);
79 return;
80 }
81 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.ScreenSaver"),
82 QStringLiteral("/ScreenSaver"),
83 QStringLiteral("org.freedesktop.ScreenSaver"),
84 QStringLiteral("Inhibit"));
85 msg << QCoreApplication::applicationName() << parameters().value(QStringLiteral("reason")).toString();
87 m_lockInhibitionCookie = reply.isValid() ? reply.value() : -1;
88 setResult(reply.isValid());
89 return;
90 } else if (operation == QLatin1String("stopSuppressingScreenPowerManagement")) {
91 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.ScreenSaver"),
92 QStringLiteral("/ScreenSaver"),
93 QStringLiteral("org.freedesktop.ScreenSaver"),
94 QStringLiteral("UnInhibit"));
95 msg << m_lockInhibitionCookie;
97 m_lockInhibitionCookie = reply.isValid() ? -1 : m_lockInhibitionCookie; // reset cookie if the stop request was successful
98 setResult(reply.isValid());
99 return;
100 } else if (operation == QLatin1String("setPowerProfile")) {
101 auto pending = setPowerProfile(parameters().value(QStringLiteral("profile")).toString());
102 callWhenFinished(
103 pending,
104 [this](bool success) {
105 setResult(success);
106 },
107 this);
108 return;
109 } else if (operation == QLatin1String("showPowerProfileOsd")) {
110 QDBusMessage osdMsg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.plasmashell"),
111 QStringLiteral("/org/kde/osdService"),
112 QStringLiteral("org.kde.osdService"),
113 QStringLiteral("powerProfileChanged"));
114 osdMsg << parameters().value(QStringLiteral("profile")).toString();
116 }
117
118 qDebug() << "don't know what to do with " << operation;
119 setResult(false);
120}
121
122QDBusPendingCall PowerManagementJob::setPowerProfile(const QString &value)
123{
124 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.Solid.PowerManagement"),
125 QStringLiteral("/org/kde/Solid/PowerManagement/Actions/PowerProfile"),
126 QStringLiteral("org.kde.Solid.PowerManagement.Actions.PowerProfile"),
127 QStringLiteral("setProfile"));
128 msg << value;
130}
QVariantMap parameters() const
void setResult(const QVariant &result)
Sets the result for an operation.
char * toString(const EngineQuery &query)
QDBusPendingCall asyncCall(const QDBusMessage &message, int timeout) const const
QDBusMessage call(const QDBusMessage &message, QDBus::CallMode mode, int timeout) const const
QDBusConnection sessionBus()
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
void finished(QDBusPendingCallWatcher *self)
bool isValid() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.