~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to libs/solid/control/backends/fakenet/fakenetworkmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  This file is part of the KDE project
 
2
    Copyright (C) 2006,2008 Will Stephenson <wstephenson@kde.org>
 
3
 
 
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.
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
18
*/
 
19
#include "fakenetworkmanager.h"
 
20
 
 
21
#include <QFile>
 
22
#include <QtXml/QtXml>
 
23
#include <QLatin1String>
 
24
 
 
25
#include <kstandarddirs.h>
 
26
#include <kdebug.h>
 
27
 
 
28
#include "fakeaccesspoint.h"
 
29
#include "fakewirednetworkinterface.h"
 
30
#include "fakewirelessnetworkinterface.h"
 
31
 
 
32
FakeNetworkManager::FakeNetworkManager(QObject * parent, const QVariantList  &)
 
33
    : Solid::Control::Ifaces::NetworkManager(parent)
 
34
{
 
35
    mUserNetworkingEnabled = true;
 
36
    mUserWirelessEnabled = true;
 
37
    mRfKillEnabled = false;
 
38
    mUserWwanEnabled = false;
 
39
    mWwanEnabled = false;
 
40
    mXmlFile = KStandardDirs::locate("data", "solidfakebackend/fakenetworking.xml");
 
41
 
 
42
//     QDBusConnection::sessionBus().registerObject("/org/kde/solid/fake", this, QDBusConnection::ExportNonScriptableSlots);
 
43
 
 
44
    parseNetworkingFile();
 
45
}
 
46
 
 
47
FakeNetworkManager::FakeNetworkManager(QObject * parent, const QStringList &, const QString &xmlFile) : Solid::Control::Ifaces::NetworkManager(parent)
 
48
{
 
49
    mUserNetworkingEnabled = true;
 
50
    mUserWirelessEnabled = true;
 
51
    mUserWwanEnabled = true;
 
52
    mWwanEnabled = true;
 
53
 
 
54
    mXmlFile = xmlFile;
 
55
    if (mXmlFile.isEmpty())
 
56
    {
 
57
       kDebug() << "Falling back to installed networking xml";
 
58
       mXmlFile = KStandardDirs::locate("data", "solidfakebackend/fakenetworking.xml");
 
59
    }
 
60
    parseNetworkingFile();
 
61
}
 
62
 
 
63
FakeNetworkManager::~FakeNetworkManager()
 
64
{
 
65
}
 
66
 
 
67
Solid::Networking::Status FakeNetworkManager::status() const
 
68
{
 
69
    return Solid::Networking::Unknown;
 
70
}
 
71
 
 
72
QStringList FakeNetworkManager::networkInterfaces() const
 
73
{
 
74
    return mNetworkInterfaces.keys();
 
75
}
 
76
 
 
77
QStringList FakeNetworkManager::activeNetworkInterfaces() const
 
78
{
 
79
    QStringList activeDevices;
 
80
    QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
 
81
    while (it.hasNext())
 
82
    {
 
83
        it.next();
 
84
        FakeNetworkInterface * netDevice = it.value();
 
85
        if (netDevice->isActive())
 
86
            activeDevices.append(netDevice->uni());
 
87
    }
 
88
    return activeDevices;
 
89
}
 
90
 
 
91
QObject * FakeNetworkManager::createNetworkInterface(const QString  & undi)
 
92
{
 
93
    if (mNetworkInterfaces.contains(undi))
 
94
        return mNetworkInterfaces[undi];
 
95
    else
 
96
        return 0;
 
97
}
 
98
 
 
99
bool FakeNetworkManager::isWirelessEnabled() const
 
100
{
 
101
    return mUserWirelessEnabled;
 
102
}
 
103
 
 
104
bool FakeNetworkManager::isNetworkingEnabled() const
 
