1
/* This file is part of the KDE project
2
Copyright (C) 2006 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.
21
#include <QLatin1String>
23
#include <kstandarddirs.h>
26
#include "fakenetworkmanager.h"
27
#include "fakenetwork.h"
28
#include "fakewirelessnetwork.h"
30
FakeAuthenticationValidator::FakeAuthenticationValidator(QObject * parent) : QObject(parent)
34
FakeAuthenticationValidator::~FakeAuthenticationValidator()
38
bool FakeAuthenticationValidator::validate(const Solid::Control::Authentication *)
43
FakeNetworkManager::FakeNetworkManager(QObject * parent, const QStringList &) : Solid::Control::Ifaces::NetworkManager(parent), mAuthValidator(0)
45
mUserNetworkingEnabled = true;
46
mUserWirelessEnabled = true;
48
mXmlFile = KStandardDirs::locate("data", "solidfakebackend/fakenetworking.xml");
50
// QDBusConnection::sessionBus().registerObject("/org/kde/solid/fake", this, QDBusConnection::ExportNonScriptableSlots);
52
parseNetworkingFile();
55
FakeNetworkManager::FakeNetworkManager(QObject * parent, const QStringList &, const QString &xmlFile) : Solid::Control::Ifaces::NetworkManager(parent), mAuthValidator(0)
57
mUserNetworkingEnabled = true;
58
mUserWirelessEnabled = true;
61
if (mXmlFile.isEmpty())
63
kDebug() << "Falling back to installed networking xml";
64
mXmlFile = KStandardDirs::locate("data", "solidfakebackend/fakenetworking.xml");
66
parseNetworkingFile();
69
FakeNetworkManager::~FakeNetworkManager()
71
delete mAuthValidator;
74
QStringList FakeNetworkManager::networkInterfaces() const
76
return mNetworkInterfaces.keys();
79
QStringList FakeNetworkManager::activeNetworkInterfaces() const
81
QStringList activeDevices;
82
QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
86
FakeNetworkInterface * netDevice = it.value();
87
if (netDevice->isActive())
88
activeDevices.append(netDevice->uni());
93
QObject * FakeNetworkManager::createNetworkInterface(const QString & undi)
95
if (mNetworkInterfaces.contains(undi))
96
return mNetworkInterfaces[undi];
101
QObject * FakeNetworkManager::createAuthenticationValidator()
103
if (mAuthValidator == 0)
104
mAuthValidator = new FakeAuthenticationValidator(this);
105
return mAuthValidator;
108
bool FakeNetworkManager::isWirelessEnabled() const
110
QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
114
FakeNetworkInterface * netDevice = it.value();
115
if (netDevice->type() == Solid::Control::NetworkInterface::Ieee80211)
116
if (netDevice->isActive())
122
bool FakeNetworkManager::isNetworkingEnabled() const
124
QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
128
FakeNetworkInterface * netDevice = it.value();
129
if (netDevice->isActive())
135
void FakeNetworkManager::setWirelessEnabled(bool enabled)
137
QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
138
if (mUserNetworkingEnabled)
143
FakeNetworkInterface * netDevice = it.value();
144
if (netDevice->type() == Solid::Control::NetworkInterface::Ieee80211)
145
netDevice->setActive(enabled);
148
mUserWirelessEnabled = enabled;
151
void FakeNetworkManager::setNetworkingEnabled(bool enabled)
153
QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
157
FakeNetworkInterface * netDevice = it.value();
158
if ((netDevice->type() == Solid::Control::NetworkInterface::Ieee80211 && mUserWirelessEnabled)
159
|| netDevice->type() == Solid::Control::NetworkInterface::Ieee8023)
160
netDevice->setActive(enabled);
162
mUserNetworkingEnabled = enabled;
165
void FakeNetworkManager::notifyHiddenNetwork(const QString & essid)
167
// look up the device hosting the hidden net.
168
// move the hidden net into the device's networks list
171
void FakeNetworkManager::parseNetworkingFile()
173
QFile machineFile(mXmlFile);
174
if (!machineFile.open(QIODevice::ReadOnly))
176
kDebug() << "Error while opening " << mXmlFile;
180
QDomDocument fakeDocument;
181
if (!fakeDocument.setContent(&machineFile))
183
kDebug() << "Error while creating the QDomDocument.";
189
kDebug() << "Parsing fake computer XML: " << mXmlFile;
190
QDomElement mainElement = fakeDocument.documentElement();
191
QDomNode node = mainElement.firstChild();
192
while (!node.isNull())
194
QDomElement tempElement = node.toElement();
195
if (!tempElement.isNull() && tempElement.tagName() == QLatin1String("device"))
197
FakeNetworkInterface *tempDevice = parseDeviceElement(tempElement);
200
mNetworkInterfaces.insert(tempDevice->uni(), tempDevice);
201
// Use the DeviceManager for now, the udi/uni should
202
// emit deviceAdded(tempDevice->uni());
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< FakeNetwork * > networks;
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
//kDebug() << "Got property key=" << propertyKey << ", value=" << propertyValue.toString();
232
propertyMap.insert(propertyKey, propertyValue);
234
else if (!childElement.isNull() && childElement.tagName() == QLatin1String("network"))
236
QString uni = childElement.attribute("uni");
237
kDebug() << "Listing properties: " << uni;
238
FakeNetwork * net = new FakeNetwork(uni, parseNetworkElement(childElement));
239
networks.append(net);
241
else if (!childElement.isNull() && childElement.tagName() == QLatin1String("wireless"))
243
QString uni = childElement.attribute("uni");
244
kDebug() << "Listing properties: " << uni;
245
FakeNetwork * wifi = new FakeWirelessNetwork(uni, parseNetworkElement(childElement));
246
networks.append(wifi);
248
childNode = childNode.nextSibling();
250
//kDebug() << "Done listing. ";
252
/* if (!propertyMap.isEmpty())
254
kDebug() << "Creating FakeNetworkDevice for " << uni;
255
device = new FakeNetworkInterface(propertyMap);
256
QListIterator< FakeNetwork * > it (networks);
259
FakeNetwork * net = it.next();
260
kDebug() << "Injecting " << net->uni();
261
device->injectNetwork(net->uni(), net);
269
QMap<QString,QVariant> FakeNetworkManager::parseNetworkElement(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();
294
#include "fakenetworkmanager.moc"