not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / libs / solid / control / bluetoothmanager.cpp
blob551ae9d36976c83cee8d3d0ef11c42991af91e9f
1 /* This file is part of the KDE project
2 Copyright (C) 2006 Will Stephenson <wstephenson@kde.org>
3 Copyright (C) 2006 Kévin Ottens <ervin@kde.org>
4 Copyright (C) 2007 Daniel Gollub <dgollub@suse.de>
5 Copyright (C) 2008 Tom Patzig <tpatzig@suse.de>
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License version 2 as published by the Free Software Foundation.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
24 #include <QMap>
25 #include <QPair>
26 #include <QStringList>
28 #include "ifaces/bluetoothmanager.h"
29 #include "ifaces/bluetoothinterface.h"
30 #include "ifaces/bluetoothinputdevice.h"
31 #include "ifaces/bluetoothsecurity.h"
33 #include "soliddefs_p.h"
34 #include "managerbase_p.h"
36 #include "bluetoothinterface.h"
37 #include "bluetoothmanager.h"
38 #include "bluetoothsecurity.h"
40 #include <kdebug.h>
42 namespace Solid
44 namespace Control
46 class BluetoothManagerPrivate : public ManagerBasePrivate
48 public:
49 BluetoothManagerPrivate(BluetoothManager *parent)
50 : q(parent) { }
52 BluetoothManager * const q;
54 QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> findRegisteredBluetoothInterface(const QString &ubi) const;
55 QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> findRegisteredBluetoothInputDevice(const QString &ubi) const;
57 void connectBackend(QObject *newBackend);
59 void _k_interfaceAdded(const QString &ubi);
60 void _k_interfaceRemoved(const QString &ubi);
61 void _k_defaultInterfaceChanged(const QString &ubi);
62 void _k_interfaceDestroyed(QObject *object);
64 void _k_inputDeviceCreated(const QString &ubi);
65 void _k_inputDeviceRemoved(const QString &ubi);
66 void _k_inputDeviceDestroyed(QObject *object);
69 mutable QMap<QString, QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> > bluetoothInterfaceMap;
70 mutable QMap<QString, QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> > bluetoothInputDeviceMap;
72 BluetoothInterface invalidInterface;
73 BluetoothInputDevice invalidInputDevice;
78 SOLID_SINGLETON_IMPLEMENTATION(Solid::Control::BluetoothManager, BluetoothManager)
81 Solid::Control::BluetoothManager::BluetoothManager()
82 : QObject(), d(new BluetoothManagerPrivate(this))
84 d->loadBackend("Bluetooth Management",
85 "SolidBluetoothManager",
86 "Solid::Control::Ifaces::BluetoothManager");
88 if (d->managerBackend() != 0) {
89 d->connectBackend(d->managerBackend());
93 Solid::Control::BluetoothManager::~BluetoothManager()
95 // Delete all the interfaces, they are now outdated
96 typedef QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> BluetoothInterfaceIfacePair;
98 // Delete all the devices, they are now outdated
99 foreach (const BluetoothInterfaceIfacePair &pair, d->bluetoothInterfaceMap.values()) {
100 delete pair.first;
101 delete pair.second;
104 d->bluetoothInterfaceMap.clear();
107 Solid::Control::BluetoothInterfaceList Solid::Control::BluetoothManager::buildDeviceList(const QStringList & ubiList) const
109 BluetoothInterfaceList list;
110 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(d->managerBackend());
112 if (backend == 0) return list;
114 kDebug() << "UBI List " << ubiList;
116 foreach (const QString &ubi, ubiList) {
117 QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> pair = d->findRegisteredBluetoothInterface(ubi);
119 if (pair.first != 0) {
120 list.append(*pair.first);
124 return list;
128 Solid::Control::BluetoothInterfaceList Solid::Control::BluetoothManager::bluetoothInterfaces() const
130 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(d->managerBackend());
132 if (backend != 0) {
133 return buildDeviceList(backend->bluetoothInterfaces());
134 } else {
135 return BluetoothInterfaceList();
139 QStringList Solid::Control::BluetoothManager::bluetoothInterfaces() const
141 return_SOLID_CALL(Ifaces::BluetoothManager *, d->managerBackend(), QStringList(), bluetoothInterfaces());
144 QString Solid::Control::BluetoothManager::defaultInterface() const
146 return_SOLID_CALL(Ifaces::BluetoothManager *, d->managerBackend(), QString(), defaultInterface());
149 Solid::Control::BluetoothInterface Solid::Control::BluetoothManager::findBluetoothInterface(const QString &ubi) const
151 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(d->managerBackend());
153 if (backend == 0) return d->invalidInterface;
155 QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> pair = d->findRegisteredBluetoothInterface(ubi);
157 if (pair.first != 0) {
158 return *pair.first;
159 } else {
160 return d->invalidInterface;
164 QString Solid::Control::BluetoothManager::findInterface(const QString &dev) const
166 return_SOLID_CALL(Ifaces::BluetoothManager *, d->managerBackend(), QString(), findInterface(dev));
170 Solid::Control::BluetoothInputDevice Solid::Control::BluetoothManager::findBluetoothInputDevice(const QString &ubi) const
172 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(d->managerBackend());
174 if (backend == 0) return d->invalidInputDevice;
176 QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> pair = d->findRegisteredBluetoothInputDevice(ubi);
178 if (pair.first != 0) {
179 return *pair.first;
180 } else {
181 return d->invalidInputDevice;
187 Solid::Control::BluetoothInputDevice* Solid::Control::BluetoothManager::createBluetoothInputDevice(const QString &ubi)
189 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(d->managerBackend());
190 Ifaces::BluetoothInputDevice *iface = 0;
191 if (backend != 0) {
192 iface = qobject_cast<Ifaces::BluetoothInputDevice *>(backend->createBluetoothInputDevice(ubi));
194 if (iface != 0) {
195 BluetoothInputDevice *device = new BluetoothInputDevice(iface);
196 return device;
198 } else {
199 return &d->invalidInputDevice;
204 KJob *Solid::Control::BluetoothManager::setupInputDevice(const QString &ubi)
206 return_SOLID_CALL(Ifaces::BluetoothManager *, d->managerBackend(), 0, setupInputDevice(ubi));
209 Solid::Control::BluetoothInputDeviceList Solid::Control::BluetoothManager::bluetoothInputDevices() const
211 BluetoothInputDeviceList list;
212 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(d->managerBackend());
214 if (backend == 0) return list;
216 QStringList ubis = backend->bluetoothInputDevices();
218 foreach (const QString &ubi, ubis) {
219 QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> pair = d->findRegisteredBluetoothInputDevice(ubi);
221 if (pair.first != 0) {
222 list.append(*pair.first);
225 return list;
228 Solid::Control::BluetoothSecurity *Solid::Control::BluetoothManager::security(const QString &interface)
230 Ifaces::BluetoothManager *backendManager = qobject_cast<Ifaces::BluetoothManager*>(d->managerBackend());
231 if (backendManager!=0) {
232 Ifaces::BluetoothSecurity *backendSecurity = backendManager->security(interface);
233 return new Solid::Control::BluetoothSecurity(backendSecurity);
235 return 0;
238 void Solid::Control::BluetoothManager::removeInputDevice(const QString &ubi)
240 SOLID_CALL(Ifaces::BluetoothManager *, d->managerBackend(), removeInputDevice(ubi));
243 void Solid::Control::BluetoothManagerPrivate::_k_interfaceAdded(const QString &ubi)
245 kDebug() << "Size of InterfaceList " << bluetoothInterfaceMap.size();
246 QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> pair = findRegisteredBluetoothInterface(ubi);
247 /* QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> pair = bluetoothInterfaceMap.take(ubi);
249 if (pair.first != 0) {
250 // Oops, I'm not sure it should happen...
251 // But well in this case we'd better kill the old device we got, it's probably outdated
253 delete pair.first;
254 delete pair.second;
257 emit q->interfaceAdded(ubi);
260 void Solid::Control::BluetoothManagerPrivate::_k_interfaceRemoved(const QString &ubi)
262 QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> pair = bluetoothInterfaceMap.take(ubi);
264 if (pair.first != 0) {
265 delete pair.first;
266 delete pair.second;
269 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(managerBackend());
270 backend->removeInterface(ubi);
271 emit q->interfaceRemoved(ubi);
274 void Solid::Control::BluetoothManagerPrivate::_k_defaultInterfaceChanged(const QString &ubi)
276 emit q->defaultInterfaceChanged(ubi);
279 void Solid::Control::BluetoothManagerPrivate::_k_interfaceDestroyed(QObject *object)
281 kDebug() << "Interface detroyed";
282 Ifaces::BluetoothInterface *device = qobject_cast<Ifaces::BluetoothInterface *>(object);
284 if (device != 0) {
285 QString ubi = device->ubi();
286 QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> pair = bluetoothInterfaceMap.take(ubi);
287 delete pair.first;
292 void Solid::Control::BluetoothManagerPrivate::_k_inputDeviceCreated(const QString &ubi)
294 QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> pair = bluetoothInputDeviceMap.take(ubi);
296 if (pair.first != 0) {
297 // Oops, I'm not sure it should happen...
298 // But well in this case we'd better kill the old device we got, it's probably outdated
300 delete pair.first;
301 delete pair.second;
304 emit q->inputDeviceCreated(ubi);
307 void Solid::Control::BluetoothManagerPrivate::_k_inputDeviceRemoved(const QString &ubi)
309 QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> pair = bluetoothInputDeviceMap.take(ubi);
311 if (pair.first != 0) {
312 delete pair.first;
313 delete pair.second;
316 emit q->inputDeviceRemoved(ubi);
319 void Solid::Control::BluetoothManagerPrivate::_k_inputDeviceDestroyed(QObject *object)
321 Ifaces::BluetoothInputDevice *device = qobject_cast<Ifaces::BluetoothInputDevice *>(object);
323 if (device != 0) {
324 QString ubi = device->ubi();
325 QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> pair = bluetoothInputDeviceMap.take(ubi);
326 delete pair.first;
331 /***************************************************************************/
333 void Solid::Control::BluetoothManagerPrivate::connectBackend(QObject *newBackend)
335 QObject::connect(newBackend, SIGNAL(interfaceAdded(const QString &)),
336 q, SLOT(_k_interfaceAdded(const QString &)));
337 QObject::connect(newBackend, SIGNAL(interfaceRemoved(const QString &)),
338 q, SLOT(_k_interfaceRemoved(const QString &)));
339 QObject::connect(newBackend, SIGNAL(defaultInterfaceChanged(const QString &)),
340 q, SLOT(_k_defaultInterfaceChanged(const QString &)));
343 QObject::connect(newBackend, SIGNAL(inputDeviceCreated(const QString &)),
344 q, SLOT(_k_inputDeviceCreated(const QString &)));
345 QObject::connect(newBackend, SIGNAL(inputDeviceRemoved(const QString &)),
346 q, SLOT(_k_inputDeviceRemoved(const QString &)));
351 QPair<Solid::Control::BluetoothInterface *, Solid::Control::Ifaces::BluetoothInterface *> Solid::Control::BluetoothManagerPrivate::findRegisteredBluetoothInterface(const QString &ubi) const
354 kDebug() << "findRegisteredBluetoothInterface " << ubi;
355 if (bluetoothInterfaceMap.contains(ubi)) {
356 return bluetoothInterfaceMap[ubi];
357 } else {
358 kDebug() << "Creating New Interface " << ubi;
359 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(managerBackend());
360 Ifaces::BluetoothInterface *iface = 0;
362 if (backend != 0) {
363 kDebug() << "Calling Backend to Creating New Interface " << ubi;
364 iface = qobject_cast<Ifaces::BluetoothInterface *>(backend->createInterface(ubi));
367 if (iface != 0) {
368 kDebug() << "BackendIface created ";
369 BluetoothInterface *device = new BluetoothInterface(iface);
370 QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> pair(device, iface);
371 QObject::connect(iface, SIGNAL(destroyed(QObject *)),
372 q, SLOT(_k_interfaceDestroyed(QObject *)));
373 bluetoothInterfaceMap[ubi] = pair;
374 return pair;
375 } else {
376 return QPair<BluetoothInterface *, Ifaces::BluetoothInterface *>(0, 0);
382 QPair<Solid::Control::BluetoothInputDevice *, Solid::Control::Ifaces::BluetoothInputDevice *> Solid::Control::BluetoothManagerPrivate::findRegisteredBluetoothInputDevice(const QString &ubi) const
385 if (bluetoothInputDeviceMap.contains(ubi)) {
386 return bluetoothInputDeviceMap[ubi];
387 } else {
388 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(managerBackend());
389 Ifaces::BluetoothInputDevice *iface = 0;
391 if (backend != 0) {
392 iface = qobject_cast<Ifaces::BluetoothInputDevice *>(backend->createBluetoothInputDevice(ubi));
395 if (iface != 0) {
396 BluetoothInputDevice *device = new BluetoothInputDevice(iface);
397 QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> pair(device, iface);
398 bluetoothInputDeviceMap[ubi] = pair;
399 QObject::connect(iface, SIGNAL(destroyed(QObject *)),
400 q, SLOT(_k_inputDeviceDestroyed(QObject *)));
401 return pair;
402 } else {
403 return QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *>(0, 0);
412 #include "bluetoothmanager.moc"