105
{
 
106
    QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
 
107
    while (it.hasNext())
 
108
    {
 
109
        it.next();
 
110
        FakeNetworkInterface * netDevice = it.value();
 
111
        if (netDevice->isActive())
 
112
            return true;
 
113
    }
 
114
    return false;
 
115
}
 
116
 
 
117
bool FakeNetworkManager::isWirelessHardwareEnabled() const
 
118
{
 
119
    return mRfKillEnabled;
 
120
}
 
121
 
 
122
bool FakeNetworkManager::isWwanEnabled() const
 
123
{
 
124
    return mUserWwanEnabled;
 
125
}
 
126
 
 
127
bool FakeNetworkManager::isWwanHardwareEnabled() const
 
128
{
 
129
    return mWwanEnabled;
 
130
}
 
131
 
 
132
void FakeNetworkManager::setWirelessEnabled(bool enabled)
 
133
{
 
134
    mUserWirelessEnabled = enabled;
 
135
}
 
136
 
 
137
void FakeNetworkManager::setWwanEnabled(bool enabled)
 
138
{
 
139
    mUserWwanEnabled = enabled;
 
140
}
 
141
 
 
142
void FakeNetworkManager::setNetworkingEnabled(bool enabled)
 
143
{
 
144
    QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
 
145
    while (it.hasNext())
 
146
    {
 
147
        it.next();
 
148
        FakeNetworkInterface * netDevice = it.value();
 
149
        Q_UNUSED(netDevice);
 
150
    //    if ((netDevice->type() == Solid::Control::NetworkInterface::Ieee80211 && mUserWirelessEnabled)
 
151
      //     || netDevice->type() == Solid::Control::NetworkInterface::Ieee8023)
 
152
        //    netDevice->setActive(enabled);
 
153
    }
 
154
    mUserNetworkingEnabled = enabled;
 
155
}
 
156
 
 
157
void FakeNetworkManager::parseNetworkingFile()
 
158
{
 
159
    QFile machineFile(mXmlFile);
 
160
    if (!machineFile.open(QIODevice::ReadOnly))
 
161
    {
 
162
        kDebug() << "Error while opening " << mXmlFile;
 
163
        return;
 
164
    }
 
165
 
 
166
    QDomDocument fakeDocument;
 
167
    if (!fakeDocument.setContent(&machineFile))
 
168
    {
 
169
        kDebug() << "Error while creating the QDomDocument.";
 
170
        machineFile.close();
 
171
        return;
 
172
    }
 
173
    machineFile.close();
 
174
 
 
175
    kDebug() << "Parsing fake computer XML: " << mXmlFile;
 
176
    QDomElement mainElement = fakeDocument.documentElement();
 
177
    QDomNode node = mainElement.firstChild();
 
178
    while (!node.isNull())
 
179
    {
 
180
        QDomElement tempElement = node.toElement();
 
181
        if (!tempElement.isNull() && tempElement.tagName() == QLatin1String("device"))
 
182
        {
 
183
            FakeNetworkInterface *tempDevice = parseDeviceElement(tempElement);
 
184
            if(tempDevice)
 
185
            {
 
186
               mNetworkInterfaces.insert(tempDevice->uni(), tempDevice);
 
187
// Use the DeviceManager for now, the udi/uni should
 
188
//                emit deviceAdded(tempDevice->uni());
 
189
            }
 
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();
 
203
            }
 
204
        }
 
205
        node = node.nextSibling();
 
206
    }
 
207
}
 
208
 
 
209
FakeNetworkInterface *FakeNetworkManager::parseDeviceElement(const QDomElement &deviceElement)
 
210
{
 
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())
 
