cutting release 0.0.4
[profile/ivi/bluetooth-qt.git] / bluetoothdevice.cpp
1 /*  -*- Mode: C++ -*-
2  *
3  * meego handset bluetooth
4  * Copyright © 2010, Intel Corporation.
5  *
6  * This program is licensed under the terms and conditions of the
7  * Apache License, version 2.0.  The full text of the Apache License is at
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  */
11
12 #include "bluetoothdevice.h"
13 #include "blueadapter.h"
14 #include "bluemanager.h"
15 #include "btprofiles.h"
16 #include "audiosink.h"
17 #include "audiosource.h"
18 #include "headset.h"
19
20 BluetoothDevice::BluetoothDevice(QDBusObjectPath path, QObject *parent) :
21                 QObject(parent),m_device(new OrgBluezDeviceInterface("org.bluez",path.path(),QDBusConnection::systemBus(),this))
22 {
23         if(!m_device->isValid())
24                 return;
25         QObject::connect(m_device,SIGNAL(PropertyChanged(QString,QDBusVariant)),
26                                          this,SLOT(propertyChanged(QString,QDBusVariant)));
27 }
28
29 void BluetoothDevice::unpair()
30 {
31         OrgBluezManagerInterface manager(
32                         "org.bluez",
33                         "/", QDBusConnection::systemBus());
34
35         QDBusObjectPath adapterpath = manager.DefaultAdapter();
36
37         if(adapterpath.path().isEmpty()) return;
38
39         OrgBluezAdapterInterface adapter(
40                         "org.bluez",
41                         adapterpath.path(),
42                         QDBusConnection::systemBus());
43
44         adapter.RemoveDevice(QDBusObjectPath(m_device->path()));
45 }
46
47 void BluetoothDevice::connect(QString profile)
48 {
49         if(profile == BluetoothProfiles::a2sink)
50         {
51                 OrgBluezAudioSinkInterface sink("org.bluez",m_device->path(),
52                                                                                 QDBusConnection::systemBus());
53                 sink.Connect();
54         }
55         else if(profile == BluetoothProfiles::a2src)
56         {
57                 OrgBluezAudioSourceInterface source("org.bluez",m_device->path(),
58                                                                                 QDBusConnection::systemBus());
59                 source.Connect();
60         }
61         else if(profile == BluetoothProfiles::hs)
62         {
63                 OrgBluezHeadsetInterface headset("org.bluez",m_device->path(),
64                                                                                 QDBusConnection::systemBus());
65                 headset.Connect();
66         }
67         else if(profile == BluetoothProfiles::spp)
68         {
69                 QDBusInterface interface("org.bluez",m_device->path(),"org.bluez.Serial",QDBusConnection::systemBus());
70                 interface.call(QDBus::AutoDetect, "Connect","spp");
71         }
72 }
73
74 void BluetoothDevice::disconnect()
75 {
76         m_device->Disconnect();
77 }
78
79 QStringList BluetoothDevice::profiles()
80 {
81         QVariantMap props = m_device->GetProperties();
82
83         QStringList uuidlist = props["UUIDs"].toStringList();
84
85         return uuidlist;
86 }
87
88 bool BluetoothDevice::isProfileSupported(QString profile)
89 {
90         QVariantMap props = m_device->GetProperties();
91
92         QStringList uuidlist = props["UUIDs"].toStringList();
93
94         return uuidlist.contains(profile.toLower());
95 }
96
97 bool BluetoothDevice::connected()
98 {
99         QVariantMap props = m_device->GetProperties();
100         return props["Connected"].toBool();
101 }
102
103 QString BluetoothDevice::alias()
104 {
105         QVariantMap props = m_device->GetProperties();
106         return props["Alias"].toString();
107 }
108
109 QString BluetoothDevice::name()
110 {
111         QVariantMap props = m_device->GetProperties();
112         return props["Name"].toString();
113 }
114
115 QString BluetoothDevice::address()
116 {
117         QVariantMap props = m_device->GetProperties();
118         return props["Address"].toString();
119 }
120
121 QString BluetoothDevice::icon()
122 {
123         QVariantMap props = m_device->GetProperties();
124         return props["Icon"].toString();
125 }
126
127 QString BluetoothDevice::path()
128 {
129         return m_device->path();
130 }
131
132 void BluetoothDevice::propertyChanged(QString name,QDBusVariant value)
133 {
134         emit propertyChanged(name,value.variant());
135
136         if(name == "Connected")
137         {
138                 emit connetedChanged();
139         }
140 }