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