~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

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 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 <QFile>
 
20
#include <QtXml>
 
21
#include <QLatin1String>
 
22
 
 
23
#include <kstandarddirs.h>
 
24
#include <kdebug.h>
 
25
 
 
26
#include "fakenetworkmanager.h"
 
27
#include "fakenetwork.h"
 
28
#include "fakewirelessnetwork.h"
 
29
 
 
30
FakeAuthenticationValidator::FakeAuthenticationValidator(QObject * parent) : QObject(parent)
 
31
{
 
32
}
 
33
 
 
34
FakeAuthenticationValidator::~FakeAuthenticationValidator()
 
35
{
 
36
}
 
37
 
 
38
bool FakeAuthenticationValidator::validate(const Solid::Control::Authentication *)
 
39
{
 
40
    return true;
 
41
}
 
42
 
 
43
FakeNetworkManager::FakeNetworkManager(QObject * parent, const QStringList  &) : Solid::Control::Ifaces::NetworkManager(parent), mAuthValidator(0)
 
44
{
 
45
    mUserNetworkingEnabled = true;
 
46
    mUserWirelessEnabled = true;
 
47
 
 
48
    mXmlFile = KStandardDirs::locate("data", "solidfakebackend/fakenetworking.xml");
 
49
 
 
50
//     QDBusConnection::sessionBus().registerObject("/org/kde/solid/fake", this, QDBusConnection::ExportNonScriptableSlots);
 
51
 
 
52
    parseNetworkingFile();
 
53
}
 
54
 
 
55
FakeNetworkManager::FakeNetworkManager(QObject * parent, const QStringList &, const QString &xmlFile) : Solid::Control::Ifaces::NetworkManager(parent), mAuthValidator(0)
 
56
{
 
57
    mUserNetworkingEnabled = true;
 
58
    mUserWirelessEnabled = true;
 
59
 
 
60
    mXmlFile = xmlFile;
 
61
    if (mXmlFile.isEmpty())
 
62
    {
 
63
       kDebug() << "Falling back to installed networking xml";
 
64
       mXmlFile = KStandardDirs::locate("data", "solidfakebackend/fakenetworking.xml");
 
65
    }
 
66
    parseNetworkingFile();
 
67
}
 
68
 
 
69
FakeNetworkManager::~FakeNetworkManager()
 
70
{
 
71
    delete mAuthValidator;
 
72
}
 
73
 
 
74
QStringList FakeNetworkManager::networkInterfaces() const
 
75
{
 
76
    return mNetworkInterfaces.keys();
 
77
}
 
78
 
 
79
QStringList FakeNetworkManager::activeNetworkInterfaces() const
 
80
{
 
81
    QStringList activeDevices;
 
82
    QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
 
83
    while (it.hasNext())
 
84
    {
 
85
        it.next();
 
86
        FakeNetworkInterface * netDevice = it.value();
 
87
        if (netDevice->isActive())
 
88
            activeDevices.append(netDevice->uni());
 
89
    }
 
90
    return activeDevices;
 
91
}
 
92
 
 
93
QObject * FakeNetworkManager::createNetworkInterface(const QString  & undi)
 
94
{
 
95
    if (mNetworkInterfaces.contains(undi))
 
96
        return mNetworkInterfaces[undi];
 
97
    else
 
98
        return 0;
 
99
}
 
100
 
 
101
QObject * FakeNetworkManager::createAuthenticationValidator()
 
102
{
 
103
    if (mAuthValidator == 0)
 
104
        mAuthValidator = new FakeAuthenticationValidator(this);
 
105
    return mAuthValidator;
 
106
}
 
107
 
 
108
bool FakeNetworkManager::isWirelessEnabled() const
 
109
{
 
110
    QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
 
111
    while (it.hasNext())
 
112
    {
 
113
        it.next();
 
114
        FakeNetworkInterface * netDevice = it.value();
 
115
        if (netDevice->type() == Solid::Control::NetworkInterface::Ieee80211)
 
116
            if (netDevice->isActive())
 
117
                return true;
 
118
    }
 
119
    return false;
 
120
}
 
121
 
 
122
bool FakeNetworkManager::isNetworkingEnabled() const
 
123
{
 
124
    QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
 
125
    while (it.hasNext())
 
126
    {
 
127
        it.next();
 
128
        FakeNetworkInterface * netDevice = it.value();
 
129
        if (netDevice->isActive())
 
130
            return true;
 
131
    }
 
132
    return false;
 
133
}
 
134
 
 
135
void FakeNetworkManager::setWirelessEnabled(bool enabled)
 