221
    {
 
222
        QDomElement childElement = childNode.toElement();
 
223
        //kDebug() << "found child=" << childElement.tagName();
 
224
        if (!childElement.isNull() && childElement.tagName() == QLatin1String("property"))
 
225
        {
 
226
            QString propertyKey;
 
227
            QVariant propertyValue;
 
228
 
 
229
            propertyKey = childElement.attribute("key");
 
230
            propertyValue = QVariant(childElement.text());
 
231
            if ( propertyValue == "ieee80211" ) {
 
232
                wireless = true;
 
233
            }
 
234
            //kDebug() << "Got property key=" << propertyKey << ", value=" << propertyValue.toString();
 
235
            propertyMap.insert(propertyKey, propertyValue);
 
236
        }
 
237
        else if (!childElement.isNull() && childElement.tagName() == QLatin1String("accesspoint"))
 
238
        {
 
239
            QString uni = childElement.attribute("uni");
 
240
            kDebug() << "Listing properties: " << uni;
 
241
            FakeAccessPoint * wifi = new FakeAccessPoint(parseAPElement(childElement), this);
 
242
            networks.append(wifi);
 
243
        }
 
244
        childNode = childNode.nextSibling();
 
245
    }
 
246
    //kDebug() << "Done listing. ";
 
247
 
 
248
/*    if (!propertyMap.isEmpty())
 
249
    { */
 
250
        kDebug() << "Creating FakeNetworkDevice for " << uni;
 
251
        if (wireless) {
 
252
            FakeWirelessNetworkInterface * wifi = new FakeWirelessNetworkInterface(propertyMap);
 
253
            foreach( FakeAccessPoint * net, networks)
 
254
            {
 
255
                kDebug() << "Injecting " << net->uni();
 
256
                wifi->injectAccessPoint(net);
 
257
            }
 
258
            device = wifi;
 
259
        } else {
 
260
            device = new FakeWiredNetworkInterface(propertyMap);
 
261
        }
 
262
 
 
263
 
 
264
//     }
 
265
 
 
266
    return device;
 
267
}
 
268
 
 
269
QMap<QString,QVariant> FakeNetworkManager::parseAPElement(const QDomElement &deviceElement)
 
270
{
 
271
    QMap<QString,QVariant> propertyMap;
 
272
 
 
273
    QDomNode propertyNode = deviceElement.firstChild();
 
274
    while (!propertyNode.isNull())
 
275
    {
 
276
        QDomElement propertyElement = propertyNode.toElement();
 
277
        if (!propertyElement.isNull() && propertyElement.tagName() == QLatin1String("property"))
 
278
        {
 
279
            QString propertyKey;
 
280
            QVariant propertyValue;
 
281
 
 
282
            propertyKey = propertyElement.attribute("key");
 
283
            propertyValue = QVariant(propertyElement.text());
 
284
            //kDebug() << "Got property key=" << propertyKey << ", value=" << propertyValue.toString();
 
285
            propertyMap.insert(propertyKey, propertyValue);
 
286
        }
 
287
 
 
288
        propertyNode = propertyNode.nextSibling();
 
289
    }
 
290
    return propertyMap;
 
291
}
 
292
 
 
293
void FakeNetworkManager::activateConnection(const QString & interfaceUni, const QString & connectionUni, const QVariantMap & connectionParameters)
 
294
{
 
295
    Q_UNUSED(interfaceUni);
 
296
    Q_UNUSED(connectionUni);
 
297
    Q_UNUSED(connectionParameters);
 
298
 
 
299
    mActiveConnections.append(connectionUni);
 
300
    QTimer::singleShot(0, this, SIGNAL(activeConnectionsChanged()));
 
301
}
 
302
 
 
303
void FakeNetworkManager::deactivateConnection(const QString & activeConnection)
 
304
{
 
305
    mActiveConnections.removeAll(activeConnection);
 
306
}
 
307
 
 
308
QStringList FakeNetworkManager::activeConnections() const
 
309
{
 
310
    return mActiveConnections;
 
311
}
 
312
 
 
313
Solid::Control::NetworkInterface::Types FakeNetworkManager::supportedInterfaceTypes() const
 
314
{
 
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
 
321
           );
 
322
}
 
323
 
 
324
#include "fakenetworkmanager.moc"
 
325