-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
powermanagement_inhibition.cpp
98 lines (86 loc) · 4.28 KB
/
powermanagement_inhibition.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
SPDX-FileCopyrightText: 2016 Martin Gräßlin <[email protected]>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "powermanagement_inhibition.h"
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusMetaType>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
#include <QDBusServiceWatcher>
static const QString s_solidPowerService = QStringLiteral("org.kde.Solid.PowerManagement.PolicyAgent");
static const QString s_solidPath = QStringLiteral("/org/kde/Solid/PowerManagement/PolicyAgent");
Q_DECLARE_METATYPE(QList<InhibitionInfo>)
Q_DECLARE_METATYPE(InhibitionInfo)
PowerManagementInhibition::PowerManagementInhibition(QObject *parent)
: QObject(parent)
, m_solidPowerServiceWatcher(new QDBusServiceWatcher(s_solidPowerService,
QDBusConnection::sessionBus(),
QDBusServiceWatcher::WatchForUnregistration | QDBusServiceWatcher::WatchForRegistration))
{
qDBusRegisterMetaType<QList<InhibitionInfo>>();
qDBusRegisterMetaType<InhibitionInfo>();
connect(m_solidPowerServiceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, [this] {
m_serviceRegistered = false;
m_inhibited = false;
QDBusConnection::sessionBus().disconnect(s_solidPowerService,
s_solidPath,
s_solidPowerService,
QStringLiteral("InhibitionsChanged"),
this,
SLOT(inhibitionsChanged(QList<InhibitionInfo>, QStringList)));
});
connect(m_solidPowerServiceWatcher, &QDBusServiceWatcher::serviceRegistered, this, &PowerManagementInhibition::update);
// check whether the service is registered
QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.DBus"),
QStringLiteral("/"),
QStringLiteral("org.freedesktop.DBus"),
QStringLiteral("ListNames"));
QDBusPendingReply<QStringList> async = QDBusConnection::sessionBus().asyncCall(message);
QDBusPendingCallWatcher *callWatcher = new QDBusPendingCallWatcher(async, this);
connect(callWatcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *self) {
QDBusPendingReply<QStringList> reply = *self;
self->deleteLater();
if (!reply.isValid()) {
return;
}
if (reply.value().contains(s_solidPowerService)) {
update();
}
});
}
PowerManagementInhibition::~PowerManagementInhibition() = default;
void PowerManagementInhibition::update()
{
m_serviceRegistered = true;
QDBusConnection::sessionBus().connect(s_solidPowerService,
s_solidPath,
s_solidPowerService,
QStringLiteral("InhibitionsChanged"),
this,
SLOT(inhibitionsChanged(QList<InhibitionInfo>, QStringList)));
checkInhibition();
}
void PowerManagementInhibition::inhibitionsChanged(const QList<InhibitionInfo> &added, const QStringList &removed)
{
Q_UNUSED(added)
Q_UNUSED(removed)
checkInhibition();
}
void PowerManagementInhibition::checkInhibition()
{
QDBusMessage msg = QDBusMessage::createMethodCall(s_solidPowerService, s_solidPath, s_solidPowerService, QStringLiteral("HasInhibition"));
msg << (uint)4; // PowerDevil::PolicyAgent::RequiredPolicy::ChangeScreenSettings
QDBusPendingReply<bool> pendingReply = QDBusConnection::sessionBus().asyncCall(msg);
QDBusPendingCallWatcher *callWatcher = new QDBusPendingCallWatcher(pendingReply, this);
connect(callWatcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *self) {
QDBusPendingReply<bool> reply = *self;
self->deleteLater();
if (!reply.isValid()) {
return;
}
m_inhibited = reply.value();
});
}
#include "moc_powermanagement_inhibition.cpp"