136
{
 
137
    QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
 
138
    if (mUserNetworkingEnabled)
 
139
    {
 
140
        while (it.hasNext())
 
141
        {
 
142
            it.next();
 
143
            FakeNetworkInterface * netDevice = it.value();
 
144
            if (netDevice->type() == Solid::Control::NetworkInterface::Ieee80211)
 
145
                netDevice->setActive(enabled);
 
146
        }
 
147
    }
 
148
    mUserWirelessEnabled = enabled;
 
149
}
 
150
 
 
151
void FakeNetworkManager::setNetworkingEnabled(bool enabled)
 
152
{
 
153
    QMapIterator<QString, FakeNetworkInterface *> it(mNetworkInterfaces);
 
154
    while (it.hasNext())
 
155
    {
 
156
        it.next();
 
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);
 
161
    }
 
162
    mUserNetworkingEnabled = enabled;
 
163
}
 
164
 
 
165
void FakeNetworkManager::notifyHiddenNetwork(const QString  & essid)
 
166
{
 
167
    // look up the device hosting the hidden net.
 
168
    // move the hidden net into the device's networks list
 
169
}
 
170
 
 
171
void FakeNetworkManager::parseNetworkingFile()
 
172
{
 
173
    QFile machineFile(mXmlFile);
 
174
    if (!machineFile.open(QIODevice::ReadOnly))
 
175
    {
 
176
        kDebug() << "Error while opening " << mXmlFile;
 
177
        return;
 
178
    }
 
179
 
 
180
    QDomDocument fakeDocument;
 
181
    if (!fakeDocument.setContent(&machineFile))
 
182
    {
 
183
        kDebug() << "Error while creating the QDomDocument.";
 
184
        machineFile.close();
 
185
        return;
 
186
    }
 
187
    machineFile.close();
 
188
 
 
189
    kDebug() << "Parsing fake computer XML: " << mXmlFile;
 
190
    QDomElement mainElement = fakeDocument.documentElement();
 
191
    QDomNode node = mainElement.firstChild();
 
192
    while (!node.isNull())
 
193
    {
 
194
        QDomElement tempElement = node.toElement();
 
195
        if (!tempElement.isNull() && tempElement.tagName() == QLatin1String("device"))
 
196
        {
 
197
            FakeNetworkInterface *tempDevice = parseDeviceElement(tempElement);
 
198
            if(tempDevice)
 
199
            {
 
200
               mNetworkInterfaces.insert(tempDevice->uni(), tempDevice);
 
201
// Use the DeviceManager for now, the udi/uni should
 
202
//                emit deviceAdded(tempDevice->uni());
 
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< FakeNetwork * > networks;
 
218
 
 
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
            //kDebug() << "Got property key=" << propertyKey << ", value=" << propertyValue.toString();
 
232
            propertyMap.insert(propertyKey, propertyValue);
 
233
        }
 
234
        else if (!childElement.isNull() && childElement.tagName() == QLatin1String("network"))
 
235
        {
 
236
            QString uni = childElement.attribute("uni");
 
237
            kDebug() << "Listing properties: " << uni;
 
238
            FakeNetwork * net = new FakeNetwork(uni, parseNetworkElement(childElement));
 
239
            networks.append(net);
 
240
        }
 
241
        else if (!childElement.isNull() && childElement.tagName() == QLatin1String("wireless"))
 
242
        {
 
243
            QString uni = childElement.attribute("uni");
 
244
            kDebug() << "Listing properties: " << uni;
 
245
            FakeNetwork * wifi = new FakeWirelessNetwork(uni, parseNetworkElement(childElement));
 
246
            networks.append(wifi);
 
247
        }
 
248
        childNode = childNode.nextSibling();
 
249
    }
 
250
    //kDebug() << "Done listing. ";
 
251
 
 
252
/*    if (!propertyMap.isEmpty())
 
253
    { */
 
254
        kDebug() << "Creating FakeNetworkDevice for " << uni;
 
255
        device = new FakeNetworkInterface(propertyMap);
 
256
        QListIterator< FakeNetwork * > it (networks);
 
257
        while (it.hasNext())
 
258
        {
 
259
            FakeNetwork * net = it.next();
 
260
            kDebug() << "Injecting " << net->uni();
 
261
            device->injectNetwork(net->uni(), net);
 
262
        }
 
263
 
 
264
//     }
 
265
 
 
266
    return device;
 
267
}
 
268
 
 
269
QMap<QString,QVariant> FakeNetworkManager::parseNetworkElement(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
 
 
294
#include "fakenetworkmanager.moc"
 
295