not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / solid / bluez / bluez-bluetoothmanager.cpp
blobe02cf7e95f3345d3590e10bec5b9e78de629766d
1 /* This file is part of the KDE project
2 Copyright (C) 2007 Will Stephenson <wstephenson@kde.org>
3 Copyright (C) 2007 Daniel Gollub <dgollub@suse.de>
4 Copyright (C) 2008 Tom Patzig <tpatzig@suse.de>
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License version 2 as published by the Free Software Foundation.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
23 #include "bluez-bluetoothmanager.h"
25 #include <QtDBus>
27 #include <kdebug.h>
29 #include "bluez-bluetoothinterface.h"
30 #include "bluez-bluetoothinputdevice.h"
31 #include "bluez-bluetoothsecurity.h"
32 #include "bluez-bluetoothsecurityadaptor.h"
33 #include "bluezcalljob.h"
35 class BluezBluetoothManagerPrivate
37 public:
40 BluezBluetoothManagerPrivate() : manager("org.bluez", "/", "org.bluez.Manager", QDBusConnection::systemBus())
43 QDBusInterface manager;
44 // QDBusInterface *inputManager;
46 QMap<QString, BluezBluetoothInterface *> interfaces;
47 // QMap<QString, BluezBluetoothInputDevice *> inputDevices;
51 BluezBluetoothManager::BluezBluetoothManager(QObject * parent, const QStringList & /*args */)
52 : BluetoothManager(parent), d(new BluezBluetoothManagerPrivate)
54 #define connectManagerToThis(signal, slot) \
55 d->manager.connection().connect("org.bluez", \
56 "/", \
57 "org.bluez.Manager", \
58 signal, this, SLOT(slot));
59 connectManagerToThis("AdapterAdded", slotDeviceAdded(const QDBusObjectPath &));
60 connectManagerToThis("AdapterRemoved", slotDeviceRemoved(const QDBusObjectPath &));
61 connectManagerToThis("DefaultAdapterChanged", slotDefaultDeviceChanged(const QDBusObjectPath &));
63 // in bluez4 no input Manager needed
65 /* QDBusReply< QString > busId = d->manager.call("ActivateService", "input");
66 if (busId.isValid()) {
67 m_inputManagerDest = busId.value();
71 d->inputManager = new QDBusInterface(m_inputManagerDest, "/org/bluez/input",
72 "org.bluez.input.Manager", QDBusConnection::systemBus());
74 #define connectInputManagerToThis(signal, slot) \
75 d->inputManager->connection().connect(m_inputManagerDest, \
76 "/org/bluez/input", \
77 "org.bluez.input.Manager", \
78 signal, this, SLOT(slot));
80 connectInputManagerToThis("DeviceCreated", inputDeviceCreated(const QString &));
81 connectInputManagerToThis("DeviceRemoved", inputDeviceRemoved(const QString &));
85 BluezBluetoothManager::~BluezBluetoothManager()
87 // delete d->inputManager;
88 delete d;
91 QStringList BluezBluetoothManager::bluetoothInterfaces() const
93 QStringList bluetoothInterfaces;
95 QDBusReply< QList<QDBusObjectPath> > deviceList = d->manager.call("ListAdapters");
96 if (deviceList.isValid()) {
97 QList<QDBusObjectPath> devices = deviceList.value();
98 foreach (const QDBusObjectPath& path, devices) {
99 bluetoothInterfaces.append(path.path());
102 return bluetoothInterfaces;
105 QString BluezBluetoothManager::defaultInterface() const
107 kDebug() << "Calling Backend Default Interface";
108 QDBusReply< QDBusObjectPath > path = d->manager.call("DefaultAdapter");
109 if (!path.isValid())
110 return QString();
112 return path.value().path();
115 QString BluezBluetoothManager::findInterface(const QString &adapterName) const
117 QDBusReply< QDBusObjectPath > devicePath = d->manager.call("FindAdapter",adapterName);
118 if (!devicePath.isValid())
119 return QString();
121 return devicePath.value().path();
124 QObject * BluezBluetoothManager::createInterface(const QString & ubi)
126 BluezBluetoothInterface * bluetoothInterface;
127 if (d->interfaces.contains(ubi)) {
128 bluetoothInterface = d->interfaces[ubi];
129 } else {
130 bluetoothInterface = new BluezBluetoothInterface(ubi);
131 d->interfaces.insert(ubi, bluetoothInterface);
133 return bluetoothInterface;
136 void BluezBluetoothManager::removeInterface(const QString& ubi)
139 if (d->interfaces.contains(ubi)) {
140 kDebug() << "Removing Interface" << ubi;
141 BluezBluetoothInterface * bluetoothInterface = d->interfaces.take(ubi);
142 bluetoothInterface = 0;
147 KJob *BluezBluetoothManager::setupInputDevice(const QString &ubi)
149 QString address = ubi.right(17);
151 QList<QVariant> params;
152 params << address;
154 return new BluezCallJob(QDBusConnection::systemBus(), m_inputManagerDest, "/org/bluez/input", "org.bluez.input.Manager",
155 "CreateDevice", params);
160 QStringList BluezBluetoothManager::bluetoothInputDevices() const
162 QStringList bluetoothInputDevices;
164 QDBusReply< QStringList > deviceList = d->inputManager->call("ListDevices");
165 if (deviceList.isValid()) {
166 QStringList devices = deviceList.value();
167 foreach (const QString& path, devices) {
168 bluetoothInputDevices.append(path);
171 return bluetoothInputDevices;
174 void BluezBluetoothManager::removeInputDevice(const QString &ubi)
176 d->inputManager->call("RemoveDevice", ubi);
179 QObject *BluezBluetoothManager::createBluetoothInputDevice(QString const &ubi)
181 BluezBluetoothInputDevice *bluetoothInputDevice;
182 if (d->inputDevices.contains(ubi)) {
183 bluetoothInputDevice = d->inputDevices[ubi];
184 } else {
185 bluetoothInputDevice = new BluezBluetoothInputDevice(ubi, m_inputManagerDest);
186 d->inputDevices.insert(ubi, bluetoothInputDevice);
188 return bluetoothInputDevice;
192 void BluezBluetoothManager::slotDeviceAdded(const QDBusObjectPath &adapter)
194 // TODO free the adapter device...
195 kDebug() << "interfaceAdded " << adapter.path();
196 emit interfaceAdded(adapter.path());
199 void BluezBluetoothManager::slotDeviceRemoved(const QDBusObjectPath &adapter)
201 kDebug() << "interfaceRemoved " << adapter.path();
202 emit interfaceRemoved(adapter.path());
205 void BluezBluetoothManager::slotDefaultDeviceChanged(const QDBusObjectPath &adapter)
207 kDebug() << "defaultDeviceChanged " << adapter.path();
208 emit defaultInterfaceChanged(adapter.path());
212 void BluezBluetoothManager::slotInputDeviceCreated(const QString &path)
214 emit inputDeviceCreated(path);
217 void BluezBluetoothManager::slotInputDeviceRemoved(const QString &path)
219 // TODO free the input device...
220 emit inputDeviceRemoved(path);
225 Solid::Control::Ifaces::BluetoothSecurity *BluezBluetoothManager::security(const QString &interface)
227 BluezBluetoothSecurity *out;
228 if (interface.isEmpty()) {
229 out = new BluezBluetoothSecurity(this);
230 } else {
231 out = new BluezBluetoothSecurity(interface,this);
233 new BluezBluetoothSecurityPasskeyAgentAdaptor(out);
234 new BluezBluetoothSecurityAuthorizationAgentAdaptor(out);
235 return out;
238 #include "bluez-bluetoothmanager.moc"