1
/* This file is part of the KDE project
2
Copyright (C) 2006,2008 Will Stephenson <wstephenson@kde.org>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License version 2 as published by the Free Software Foundation.
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Library General Public License for more details.
13
You should have received a copy of the GNU Library General Public License
14
along with this library; see the file COPYING.LIB. If not, write to
15
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16
Boston, MA 02110-1301, USA.
19
#include "fakenetworkmanager.h"
22
#include <QtXml/QtXml>
23
#include <QLatin1String>
25
#include <kstandarddirs.h>
28
#include "fakeaccesspoint.h"
29
#include "fakewirednetworkinterface.h"
30
#include "fakewirelessnetworkinterface.h"
32
FakeNetworkManager::FakeNetworkManager(QObject * parent, const QVariantList &)
33
: Solid::Control::Ifaces::NetworkManager(parent)
35
mUserNetworkingEnabled = true;
36
mUserWirelessEnabled = true;
37
mRfKillEnabled = false;
38
mUserWwanEnabled = false;
40
mXmlFile = KStandardDirs::locate("data", "solidfakebackend/fakenetworking.xml");
42
// QDBusConnection::sessionBus().registerObject("/org/kde/solid/fake", this, QDBusConnection::ExportNonScriptableSlots);
44
parseNetworkingFile();
47
FakeNetworkManager::FakeNetworkManager(QObject * parent, const QStringList &, const QString &xmlFile) : Solid::Control::Ifaces::NetworkManager(parent)
49
mUserNetworkingEnabled = true;
50
mUserWirelessEnabled = true;
51
mUserWwanEnabled = true;
55
if (mXmlFile.isEmpty())
57
kDebug() << "Falling back to installed networking xml";
58
mXmlFile = KStandardDirs::locate("data", "solidfakebackend/fakenetworking.xml");
60
parseNetworkingFile();
63
FakeNetworkManager::~FakeNetworkManager()
67
Solid::Networking::Status FakeNetworkManager::status() const
69
return Solid::Networking::Unknown;
72
QStringList FakeNetworkManager::networkInterfaces() const
74
return mNetworkInterfaces.keys();
77
QStringList FakeNetworkManager::activeNetworkInterfaces() const
79
QStringList activeDevices;
80
QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
84
FakeNetworkInterface * netDevice = it.value();
85
if (netDevice->isActive())
86
activeDevices.append(netDevice->uni());
91
QObject * FakeNetworkManager::createNetworkInterface(const QString & undi)
93
if (mNetworkInterfaces.contains(undi))
94
return mNetworkInterfaces[undi];
99
bool FakeNetworkManager::isWirelessEnabled() const
101
return mUserWirelessEnabled;
104
bool FakeNetworkManager::isNetworkingEnabled() const
106
QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
110
FakeNetworkInterface * netDevice = it.value();
111
if (netDevice->isActive())
117
bool FakeNetworkManager::isWirelessHardwareEnabled() const
119
return mRfKillEnabled;
122
bool FakeNetworkManager::isWwanEnabled() const
124
return mUserWwanEnabled;
127
bool FakeNetworkManager::isWwanHardwareEnabled() const
132
void FakeNetworkManager::setWirelessEnabled(bool enabled)
134
mUserWirelessEnabled = enabled;
137
void FakeNetworkManager::setWwanEnabled(bool enabled)
139
mUserWwanEnabled = enabled;
142
void FakeNetworkManager::setNetworkingEnabled(bool enabled)
144
QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
148
FakeNetworkInterface * netDevice = it.value();
150
// if ((netDevice->type() == Solid::Control::NetworkInterface::Ieee80211 && mUserWirelessEnabled)
151
// || netDevice->type() == Solid::Control::NetworkInterface::Ieee8023)
152
// netDevice->setActive(enabled);
154
mUserNetworkingEnabled = enabled;
157
void FakeNetworkManager::parseNetworkingFile()
159
QFile machineFile(mXmlFile);
160
if (!machineFile.open(QIODevice::ReadOnly))
162
kDebug() << "Error while opening " << mXmlFile;
166
QDomDocument fakeDocument;
167
if (!fakeDocument.setContent(&machineFile))
169
kDebug() << "Error while creating the QDomDocument.";
175
kDebug() << "Parsing fake computer XML: " << mXmlFile;
176
QDomElement mainElement = fakeDocument.documentElement();
177
QDomNode node = mainElement.firstChild();
178
while (!node.isNull())
180
QDomElement tempElement = node.toElement();
181
if (!tempElement.isNull() && tempElement.tagName() == QLatin1String("device"))
183
FakeNetworkInterface *tempDevice = parseDeviceElement(tempElement);
186
mNetworkInterfaces.insert(tempDevice->uni(), tempDevice);
187
// Use the DeviceManager for now, the udi/uni should
188
// emit deviceAdded(tempDevice->uni());
190
} else if (tempElement.tagName() == QLatin1String("property")) {
191
QString propertyKey = tempElement.attribute("key");
192
QVariant propertyValue = QVariant(tempElement.text());
193
if ( propertyKey== QLatin1String("networking")) {
194
mUserNetworkingEnabled = propertyValue.toBool();
195
} else if ( propertyKey== QLatin1String("wireless")) {
196
mUserWirelessEnabled = propertyValue.toBool();
197
} else if ( propertyKey== QLatin1String("rfkill")) {
198
mRfKillEnabled = propertyValue.toBool();
199
} else if ( propertyKey== QLatin1String("wwan")) {
200
mUserWwanEnabled = propertyValue.toBool();
201
} else if ( propertyKey== QLatin1String("wwanHardware")) {
202
mWwanEnabled = propertyValue.toBool();
205
node = node.nextSibling();
209
FakeNetworkInterface *FakeNetworkManager::parseDeviceElement(const QDomElement &deviceElement)
211
FakeNetworkInterface *device = 0;
212
QMap<QString,QVariant> propertyMap;
213
QString uni = deviceElement.attribute("uni");
214
propertyMap.insert("uni", uni);
215
kDebug() << "Listing device: " << uni;
216
propertyMap.insert("uni", QVariant(uni));
217
QList< FakeAccessPoint * > networks;
218
bool wireless = false;
219
QDomNode childNode = deviceElement.firstChild();
220
while (!childNode.isNull())
222
QDomElement childElement = childNode.toElement();
223
//kDebug() << "found child=" << childElement.tagName();
224
if (!childElement.isNull() && childElement.tagName() == QLatin1String("property"))
227
QVariant propertyValue;
229
propertyKey = childElement.attribute("key");
230
propertyValue = QVariant(childElement.text());
231
if ( propertyValue == "ieee80211" ) {
234
//kDebug() << "Got property key=" << propertyKey << ", value=" << propertyValue.toString();
235
propertyMap.insert(propertyKey, propertyValue);
237
else if (!childElement.isNull() && childElement.tagName() == QLatin1String("accesspoint"))
239
QString uni = childElement.attribute("uni");
240
kDebug() << "Listing properties: " << uni;
241
FakeAccessPoint * wifi = new FakeAccessPoint(parseAPElement(childElement), this);
242
networks.append(wifi);
244
childNode = childNode.nextSibling();
246
//kDebug() << "Done listing. ";
248
/* if (!propertyMap.isEmpty())
250
kDebug() << "Creating FakeNetworkDevice for " << uni;
252
FakeWirelessNetworkInterface * wifi = new FakeWirelessNetworkInterface(propertyMap);
253
foreach( FakeAccessPoint * net, networks)
255
kDebug() << "Injecting " << net->uni();
256
wifi->injectAccessPoint(net);
260
device = new FakeWiredNetworkInterface(propertyMap);
269
QMap<QString,QVariant> FakeNetworkManager::parseAPElement(const QDomElement &deviceElement)
271
QMap<QString,QVariant> propertyMap;
273
QDomNode propertyNode = deviceElement.firstChild();
274
while (!propertyNode.isNull())
276
QDomElement propertyElement = propertyNode.toElement();
277
if (!propertyElement.isNull() && propertyElement.tagName() == QLatin1String("property"))
280
QVariant propertyValue;
282
propertyKey = propertyElement.attribute("key");
283
propertyValue = QVariant(propertyElement.text());
284
//kDebug() << "Got property key=" << propertyKey << ", value=" << propertyValue.toString();
285
propertyMap.insert(propertyKey, propertyValue);
288
propertyNode = propertyNode.nextSibling();
293
void FakeNetworkManager::activateConnection(const QString & interfaceUni, const QString & connectionUni, const QVariantMap & connectionParameters)
295
Q_UNUSED(interfaceUni);
296
Q_UNUSED(connectionUni);
297
Q_UNUSED(connectionParameters);
299
mActiveConnections.append(connectionUni);
300
QTimer::singleShot(0, this, SIGNAL(activeConnectionsChanged()));
303
void FakeNetworkManager::deactivateConnection(const QString & activeConnection)
305
mActiveConnections.removeAll(activeConnection);
308
QStringList FakeNetworkManager::activeConnections() const
310
return mActiveConnections;
313
Solid::Control::NetworkInterface::Types FakeNetworkManager::supportedInterfaceTypes() const
315
return (Solid::Control::NetworkInterface::Types) (
316
Solid::Control::NetworkInterface::Ieee80211 |
317
Solid::Control::NetworkInterface::Ieee8023 |
318
Solid::Control::NetworkInterface::Gsm |
319
Solid::Control::NetworkInterface::Cdma |
320
Solid::Control::NetworkInterface::Serial
324
#include "fakenetworkmanager.moc"