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 #include <QVariant>
00027
00028 #include <KDebug>
00029
00030 #include "ifaces/bluetoothinterface.h"
00031
00032 #include "frontendobject_p.h"
00033
00034 #include "soliddefs_p.h"
00035 #include "bluetoothmanager.h"
00036 #include "bluetoothinterface.h"
00037 #include "bluetoothremotedevice.h"
00038
00039 namespace Solid
00040 {
00041 namespace Control
00042 {
00043 class BluetoothInterfacePrivate : public FrontendObjectPrivate
00044 {
00045 public:
00046 BluetoothInterfacePrivate(QObject *parent)
00047 : FrontendObjectPrivate(parent) { }
00048
00049 void setBackendObject(QObject *object);
00050
00051 QPair<BluetoothRemoteDevice *, Ifaces::BluetoothRemoteDevice *> findRegisteredBluetoothRemoteDevice(const QString &ubi) const;
00052 QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> findRegisteredBluetoothInputDevice(const QString &ubi) const;
00053
00054 mutable QMap<QString, QPair<BluetoothRemoteDevice *, Ifaces::BluetoothRemoteDevice *> > remoteDeviceMap;
00055 mutable QMap<QString, QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> > inputDeviceMap;
00056 mutable BluetoothRemoteDevice invalidDevice;
00057 mutable BluetoothInputDevice invalidInputDevice;
00058 };
00059 }
00060 }
00061
00062 Solid::Control::BluetoothInterface::BluetoothInterface()
00063 : QObject(), d(new BluetoothInterfacePrivate(this))
00064 {}
00065
00066 Solid::Control::BluetoothInterface::BluetoothInterface(const QString &ubi)
00067 : QObject(), d(new BluetoothInterfacePrivate(this))
00068 {
00069 const BluetoothInterface &device = BluetoothManager::self().findBluetoothInterface(ubi);
00070 d->setBackendObject(device.d->backendObject());
00071 QObject::connect(d->backendObject(), SIGNAL(deviceCreated(const QString &)),this, SLOT(slotDeviceCreated(const QString &)));
00072 }
00073
00074 Solid::Control::BluetoothInterface::BluetoothInterface(QObject *backendObject)
00075 : QObject(), d(new BluetoothInterfacePrivate(this))
00076 {
00077 d->setBackendObject(backendObject);
00078 QObject::connect(d->backendObject(), SIGNAL(deviceCreated(const QString &)),this, SLOT(slotDeviceCreated(const QString &)));
00079 }
00080
00081 Solid::Control::BluetoothInterface::BluetoothInterface(const BluetoothInterface &device)
00082 : QObject(), d(new BluetoothInterfacePrivate(this))
00083 {
00084 d->setBackendObject(device.d->backendObject());
00085 QObject::connect(d->backendObject(), SIGNAL(deviceCreated(const QString &)),this, SLOT(slotDeviceCreated(const QString &)));
00086 }
00087
00088 Solid::Control::BluetoothInterface::~BluetoothInterface()
00089 {
00090
00091 typedef QPair<BluetoothRemoteDevice *, Ifaces::BluetoothRemoteDevice *> BluetoothRemoteDeviceIfacePair;
00092 typedef QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> BluetoothInputDeviceIfacePair;
00093
00094
00095 foreach (const BluetoothRemoteDeviceIfacePair &pair, d->remoteDeviceMap.values()) {
00096 delete pair.first;
00097 delete pair.second;
00098 }
00099 foreach (const BluetoothInputDeviceIfacePair &pair, d->inputDeviceMap.values()) {
00100 delete pair.first;
00101 delete pair.second;
00102 }
00103
00104 }
00105
00106 Solid::Control::BluetoothInterface &Solid::Control::BluetoothInterface::operator=(const Solid::Control::BluetoothInterface & dev)
00107 {
00108 d->setBackendObject(dev.d->backendObject());
00109
00110 return *this;
00111 }
00112
00113 QString Solid::Control::BluetoothInterface::ubi() const
00114 {
00115 return_SOLID_CALL(Ifaces::BluetoothInterface *, d->backendObject(), QString(), ubi());
00116 }
00117
00118 void Solid::Control::BluetoothInterface::cancelDeviceCreation(const QString &address) const
00119 {
00120 SOLID_CALL(Ifaces::BluetoothInterface *, d->backendObject(), cancelDeviceCreation(address));
00121 }
00122
00123 void Solid::Control::BluetoothInterface::createPairedDevice(const QString &address,const QString &adapterPath, const QString &capab) const
00124 {
00125 Ifaces::BluetoothInterface *backend = qobject_cast<Ifaces::BluetoothInterface *>(d->backendObject());
00126 if (backend == 0)
00127 return;
00128 backend->createPairedDevice(address,adapterPath,capab);
00129 }
00130
00131 QMap<QString, QVariant> Solid::Control::BluetoothInterface::getProperties() const
00132 {
00133 return_SOLID_CALL(Ifaces::BluetoothInterface *, d->backendObject(), (QMap<QString,QVariant>()), getProperties());
00134 }
00135
00136 QVariant Solid::Control::BluetoothInterface::getProperty(const QString &key) const
00137 {
00138 QMap<QString, QVariant> props = getProperties();
00139 if (props.contains(key))
00140 return props[key];
00141 else
00142 return QVariant();
00143 }
00144
00145 Solid::Control::BluetoothRemoteDeviceList Solid::Control::BluetoothInterface::listDevices() const
00146 {
00147 Ifaces::BluetoothInterface *backend = qobject_cast<Ifaces::BluetoothInterface *>(d->backendObject());
00148 if (backend == 0)
00149 return Solid::Control::BluetoothRemoteDeviceList();
00150
00151 QStringList ubis = backend->listDevices();
00152
00153 Solid::Control::BluetoothRemoteDeviceList list;
00154 foreach (const QString& ubi,ubis) {
00155 BluetoothRemoteDevice* remoteDevice = findBluetoothRemoteDeviceUBI(ubi);
00156 list.append(remoteDevice);
00157 }
00158 return list;
00159 }
00160
00161 void Solid::Control::BluetoothInterface::registerAgent(const QString &path, const QString &capab) const
00162 {
00163 SOLID_CALL(Ifaces::BluetoothInterface *, d->backendObject(), registerAgent(path,capab));
00164 }
00165
00166 void Solid::Control::BluetoothInterface::releaseSession() const
00167 {
00168 SOLID_CALL(Ifaces::BluetoothInterface *, d->backendObject(), releaseSession());
00169 }
00170
00171 void Solid::Control::BluetoothInterface::requestSession() const
00172 {
00173 SOLID_CALL(Ifaces::BluetoothInterface *, d->backendObject(), requestSession());
00174 }
00175
00176 void Solid::Control::BluetoothInterface::removeDevice(const QString &path) const
00177 {
00178 SOLID_CALL(Ifaces::BluetoothInterface *, d->backendObject(), removeDevice(path));
00179 }
00180
00181 void Solid::Control::BluetoothInterface::setProperty(const QString &property, const QVariant &value) const
00182 {
00183 SOLID_CALL(Ifaces::BluetoothInterface *, d->backendObject(), setProperty(property,value));
00184 }
00185
00186 void Solid::Control::BluetoothInterface::startDiscovery() const
00187 {
00188 SOLID_CALL(Ifaces::BluetoothInterface *, d->backendObject(), startDiscovery());
00189 }
00190
00191 void Solid::Control::BluetoothInterface::stopDiscovery() const
00192 {
00193 SOLID_CALL(Ifaces::BluetoothInterface *, d->backendObject(), stopDiscovery());
00194 }
00195
00196 void Solid::Control::BluetoothInterface::unregisterAgent(const QString &path) const
00197 {
00198 SOLID_CALL(Ifaces::BluetoothInterface *, d->backendObject(), unregisterAgent(path));
00199 }
00200
00201
00202 Solid::Control::BluetoothRemoteDevice Solid::Control::BluetoothInterface::findBluetoothRemoteDeviceAddr(const QString &address) const
00203 {
00204 Ifaces::BluetoothInterface *backend = qobject_cast<Ifaces::BluetoothInterface *>(d->backendObject());
00205 if (backend == 0)
00206 return d->invalidDevice;
00207
00208 const QString ubi = getBluetoothRemoteDeviceUBI(address);
00209
00210 QPair<BluetoothRemoteDevice *, Ifaces::BluetoothRemoteDevice *> pair = d->findRegisteredBluetoothRemoteDevice(ubi);
00211
00212 if (pair.first != 0) {
00213 return *pair.first;
00214 } else {
00215 return d->invalidDevice;
00216 }
00217 }
00218
00219
00220 const QString Solid::Control::BluetoothInterface::getBluetoothRemoteDeviceUBI(const QString &address) const
00221 {
00222 Ifaces::BluetoothInterface *backend = qobject_cast<Ifaces::BluetoothInterface *>(d->backendObject());
00223 if (backend == 0)
00224 return "";
00225
00226 const QString ubi = backend->findDevice(address);
00227 return ubi;
00228 }
00229
00230
00231 void Solid::Control::BluetoothInterface::createBluetoothRemoteDevice(const QString &address)
00232 {
00233 Ifaces::BluetoothInterface *backend = qobject_cast<Ifaces::BluetoothInterface *>(d->backendObject());
00234 if (backend == 0)
00235 return;
00236 backend->createDevice(address);
00237 }
00238
00239
00240 Solid::Control::BluetoothRemoteDevice* Solid::Control::BluetoothInterface::findBluetoothRemoteDeviceUBI(const QString &ubi) const
00241 {
00242 QPair<BluetoothRemoteDevice *, Ifaces::BluetoothRemoteDevice *> pair = d->findRegisteredBluetoothRemoteDevice(ubi);
00243 return pair.first;
00244 }
00245
00246 Solid::Control::BluetoothInputDevice* Solid::Control::BluetoothInterface::findBluetoothInputDeviceUBI(const QString &ubi) const
00247 {
00248 QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> pair = d->findRegisteredBluetoothInputDevice(ubi);
00249 return pair.first;
00250 }
00251
00252 void Solid::Control::BluetoothInterface::slotDeviceCreated(const QString& ubi)
00253 {
00254
00255 Ifaces::BluetoothInterface *backend = qobject_cast<Ifaces::BluetoothInterface *>(d->backendObject());
00256 Ifaces::BluetoothRemoteDevice *iface = 0;
00257
00258 if (backend != 0) {
00259 iface = qobject_cast<Ifaces::BluetoothRemoteDevice *>(backend->createBluetoothRemoteDevice(ubi));
00260 }
00261
00262 if (iface != 0) {
00263 BluetoothRemoteDevice *device = new BluetoothRemoteDevice(iface);
00264
00265 QPair<BluetoothRemoteDevice *, Ifaces::BluetoothRemoteDevice *> pair(device, iface);
00266 d->remoteDeviceMap[ubi] = pair;
00267 }
00268 }
00269
00270 QString Solid::Control::BluetoothInterface::address() const
00271 {
00272 QVariant var = getProperty("Address");
00273
00274 if (var.isValid())
00275 return var.value<QString>();
00276 else
00277 return QString();
00278
00279
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305 int Solid::Control::BluetoothInterface::discoverableTimeout() const
00306 {
00307 QVariant var = getProperty("DiscoverableTimeout");
00308
00309 if (var.isValid())
00310 return var.value<int>();
00311 else
00312 return 0;
00313
00314
00315 }
00316
00317 bool Solid::Control::BluetoothInterface::isDiscoverable() const
00318 {
00319 QVariant var = getProperty("Discoverable");
00320
00321 if (var.isValid())
00322 return var.value<bool>();
00323 else
00324 return false;
00325
00326
00327 }
00328
00329 bool Solid::Control::BluetoothInterface::isDiscovering() const
00330 {
00331 QVariant var = getProperty("Discovering");
00332
00333 if (var.isValid())
00334 return var.value<bool>();
00335 else
00336 return false;
00337 }
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378 QString Solid::Control::BluetoothInterface::name() const
00379 {
00380 QVariant var = getProperty("Name");
00381
00382 if (var.isValid())
00383 return var.value<QString>();
00384 else
00385 return QString();
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
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435 void Solid::Control::BluetoothInterface::setDiscoverable(bool status)
00436 {
00437 QVariant var(status);
00438 setProperty("Discoverable",var);
00439 }
00440
00441 void Solid::Control::BluetoothInterface::setDiscoverableTimeout(int timeout)
00442 {
00443 QVariant var(timeout);
00444 setProperty("DiscoverableTimeout",var);
00445 }
00446
00447
00448
00449
00450
00451
00452
00453 void Solid::Control::BluetoothInterface::setName(const QString &name)
00454 {
00455 QVariant var(name);
00456 setProperty("Name",var);
00457 }
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 void Solid::Control::BluetoothInterfacePrivate::setBackendObject(QObject *object)
00502 {
00503 FrontendObjectPrivate::setBackendObject(object);
00504
00505 if (object) {
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537 QObject::connect(object, SIGNAL(deviceCreated(const QString &)),
00538 parent(), SIGNAL(deviceCreated(const QString &)));
00539 QObject::connect(object, SIGNAL(deviceDisappeared(const QString &)),
00540 parent(), SIGNAL(deviceDisappeared(const QString &)));
00541 QObject::connect(object, SIGNAL(deviceFound(const QString &, const QMap<QString,QVariant> &)),
00542 parent(), SIGNAL(deviceFound(const QString &, const QMap<QString,QVariant> &)));
00543 QObject::connect(object, SIGNAL(deviceRemoved(const QString &)),
00544 parent(), SIGNAL(deviceRemoved(const QString &)));
00545 QObject::connect(object, SIGNAL(propertyChanged(const QString &,const QVariant &)),
00546 parent(), SIGNAL(propertyChanged(const QString &, const QVariant &)));
00547
00548 }
00549 }
00550
00551 QPair<Solid::Control::BluetoothRemoteDevice *, Solid::Control::Ifaces::BluetoothRemoteDevice *> Solid::Control::BluetoothInterfacePrivate::findRegisteredBluetoothRemoteDevice(const QString &ubi) const
00552 {
00553 if (remoteDeviceMap.contains(ubi)) {
00554 return remoteDeviceMap[ubi];
00555 } else {
00556 Ifaces::BluetoothInterface *backend = qobject_cast<Ifaces::BluetoothInterface *>(backendObject());
00557 Ifaces::BluetoothRemoteDevice *iface = 0;
00558
00559 if (backend != 0) {
00560 iface = qobject_cast<Ifaces::BluetoothRemoteDevice *>(backend->createBluetoothRemoteDevice(ubi));
00561 }
00562
00563 if (iface != 0) {
00564 BluetoothRemoteDevice *device = new BluetoothRemoteDevice(iface);
00565
00566 QPair<BluetoothRemoteDevice *, Ifaces::BluetoothRemoteDevice *> pair(device, iface);
00567 remoteDeviceMap[ubi] = pair;
00568
00569 return pair;
00570 } else {
00571 return QPair<BluetoothRemoteDevice *, Ifaces::BluetoothRemoteDevice *>(0, 0);
00572 }
00573
00574 }
00575 }
00576
00577 QPair<Solid::Control::BluetoothInputDevice *, Solid::Control::Ifaces::BluetoothInputDevice *> Solid::Control::BluetoothInterfacePrivate::findRegisteredBluetoothInputDevice(const QString &ubi) const
00578 {
00579 if (inputDeviceMap.contains(ubi)) {
00580 return inputDeviceMap[ubi];
00581 } else {
00582 Ifaces::BluetoothInterface *backend = qobject_cast<Ifaces::BluetoothInterface *>(backendObject());
00583 Ifaces::BluetoothInputDevice *iface = 0;
00584
00585 if (backend != 0) {
00586 iface = qobject_cast<Ifaces::BluetoothInputDevice *>(backend->createBluetoothInputDevice(ubi));
00587 }
00588
00589 if (iface != 0) {
00590 BluetoothInputDevice *device = new BluetoothInputDevice(iface);
00591
00592 QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *> pair(device, iface);
00593 inputDeviceMap[ubi] = pair;
00594
00595 return pair;
00596 } else {
00597 return QPair<BluetoothInputDevice *, Ifaces::BluetoothInputDevice *>(0, 0);
00598 }
00599
00600 }
00601 }
00602
00603 #include "bluetoothinterface.moc"