Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
5
#
include
"qconnmanservice.h"
6
#
include
"qconnmannetworkinformationbackend.h"
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
25
QT_BEGIN_NAMESPACE
26
27
using
namespace
Qt::StringLiterals;
28
29
using
ConnmanService = QMap<QString, QVariant>;
30
using
ConnmanServiceEntry = std::pair<QDBusObjectPath, ConnmanService>;
31
using
ConnmanServices = QList<ConnmanServiceEntry>;
32
33
namespace
{
34
constexpr
QLatin1StringView propertyChangedKey =
"PropertyChanged"_L1
;
35
constexpr
QLatin1StringView stateKey =
"State"_L1
;
36
constexpr
QLatin1StringView typeKey =
"Type"_L1
;
37
}
// namespace
38
39
QConnManInterfaceBase
::QConnManInterfaceBase(QObject *parent)
40
: QDBusAbstractInterface(
CONNMAN_DBUS_SERVICE
,
CONNMAN_DBUS_PATH
,
CONNMAN_DBUS_INTERFACE
,
41
QDBusConnection::systemBus(), parent)
42
{
43
}
44
45
bool
QConnManInterfaceBase
::
connmanAvailable
()
46
{
47
return
QConnManInterfaceBase
().isValid();
48
}
49
50
QConnManInterface
::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(
72
CONNMAN_DBUS_SERVICE
,
CONNMAN_DBUS_PATH
,
CONNMAN_DBUS_INTERFACE_L1
, propertyChangedKey,
73
this
, SLOT(propertyChanged(QString,QDBusVariant)));
74
}
75
76
QConnManInterface
::~
QConnManInterface
()
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
83
QString
QConnManInterface
::
state
()
const
84
{
85
return
m_state;
86
}
87
88
QString
QConnManInterface
::
type
()
const
89
{
90
return
m_type;
91
}
92
93
void
QConnManInterface
::
setBackend
(
QConnManNetworkInformationBackend
*ourBackend)
94
{
95
backend = ourBackend;
96
}
97
98
void
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
112
QString
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
132
QT_END_NAMESPACE
133
134
#
include
"moc_qconnmanservice.cpp"
QConnManInterfaceBase
Definition
qconnmanservice.h:29
QConnManInterfaceBase::connmanAvailable
static bool connmanAvailable()
Definition
qconnmanservice.cpp:45
QConnManInterface::state
QString state() const
Definition
qconnmanservice.cpp:83
QConnManInterface::type
QString type() const
Definition
qconnmanservice.cpp:88
QConnManInterface::~QConnManInterface
~QConnManInterface()
Definition
qconnmanservice.cpp:76
QConnManInterface::setBackend
void setBackend(QConnManNetworkInformationBackend *ourBackend)
Definition
qconnmanservice.cpp:93
QConnManInterface::QConnManInterface
QConnManInterface(QObject *parent=nullptr)
Definition
qconnmanservice.cpp:50
QConnManNetworkInformationBackend
Definition
qconnmannetworkinformationbackend.h:25
CONNMAN_DBUS_INTERFACE_L1
#define CONNMAN_DBUS_INTERFACE_L1
Definition
qconnmanservice.cpp:22
CONNMAN_DBUS_INTERFACE
#define CONNMAN_DBUS_INTERFACE
Definition
qconnmanservice.cpp:21
CONNMAN_DBUS_PATH
#define CONNMAN_DBUS_PATH
Definition
qconnmanservice.cpp:23
CONNMAN_DBUS_SERVICE
#define CONNMAN_DBUS_SERVICE
Definition
qconnmanservice.cpp:20
qtbase
src
plugins
networkinformation
connman
qconnmanservice.cpp
Generated on
for Qt by
1.18.0