add more spacing
[personal-kdebase.git] / workspace / solid / bluez / bluez-bluetoothinterface.cpp
blob280ecd8f3cd3df6285791a604e40a482bf1b1b66
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-bluetoothinterface.h"
25 #include <solid/control/bluetoothinterface.h>
27 #include "bluez-bluetoothremotedevice.h"
28 #include "bluez-bluetoothinputdevice.h"
29 #include <KDebug>
33 class BluezBluetoothInterfacePrivate
35 public:
36 BluezBluetoothInterfacePrivate(const QString & objPath)
37 : iface("org.bluez",
38 objPath,
39 "org.bluez.Adapter",
40 QDBusConnection::systemBus()),
41 objectPath(objPath)
42 { }
43 QDBusInterface iface;
44 QString objectPath;
46 QMap<QString, BluezBluetoothRemoteDevice *> devices;
47 QMap<QString, BluezBluetoothInputDevice *> inputDevices;
52 BluezBluetoothInterface::BluezBluetoothInterface(const QString & objectPath)
53 : BluetoothInterface(0), d(new BluezBluetoothInterfacePrivate(objectPath))
56 #define connectInterfaceToThis(signal, slot) \
57 d->iface.connection().connect("org.bluez", \
58 objectPath, \
59 "org.bluez.Adapter", \
60 signal, this, SLOT(slot));
62 connectInterfaceToThis("PropertyChanged", slotPropertyChanged(const QString &, const QVariant &));
63 connectInterfaceToThis("DeviceCreated", slotDeviceCreated(const QDBusObjectPath &));
64 connectInterfaceToThis("DeviceRemoved", slotDeviceRemoved(const QDBusObjectPath &));
65 connectInterfaceToThis("DeviceDisappeared", slotDeviceDisappeared(const QString &));
66 connectInterfaceToThis("DeviceFound", slotDeviceFound(const QString &, const QMap< QString,QVariant > &));
71 BluezBluetoothInterface::~BluezBluetoothInterface()
73 delete d;
76 QString BluezBluetoothInterface::ubi() const
78 return d->objectPath;
81 void BluezBluetoothInterface::cancelDeviceCreation(const QString &addr)
83 d->iface.call("CancelDeviceCreation",addr);
86 void BluezBluetoothInterface::createDevice(const QString &addr) const
88 d->iface.call("CreateDevice",addr);
91 void BluezBluetoothInterface::createPairedDevice(const QString &addr, const QString &agentUBI, const QString &capab) const
93 d->iface.call("CreatePairedDevice",addr,qVariantFromValue(QDBusObjectPath(agentUBI)),capab);
96 QString BluezBluetoothInterface::findDevice(const QString &addr) const
98 QDBusObjectPath path = objectReply("FindDevice",addr);
99 return path.path();
103 QMap<QString, QVariant> BluezBluetoothInterface::getProperties() const
105 QDBusReply< QMap<QString,QVariant> > prop = d->iface.call("GetProperties");
106 if (!prop.isValid()) {
107 return QMap< QString,QVariant >();
109 return prop.value();
112 QStringList BluezBluetoothInterface::listDevices() const
114 QStringList deviceList;
116 QDBusReply< QList<QDBusObjectPath> > devices = d->iface.call("ListDevices");
117 if(!devices.isValid()) {
118 return QStringList();
120 foreach(QDBusObjectPath path, devices.value()) {
121 deviceList.append(path.path());
123 return deviceList;
126 void BluezBluetoothInterface::registerAgent(const QString &agentUBI, const QString &capab)
128 d->iface.call("RegisterAgent",qVariantFromValue(QDBusObjectPath(agentUBI)),capab);
131 void BluezBluetoothInterface::releaseSession()
133 d->iface.call("ReleaseSession");
136 void BluezBluetoothInterface::removeDevice(const QString &deviceUBI )
138 d->iface.call("RemoveDevice",qVariantFromValue(QDBusObjectPath(deviceUBI)));
141 void BluezBluetoothInterface::requestSession()
143 d->iface.call("RequestSession");
146 void BluezBluetoothInterface::setProperty(const QString &property, const QVariant &value)
148 d->iface.call("SetProperty",property, qVariantFromValue(QDBusVariant(value)));
152 void BluezBluetoothInterface::startDiscovery()
154 d->iface.call("StartDiscovery");
157 void BluezBluetoothInterface::stopDiscovery()
159 d->iface.call("StopDiscovery");
162 void BluezBluetoothInterface::unregisterAgent(const QString &agentUBI)
164 d->iface.call("UnregisterAgent",qVariantFromValue(QDBusObjectPath(agentUBI)));
169 void BluezBluetoothInterface::slotDeviceCreated(const QDBusObjectPath &path)
171 kDebug() << "device created";
173 if (!d->devices.contains(path.path())) {
174 BluezBluetoothRemoteDevice* bluetoothRemoteDev = new BluezBluetoothRemoteDevice(path.path());
175 d->devices.insert(path.path(), bluetoothRemoteDev);
178 emit deviceCreated(path.path());
181 void BluezBluetoothInterface::slotDeviceDisappeared(const QString &address)
183 kDebug() << "device disappeared";
184 emit deviceDisappeared(address);
187 void BluezBluetoothInterface::slotDeviceFound(const QString &address, const QMap< QString, QVariant > &properties)
189 kDebug() << "device found " << address << " " << properties["Name"];
190 emit deviceFound(address,properties);
193 void BluezBluetoothInterface::slotDeviceRemoved(const QDBusObjectPath &path)
195 kDebug() << "device removed";
196 emit deviceRemoved(path.path());
199 void BluezBluetoothInterface::slotPropertyChanged(const QString & property, const QVariant &value)
201 kDebug() << "Property " << property << " changed to " << value;
202 emit propertyChanged(property,value);
207 QObject *BluezBluetoothInterface::createBluetoothRemoteDevice(const QString &ubi)
209 BluezBluetoothRemoteDevice *bluetoothInterface;
210 if (d->devices.contains(ubi)) {
211 bluetoothInterface = d->devices[ubi];
212 } else {
213 bluetoothInterface = new BluezBluetoothRemoteDevice(ubi);
214 d->devices.insert(ubi, bluetoothInterface);
216 return bluetoothInterface;
219 QObject *BluezBluetoothInterface::createBluetoothInputDevice(const QString &ubi)
221 BluezBluetoothInputDevice *bluetoothInputDev;
222 if (d->inputDevices.contains(ubi)) {
223 bluetoothInputDev = d->inputDevices[ubi];
224 } else {
225 bluetoothInputDev = new BluezBluetoothInputDevice(ubi);
226 d->inputDevices.insert(ubi, bluetoothInputDev);
228 return bluetoothInputDev;
233 /******************* DBus Calls *******************************/
235 QStringList BluezBluetoothInterface::listReply(const QString &method) const
237 QDBusReply< QStringList > list = d->iface.call(method);
238 if (!list.isValid()) {
239 return QStringList();
242 return list.value();
245 QString BluezBluetoothInterface::stringReply(const QString &method, const QString &param) const
247 QDBusReply< QString > reply;
249 if (param.isEmpty())
250 reply = d->iface.call(method);
251 else
252 reply = d->iface.call(method, param);
254 if (reply.isValid()) {
255 return reply.value();
258 return QString();
261 bool BluezBluetoothInterface::boolReply(const QString &method, const QString &param) const
263 QDBusReply< bool > reply;
265 if (param.isEmpty())
266 reply = d->iface.call(method);
267 else
268 reply = d->iface.call(method, param);
270 if (reply.isValid()) {
271 return reply.value();
274 return false;
277 QDBusObjectPath BluezBluetoothInterface::objectReply(const QString &method, const QString &param) const
279 QDBusReply< QDBusObjectPath > reply;
281 if (param.isEmpty())
282 reply = d->iface.call(method);
283 else {
284 qDebug() << "ObjectReply calling: " << method << " " << param;
285 reply = d->iface.call(method, param);
288 if (reply.isValid()) {
289 qDebug() << "ObjectReply Valid? "<< reply.value().path();
290 return reply.value();
293 return QDBusObjectPath();
296 #include "bluez-bluetoothinterface.moc"