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