libsolidcontrol
bluetoothmanager.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <QMap>
00025 #include <QPair>
00026 #include <QStringList>
00027
00028 #include "ifaces/bluetoothmanager.h"
00029 #include "ifaces/bluetoothinterface.h"
00030 #include "ifaces/bluetoothinputdevice.h"
00031 #include "ifaces/bluetoothsecurity.h"
00032
00033 #include "soliddefs_p.h"
00034 #include "managerbase_p.h"
00035
00036 #include "bluetoothinterface.h"
00037 #include "bluetoothmanager.h"
00038 #include "bluetoothsecurity.h"
00039
00040 #include <kdebug.h>
00041
00042 namespace Solid
00043 {
00044 namespace Control
00045 {
00046 class BluetoothManagerPrivate : public ManagerBasePrivate
00047 {
00048 public:
00049 BluetoothManagerPrivate(BluetoothManager *parent)
00050 : q(parent) { }
00051
00052 BluetoothManager * const q;
00053
00054 QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> findRegisteredBluetoothInterface(const QString &ubi) const;
00055 QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> findRegisteredBluetoothInputDevice(const QString &ubi) const;
00056
00057 void connectBackend(QObject *newBackend);
00058
00059 void _k_interfaceAdded(const QString &ubi);
00060 void _k_interfaceRemoved(const QString &ubi);
00061 void _k_defaultInterfaceChanged(const QString &ubi);
00062 void _k_interfaceDestroyed(QObject *object);
00063
00064
00065
00066
00067
00068
00069 mutable QMap<QString, QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> > bluetoothInterfaceMap;
00070 mutable QMap<QString, QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> > bluetoothInputDeviceMap;
00071
00072 BluetoothInterface invalidInterface;
00073 BluetoothInputDevice invalidInputDevice;
00074 };
00075 }
00076 }
00077
00078 SOLID_SINGLETON_IMPLEMENTATION(Solid::Control::BluetoothManager, BluetoothManager)
00079
00080
00081 Solid::Control::BluetoothManager::BluetoothManager()
00082 : QObject(), d(new BluetoothManagerPrivate(this))
00083 {
00084 d->loadBackend("Bluetooth Management",
00085 "SolidBluetoothManager",
00086 "Solid::Control::Ifaces::BluetoothManager");
00087
00088 if (d->managerBackend() != 0) {
00089 d->connectBackend(d->managerBackend());
00090 }
00091 }
00092
00093 Solid::Control::BluetoothManager::~BluetoothManager()
00094 {
00095
00096 typedef QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> BluetoothInterfaceIfacePair;
00097
00098
00099 foreach (const BluetoothInterfaceIfacePair &pair, d->bluetoothInterfaceMap.values()) {
00100 delete pair.first;
00101 delete pair.second;
00102 }
00103
00104 d->bluetoothInterfaceMap.clear();
00105 }
00106
00107 Solid::Control::BluetoothInterfaceList Solid::Control::BluetoothManager::buildDeviceList(const QStringList & ubiList) const
00108 {
00109 BluetoothInterfaceList list;
00110 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(d->managerBackend());
00111
00112 if (backend == 0) return list;
00113
00114 kDebug() << "UBI List " << ubiList;
00115
00116 foreach (const QString &ubi, ubiList) {
00117 QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> pair = d->findRegisteredBluetoothInterface(ubi);
00118
00119 if (pair.first != 0) {
00120 list.append(*pair.first);
00121 }
00122 }
00123
00124 return list;
00125 }
00126
00127
00128 Solid::Control::BluetoothInterfaceList Solid::Control::BluetoothManager::bluetoothInterfaces() const
00129 {
00130 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(d->managerBackend());
00131
00132 if (backend != 0) {
00133 return buildDeviceList(backend->bluetoothInterfaces());
00134 } else {
00135 return BluetoothInterfaceList();
00136 }
00137 }
00138
00139
00140
00141
00142
00143
00144 QString Solid::Control::BluetoothManager::defaultInterface() const
00145 {
00146 return_SOLID_CALL(Ifaces::BluetoothManager *, d->managerBackend(), QString(), defaultInterface());
00147 }
00148
00149 Solid::Control::BluetoothInterface Solid::Control::BluetoothManager::findBluetoothInterface(const QString &ubi) const
00150 {
00151 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(d->managerBackend());
00152
00153 if (backend == 0) return d->invalidInterface;
00154
00155 QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> pair = d->findRegisteredBluetoothInterface(ubi);
00156
00157 if (pair.first != 0) {
00158 return *pair.first;
00159 } else {
00160 return d->invalidInterface;
00161 }
00162 }
00163
00164 QString Solid::Control::BluetoothManager::findInterface(const QString &dev) const
00165 {
00166 return_SOLID_CALL(Ifaces::BluetoothManager *, d->managerBackend(), QString(), findInterface(dev));
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243 void Solid::Control::BluetoothManagerPrivate::_k_interfaceAdded(const QString &ubi)
00244 {
00245 kDebug() << "Size of InterfaceList " << bluetoothInterfaceMap.size();
00246 QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> pair = findRegisteredBluetoothInterface(ubi);
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 emit q->interfaceAdded(ubi);
00258 }
00259
00260 void Solid::Control::BluetoothManagerPrivate::_k_interfaceRemoved(const QString &ubi)
00261 {
00262 QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> pair = bluetoothInterfaceMap.take(ubi);
00263
00264 if (pair.first != 0) {
00265 delete pair.first;
00266 delete pair.second;
00267 }
00268
00269 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(managerBackend());
00270 backend->removeInterface(ubi);
00271 emit q->interfaceRemoved(ubi);
00272 }
00273
00274 void Solid::Control::BluetoothManagerPrivate::_k_defaultInterfaceChanged(const QString &ubi)
00275 {
00276 emit q->defaultInterfaceChanged(ubi);
00277 }
00278
00279 void Solid::Control::BluetoothManagerPrivate::_k_interfaceDestroyed(QObject *object)
00280 {
00281 kDebug() << "Interface detroyed";
00282 Ifaces::BluetoothInterface *device = qobject_cast<Ifaces::BluetoothInterface *>(object);
00283
00284 if (device != 0) {
00285 QString ubi = device->ubi();
00286 QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> pair = bluetoothInterfaceMap.take(ubi);
00287 delete pair.first;
00288 }
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 void Solid::Control::BluetoothManagerPrivate::connectBackend(QObject *newBackend)
00334 {
00335 QObject::connect(newBackend, SIGNAL(interfaceAdded(const QString &)),
00336 q, SLOT(_k_interfaceAdded(const QString &)));
00337 QObject::connect(newBackend, SIGNAL(interfaceRemoved(const QString &)),
00338 q, SLOT(_k_interfaceRemoved(const QString &)));
00339 QObject::connect(newBackend, SIGNAL(defaultInterfaceChanged(const QString &)),
00340 q, SLOT(_k_defaultInterfaceChanged(const QString &)));
00341
00342
00343
00344
00345
00346
00347
00348
00349 }
00350
00351 QPair<Solid::Control::BluetoothInterface *, Solid::Control::Ifaces::BluetoothInterface *> Solid::Control::BluetoothManagerPrivate::findRegisteredBluetoothInterface(const QString &ubi) const
00352 {
00353
00354 kDebug() << "findRegisteredBluetoothInterface " << ubi;
00355 if (bluetoothInterfaceMap.contains(ubi)) {
00356 return bluetoothInterfaceMap[ubi];
00357 } else {
00358 kDebug() << "Creating New Interface " << ubi;
00359 Ifaces::BluetoothManager *backend = qobject_cast<Ifaces::BluetoothManager *>(managerBackend());
00360 Ifaces::BluetoothInterface *iface = 0;
00361
00362 if (backend != 0) {
00363 kDebug() << "Calling Backend to Creating New Interface " << ubi;
00364 iface = qobject_cast<Ifaces::BluetoothInterface *>(backend->createInterface(ubi));
00365 }
00366
00367 if (iface != 0) {
00368 kDebug() << "BackendIface created ";
00369 BluetoothInterface *device = new BluetoothInterface(iface);
00370 QPair<BluetoothInterface *, Ifaces::BluetoothInterface *> pair(device, iface);
00371 QObject::connect(iface, SIGNAL(destroyed(QObject *)),
00372 q, SLOT(_k_interfaceDestroyed(QObject *)));
00373 bluetoothInterfaceMap[ubi] = pair;
00374 return pair;
00375 } else {
00376 return QPair<BluetoothInterface *, Ifaces::BluetoothInterface *>(0, 0);
00377 }
00378 }
00379 }
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412 #include "bluetoothmanager.moc"