/
connectivitymonitor.cpp
165 lines (136 loc) · 5.6 KB
/
connectivitymonitor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/***************************************************************************
**
** Copyright (c) 2016 Jolla Ltd.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#include "connectivitymonitor.h"
#include "connmanmanagerproxy.h"
#include "connmanserviceproxy.h"
#include <QDBusMessage>
#include <QDBusConnection>
ConnectivityMonitor::ConnectivityMonitor(QObject *parent)
: QObject(parent)
, connmanManager_(new ConnmanManagerProxy("net.connman", "/", QDBusConnection::systemBus(), this))
{
qDBusRegisterMetaType<PathProperties>();
qDBusRegisterMetaType<PathPropertiesArray>();
connect(connmanManager_, &ConnmanManagerProxy::ServicesChanged,
this, [this](const PathPropertiesArray &services, const QList<QDBusObjectPath> &removedPaths) {
const QList<QString> original(activeConnectionTypes());
foreach (const QDBusObjectPath &objectPath, removedPaths) {
const QString path(objectPath.path());
serviceInactive(path);
forgetService(path);
}
for (const PathProperties &service : services) {
const QString &path(service.first.path());
const QVariantMap &properties(service.second);
const QString type(properties[QStringLiteral("Type")].toString());
if (type == QStringLiteral("cellular") || type == QStringLiteral("wifi")) {
const QString state(properties[QStringLiteral("State")].toString());
if (state == QStringLiteral("online")) {
serviceActive(type, path);
} else {
serviceInactive(path);
}
monitorService(type, path);
}
}
if (activeConnectionTypes() != original) {
emit connectivityChanged(activeConnectionTypes());
}
});
fetchServiceList();
}
ConnectivityMonitor::~ConnectivityMonitor()
{
}
QList<QString> ConnectivityMonitor::activeConnectionTypes() const
{
return activeServices_.keys().toSet().toList();
}
void ConnectivityMonitor::fetchServiceList()
{
QDBusPendingCall call = connmanManager_->GetServices();
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<PathPropertiesArray> reply = *watcher;
watcher->deleteLater();
if (reply.isError()) {
qWarning() << "Unable to fetch Connman services:" << reply.error().message();
} else {
const PathPropertiesArray &services(reply.value());
for (const PathProperties &service : services) {
const QString &path(service.first.path());
const QVariantMap &properties(service.second);
const QString type(properties[QStringLiteral("Type")].toString());
if (type == QStringLiteral("cellular") || type == QStringLiteral("wifi")) {
const QString state(properties[QStringLiteral("State")].toString());
if (state == QStringLiteral("online")) {
const QString name(properties[QStringLiteral("Name")].toString());
serviceActive(type, path);
}
monitorService(type, path);
}
}
if (!activeServices_.isEmpty()) {
emit connectivityChanged(activeConnectionTypes());
}
}
});
}
void ConnectivityMonitor::monitorService(const QString &type, const QString &path)
{
auto it = connmanServices_.find(path);
if (it == connmanServices_.end()) {
ConnmanServiceProxy *proxy = new ConnmanServiceProxy("net.connman", path, QDBusConnection::systemBus(), nullptr);
connmanServices_.insert(path, proxy);
connect(proxy, &ConnmanServiceProxy::PropertyChanged, this, [this, type, path](const QString &name, const QDBusVariant &value) {
if (name == QStringLiteral("State")) {
const QList<QString> original(activeConnectionTypes());
const QString state = value.variant().toString();
if (state == QStringLiteral("online")) {
serviceActive(type, path);
} else {
serviceInactive(path);
}
if (activeConnectionTypes() != original) {
emit connectivityChanged(activeConnectionTypes());
}
}
});
}
}
void ConnectivityMonitor::forgetService(const QString &path)
{
auto it = connmanServices_.find(path);
if (it != connmanServices_.end()) {
ConnmanServiceProxy *proxy = (*it);
delete proxy;
connmanServices_.erase(it);
}
}
void ConnectivityMonitor::serviceActive(const QString &type, const QString &path)
{
QList<QString> &services(activeServices_[type]);
if (!services.contains(path)) {
services.append(path);
}
}
void ConnectivityMonitor::serviceInactive(const QString &path)
{
for (auto ait = activeServices_.begin(); ait != activeServices_.end(); ) {
QList<QString> &services(ait.value());
if ((services.removeAll(path) > 0) && services.isEmpty()) {
ait = activeServices_.erase(ait);
} else {
++ait;
}
}
}