libsolidcontrol
fakenetworkmanager.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "fakenetworkmanager.h"
00020
00021 #include <QFile>
00022 #include <QtXml/QtXml>
00023 #include <QLatin1String>
00024
00025 #include <kstandarddirs.h>
00026 #include <kdebug.h>
00027
00028 #include "fakeaccesspoint.h"
00029 #include "fakewirednetworkinterface.h"
00030 #include "fakewirelessnetworkinterface.h"
00031
00032 FakeNetworkManager::FakeNetworkManager(QObject * parent, const QStringList &) : Solid::Control::Ifaces::NetworkManager(parent)
00033 {
00034 mUserNetworkingEnabled = true;
00035 mUserWirelessEnabled = true;
00036 mRfKillEnabled = false;
00037 mXmlFile = KStandardDirs::locate("data", "solidfakebackend/fakenetworking.xml");
00038
00039
00040
00041 parseNetworkingFile();
00042 }
00043
00044 FakeNetworkManager::FakeNetworkManager(QObject * parent, const QStringList &, const QString &xmlFile) : Solid::Control::Ifaces::NetworkManager(parent)
00045 {
00046 mUserNetworkingEnabled = true;
00047 mUserWirelessEnabled = true;
00048
00049 mXmlFile = xmlFile;
00050 if (mXmlFile.isEmpty())
00051 {
00052 kDebug() << "Falling back to installed networking xml";
00053 mXmlFile = KStandardDirs::locate("data", "solidfakebackend/fakenetworking.xml");
00054 }
00055 parseNetworkingFile();
00056 }
00057
00058 FakeNetworkManager::~FakeNetworkManager()
00059 {
00060 }
00061
00062 Solid::Networking::Status FakeNetworkManager::status() const
00063 {
00064 return Solid::Networking::Unknown;
00065 }
00066
00067 QStringList FakeNetworkManager::networkInterfaces() const
00068 {
00069 return mNetworkInterfaces.keys();
00070 }
00071
00072 QStringList FakeNetworkManager::activeNetworkInterfaces() const
00073 {
00074 QStringList activeDevices;
00075 QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
00076 while (it.hasNext())
00077 {
00078 it.next();
00079 FakeNetworkInterface * netDevice = it.value();
00080 if (netDevice->isActive())
00081 activeDevices.append(netDevice->uni());
00082 }
00083 return activeDevices;
00084 }
00085
00086 QObject * FakeNetworkManager::createNetworkInterface(const QString & undi)
00087 {
00088 if (mNetworkInterfaces.contains(undi))
00089 return mNetworkInterfaces[undi];
00090 else
00091 return 0;
00092 }
00093
00094 bool FakeNetworkManager::isWirelessEnabled() const
00095 {
00096 return mUserWirelessEnabled;
00097 }
00098
00099 bool FakeNetworkManager::isNetworkingEnabled() const
00100 {
00101 QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
00102 while (it.hasNext())
00103 {
00104 it.next();
00105 FakeNetworkInterface * netDevice = it.value();
00106 if (netDevice->isActive())
00107 return true;
00108 }
00109 return false;
00110 }
00111
00112 bool FakeNetworkManager::isWirelessHardwareEnabled() const
00113 {
00114 return mRfKillEnabled;
00115 }
00116
00117 void FakeNetworkManager::setWirelessEnabled(bool enabled)
00118 {
00119 mUserWirelessEnabled = enabled;
00120 }
00121
00122 void FakeNetworkManager::setNetworkingEnabled(bool enabled)
00123 {
00124 QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
00125 while (it.hasNext())
00126 {
00127 it.next();
00128 FakeNetworkInterface * netDevice = it.value();
00129
00130
00131
00132 }
00133 mUserNetworkingEnabled = enabled;
00134 }
00135
00136 void FakeNetworkManager::parseNetworkingFile()
00137 {
00138 QFile machineFile(mXmlFile);
00139 if (!machineFile.open(QIODevice::ReadOnly))
00140 {
00141 kDebug() << "Error while opening " << mXmlFile;
00142 return;
00143 }
00144
00145 QDomDocument fakeDocument;
00146 if (!fakeDocument.setContent(&machineFile))
00147 {
00148 kDebug() << "Error while creating the QDomDocument.";
00149 machineFile.close();
00150 return;
00151 }
00152 machineFile.close();
00153
00154 kDebug() << "Parsing fake computer XML: " << mXmlFile;
00155 QDomElement mainElement = fakeDocument.documentElement();
00156 QDomNode node = mainElement.firstChild();
00157 while (!node.isNull())
00158 {
00159 QDomElement tempElement = node.toElement();
00160 if (!tempElement.isNull() && tempElement.tagName() == QLatin1String("device"))
00161 {
00162 FakeNetworkInterface *tempDevice = parseDeviceElement(tempElement);
00163 if(tempDevice)
00164 {
00165 mNetworkInterfaces.insert(tempDevice->uni(), tempDevice);
00166
00167
00168 }
00169 } else if (tempElement.tagName() == QLatin1String("property")) {
00170 QString propertyKey = tempElement.attribute("key");
00171 QVariant propertyValue = QVariant(tempElement.text());
00172 if ( propertyKey== QLatin1String("networking")) {
00173 mUserNetworkingEnabled = propertyValue.toBool();
00174 } else if ( propertyKey== QLatin1String("wireless")) {
00175 mUserWirelessEnabled = propertyValue.toBool();
00176 } else if ( propertyKey== QLatin1String("rfkill")) {
00177 mRfKillEnabled = propertyValue.toBool();
00178 }
00179 }
00180 node = node.nextSibling();
00181 }
00182 }
00183
00184 FakeNetworkInterface *FakeNetworkManager::parseDeviceElement(const QDomElement &deviceElement)
00185 {
00186 FakeNetworkInterface *device = 0;
00187 QMap<QString,QVariant> propertyMap;
00188 QString uni = deviceElement.attribute("uni");
00189 propertyMap.insert("uni", uni);
00190 kDebug() << "Listing device: " << uni;
00191 propertyMap.insert("uni", QVariant(uni));
00192 QList< FakeAccessPoint * > networks;
00193 bool wireless = false;
00194 QDomNode childNode = deviceElement.firstChild();
00195 while (!childNode.isNull())
00196 {
00197 QDomElement childElement = childNode.toElement();
00198
00199 if (!childElement.isNull() && childElement.tagName() == QLatin1String("property"))
00200 {
00201 QString propertyKey;
00202 QVariant propertyValue;
00203
00204 propertyKey = childElement.attribute("key");
00205 propertyValue = QVariant(childElement.text());
00206 if ( propertyValue == "ieee80211" ) {
00207 wireless = true;
00208 }
00209
00210 propertyMap.insert(propertyKey, propertyValue);
00211 }
00212 else if (!childElement.isNull() && childElement.tagName() == QLatin1String("accesspoint"))
00213 {
00214 QString uni = childElement.attribute("uni");
00215 kDebug() << "Listing properties: " << uni;
00216 FakeAccessPoint * wifi = new FakeAccessPoint(parseAPElement(childElement), this);
00217 networks.append(wifi);
00218 }
00219 childNode = childNode.nextSibling();
00220 }
00221
00222
00223
00224
00225 kDebug() << "Creating FakeNetworkDevice for " << uni;
00226 if (wireless) {
00227 FakeWirelessNetworkInterface * wifi = new FakeWirelessNetworkInterface(propertyMap);
00228 foreach( FakeAccessPoint * net, networks)
00229 {
00230 kDebug() << "Injecting " << net->uni();
00231 wifi->injectAccessPoint(net);
00232 }
00233 device = wifi;
00234 } else {
00235 device = new FakeWiredNetworkInterface(propertyMap);
00236 }
00237
00238
00239
00240
00241 return device;
00242 }
00243
00244 QMap<QString,QVariant> FakeNetworkManager::parseAPElement(const QDomElement &deviceElement)
00245 {
00246 QMap<QString,QVariant> propertyMap;
00247
00248 QDomNode propertyNode = deviceElement.firstChild();
00249 while (!propertyNode.isNull())
00250 {
00251 QDomElement propertyElement = propertyNode.toElement();
00252 if (!propertyElement.isNull() && propertyElement.tagName() == QLatin1String("property"))
00253 {
00254 QString propertyKey;
00255 QVariant propertyValue;
00256
00257 propertyKey = propertyElement.attribute("key");
00258 propertyValue = QVariant(propertyElement.text());
00259
00260 propertyMap.insert(propertyKey, propertyValue);
00261 }
00262
00263 propertyNode = propertyNode.nextSibling();
00264 }
00265 return propertyMap;
00266 }
00267
00268 void FakeNetworkManager::activateConnection(const QString & interfaceUni, const QString & connectionUni, const QVariantMap & connectionParameters)
00269 {
00270 mActiveConnections.append(connectionUni);
00271 QTimer::singleShot(0, this, SIGNAL(activeConnectionsChanged()));
00272 }
00273
00274 void FakeNetworkManager::deactivateConnection(const QString & activeConnection)
00275 {
00276 mActiveConnections.removeAll(activeConnection);
00277 }
00278
00279 QStringList FakeNetworkManager::activeConnections() const
00280 {
00281 return mActiveConnections;
00282 }
00283
00284 #include "fakenetworkmanager.moc"
00285