Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qnetworkmanagerservice.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QObject>
7#include <QList>
8#include <QtDBus/QDBusConnection>
9#include <QtDBus/QDBusError>
10#include <QtDBus/QDBusInterface>
11#include <QtDBus/QDBusMessage>
12#include <QtDBus/QDBusReply>
13#include <QtDBus/QDBusPendingCallWatcher>
14#include <QtDBus/QDBusObjectPath>
15#include <QtDBus/QDBusPendingCall>
16
17#define DBUS_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
18
19#define NM_DBUS_SERVICE "org.freedesktop.NetworkManager"
20
21#define NM_DBUS_PATH "/org/freedesktop/NetworkManager"
22#define NM_DBUS_INTERFACE NM_DBUS_SERVICE
23#define NM_CONNECTION_DBUS_INTERFACE NM_DBUS_SERVICE ".Connection.Active"
24#define NM_DEVICE_DBUS_INTERFACE NM_DBUS_SERVICE ".Device"
25
27
28using namespace Qt::StringLiterals;
29
33{
34}
35
37{
39}
40
43{
44 if (!isValid())
45 return;
46
47 PropertiesDBusInterface managerPropertiesInterface(
50 QList<QVariant> argumentList;
51 argumentList << NM_DBUS_INTERFACE ""_L1;
52 QDBusPendingReply<QVariantMap> propsReply = managerPropertiesInterface.callWithArgumentList(
53 QDBus::Block, "GetAll"_L1, argumentList);
54 if (!propsReply.isError()) {
55 propertyMap = propsReply.value();
56 } else {
57 qWarning() << "propsReply" << propsReply.error().message();
58 }
59
61 DBUS_PROPERTIES_INTERFACE""_L1, "PropertiesChanged"_L1, this,
63}
64
66{
68 DBUS_PROPERTIES_INTERFACE ""_L1, "PropertiesChanged"_L1, this,
70}
71
73{
74 if (propertyMap.contains("State"))
75 return static_cast<QNetworkManagerInterface::NMState>(propertyMap.value("State").toUInt());
77}
78
80{
81 if (propertyMap.contains("Connectivity"))
82 return static_cast<NMConnectivityState>(propertyMap.value("Connectivity").toUInt());
84}
85
86static std::optional<QDBusInterface> getPrimaryDevice(const QDBusObjectPath &devicePath)
87{
90 if (!connection.isValid())
91 return std::nullopt;
92
93 const auto devicePaths = connection.property("Devices").value<QList<QDBusObjectPath>>();
94 if (devicePaths.isEmpty())
95 return std::nullopt;
96
97 const QDBusObjectPath primaryDevicePath = devicePaths.front();
98 return std::make_optional<QDBusInterface>(NM_DBUS_SERVICE, primaryDevicePath.path(),
101}
102
103std::optional<QDBusObjectPath> QNetworkManagerInterface::primaryConnectionDevicePath() const
104{
105 auto it = propertyMap.constFind(u"PrimaryConnection"_s);
106 if (it != propertyMap.cend())
107 return it->value<QDBusObjectPath>();
108 return std::nullopt;
109}
110
112{
113 if (const auto path = primaryConnectionDevicePath())
114 return extractDeviceType(*path);
116}
117
119{
120 if (const auto path = primaryConnectionDevicePath())
121 return extractDeviceMetered(*path);
122 return NM_METERED_UNKNOWN;
123}
124
125auto QNetworkManagerInterface::extractDeviceType(const QDBusObjectPath &devicePath) const
126 -> NMDeviceType
127{
128 const auto primaryDevice = getPrimaryDevice(devicePath);
129 if (!primaryDevice)
130 return NM_DEVICE_TYPE_UNKNOWN;
131 if (!primaryDevice->isValid())
132 return NM_DEVICE_TYPE_UNKNOWN;
133 const QVariant deviceType = primaryDevice->property("DeviceType");
134 if (!deviceType.isValid())
135 return NM_DEVICE_TYPE_UNKNOWN;
136 return static_cast<NMDeviceType>(deviceType.toUInt());
137}
138
139auto QNetworkManagerInterface::extractDeviceMetered(const QDBusObjectPath &devicePath) const
140 -> NMMetered
141{
142 const auto primaryDevice = getPrimaryDevice(devicePath);
143 if (!primaryDevice)
144 return NM_METERED_UNKNOWN;
145 if (!primaryDevice->isValid())
146 return NM_METERED_UNKNOWN;
147 const QVariant metered = primaryDevice->property("Metered");
148 if (!metered.isValid())
149 return NM_METERED_UNKNOWN;
150 return static_cast<NMMetered>(metered.toUInt());
151}
152
153void QNetworkManagerInterface::setProperties(const QString &interfaceName,
155 const QStringList &invalidatedProperties)
156{
157 Q_UNUSED(interfaceName);
158 Q_UNUSED(invalidatedProperties);
159
160 for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) {
161 bool valueChanged = true;
162
163 auto it = propertyMap.lowerBound(i.key());
164 if (it != propertyMap.end() && it.key() == i.key()) {
165 valueChanged = (it.value() != i.value());
166 *it = *i;
167 } else {
168 propertyMap.insert(it, i.key(), i.value());
169 }
170
171 if (valueChanged) {
172 if (i.key() == "State"_L1) {
173 quint32 state = i.value().toUInt();
174 Q_EMIT stateChanged(static_cast<NMState>(state));
175 } else if (i.key() == "Connectivity"_L1) {
176 quint32 state = i.value().toUInt();
178 } else if (i.key() == "PrimaryConnection"_L1) {
179 const QDBusObjectPath devicePath = i->value<QDBusObjectPath>();
180 Q_EMIT deviceTypeChanged(extractDeviceType(devicePath));
181 Q_EMIT meteredChanged(extractDeviceMetered(devicePath));
182 } else if (i.key() == "Metered"_L1) {
183 Q_EMIT meteredChanged(static_cast<NMMetered>(i->toUInt()));
184 }
185 }
186 }
187}
188
190
191#include "moc_qnetworkmanagerservice.cpp"
QDBusMessage callWithArgumentList(QDBus::CallMode mode, const QString &method, const QList< QVariant > &args)
Places a call to the remote method specified by method on this interface, using args as arguments.
bool isValid() const
Returns true if this is a valid reference to a remote object.
\inmodule QtDBus
static QDBusConnection systemBus()
Returns a QDBusConnection object opened with the system bus.
bool disconnect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
Disconnects the signal specified by the service, path, interface and name parameters from the slot sl...
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
Connects the signal specified by the service, path, interface and name parameters to the slot slot in...
QString message() const
Returns the message that the callee associated with this error.
\inmodule QtDBus
\inmodule QtDBus
QString path() const
Returns this object path.
QDBusError error() const
\inmodule QtDBus
Select< 0 >::Type value() const
Returns the first argument in this reply, cast to type Types[0] (the first template parameter of this...
Definition qlist.h:74
Definition qmap.h:186
iterator insert(const Key &key, const T &value)
Definition qmap.h:687
T value(const Key &key, const T &defaultValue=T()) const
Definition qmap.h:356
bool contains(const Key &key) const
Definition qmap.h:340
const_iterator cend() const
Definition qmap.h:604
const_iterator cbegin() const
Definition qmap.h:600
const_iterator constFind(const Key &key) const
Definition qmap.h:654
iterator lowerBound(const Key &key)
Definition qmap.h:659
iterator end()
Definition qmap.h:601
QNetworkManagerInterfaceBase(QObject *parent=nullptr)
void stateChanged(NMState)
NMConnectivityState connectivityState() const
void meteredChanged(NMMetered)
void connectivityChanged(NMConnectivityState)
QNetworkManagerInterface(QObject *parent=nullptr)
void deviceTypeChanged(NMDeviceType)
\inmodule QtCore
Definition qobject.h:90
QVariant property(const char *name) const
Returns the value of the object's name property.
Definition qobject.cpp:4187
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore
Definition qvariant.h:64
T value() const &
Definition qvariant.h:511
bool isValid() const
Returns true if the storage type of this variant is not QMetaType::UnknownType; otherwise returns fal...
Definition qvariant.h:707
uint toUInt(bool *ok=nullptr) const
Returns the variant as an unsigned int if the variant has userType() \l QMetaType::UInt,...
QMap< QString, QString > map
[6]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
DBusConnection * connection
#define qWarning
Definition qlogging.h:162
#define NM_DEVICE_DBUS_INTERFACE
#define NM_DBUS_INTERFACE
#define NM_DBUS_PATH
static std::optional< QDBusInterface > getPrimaryDevice(const QDBusObjectPath &devicePath)
#define DBUS_PROPERTIES_INTERFACE
#define NM_CONNECTION_DBUS_INTERFACE
#define NM_DBUS_SERVICE
#define SLOT(a)
Definition qobjectdefs.h:51
GLuint GLuint end
GLsizei const GLchar *const * path
#define Q_EMIT
#define Q_UNUSED(x)
unsigned int quint32
Definition qtypes.h:45
static QInputDevice::DeviceType deviceType(const UINT cursorType)
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent