add more spacing
[personal-kdebase.git] / workspace / libs / solid / control / backends / fakenet / fakenetworkmanager.cpp
bloba5f548158a21d6553b1bc80a186222b931b67c0d
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"
21 #include <QFile>
22 #include <QtXml/QtXml>
23 #include <QLatin1String>
25 #include <kstandarddirs.h>
26 #include <kdebug.h>
28 #include "fakeaccesspoint.h"
29 #include "fakewirednetworkinterface.h"
30 #include "fakewirelessnetworkinterface.h"
32 FakeNetworkManager::FakeNetworkManager(QObject * parent, const QStringList &) : Solid::Control::Ifaces::NetworkManager(parent)
34 mUserNetworkingEnabled = true;
35 mUserWirelessEnabled = true;
36 mRfKillEnabled = false;
37 mXmlFile = KStandardDirs::locate("data", "solidfakebackend/fakenetworking.xml");
39 // QDBusConnection::sessionBus().registerObject("/org/kde/solid/fake", this, QDBusConnection::ExportNonScriptableSlots);
41 parseNetworkingFile();
44 FakeNetworkManager::FakeNetworkManager(QObject * parent, const QStringList &, const QString &xmlFile) : Solid::Control::Ifaces::NetworkManager(parent)
46 mUserNetworkingEnabled = true;
47 mUserWirelessEnabled = true;
49 mXmlFile = xmlFile;
50 if (mXmlFile.isEmpty())
52 kDebug() << "Falling back to installed networking xml";
53 mXmlFile = KStandardDirs::locate("data", "solidfakebackend/fakenetworking.xml");
55 parseNetworkingFile();
58 FakeNetworkManager::~FakeNetworkManager()
62 Solid::Networking::Status FakeNetworkManager::status() const
64 return Solid::Networking::Unknown;
67 QStringList FakeNetworkManager::networkInterfaces() const
69 return mNetworkInterfaces.keys();
72 QStringList FakeNetworkManager::activeNetworkInterfaces() const
74 QStringList activeDevices;
75 QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
76 while (it.hasNext())
78 it.next();
79 FakeNetworkInterface * netDevice = it.value();
80 if (netDevice->isActive())
81 activeDevices.append(netDevice->uni());
83 return activeDevices;
86 QObject * FakeNetworkManager::createNetworkInterface(const QString & undi)
88 if (mNetworkInterfaces.contains(undi))
89 return mNetworkInterfaces[undi];
90 else
91 return 0;
94 bool FakeNetworkManager::isWirelessEnabled() const
96 return mUserWirelessEnabled;
99 bool FakeNetworkManager::isNetworkingEnabled() const
101 QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
102 while (it.hasNext())
104 it.next();
105 FakeNetworkInterface * netDevice = it.value();
106 if (netDevice->isActive())
107 return true;
109 return false;
112 bool FakeNetworkManager::isWirelessHardwareEnabled() const
114 return mRfKillEnabled;
117 void FakeNetworkManager::setWirelessEnabled(bool enabled)
119 mUserWirelessEnabled = enabled;
122 void FakeNetworkManager::setNetworkingEnabled(bool enabled)
124 QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
125 while (it.hasNext())
127 it.next();
128 FakeNetworkInterface * netDevice = it.value();
129 // if ((netDevice->type() == Solid::Control::NetworkInterface::Ieee80211 && mUserWirelessEnabled)
130 // || netDevice->type() == Solid::Control::NetworkInterface::Ieee8023)
131 // netDevice->setActive(enabled);
133 mUserNetworkingEnabled = enabled;
136 void FakeNetworkManager::parseNetworkingFile()
138 QFile machineFile(mXmlFile);
139 if (!machineFile.open(QIODevice::ReadOnly))
141 kDebug() << "Error while opening " << mXmlFile;
142 return;
145 QDomDocument fakeDocument;
146 if (!fakeDocument.setContent(&machineFile))
148 kDebug() << "Error while creating the QDomDocument.";
149 machineFile.close();
150 return;
152 machineFile.close();
154 kDebug() << "Parsing fake computer XML: " << mXmlFile;
155 QDomElement mainElement = fakeDocument.documentElement();
156 QDomNode node = mainElement.firstChild();
157 while (!node.isNull())
159 QDomElement tempElement = node.toElement();
160 if (!tempElement.isNull() && tempElement.tagName() == QLatin1String("device"))
162 FakeNetworkInterface *tempDevice = parseDeviceElement(tempElement);
163 if(tempDevice)
165 mNetworkInterfaces.insert(tempDevice->uni(), tempDevice);
166 // Use the DeviceManager for now, the udi/uni should
167 // emit deviceAdded(tempDevice->uni());
169 } else if (tempElement.tagName() == QLatin1String("property")) {
170 QString propertyKey = tempElement.attribute("key");
171 QVariant propertyValue = QVariant(tempElement.text());
172 if ( propertyKey== QLatin1String("networking")) {
173 mUserNetworkingEnabled = propertyValue.toBool();
174 } else if ( propertyKey== QLatin1String("wireless")) {
175 mUserWirelessEnabled = propertyValue.toBool();
176 } else if ( propertyKey== QLatin1String("rfkill")) {
177 mRfKillEnabled = propertyValue.toBool();
180 node = node.nextSibling();
184 FakeNetworkInterface *FakeNetworkManager::parseDeviceElement(const QDomElement &deviceElement)
186 FakeNetworkInterface *device = 0;
187 QMap<QString,QVariant> propertyMap;
188 QString uni = deviceElement.attribute("uni");
189 propertyMap.insert("uni", uni);
190 kDebug() << "Listing device: " << uni;
191 propertyMap.insert("uni", QVariant(uni));
192 QList< FakeAccessPoint * > networks;
193 bool wireless = false;
194 QDomNode childNode = deviceElement.firstChild();
195 while (!childNode.isNull())
197 QDomElement childElement = childNode.toElement();
198 //kDebug() << "found child=" << childElement.tagName();
199 if (!childElement.isNull() && childElement.tagName() == QLatin1String("property"))
201 QString propertyKey;
202 QVariant propertyValue;
204 propertyKey = childElement.attribute("key");
205 propertyValue = QVariant(childElement.text());
206 if ( propertyValue == "ieee80211" ) {
207 wireless = true;
209 //kDebug() << "Got property key=" << propertyKey << ", value=" << propertyValue.toString();
210 propertyMap.insert(propertyKey, propertyValue);
212 else if (!childElement.isNull() && childElement.tagName() == QLatin1String("accesspoint"))
214 QString uni = childElement.attribute("uni");
215 kDebug() << "Listing properties: " << uni;
216 FakeAccessPoint * wifi = new FakeAccessPoint(parseAPElement(childElement), this);
217 networks.append(wifi);
219 childNode = childNode.nextSibling();
221 //kDebug() << "Done listing. ";
223 /* if (!propertyMap.isEmpty())
224 { */
225 kDebug() << "Creating FakeNetworkDevice for " << uni;
226 if (wireless) {
227 FakeWirelessNetworkInterface * wifi = new FakeWirelessNetworkInterface(propertyMap);
228 foreach( FakeAccessPoint * net, networks)
230 kDebug() << "Injecting " << net->uni();
231 wifi->injectAccessPoint(net);
233 device = wifi;
234 } else {
235 device = new FakeWiredNetworkInterface(propertyMap);
239 // }
241 return device;
244 QMap<QString,QVariant> FakeNetworkManager::parseAPElement(const QDomElement &deviceElement)
246 QMap<QString,QVariant> propertyMap;
248 QDomNode propertyNode = deviceElement.firstChild();
249 while (!propertyNode.isNull())
251 QDomElement propertyElement = propertyNode.toElement();
252 if (!propertyElement.isNull() && propertyElement.tagName() == QLatin1String("property"))
254 QString propertyKey;
255 QVariant propertyValue;
257 propertyKey = propertyElement.attribute("key");
258 propertyValue = QVariant(propertyElement.text());
259 //kDebug() << "Got property key=" << propertyKey << ", value=" << propertyValue.toString();
260 propertyMap.insert(propertyKey, propertyValue);
263 propertyNode = propertyNode.nextSibling();
265 return propertyMap;
268 void FakeNetworkManager::activateConnection(const QString & interfaceUni, const QString & connectionUni, const QVariantMap & connectionParameters)
270 mActiveConnections.append(connectionUni);
271 QTimer::singleShot(0, this, SIGNAL(activeConnectionsChanged()));
274 void FakeNetworkManager::deactivateConnection(const QString & activeConnection)
276 mActiveConnections.removeAll(activeConnection);
279 QStringList FakeNetworkManager::activeConnections() const
281 return mActiveConnections;
284 #include "fakenetworkmanager.moc"