Debian Codegraph @

src/systeminfo/linux/qdevicekitservice_linux.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qdevicekitservice_linux_p.h"
41
42 QT_BEGIN_NAMESPACE
43
44 #define UPOWER_SERVICE "org.freedesktop.UPower"
45 #define UPOWER_PATH "/org/freedesktop/UPower"
46 #define UPOWER_DEVICE_SERVICE "org.freedesktop.UPower.Device"
47 #define UPOWER_DEVICE_PATH "/org/freedesktop/UPower/Device"
48
49 QUPowerInterface::QUPowerInterface(QObject *parent)
50 : QDBusAbstractInterface(QLatin1String(UPOWER_SERVICE)
51 , QLatin1String(UPOWER_PATH)
52 , UPOWER_SERVICE
53 , QDBusConnection::systemBus()
54 , parent)
55 {
56 }
57
58 QUPowerInterface::~QUPowerInterface()
59 {
60 }
61
62 QList<QDBusObjectPath> QUPowerInterface::enumerateDevices()
63 {
64 QDBusPendingReply<QList<QDBusObjectPath> > reply = call(QLatin1String("EnumerateDevices"));
65 reply.waitForFinished();
66 if (reply.isError())
67 qDebug() << reply.error().message();
68 return reply.value();
69 }
70
71 void QUPowerInterface::connectNotify(const QMetaMethod &signal)
72 {
73 static const QMetaMethod addedSignal = QMetaMethod::fromSignal(&QUPowerInterface::deviceAdded);
74 static const QMetaMethod removedSignal = QMetaMethod::fromSignal(&QUPowerInterface::deviceRemoved);
75
76 if (signal == addedSignal) {
77 if (!connection().connect(QStringLiteral(UPOWER_SERVICE),
78 QStringLiteral(UPOWER_PATH),
79 QStringLiteral(UPOWER_SERVICE),
80 QStringLiteral("DeviceAdded"),
81 this, SIGNAL(onDeviceAdded(QDBusObjectPath)))) {
82 qDebug() << "Error"<<connection().lastError().message();
83 }
84
85 }
86 if (signal == removedSignal) {
87 if (!connection().connect(QStringLiteral(UPOWER_SERVICE),
88 QStringLiteral(UPOWER_PATH),
89 QStringLiteral(UPOWER_SERVICE),
90 QStringLiteral("DeviceRemoved"),
91 this, SIGNAL(onDeviceRemoved(QDBusObjectPath)))) {
92 qDebug() << "Error"<<connection().lastError().message();
93 }
94 }
95 }
96
97 void QUPowerInterface::disconnectNotify(const QMetaMethod &signal)
98 {
99 static const QMetaMethod addedSignal = QMetaMethod::fromSignal(&QUPowerInterface::deviceAdded);
100 static const QMetaMethod removedSignal = QMetaMethod::fromSignal(&QUPowerInterface::deviceRemoved);
101
102 if (signal == addedSignal) {
103 if (!connection().disconnect(QStringLiteral(UPOWER_SERVICE),
104 QStringLiteral(UPOWER_PATH),
105 QStringLiteral(UPOWER_SERVICE),
106 QStringLiteral("DeviceAdded"),
107 this, SLOT(onDeviceAdded(QDBusObjectPath)))) {
108 qDebug() << "Error"<<connection().lastError().message();
109 }
110
111 }
112 if (signal == removedSignal) {
113 if (!connection().disconnect(QStringLiteral(UPOWER_SERVICE),
114 QStringLiteral(UPOWER_PATH),
115 QStringLiteral(UPOWER_SERVICE),
116 QStringLiteral("DeviceRemoved"),
117 this, SLOT(onDeviceRemoved(QDBusObjectPath)))) {
118 qDebug() << "Error"<<connection().lastError().message();
119 }
120 }
121
122 }
123
124 void QUPowerInterface::onDeviceAdded(const QDBusObjectPath &path)
125 {
126 Q_EMIT deviceAdded(path.path());
127 }
128
129 void QUPowerInterface::onDeviceRemoved(const QDBusObjectPath &path)
130 {
131 Q_EMIT deviceRemoved(path.path());
132 }
133
134 QUPowerDeviceInterface::QUPowerDeviceInterface(const QString &dbusPathName, QObject *parent)
135 : QDBusAbstractInterface(QLatin1String(UPOWER_SERVICE)
136 , dbusPathName
137 , UPOWER_DEVICE_SERVICE
138 , QDBusConnection::systemBus()
139 , parent)
140 {
141 // We make a synchronous properties request here so we have a valid state
142 QDBusMessage propGetMsg = QDBusMessage::createMethodCall(UPOWER_SERVICE, path(), QStringLiteral("org.freedesktop.DBus.Properties"), QLatin1String("GetAll"));
143 QList<QVariant> arguments;
144 arguments << QLatin1String("org.freedesktop.UPower.Device");
145 propGetMsg.setArguments(arguments);
146 QDBusReply<QVariantMap> propReply = QDBusConnection::systemBus().call(propGetMsg);
147
148 if (!propReply.isValid()) {
149 qWarning() << "QUPowerDeviceInterface: error getting properties: " << propReply.error();
150 return;
151 }
152
153 pMap = propReply.value();
154 }
155
156 QUPowerDeviceInterface::~QUPowerDeviceInterface()
157 {
158 }
159
160 quint16 QUPowerDeviceInterface::type()
161 {
162 return pMap.value(QStringLiteral("Type")).toUInt();
163 }
164
165 bool QUPowerDeviceInterface::isPowerSupply()
166 {
167 return pMap.value(QStringLiteral("PowerSupply")).toBool();
168 }
169
170 bool QUPowerDeviceInterface::isOnline()
171 {
172 return pMap.value(QStringLiteral("Online")).toBool();
173 }
174
175 double QUPowerDeviceInterface::currentEnergy()
176 {
177 return pMap.value(QStringLiteral("Energy")).toDouble();
178 }
179
180 double QUPowerDeviceInterface::energyWhenFull()
181 {
182 return pMap.value(QStringLiteral("EnergyFull")).toDouble();
183 }
184
185 double QUPowerDeviceInterface::energyDischargeRate()
186 {
187 return pMap.value(QStringLiteral("EnergyRate")).toDouble();
188 }
189
190 double QUPowerDeviceInterface::voltage()
191 {
192 return pMap.value(QStringLiteral("Voltage")).toDouble();
193 }
194
195 qint64 QUPowerDeviceInterface::timeToFull()
196 {
197 return pMap.value(QStringLiteral("TimeToFull")).toUInt();
198 }
199
200 double QUPowerDeviceInterface::percentLeft()
201 {
202 return pMap.value(QStringLiteral("Percentage")).toDouble();
203 }
204
205 quint16 QUPowerDeviceInterface::state()
206 {
207 return pMap.value(QStringLiteral("State")).toUInt();
208 }
209
210 void QUPowerDeviceInterface::connectNotify(const QMetaMethod &signal)
211 {
212 static const QMetaMethod changedSignal = QMetaMethod::fromSignal(&QUPowerDeviceInterface::changed);
213 static const QMetaMethod propertyChangedSignal = QMetaMethod::fromSignal(&QUPowerDeviceInterface::propertyChanged);
214
215 if (signal == changedSignal) {
216 if (!connection().connect(QLatin1String(UPOWER_SERVICE),
217 path(),
218 QLatin1String(UPOWER_DEVICE_SERVICE),
219 QLatin1String("Changed"),
220 this, SIGNAL(changed()))) {
221 qDebug() << "Error" << connection().lastError().message();
222 }
223 }
224 if (signal == propertyChangedSignal) {
225 if (!connection().connect(QLatin1String(UPOWER_SERVICE),
226 path(),
227 QLatin1String(UPOWER_DEVICE_SERVICE),
228 QLatin1String("Changed"),
229 this, SIGNAL(propChanged()))) {
230 qDebug() << "Error" << connection().lastError().message();
231 }
232 }
233 }
234
235 void QUPowerDeviceInterface::disconnectNotify(const QMetaMethod &signal)
236 {
237 static const QMetaMethod changedSignal = QMetaMethod::fromSignal(&QUPowerDeviceInterface::changed);
238 static const QMetaMethod propertyChangedSignal = QMetaMethod::fromSignal(&QUPowerDeviceInterface::propertyChanged);
239 if (signal == changedSignal) {
240 connection().disconnect(QLatin1String(UPOWER_SERVICE),
241 path(),
242 QLatin1String(UPOWER_DEVICE_SERVICE),
243 QLatin1String("Changed"),
244 this, SIGNAL(changed()));
245 }
246 if (signal == propertyChangedSignal) {
247 connection().disconnect(QLatin1String(UPOWER_SERVICE),
248 path(),
249 QLatin1String(UPOWER_DEVICE_SERVICE),
250 QLatin1String("Changed"),
251 this, SIGNAL(propChanged()));
252 }
253 }
254
255 void QUPowerDeviceInterface::propChanged()
256 {
257 QDBusMessage propGetMsg = QDBusMessage::createMethodCall(UPOWER_SERVICE, path(), QStringLiteral("org.freedesktop.DBus.Properties"), QLatin1String("GetAll"));
258 QList<QVariant> arguments;
259 arguments << QLatin1String("org.freedesktop.UPower.Device");
260 propGetMsg.setArguments(arguments);
261 QDBusPendingCall asyncPropReply = QDBusConnection::systemBus().asyncCall(propGetMsg);
262 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(asyncPropReply, this);
263
264 connect(watcher, &QDBusPendingCallWatcher::finished, this, &QUPowerDeviceInterface::propRequestFinished);
265 }
266
267 void QUPowerDeviceInterface::propRequestFinished(QDBusPendingCallWatcher *call)
268 {
269 QDBusPendingReply<QVariantMap> reply = *call;
270 call->deleteLater();
271
272 if (!reply.isValid()) {
273 // don't throw away the existing map if the call fails
274 qWarning() << "QUPowerDeviceInterface: fetching properties failed: " << reply.error();
275 return;
276 }
277
278 QVariantMap map = pMap; // copy to compare
279 pMap = reply.value();
280 QMapIterator<QString, QVariant> i(reply.value());
281
282 while (i.hasNext()) {
283 i.next();
284 if (pMap.value(i.key()) != map.value(i.key())) {
285 Q_EMIT propertyChanged(i.key(), QVariant::fromValue(i.value()));
286 }
287 }
288 }
289
290 QString QUPowerDeviceInterface::nativePath()
291 {
292 return pMap.value(QStringLiteral("NativePath")).toString();
293 }
294
295 quint16 QUPowerDeviceInterface::technology()
296 {
297 return pMap.value(QStringLiteral("Technology")).toUInt();
298 }
299
300 QT_END_NAMESPACE