Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qconnmanservice.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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// Qt-Security score:significant reason:default
4
7
8#include <QObject>
9#include <QList>
10#include <QtDBus/QDBusConnection>
11#include <QtDBus/QDBusError>
12#include <QtDBus/QDBusInterface>
13#include <QtDBus/QDBusMessage>
14#include <QtDBus/QDBusMetaType>
15#include <QtDBus/QDBusReply>
16#include <QtDBus/QDBusObjectPath>
17
18#include <utility> // for std::pair
19
20#define CONNMAN_DBUS_SERVICE "net.connman"_L1
21#define CONNMAN_DBUS_INTERFACE "net.connman.Manager"
22#define CONNMAN_DBUS_INTERFACE_L1 CONNMAN_DBUS_INTERFACE ""_L1
23#define CONNMAN_DBUS_PATH "/"_L1
24
25QT_BEGIN_NAMESPACE
26
27using namespace Qt::StringLiterals;
28
29using ConnmanService = QMap<QString, QVariant>;
30using ConnmanServiceEntry = std::pair<QDBusObjectPath, ConnmanService>;
31using ConnmanServices = QList<ConnmanServiceEntry>;
32
33namespace {
34constexpr QLatin1StringView propertyChangedKey = "PropertyChanged"_L1;
35constexpr QLatin1StringView stateKey = "State"_L1;
36constexpr QLatin1StringView typeKey = "Type"_L1;
37} // namespace
38
39QConnManInterfaceBase::QConnManInterfaceBase(QObject *parent)
41 QDBusConnection::systemBus(), parent)
42{
43}
44
46{
47 return QConnManInterfaceBase().isValid();
48}
49
50QConnManInterface::QConnManInterface(QObject *parent) : QConnManInterfaceBase(parent)
51{
52 if (!QDBusAbstractInterface::isValid())
53 return;
54
55 qDBusRegisterMetaType<ConnmanServiceEntry>();
56 qDBusRegisterMetaType<ConnmanServices>();
57
58 QDBusReply<QVariantMap> propertiesReply = call(QDBus::Block, "GetProperties"_L1);
59 if (!propertiesReply.isValid()) {
60 validDBusConnection = false;
61 if (propertiesReply.error().type() != QDBusError::AccessDenied) {
62 qWarning("Failed to query ConnMan properties: %ls",
63 qUtf16Printable(propertiesReply.error().message()));
64 }
65 return;
66 }
67 auto map = propertiesReply.value();
68 m_state = map.value(stateKey).toString();
69 m_type = findServiceType();
70
71 validDBusConnection = QDBusConnection::systemBus().connect(
73 this, SLOT(propertyChanged(QString,QDBusVariant)));
74}
75
77{
78 QDBusConnection::systemBus().disconnect(CONNMAN_DBUS_SERVICE, CONNMAN_DBUS_PATH,
79 CONNMAN_DBUS_INTERFACE_L1, propertyChangedKey, this,
80 SLOT(propertyChanged(QString,QDBusVariant)));
81}
82
84{
85 return m_state;
86}
87
89{
90 return m_type;
91}
92
94{
95 backend = ourBackend;
96}
97
98void QConnManInterface::propertyChanged(const QString &name, const QDBusVariant &value)
99{
100 if (name == stateKey) {
101 m_state = value.variant().toString();
102 backend->onStateChanged(m_state);
103
104 QString type = findServiceType();
105 if (type != m_type) {
106 m_type = std::move(type);
107 backend->onTypeChanged(m_type);
108 }
109 }
110}
111
112QString QConnManInterface::findServiceType()
113{
114 QDBusReply<ConnmanServices> servicesReply = call(QDBus::Block, "GetServices"_L1);
115 if (!servicesReply.isValid()) {
116 if (servicesReply.error().type() != QDBusError::AccessDenied) {
117 qWarning("Failed to query ConnMan services: %ls",
118 qUtf16Printable(servicesReply.error().message()));
119 }
120 return QString();
121 }
122
123 // The services list is sorted by connman and the first service matching
124 // the current state is the most relevant.
125 for (const auto &[_, service] : servicesReply.value()) {
126 if (service.value(stateKey).toString() == m_state)
127 return service.value(typeKey).toString();
128 }
129 return QString();
130}
131
132QT_END_NAMESPACE
133
134#include "moc_qconnmanservice.cpp"
static bool connmanAvailable()
QString state() const
QString type() const
void setBackend(QConnManNetworkInformationBackend *ourBackend)
QConnManInterface(QObject *parent=nullptr)
#define CONNMAN_DBUS_INTERFACE_L1
#define CONNMAN_DBUS_INTERFACE
#define CONNMAN_DBUS_PATH
#define CONNMAN_DBUS_SERVICE