1 /* This file is part of the KDE project
2 Copyright (C) 2007 Daniel Gollub <dgollub@suse.de>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
21 #include <QtXml/QtXml>
22 #include <QLatin1String>
24 #include <kstandarddirs.h>
27 #include "fakebluetoothmanager.h"
29 FakeBluetoothManager::FakeBluetoothManager(QObject
* parent
, const QStringList
&) : Solid::Control::Ifaces::BluetoothManager(parent
)
33 mXmlFile
= KStandardDirs::locate("data", "solidfakebackend/fakebluetooth.xml");
35 // QDBusConnection::sessionBus().registerObject("/org/kde/solid/fake", this, QDBusConnection::ExportNonScriptableSlots);
40 FakeBluetoothManager::FakeBluetoothManager(QObject
* parent
, const QStringList
&, const QString
&xmlFile
) : Solid::Control::Ifaces::BluetoothManager(parent
)
44 if (mXmlFile
.isEmpty()) {
45 kDebug() << "Falling back to installed bluetoothing xml";
46 mXmlFile
= KStandardDirs::locate("data", "solidfakebackend/fakebluetooth.xml");
51 FakeBluetoothManager::~FakeBluetoothManager()
53 foreach (FakeBluetoothInterface
*interface
, mBluetoothInterfaces
) {
57 mBluetoothInterfaces
.clear();
60 QStringList
FakeBluetoothManager::bluetoothInterfaces() const
62 return mBluetoothInterfaces
.keys();
65 QObject
* FakeBluetoothManager::createInterface(const QString
& ubi
)
67 if (mBluetoothInterfaces
.contains(ubi
))
68 return mBluetoothInterfaces
[ubi
];
73 QString
FakeBluetoothManager::defaultInterface() const
78 void FakeBluetoothManager::parseBluetoothFile()
80 QFile
machineFile(mXmlFile
);
81 if (!machineFile
.open(QIODevice::ReadOnly
)) {
82 kDebug() << "Error while opening " << mXmlFile
;
86 QDomDocument fakeDocument
;
89 if (!fakeDocument
.setContent(&machineFile
, &error
, &line
)) {
90 kDebug() << "Error while creating the QDomDocument: " << error
<< " line: " <<
97 kDebug() << "Parsing fake computer XML: " << mXmlFile
;
98 QDomElement mainElement
= fakeDocument
.documentElement();
99 QDomNode node
= mainElement
.firstChild();
100 while (!node
.isNull()) {
101 QDomElement tempElement
= node
.toElement();
102 if (!tempElement
.isNull() && tempElement
.tagName() == QLatin1String("interface")) {
103 FakeBluetoothInterface
*tempDevice
= parseDeviceElement(tempElement
);
105 mBluetoothInterfaces
.insert(tempDevice
->ubi(), tempDevice
);
106 // Use the DeviceManager for now, the udi/ubi should
107 // emit deviceAdded(tempDevice->ubi());
110 node
= node
.nextSibling();
114 FakeBluetoothInterface
*FakeBluetoothManager::parseDeviceElement(const QDomElement
&deviceElement
)
116 FakeBluetoothInterface
*interface
= 0;
117 QMap
<QString
, QVariant
> propertyMap
;
118 QString ubi
= deviceElement
.attribute("ubi");
119 propertyMap
.insert("ubi", ubi
);
120 kDebug() << "Listing device: " << ubi
;
121 propertyMap
.insert("ubi", QVariant(ubi
));
123 QDomNode childNode
= deviceElement
.firstChild();
124 while (!childNode
.isNull()) {
125 QDomElement childElement
= childNode
.toElement();
126 //kDebug() << "found child=" << childElement.tagName();
127 if (!childElement
.isNull() && childElement
.tagName() == QLatin1String("property")) {
129 QVariant propertyValue
;
131 propertyKey
= childElement
.attribute("key");
132 propertyValue
= QVariant(childElement
.text());
133 // kDebug() << "Got property key=" << propertyKey << ", value=" << propertyValue.toString();
134 propertyMap
.insert(propertyKey
, propertyValue
);
135 } else if (!childElement
.isNull() && childElement
.tagName() == QLatin1String("device")) {
136 QString ubi
= childElement
.attribute("ubi");
137 kDebug() << "Listing properties: " << ubi
;
138 FakeBluetoothRemoteDevice
* remoteDevice
= new FakeBluetoothRemoteDevice(parseBluetoothElement(childElement
));
139 mBluetoothRemoteDevices
.insert(ubi
, remoteDevice
);
141 childNode
= childNode
.nextSibling();
143 //kDebug() << "Done listing. ";
145 kDebug() << "Creating FakeBluetoothInterface for " << ubi
;
146 interface
= new FakeBluetoothInterface(propertyMap
);
148 // Inject Remote devices....
149 foreach (FakeBluetoothRemoteDevice
*device
, mBluetoothRemoteDevices
) {
150 kDebug() << "Injecting " << device
->name();
151 interface
->injectDevice(device
->ubi(), device
);
154 mBluetoothRemoteDevices
.clear();
159 QMap
<QString
, QVariant
> FakeBluetoothManager::parseBluetoothElement(const QDomElement
&deviceElement
)
161 QMap
<QString
, QVariant
> propertyMap
;
163 QString ubi
= deviceElement
.attribute("ubi");
164 propertyMap
.insert("ubi", ubi
);
166 QDomNode propertyNode
= deviceElement
.firstChild();
167 while (!propertyNode
.isNull()) {
168 QDomElement propertyElement
= propertyNode
.toElement();
169 if (!propertyElement
.isNull() && propertyElement
.tagName() == QLatin1String("property")) {
171 QVariant propertyValue
;
173 propertyKey
= propertyElement
.attribute("key");
174 propertyValue
= QVariant(propertyElement
.text());
175 // kDebug() << "Got property key=" << propertyKey << ", value=" << propertyValue.toString();
176 propertyMap
.insert(propertyKey
, propertyValue
);
179 propertyNode
= propertyNode
.nextSibling();
184 FakeBluetoothInterface
*FakeBluetoothManager::createBluetoothInterface(const QString
&ubi
)
186 if (mBluetoothInterfaces
.contains(ubi
))
187 return mBluetoothInterfaces
[ubi
];
192 QStringList
FakeBluetoothManager::bluetoothInputDevices() const
194 return QStringList();
197 FakeBluetoothInputDevice
*FakeBluetoothManager::createBluetoothInputDevice(const QString
&ubi
)
199 if (mBluetoothInputDevices
.contains(ubi
))
200 return mBluetoothInputDevices
[ubi
];
205 KJob
*FakeBluetoothManager::setupInputDevice(const QString
& /*ubi */)
210 Solid::Control::Ifaces::BluetoothSecurity
*FakeBluetoothManager::security(const QString
&/*interface*/)
215 void FakeBluetoothManager::removeInputDevice(const QString
& /*ubi */)
221 #include "fakebluetoothmanager.moc"