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.
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"
46 class BluetoothManagerPrivate
: public ManagerBasePrivate
49 BluetoothManagerPrivate(BluetoothManager
*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()) {
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
);
128 Solid::Control::BluetoothInterfaceList
Solid::Control::BluetoothManager::bluetoothInterfaces() const
130 Ifaces::BluetoothManager
*backend
= qobject_cast
<Ifaces::BluetoothManager
*>(d
->managerBackend());
133 return buildDeviceList(backend
->bluetoothInterfaces());
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) {
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) {
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;
192 iface = qobject_cast<Ifaces::BluetoothInputDevice *>(backend->createBluetoothInputDevice(ubi));
195 BluetoothInputDevice *device = new BluetoothInputDevice(iface);
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);
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);
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
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) {
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
);
285 QString ubi
= device
->ubi();
286 QPair
<BluetoothInterface
*, Ifaces::BluetoothInterface
*> pair
= bluetoothInterfaceMap
.take(ubi
);
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
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) {
316 emit q->inputDeviceRemoved(ubi);
319 void Solid::Control::BluetoothManagerPrivate::_k_inputDeviceDestroyed(QObject *object)
321 Ifaces::BluetoothInputDevice *device = qobject_cast<Ifaces::BluetoothInputDevice *>(object);
324 QString ubi = device->ubi();
325 QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> pair = bluetoothInputDeviceMap.take(ubi);
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
];
358 kDebug() << "Creating New Interface " << ubi
;
359 Ifaces::BluetoothManager
*backend
= qobject_cast
<Ifaces::BluetoothManager
*>(managerBackend());
360 Ifaces::BluetoothInterface
*iface
= 0;
363 kDebug() << "Calling Backend to Creating New Interface " << ubi
;
364 iface
= qobject_cast
<Ifaces::BluetoothInterface
*>(backend
->createInterface(ubi
));
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
;
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];
388 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(managerBackend());
389 Ifaces::BluetoothInputDevice *iface = 0;
392 iface = qobject_cast<Ifaces::BluetoothInputDevice *>(backend->createBluetoothInputDevice(ubi));
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 *)));
403 return QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *>(0, 0);
412 #include "bluetoothmanager.moc"