Repository navigation
Expand file tree
/
Copy pathnetworkmanager.cpp
More file actions
288 lines (230 loc) · 8.01 KB
/
Copy pathnetworkmanager.cpp
File metadata and controls
288 lines (230 loc) · 8.01 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* This file is part of the KDE project
Copyright (C) 2008 Dario Freddi <drf54321@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "networkmanager.h"
#include "wicd-defines.h"
#include "wicdcustomtypes.h"
#include "wicddbusinterface.h"
#include "wirednetworkinterface.h"
#include "wirelessnetworkinterface.h"
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusConnectionInterface>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusMetaType>
#include <QtDBus/QDBusReply>
#include <QProcess>
#include <kdebug.h>
class WicdNetworkManagerPrivate
{
public:
WicdNetworkManagerPrivate();
bool recacheState();
Solid::Networking::Status cachedState;
QHash<QString, WicdNetworkInterface *> interfaces;
};
WicdNetworkManagerPrivate::WicdNetworkManagerPrivate()
: cachedState(Solid::Networking::Unknown)
{
recacheState();
}
bool WicdNetworkManagerPrivate::recacheState()
{
QDBusMessage message = WicdDbusInterface::instance()->daemon().call("GetConnectionStatus");
WicdConnectionInfo s;
message.arguments().at(0).value<QDBusArgument>() >> s;
kDebug() << "State: " << s.status << " Info: " << s.info;
Solid::Networking::Status state;
switch (static_cast<Wicd::ConnectionStatus>(s.status)) {
case Wicd::CONNECTING:
state = Solid::Networking::Connecting;
break;
case Wicd::WIRED:
case Wicd::WIRELESS:
state = Solid::Networking::Connected;
break;
case Wicd::NOT_CONNECTED:
state = Solid::Networking::Unconnected;
break;
default:
case Wicd::SUSPENDED:
state = Solid::Networking::Unknown;
break;
}
if (state != cachedState) {
cachedState = state;
return true;
} else {
return false;
}
}
WicdNetworkManager::WicdNetworkManager(QObject * parent, const QVariantList & /*args */)
: NetworkManager(parent), d(new WicdNetworkManagerPrivate())
{
qDBusRegisterMetaType<WicdConnectionInfo>();
QDBusConnection::systemBus().connect(WICD_DBUS_SERVICE, WICD_DAEMON_DBUS_PATH, WICD_DAEMON_DBUS_INTERFACE,
"StatusChanged", this, SLOT(refreshStatus()));
}
WicdNetworkManager::~WicdNetworkManager()
{
delete d;
}
void WicdNetworkManager::refreshStatus()
{
if (d->recacheState()) {
emit statusChanged(d->cachedState);
}
}
Solid::Networking::Status WicdNetworkManager::status() const
{
return d->cachedState;
}
QStringList WicdNetworkManager::networkInterfaces() const
{
// Let's parse ifconfig here
QProcess ifconfig;
ifconfig.start(QString("ifconfig -a"));
ifconfig.waitForFinished();
QString result = ifconfig.readAllStandardOutput();
QStringList lines = result.split('\n');
QStringList ifaces;
bool enterIface = true;
foreach(const QString &line, lines) {
if (enterIface) {
if (!line.split(' ').at(0).isEmpty()) {
if (line.split(' ').at(0) != "lo" && !line.split(' ').at(0).contains("wmaster")) {
ifaces.append(line.split(' ').at(0));
}
}
enterIface = false;
}
if (line.isEmpty()) {
enterIface = true;
}
}
return ifaces;
}
QObject * WicdNetworkManager::createNetworkInterface(const QString & uni)
{
kDebug(1441) << uni;
if (!networkInterfaces().contains(uni)) {
kDebug() << "Interface not present in the available list, returning 0";
return 0;
}
WicdNetworkInterface * netInterface = 0;
QHash<QString, WicdNetworkInterface *>::Iterator it = d->interfaces.find(uni);
if (it == d->interfaces.end()) {
kDebug() << "unknown interface:" << uni << "creating it";
} else {
kDebug() << "Interface already created";
return it.value();
}
// Let's parse iwconfig here
QProcess iwconfig;
iwconfig.start(QString("iwconfig"));
iwconfig.waitForFinished();
QString result = iwconfig.readAllStandardError();
QStringList lines = result.split('\n');
QStringList wired;
foreach(const QString &line, lines) {
if (!line.isEmpty()) {
wired << line.split(' ').at(0);
}
}
if (wired.contains(uni)) {
kDebug() << "Wired interface";
netInterface = new WicdWiredNetworkInterface(uni);
} else {
kDebug() << "Wireless interface";
netInterface = new WicdWirelessNetworkInterface(uni);
}
if (netInterface) {
kDebug() << "Interface created successfully";
//netInterface->setManagerInterface(&WicdDbusInterface::instance()->daemon());
d->interfaces[uni] = netInterface;
}
return netInterface;
}
bool WicdNetworkManager::isNetworkingEnabled() const
{
return Solid::Networking::Connecting == d->cachedState ||
Solid::Networking::Connected == d->cachedState ||
Solid::Networking::Unconnected == d->cachedState;
}
bool WicdNetworkManager::isWirelessEnabled() const
{
QDBusReply< bool > state = WicdDbusInterface::instance()->wireless().call("IsWirelessUp");
if (state.isValid()) {
return state.value();
}
return false;
}
bool WicdNetworkManager::isWirelessHardwareEnabled() const
{
QDBusReply< bool > state = WicdDbusInterface::instance()->wireless().call("GetKillSwitchEnabled");
if (state.isValid()) {
return !state.value();
}
return false;
}
QStringList WicdNetworkManager::activeConnections() const
{
QStringList activeConnections;
QHash<QString, WicdNetworkInterface *>::ConstIterator it = d->interfaces.constBegin(), itEnd = d->interfaces.constEnd();
for (; it != itEnd; ++it) {
WicdNetworkInterface * interface = it.value();
if (interface && interface->isActive()) {
activeConnections << it.key();
}
}
return activeConnections;
}
void WicdNetworkManager::activateConnection(const QString & interfaceUni, const QString & connectionUni,
const QVariantMap & connectionParameters)
{
kDebug(1441) << interfaceUni << connectionUni << connectionParameters;
QHash<QString, WicdNetworkInterface *>::ConstIterator it = d->interfaces.find(interfaceUni);
if (it != d->interfaces.end()) {
WicdNetworkInterface * interface = it.value();
if (!interface) {
interface = qobject_cast<WicdNetworkInterface *>(createNetworkInterface(interfaceUni));
}
if (interface) {
bool activated = interface->activateConnection(connectionUni, connectionParameters);
Q_UNUSED(activated)
}
}
}
void WicdNetworkManager::deactivateConnection(const QString & activeConnection)
{
kDebug(1441) << activeConnection;
QHash<QString, WicdNetworkInterface *>::ConstIterator it = d->interfaces.find(activeConnection);
if (it != d->interfaces.end() && it.value()) {
WicdNetworkInterface * interface = it.value();
bool deactivated = interface->deactivateConnection();
Q_UNUSED(deactivated)
}
}
void WicdNetworkManager::setNetworkingEnabled(bool enabled)
{
WicdDbusInterface::instance()->daemon().call("SetSuspend", !enabled);
}
void WicdNetworkManager::setWirelessEnabled(bool enabled)
{
if (enabled) {
WicdDbusInterface::instance()->wireless().call("EnableWirelessInterface");
} else {
WicdDbusInterface::instance()->wireless().call("DisableWirelessInterface");
}
}
#include "networkmanager.moc"