1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the plugins 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 <QObject>
41#include <QList>
42#include <QtDBus/QtDBus>
43#include <QtDBus/QDBusConnection>
44#include <QtDBus/QDBusError>
45#include <QtDBus/QDBusInterface>
46#include <QtDBus/QDBusMessage>
47#include <QtDBus/QDBusReply>
48#include <QtDBus/QDBusPendingCallWatcher>
49#include <QtDBus/QDBusObjectPath>
50#include <QtDBus/QDBusPendingCall>
51
52#include "qnetworkmanagerservice.h"
53
54#ifndef QT_NO_DBUS
55
56#define DBUS_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
57
58QT_BEGIN_NAMESPACE
59
60
61QNetworkManagerInterface::QNetworkManagerInterface(QObject *parent)
62 : QDBusAbstractInterface(QLatin1String(NM_DBUS_SERVICE),
63 QLatin1String(NM_DBUS_PATH),
64 NM_DBUS_INTERFACE,
65 QDBusConnection::systemBus(),parent)
66{
67 if (!isValid()) {
68 return;
69 }
70
71 PropertiesDBusInterface managerPropertiesInterface(QLatin1String(NM_DBUS_SERVICE),
72 QLatin1String(NM_DBUS_PATH),
73 DBUS_PROPERTIES_INTERFACE,
74 QDBusConnection::systemBus());
75 QDBusPendingReply<QVariantMap> propsReply
76 = managerPropertiesInterface.call(mode: QDBus::Block, method: QLatin1String("GetAll"), args: QLatin1String(NM_DBUS_INTERFACE));
77
78 if (!propsReply.isError()) {
79 propertyMap = propsReply.value();
80 } else {
81 qWarning() << "propsReply" << propsReply.error().message();
82 }
83
84 QDBusPendingReply<QList <QDBusObjectPath> > nmReply
85 = call(method: QLatin1String("GetDevices"));
86 nmReply.waitForFinished();
87 if (!nmReply.isError()) {
88 devicesPathList = nmReply.value();
89 } else {
90 qWarning() << "nmReply" << nmReply.error().message();
91 }
92
93 QDBusConnection::systemBus().connect(service: QLatin1String(NM_DBUS_SERVICE),
94 path: QLatin1String(NM_DBUS_PATH),
95 interface: QLatin1String(NM_DBUS_INTERFACE),
96 name: QLatin1String("PropertiesChanged"),
97 receiver: this,SLOT(propertiesSwap(QMap<QString,QVariant>)));
98}
99
100QNetworkManagerInterface::~QNetworkManagerInterface()
101{
102 QDBusConnection::systemBus().disconnect(service: QLatin1String(NM_DBUS_SERVICE),
103 path: QLatin1String(NM_DBUS_PATH),
104 interface: QLatin1String(NM_DBUS_INTERFACE),
105 name: QLatin1String("PropertiesChanged"),
106 receiver: this,SLOT(propertiesSwap(QMap<QString,QVariant>)));
107 QDBusConnection::systemBus().disconnect(service: QLatin1String(NM_DBUS_SERVICE),
108 path: QLatin1String(NM_DBUS_PATH),
109 interface: QLatin1String(NM_DBUS_INTERFACE),
110 name: QLatin1String("DeviceAdded"),
111 receiver: this,SIGNAL(deviceAdded(QDBusObjectPath)));
112 QDBusConnection::systemBus().disconnect(service: QLatin1String(NM_DBUS_SERVICE),
113 path: QLatin1String(NM_DBUS_PATH),
114 interface: QLatin1String(NM_DBUS_INTERFACE),
115 name: QLatin1String("DeviceRemoved"),
116 receiver: this,SIGNAL(deviceRemoved(QDBusObjectPath)));
117}
118
119bool QNetworkManagerInterface::setConnections()
120{
121 if (!isValid())
122 return false;
123
124 QDBusConnection::systemBus().connect(service: QLatin1String(NM_DBUS_SERVICE),
125 path: QLatin1String(NM_DBUS_PATH),
126 interface: QLatin1String(NM_DBUS_INTERFACE),
127 name: QLatin1String("PropertiesChanged"),
128 receiver: this,SLOT(propertiesSwap(QMap<QString,QVariant>)));
129
130 bool allOk = false;
131 if (QDBusConnection::systemBus().connect(service: QLatin1String(NM_DBUS_SERVICE),
132 path: QLatin1String(NM_DBUS_PATH),
133 interface: QLatin1String(NM_DBUS_INTERFACE),
134 name: QLatin1String("DeviceAdded"),
135 receiver: this,SIGNAL(deviceAdded(QDBusObjectPath)))) {
136 allOk = true;
137 }
138 if (QDBusConnection::systemBus().connect(service: QLatin1String(NM_DBUS_SERVICE),
139 path: QLatin1String(NM_DBUS_PATH),
140 interface: QLatin1String(NM_DBUS_INTERFACE),
141 name: QLatin1String("DeviceRemoved"),
142 receiver: this,SIGNAL(deviceRemoved(QDBusObjectPath)))) {
143 allOk = true;
144 }
145
146 return allOk;
147}
148
149QList <QDBusObjectPath> QNetworkManagerInterface::getDevices()
150{
151 if (devicesPathList.isEmpty()) {
152 //qWarning("using blocking call!");
153 QDBusReply<QList<QDBusObjectPath> > reply = call(method: QLatin1String("GetDevices"));
154 devicesPathList = reply.value();
155 }
156 return devicesPathList;
157}
158
159void QNetworkManagerInterface::activateConnection(QDBusObjectPath connectionPath,
160 QDBusObjectPath devicePath,
161 QDBusObjectPath specificObject)
162{
163 QDBusPendingCall pendingCall = asyncCall(method: QLatin1String("ActivateConnection"),
164 args: QVariant::fromValue(value: connectionPath),
165 args: QVariant::fromValue(value: devicePath),
166 args: QVariant::fromValue(value: specificObject));
167
168 QDBusPendingCallWatcher *callWatcher = new QDBusPendingCallWatcher(pendingCall);
169 connect(sender: callWatcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
170 receiver: this, SIGNAL(activationFinished(QDBusPendingCallWatcher*)));
171}
172
173void QNetworkManagerInterface::deactivateConnection(QDBusObjectPath connectionPath)
174{
175 asyncCall(method: QLatin1String("DeactivateConnection"), args: QVariant::fromValue(value: connectionPath));
176}
177
178bool QNetworkManagerInterface::wirelessEnabled() const
179{
180 if (propertyMap.contains(akey: "WirelessEnabled"))
181 return propertyMap.value(akey: "WirelessEnabled").toBool();
182 return false;
183}
184
185bool QNetworkManagerInterface::wirelessHardwareEnabled() const
186{
187 if (propertyMap.contains(akey: "WirelessHardwareEnabled"))
188 return propertyMap.value(akey: "WirelessHardwareEnabled").toBool();
189 return false;
190}
191
192QList <QDBusObjectPath> QNetworkManagerInterface::activeConnections() const
193{
194 if (propertyMap.contains(akey: "ActiveConnections")) {
195
196 const QDBusArgument &dbusArgs = qvariant_cast<QDBusArgument>(v: propertyMap.value(akey: "ActiveConnections"));
197 QDBusObjectPath path;
198 QList <QDBusObjectPath> list;
199
200 dbusArgs.beginArray();
201 while (!dbusArgs.atEnd()) {
202 dbusArgs >> path;
203 list.append(t: path);
204 }
205 dbusArgs.endArray();
206
207 return list;
208 }
209
210 QList <QDBusObjectPath> list;
211 list << QDBusObjectPath();
212 return list;
213}
214
215QNetworkManagerInterface::NMState QNetworkManagerInterface::state()
216{
217 if (propertyMap.contains(akey: "State"))
218 return static_cast<QNetworkManagerInterface::NMState>(propertyMap.value(akey: "State").toUInt());
219 return QNetworkManagerInterface::NM_STATE_UNKNOWN;
220}
221
222QString QNetworkManagerInterface::version() const
223{
224 if (propertyMap.contains(akey: "Version"))
225 return propertyMap.value(akey: "Version").toString();
226 return QString();
227}
228
229void QNetworkManagerInterface::propertiesSwap(QMap<QString,QVariant> map)
230{
231 for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) {
232 propertyMap.insert(akey: i.key(),avalue: i.value());
233
234 if (i.key() == QLatin1String("State")) {
235 quint32 state = i.value().toUInt();
236 if (state == NM_DEVICE_STATE_ACTIVATED
237 || state == NM_DEVICE_STATE_DISCONNECTED
238 || state == NM_DEVICE_STATE_UNAVAILABLE
239 || state == NM_DEVICE_STATE_FAILED) {
240 Q_EMIT propertiesChanged(map);
241 Q_EMIT stateChanged(state);
242 }
243 } else if (i.key() == QLatin1String("ActiveConnections")) {
244 Q_EMIT propertiesChanged(map);
245 }
246 }
247}
248
249QNetworkManagerInterfaceAccessPoint::QNetworkManagerInterfaceAccessPoint(const QString &dbusPathName, QObject *parent)
250 : QDBusAbstractInterface(QLatin1String(NM_DBUS_SERVICE),
251 dbusPathName,
252 NM_DBUS_INTERFACE_ACCESS_POINT,
253 QDBusConnection::systemBus(),parent)
254{
255}
256
257QNetworkManagerInterfaceAccessPoint::~QNetworkManagerInterfaceAccessPoint()
258{
259}
260
261quint32 QNetworkManagerInterfaceAccessPoint::flags() const
262{
263 if (propertyMap.contains(akey: "Flags"))
264 return propertyMap.value(akey: "Flags").toUInt();
265 return 0;
266}
267
268quint32 QNetworkManagerInterfaceAccessPoint::wpaFlags() const
269{
270 if (propertyMap.contains(akey: "WpaFlags"))
271 return propertyMap.value(akey: "WpaFlags").toUInt();
272 return 0;
273}
274
275quint32 QNetworkManagerInterfaceAccessPoint::rsnFlags() const
276{
277 if (propertyMap.contains(akey: "RsnFlags"))
278 return propertyMap.value(akey: "RsnFlags").toUInt();
279 return 0;
280}
281
282QString QNetworkManagerInterfaceAccessPoint::ssid() const
283{
284 if (propertyMap.contains(akey: "Ssid"))
285 return propertyMap.value(akey: "Ssid").toString();
286 return QString();
287}
288
289quint32 QNetworkManagerInterfaceAccessPoint::frequency() const
290{
291 if (propertyMap.contains(akey: "Frequency"))
292 return propertyMap.value(akey: "Frequency").toUInt();
293 return 0;
294}
295
296QString QNetworkManagerInterfaceAccessPoint::hwAddress() const
297{
298 if (propertyMap.contains(akey: "HwAddress"))
299 return propertyMap.value(akey: "HwAddress").toString();
300 return QString();
301}
302
303quint32 QNetworkManagerInterfaceAccessPoint::mode() const
304{
305 if (propertyMap.contains(akey: "Mode"))
306 return propertyMap.value(akey: "Mode").toUInt();
307 return 0;
308}
309
310quint32 QNetworkManagerInterfaceAccessPoint::maxBitrate() const
311{
312 if (propertyMap.contains(akey: "MaxBitrate"))
313 return propertyMap.value(akey: "MaxBitrate").toUInt();
314 return 0;
315}
316
317quint32 QNetworkManagerInterfaceAccessPoint::strength() const
318{
319 if (propertyMap.contains(akey: "Strength"))
320 return propertyMap.value(akey: "Strength").toUInt();
321 return 0;
322}
323
324void QNetworkManagerInterfaceAccessPoint::propertiesSwap(QMap<QString,QVariant> map)
325{
326 for (auto i = map.cbegin(), end = map.cend(); i != end; ++i)
327 propertyMap.insert(akey: i.key(),avalue: i.value());
328}
329
330QNetworkManagerInterfaceDevice::QNetworkManagerInterfaceDevice(const QString &deviceObjectPath, QObject *parent)
331 : QDBusAbstractInterface(QLatin1String(NM_DBUS_SERVICE),
332 deviceObjectPath,
333 NM_DBUS_INTERFACE_DEVICE,
334 QDBusConnection::systemBus(),parent)
335{
336
337 if (!isValid()) {
338 return;
339 }
340 PropertiesDBusInterface devicePropertiesInterface(QLatin1String(NM_DBUS_SERVICE),
341 deviceObjectPath,
342 DBUS_PROPERTIES_INTERFACE,
343 QDBusConnection::systemBus(),parent);
344
345 QDBusPendingReply<QVariantMap> propsReply
346 = devicePropertiesInterface.call(mode: QDBus::Block, method: QLatin1String("GetAll"), args: QLatin1String(NM_DBUS_INTERFACE_DEVICE));
347
348 if (!propsReply.isError()) {
349 propertyMap = propsReply.value();
350 }
351
352 QDBusConnection::systemBus().connect(service: QLatin1String(NM_DBUS_SERVICE),
353 path: deviceObjectPath,
354 interface: QLatin1String(NM_DBUS_INTERFACE_DEVICE),
355 name: QLatin1String("PropertiesChanged"),
356 receiver: this,SLOT(propertiesSwap(QMap<QString,QVariant>)));
357}
358
359QNetworkManagerInterfaceDevice::~QNetworkManagerInterfaceDevice()
360{
361 QDBusConnection::systemBus().disconnect(service: QLatin1String(NM_DBUS_SERVICE),
362 path: path(),
363 interface: QLatin1String(NM_DBUS_INTERFACE_DEVICE),
364 name: QLatin1String("PropertiesChanged"),
365 receiver: this,SLOT(propertiesSwap(QMap<QString,QVariant>)));
366}
367
368QString QNetworkManagerInterfaceDevice::udi() const
369{
370 if (propertyMap.contains(akey: "Udi"))
371 return propertyMap.value(akey: "Udi").toString();
372 return QString();
373}
374
375QString QNetworkManagerInterfaceDevice::networkInterface() const
376{
377 if (propertyMap.contains(akey: "Interface"))
378 return propertyMap.value(akey: "Interface").toString();
379 return QString();
380}
381
382quint32 QNetworkManagerInterfaceDevice::ip4Address() const
383{
384 if (propertyMap.contains(akey: "Ip4Address"))
385 return propertyMap.value(akey: "Ip4Address").toUInt();
386 return 0;
387}
388
389quint32 QNetworkManagerInterfaceDevice::state() const
390{
391 if (propertyMap.contains(akey: "State"))
392 return propertyMap.value(akey: "State").toUInt();
393 return 0;
394}
395
396quint32 QNetworkManagerInterfaceDevice::deviceType() const
397{
398 if (propertyMap.contains(akey: "DeviceType"))
399 return propertyMap.value(akey: "DeviceType").toUInt();
400 return 0;
401}
402
403QDBusObjectPath QNetworkManagerInterfaceDevice::ip4config() const
404{
405 if (propertyMap.contains(akey: "Ip4Config"))
406 return qvariant_cast<QDBusObjectPath>(v: propertyMap.value(akey: "Ip4Config"));
407 return QDBusObjectPath();
408}
409
410void QNetworkManagerInterfaceDevice::propertiesSwap(QMap<QString,QVariant> map)
411{
412 for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) {
413 if (i.key() == QLatin1String("AvailableConnections")) { //Device
414 const QDBusArgument &dbusArgs = qvariant_cast<QDBusArgument>(v: i.value());
415 QDBusObjectPath path;
416 QStringList paths;
417 dbusArgs.beginArray();
418 while (!dbusArgs.atEnd()) {
419 dbusArgs >> path;
420 paths << path.path();
421 }
422 dbusArgs.endArray();
423 Q_EMIT connectionsChanged(paths);
424 }
425 propertyMap.insert(akey: i.key(),avalue: i.value());
426 }
427 Q_EMIT propertiesChanged(map);
428}
429
430QNetworkManagerInterfaceDeviceWired::QNetworkManagerInterfaceDeviceWired(const QString &ifaceDevicePath, QObject *parent)
431 : QDBusAbstractInterface(QLatin1String(NM_DBUS_SERVICE),
432 ifaceDevicePath,
433 NM_DBUS_INTERFACE_DEVICE_WIRED,
434 QDBusConnection::systemBus(), parent)
435{
436 if (!isValid()) {
437 return;
438 }
439 PropertiesDBusInterface deviceWiredPropertiesInterface(QLatin1String(NM_DBUS_SERVICE),
440 ifaceDevicePath,
441 DBUS_PROPERTIES_INTERFACE,
442 QDBusConnection::systemBus(),parent);
443
444 QDBusPendingReply<QVariantMap> propsReply
445 = deviceWiredPropertiesInterface.call(mode: QDBus::Block, method: QLatin1String("GetAll"), args: QLatin1String(NM_DBUS_INTERFACE_DEVICE_WIRED));
446
447 if (!propsReply.isError()) {
448 propertyMap = propsReply.value();
449 }
450
451 QDBusConnection::systemBus().connect(service: QLatin1String(NM_DBUS_SERVICE),
452 path: ifaceDevicePath,
453 interface: QLatin1String(NM_DBUS_INTERFACE_DEVICE_WIRED),
454 name: QLatin1String("PropertiesChanged"),
455 receiver: this,SLOT(propertiesSwap(QMap<QString,QVariant>)));
456}
457
458QNetworkManagerInterfaceDeviceWired::~QNetworkManagerInterfaceDeviceWired()
459{
460 QDBusConnection::systemBus().disconnect(service: QLatin1String(NM_DBUS_SERVICE),
461 path: path(),
462 interface: QLatin1String(NM_DBUS_INTERFACE_DEVICE_WIRED),
463 name: QLatin1String("PropertiesChanged"),
464 receiver: this,SLOT(propertiesSwap(QMap<QString,QVariant>)));
465}
466
467QString QNetworkManagerInterfaceDeviceWired::hwAddress() const
468{
469 if (propertyMap.contains(akey: "HwAddress"))
470 return propertyMap.value(akey: "HwAddress").toString();
471 return QString();
472}
473
474quint32 QNetworkManagerInterfaceDeviceWired::speed() const
475{
476 if (propertyMap.contains(akey: "Speed"))
477 return propertyMap.value(akey: "Speed").toUInt();
478 return 0;
479}
480
481bool QNetworkManagerInterfaceDeviceWired::carrier() const
482{
483 if (propertyMap.contains(akey: "Carrier"))
484 return propertyMap.value(akey: "Carrier").toBool();
485 return false;
486}
487
488QStringList QNetworkManagerInterfaceDeviceWired::availableConnections()
489{
490 QStringList list;
491 if (propertyMap.contains(akey: "AvailableConnections")) {
492 const QDBusArgument &dbusArgs = qvariant_cast<QDBusArgument>(v: propertyMap.value(akey: "Carrier"));
493 QDBusObjectPath path;
494 dbusArgs.beginArray();
495 while (!dbusArgs.atEnd()) {
496 dbusArgs >> path;
497 list << path.path();
498 }
499 dbusArgs.endArray();
500 }
501
502 return list;
503}
504
505void QNetworkManagerInterfaceDeviceWired::propertiesSwap(QMap<QString,QVariant> map)
506{
507 for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) {
508 propertyMap.insert(akey: i.key(),avalue: i.value());
509 if (i.key() == QLatin1String("Carrier"))
510 Q_EMIT carrierChanged(i.value().toBool());
511 }
512 Q_EMIT propertiesChanged(map);
513}
514
515QNetworkManagerInterfaceDeviceWireless::QNetworkManagerInterfaceDeviceWireless(const QString &ifaceDevicePath, QObject *parent)
516 : QDBusAbstractInterface(QLatin1String(NM_DBUS_SERVICE),
517 ifaceDevicePath,
518 NM_DBUS_INTERFACE_DEVICE_WIRELESS,
519 QDBusConnection::systemBus(), parent)
520{
521 if (!isValid()) {
522 return;
523 }
524
525 interfacePath = ifaceDevicePath;
526 QDBusPendingReply<QList <QDBusObjectPath> > nmReply
527 = call(method: QLatin1String("GetAccessPoints"));
528
529 if (!nmReply.isError()) {
530 accessPointsList = nmReply.value();
531 }
532
533 PropertiesDBusInterface deviceWirelessPropertiesInterface(QLatin1String(NM_DBUS_SERVICE),
534 interfacePath,
535 DBUS_PROPERTIES_INTERFACE,
536 QDBusConnection::systemBus(),parent);
537
538 QDBusPendingReply<QVariantMap> propsReply
539 = deviceWirelessPropertiesInterface.call(mode: QDBus::Block, method: QLatin1String("GetAll"), args: QLatin1String(NM_DBUS_INTERFACE_DEVICE_WIRELESS));
540
541 if (!propsReply.isError()) {
542 propertyMap = propsReply.value();
543 }
544
545 QDBusConnection::systemBus().connect(service: QLatin1String(NM_DBUS_SERVICE),
546 path: interfacePath,
547 interface: QLatin1String(NM_DBUS_INTERFACE_DEVICE_WIRELESS),
548 name: QLatin1String("PropertiesChanged"),
549 receiver: this,SLOT(propertiesSwap(QMap<QString,QVariant>)));
550}
551
552QNetworkManagerInterfaceDeviceWireless::~QNetworkManagerInterfaceDeviceWireless()
553{
554 QDBusConnection::systemBus().disconnect(service: QLatin1String(NM_DBUS_SERVICE),
555 path: path(),
556 interface: QLatin1String(NM_DBUS_INTERFACE_DEVICE_WIRELESS),
557 name: QLatin1String("PropertiesChanged"),
558 receiver: this,SLOT(propertiesSwap(QMap<QString,QVariant>)));
559}
560
561bool QNetworkManagerInterfaceDeviceWireless::setConnections()
562{
563 return true;
564}
565
566QList <QDBusObjectPath> QNetworkManagerInterfaceDeviceWireless::getAccessPoints()
567{
568 if (accessPointsList.isEmpty()) {
569 //qWarning("Using blocking call!");
570 QDBusReply<QList<QDBusObjectPath> > reply
571 = call(method: QLatin1String("GetAccessPoints"));
572 accessPointsList = reply.value();
573 }
574 return accessPointsList;
575}
576
577QString QNetworkManagerInterfaceDeviceWireless::hwAddress() const
578{
579 if (propertyMap.contains(akey: "HwAddress"))
580 return propertyMap.value(akey: "HwAddress").toString();
581 return QString();
582}
583
584quint32 QNetworkManagerInterfaceDeviceWireless::mode() const
585{
586 if (propertyMap.contains(akey: "Mode"))
587 return propertyMap.value(akey: "Mode").toUInt();
588 return 0;
589}
590
591quint32 QNetworkManagerInterfaceDeviceWireless::bitrate() const
592{
593 if (propertyMap.contains(akey: "Bitrate"))
594 return propertyMap.value(akey: "Bitrate").toUInt();
595 return 0;
596}
597
598QDBusObjectPath QNetworkManagerInterfaceDeviceWireless::activeAccessPoint() const
599{
600 if (propertyMap.contains(akey: "ActiveAccessPoint"))
601 return qvariant_cast<QDBusObjectPath>(v: propertyMap.value(akey: "ActiveAccessPoint"));
602 return QDBusObjectPath();
603}
604
605quint32 QNetworkManagerInterfaceDeviceWireless::wirelessCapabilities() const
606{
607 if (propertyMap.contains(akey: "WirelelessCapabilities"))
608 return propertyMap.value(akey: "WirelelessCapabilities").toUInt();
609 return 0;
610}
611
612void QNetworkManagerInterfaceDeviceWireless::requestScan()
613{
614 asyncCall(method: QLatin1String("RequestScan"));
615}
616
617void QNetworkManagerInterfaceDeviceWireless::propertiesSwap(QMap<QString,QVariant> map)
618{
619 for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) {
620 propertyMap.insert(akey: i.key(),avalue: i.value());
621 if (i.key() == QLatin1String("ActiveAccessPoint")) //DeviceWireless
622 Q_EMIT propertiesChanged(map);
623 }
624}
625
626QNetworkManagerInterfaceDeviceModem::QNetworkManagerInterfaceDeviceModem(const QString &ifaceDevicePath, QObject *parent)
627 : QDBusAbstractInterface(QLatin1String(NM_DBUS_SERVICE),
628 ifaceDevicePath,
629 NM_DBUS_INTERFACE_DEVICE_MODEM,
630 QDBusConnection::systemBus(), parent)
631{
632 if (!isValid()) {
633 return;
634 }
635 PropertiesDBusInterface deviceModemPropertiesInterface(QLatin1String(NM_DBUS_SERVICE),
636 ifaceDevicePath,
637 QLatin1String("org.freedesktop.DBus.Properties"),
638 QDBusConnection::systemBus(),parent);
639
640 QDBusPendingReply<QVariantMap> propsReply
641 = deviceModemPropertiesInterface.call(mode: QDBus::Block, method: QLatin1String("GetAll"), args: QLatin1String(NM_DBUS_INTERFACE_DEVICE_MODEM));
642
643 if (!propsReply.isError()) {
644 propertyMap = propsReply.value();
645 }
646
647 QDBusConnection::systemBus().connect(service: QLatin1String(NM_DBUS_SERVICE),
648 path: ifaceDevicePath,
649 interface: QLatin1String(NM_DBUS_INTERFACE_DEVICE_MODEM),
650 name: QLatin1String("PropertiesChanged"),
651 receiver: this,SLOT(propertiesSwap(QMap<QString,QVariant>)));
652}
653
654QNetworkManagerInterfaceDeviceModem::~QNetworkManagerInterfaceDeviceModem()
655{
656 QDBusConnection::systemBus().disconnect(service: QLatin1String(NM_DBUS_SERVICE),
657 path: path(),
658 interface: QLatin1String(NM_DBUS_PATH_SETTINGS),
659 name: QLatin1String(NM_DBUS_IFACE_SETTINGS),
660 signature: QLatin1String("NewConnection"),
661 receiver: this, SIGNAL(newConnection(QDBusObjectPath)));
662}
663
664QNetworkManagerInterfaceDeviceModem::ModemCapabilities QNetworkManagerInterfaceDeviceModem::modemCapabilities() const
665{
666 if (propertyMap.contains(akey: "ModemCapabilities"))
667 return static_cast<QNetworkManagerInterfaceDeviceModem::ModemCapabilities>(propertyMap.value(akey: "ModemCapabilities").toUInt());
668 return QNetworkManagerInterfaceDeviceModem::None;
669}
670
671QNetworkManagerInterfaceDeviceModem::ModemCapabilities QNetworkManagerInterfaceDeviceModem::currentCapabilities() const
672{
673 if (propertyMap.contains(akey: "CurrentCapabilities"))
674 return static_cast<QNetworkManagerInterfaceDeviceModem::ModemCapabilities>(propertyMap.value(akey: "CurrentCapabilities").toUInt());
675 return QNetworkManagerInterfaceDeviceModem::None;
676}
677
678void QNetworkManagerInterfaceDeviceModem::propertiesSwap(QMap<QString,QVariant> map)
679{
680 for (auto i = map.cbegin(), end = map.cend(); i != end; ++i)
681 propertyMap.insert(akey: i.key(),avalue: i.value());
682 Q_EMIT propertiesChanged(map);
683}
684
685
686QNetworkManagerSettings::QNetworkManagerSettings(const QString &settingsService, QObject *parent)
687 : QDBusAbstractInterface(settingsService,
688 NM_DBUS_PATH_SETTINGS,
689 NM_DBUS_IFACE_SETTINGS,
690 QDBusConnection::systemBus(), parent)
691{
692 if (!isValid()) {
693 return;
694 }
695 interfacePath = settingsService;
696 QDBusPendingReply<QList <QDBusObjectPath> > nmReply
697 = call(method: QLatin1String("ListConnections"));
698
699 if (!nmReply.isError()) {
700 connectionsList = nmReply.value();
701 }
702}
703
704QNetworkManagerSettings::~QNetworkManagerSettings()
705{
706}
707
708bool QNetworkManagerSettings::setConnections()
709{
710 bool allOk = true;
711 if (!QDBusConnection::systemBus().connect(service: interfacePath,
712 path: QLatin1String(NM_DBUS_PATH_SETTINGS),
713 interface: QLatin1String(NM_DBUS_IFACE_SETTINGS),
714 name: QLatin1String("NewConnection"),
715 receiver: this, SIGNAL(newConnection(QDBusObjectPath)))) {
716 allOk = false;
717 qWarning(msg: "NewConnection could not be connected");
718 }
719
720 return allOk;
721}
722
723QList <QDBusObjectPath> QNetworkManagerSettings::listConnections()
724{
725 if (connectionsList.isEmpty()) {
726 //qWarning("Using blocking call!");
727 QDBusReply<QList<QDBusObjectPath> > reply
728 = call(method: QLatin1String("ListConnections"));
729 connectionsList = reply.value();
730 }
731 return connectionsList;
732}
733
734
735QString QNetworkManagerSettings::getConnectionByUuid(const QString &uuid)
736{
737 QDBusReply<QDBusObjectPath > reply = call(mode: QDBus::Block, method: QLatin1String("GetConnectionByUuid"), args: uuid);
738 return reply.value().path();
739}
740
741QNetworkManagerSettingsConnection::QNetworkManagerSettingsConnection(const QString &settingsService, const QString &connectionObjectPath, QObject *parent)
742 : QDBusAbstractInterface(settingsService,
743 connectionObjectPath,
744 NM_DBUS_IFACE_SETTINGS_CONNECTION,
745 QDBusConnection::systemBus(), parent)
746{
747 qDBusRegisterMetaType<QNmSettingsMap>();
748 if (!isValid()) {
749 return;
750 }
751 interfacepath = connectionObjectPath;
752 QDBusPendingReply<QNmSettingsMap> nmReply
753 = call(method: QLatin1String("GetSettings"));
754 if (!nmReply.isError()) {
755 settingsMap = nmReply.value();
756 }
757}
758
759QNetworkManagerSettingsConnection::~QNetworkManagerSettingsConnection()
760{
761 QDBusConnection::systemBus().disconnect(service: service(),
762 path: path(),
763 interface: QLatin1String(NM_DBUS_IFACE_SETTINGS_CONNECTION),
764 name: QLatin1String("Updated"),
765 receiver: this, SIGNAL(updated()));
766 QDBusConnection::systemBus().disconnect(service: service(),
767 path: path(),
768 interface: QLatin1String(NM_DBUS_IFACE_SETTINGS_CONNECTION),
769 name: QLatin1String("Removed"),
770 receiver: this, SIGNAL(slotSettingsRemoved()));
771}
772
773bool QNetworkManagerSettingsConnection::setConnections()
774{
775 if (!isValid())
776 return false;
777
778 QDBusConnection dbusConnection = QDBusConnection::systemBus();
779 bool allOk = true;
780 if (!dbusConnection.connect(service: service(),
781 path: interfacepath,
782 interface: QLatin1String(NM_DBUS_IFACE_SETTINGS_CONNECTION),
783 name: QLatin1String("Updated"),
784 receiver: this, SIGNAL(updated()))) {
785 allOk = false;
786 }
787
788 if (!dbusConnection.connect(service: service(),
789 path: interfacepath,
790 interface: QLatin1String(NM_DBUS_IFACE_SETTINGS_CONNECTION),
791 name: QLatin1String("Removed"),
792 receiver: this, SIGNAL(slotSettingsRemoved()))) {
793 allOk = false;
794 }
795 return allOk;
796}
797
798void QNetworkManagerSettingsConnection::slotSettingsRemoved()
799{
800 Q_EMIT removed(path: interfacepath);
801}
802
803QNmSettingsMap QNetworkManagerSettingsConnection::getSettings()
804{
805 if (settingsMap.isEmpty()) {
806 //qWarning("Using blocking call!");
807 QDBusReply<QNmSettingsMap> reply = call(method: QLatin1String("GetSettings"));
808 settingsMap = reply.value();
809 }
810 return settingsMap;
811}
812
813NMDeviceType QNetworkManagerSettingsConnection::getType()
814{
815 const QString devType =
816 settingsMap.value(akey: QLatin1String("connection")).value(akey: QLatin1String("type")).toString();
817
818 if (devType == QLatin1String("802-3-ethernet"))
819 return DEVICE_TYPE_ETHERNET;
820 else if (devType == QLatin1String("802-11-wireless"))
821 return DEVICE_TYPE_WIFI;
822 else if (devType == QLatin1String("gsm"))
823 return DEVICE_TYPE_MODEM;
824 else
825 return DEVICE_TYPE_UNKNOWN;
826}
827
828bool QNetworkManagerSettingsConnection::isAutoConnect()
829{
830 const QVariant autoConnect =
831 settingsMap.value(akey: QLatin1String("connection")).value(akey: QLatin1String("autoconnect"));
832
833 // NetworkManager default is to auto connect
834 if (!autoConnect.isValid())
835 return true;
836
837 return autoConnect.toBool();
838}
839
840quint64 QNetworkManagerSettingsConnection::getTimestamp()
841{
842 return settingsMap.value(akey: QLatin1String("connection"))
843 .value(akey: QLatin1String("timestamp")).toUInt();
844}
845
846QString QNetworkManagerSettingsConnection::getId()
847{
848 return settingsMap.value(akey: QLatin1String("connection")).value(akey: QLatin1String("id")).toString();
849}
850
851QString QNetworkManagerSettingsConnection::getUuid()
852{
853 const QString id = settingsMap.value(akey: QLatin1String("connection"))
854 .value(akey: QLatin1String("uuid")).toString();
855
856 // is no uuid, return the connection path
857 return id.isEmpty() ? path() : id;
858}
859
860QString QNetworkManagerSettingsConnection::getSsid()
861{
862 return settingsMap.value(akey: QLatin1String("802-11-wireless"))
863 .value(akey: QLatin1String("ssid")).toString();
864}
865
866QString QNetworkManagerSettingsConnection::getMacAddress()
867{
868 NMDeviceType type = getType();
869
870 if (type == DEVICE_TYPE_ETHERNET) {
871 return settingsMap.value(akey: QLatin1String("802-3-ethernet"))
872 .value(akey: QLatin1String("mac-address")).toString();
873 } else if (type == DEVICE_TYPE_WIFI) {
874 return settingsMap.value(akey: QLatin1String("802-11-wireless"))
875 .value(akey: QLatin1String("mac-address")).toString();
876 } else {
877 return QString();
878 }
879}
880
881QStringList QNetworkManagerSettingsConnection::getSeenBssids()
882{
883 if (getType() == DEVICE_TYPE_WIFI) {
884 return settingsMap.value(akey: QLatin1String("802-11-wireless"))
885 .value(akey: QLatin1String("seen-bssids")).toStringList();
886 } else {
887 return QStringList();
888 }
889}
890
891QNetworkManagerConnectionActive::QNetworkManagerConnectionActive(const QString &activeConnectionObjectPath, QObject *parent)
892 : QDBusAbstractInterface(QLatin1String(NM_DBUS_SERVICE),
893 activeConnectionObjectPath,
894 NM_DBUS_INTERFACE_ACTIVE_CONNECTION,
895 QDBusConnection::systemBus(), parent)
896{
897 if (!isValid()) {
898 return;
899 }
900 PropertiesDBusInterface connectionActivePropertiesInterface(QLatin1String(NM_DBUS_SERVICE),
901 activeConnectionObjectPath,
902 QLatin1String("org.freedesktop.DBus.Properties"),
903 QDBusConnection::systemBus());
904
905
906 QDBusPendingReply<QVariantMap> propsReply
907 = connectionActivePropertiesInterface.call(mode: QDBus::Block, method: QLatin1String("GetAll"), args: QLatin1String(NM_DBUS_INTERFACE_ACTIVE_CONNECTION));
908
909 if (!propsReply.isError()) {
910 propertyMap = propsReply.value();
911 } else {
912 qWarning() << propsReply.error().message();
913 }
914
915 QDBusConnection::systemBus().connect(service: QLatin1String(NM_DBUS_SERVICE),
916 path: activeConnectionObjectPath,
917 interface: QLatin1String(NM_DBUS_INTERFACE_ACTIVE_CONNECTION),
918 name: QLatin1String("PropertiesChanged"),
919 receiver: this,SLOT(propertiesSwap(QMap<QString,QVariant>)));
920}
921
922QNetworkManagerConnectionActive::~QNetworkManagerConnectionActive()
923{
924 QDBusConnection::systemBus().disconnect(service: QLatin1String(NM_DBUS_SERVICE),
925 path: path(),
926 interface: QLatin1String(NM_DBUS_INTERFACE_ACTIVE_CONNECTION),
927 name: QLatin1String("PropertiesChanged"),
928 receiver: this,SLOT(propertiesSwap(QMap<QString,QVariant>)));
929}
930
931QDBusObjectPath QNetworkManagerConnectionActive::connection() const
932{
933 if (propertyMap.contains(akey: "Connection"))
934 return qvariant_cast<QDBusObjectPath>(v: propertyMap.value(akey: "Connection"));
935 return QDBusObjectPath();
936}
937
938QDBusObjectPath QNetworkManagerConnectionActive::specificObject() const
939{
940 if (propertyMap.contains(akey: "SpecificObject"))
941 return qvariant_cast<QDBusObjectPath>(v: propertyMap.value(akey: "SpecificObject"));
942 return QDBusObjectPath();
943}
944
945QStringList QNetworkManagerConnectionActive::devices() const
946{
947 QStringList list;
948 if (propertyMap.contains(akey: "Devices")) {
949 const QDBusArgument &dbusArgs = qvariant_cast<QDBusArgument>(v: propertyMap.value(akey: "Devices"));
950 QDBusObjectPath path;
951
952 dbusArgs.beginArray();
953 while (!dbusArgs.atEnd()) {
954 dbusArgs >> path;
955 list.append(t: path.path());
956 }
957 dbusArgs.endArray();
958 }
959 return list;
960}
961
962quint32 QNetworkManagerConnectionActive::state() const
963{
964 if (propertyMap.contains(akey: "State"))
965 return propertyMap.value(akey: "State").toUInt();
966 return 0;
967}
968
969bool QNetworkManagerConnectionActive::defaultRoute() const
970{
971 if (propertyMap.contains(akey: "Default"))
972 return propertyMap.value(akey: "Default").toBool();
973 return false;
974}
975
976bool QNetworkManagerConnectionActive::default6Route() const
977{
978 if (propertyMap.contains(akey: "Default6"))
979 return propertyMap.value(akey: "Default6").toBool();
980 return false;
981}
982
983void QNetworkManagerConnectionActive::propertiesSwap(QMap<QString,QVariant> map)
984{
985 for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) {
986 propertyMap.insert(akey: i.key(),avalue: i.value());
987 if (i.key() == QLatin1String("State")) {
988 quint32 state = i.value().toUInt();
989 if (state == NM_ACTIVE_CONNECTION_STATE_ACTIVATED
990 || state == NM_ACTIVE_CONNECTION_STATE_DEACTIVATED) {
991 Q_EMIT propertiesChanged(map);
992 }
993 }
994 }
995}
996
997QNetworkManagerIp4Config::QNetworkManagerIp4Config( const QString &deviceObjectPath, QObject *parent)
998 : QDBusAbstractInterface(QLatin1String(NM_DBUS_SERVICE),
999 deviceObjectPath,
1000 NM_DBUS_INTERFACE_IP4_CONFIG,
1001 QDBusConnection::systemBus(), parent)
1002{
1003 if (!isValid()) {
1004 return;
1005 }
1006}
1007
1008QNetworkManagerIp4Config::~QNetworkManagerIp4Config()
1009{
1010}
1011
1012QStringList QNetworkManagerIp4Config::domains() const
1013{
1014 return property(name: "Domains").toStringList();
1015}
1016
1017QT_END_NAMESPACE
1018
1019#endif // QT_NO_DBUS
1020

source code of qtbase/src/plugins/bearer/networkmanager/qnetworkmanagerservice.cpp