24015a509e0e691886f0ce836ead41628f3dd15a
[profile/ivi/bluetooth-qt.git] / bluetoothdevicemodel.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 "bluetoothdevicemodel.h"
13
14 BluetoothDevicesModel::BluetoothDevicesModel(QObject *parent) :
15         QAbstractListModel(parent), m_connected(false), adapter(NULL)
16 {
17         manager = new OrgBluezManagerInterface(
18                         "org.bluez",
19                         "/", QDBusConnection::systemBus(), this);
20
21         connect(manager,SIGNAL(AdapterAdded(QDBusObjectPath)),this,SLOT(adapterAdded(QDBusObjectPath)));
22         connect(manager,SIGNAL(AdapterRemoved(QDBusObjectPath)),this,SLOT(adapterRemoved(QDBusObjectPath)));
23         adapterAdded(QDBusObjectPath());
24
25         QHash<int, QByteArray> roles;
26
27         QMetaObject properties = BluetoothDevice::staticMetaObject;
28
29         for(int i=0; i<properties.propertyCount();i++)
30         {
31                 roles[i]=properties.property(i).name();
32         }
33
34         setRoleNames(roles);
35 }
36
37 int BluetoothDevicesModel::rowCount(const QModelIndex &) const
38 {
39         return m_devices.size();
40 }
41
42 QVariant BluetoothDevicesModel::data(const QModelIndex &index, int role) const
43 {
44         qDebug()<<"requested role: "<<roleNames()[role];
45
46         if(!index.isValid() || index.row() < 0)
47         {
48                 qDebug()<<"index is not valid: "<<index.row()<<","<<index.column();
49                 return QVariant(); ///this is retarded but it has to be done.
50         }
51
52         QString roleName = roleNames()[role];
53         QMetaObject object = BluetoothDevice::staticMetaObject;
54
55         for(int i=0; i<object.propertyCount(); i++)
56         {
57                 if(object.property(i).name() == roleName)
58                 {
59
60                         return object.property(i).read(m_devices[index.row()]);
61                 }
62         }
63         return QVariant();
64 }
65
66 QString BluetoothDevicesModel::devicePath(QString devicename)
67 {
68         foreach(BluetoothDevice* device, m_devices)
69         {
70                 if(device->name() == devicename)
71                         return device->path();
72         }
73         return "";
74 }
75
76 BluetoothDevice* BluetoothDevicesModel::device(QString path)
77 {
78         foreach(BluetoothDevice* device, m_devices)
79         {
80                 if(device->path() == path)
81                         return device;
82         }
83         qDebug()<<"Device not found for path: "<<path;
84         return NULL;
85 }
86
87 void BluetoothDevicesModel::makePowered(bool poweredValue)
88 {
89         if(adapter) adapter->SetProperty("Powered", QDBusVariant(poweredValue));
90 }
91
92 bool BluetoothDevicesModel::powered()
93 {
94         if(adapter)
95         {
96                 QVariantMap props = adapter->GetProperties();
97                 return props["Powered"].toBool();
98         }
99
100         return false;
101 }
102
103 void BluetoothDevicesModel::makeDiscoverable(bool discoverableValue)
104 {
105         if(adapter) adapter->SetProperty("Discoverable", QDBusVariant(discoverableValue));
106 }
107
108 bool BluetoothDevicesModel::discoverable()
109 {
110         if(adapter)
111         {
112                 QVariantMap props = adapter->GetProperties();
113                 return props["Discoverable"].toBool();
114         }
115
116         return false;
117 }
118
119 int BluetoothDevicesModel::discoverableTimeout()
120 {
121         if(adapter)
122         {
123                 QVariantMap props = adapter->GetProperties();
124                 return props["DiscoverableTimeout"].toInt();
125         }
126
127         return -1;
128 }
129
130 void BluetoothDevicesModel::setDiscoverableTimeout(int timeout)
131 {
132         if(adapter)
133         {
134                 bool success = adapter->setProperty("DiscoverableTimeout", timeout);
135                 qDebug()<<"Setting discoverable timeout to "<<timeout<<": "<<success;
136         }
137 }
138
139 void BluetoothDevicesModel::adapterAdded(QDBusObjectPath path)
140 {
141         if(adapter && adapter->path() == path.path()) return;
142
143         QDBusObjectPath adapterpath = manager->DefaultAdapter();
144
145         if(adapterpath.path() == "")
146         {
147                 ///we actually shouldn't ever get here.
148                 return;
149         }
150
151         adapter = new OrgBluezAdapterInterface(
152                         "org.bluez",
153                         adapterpath.path(),
154                         QDBusConnection::systemBus(), this);
155
156         connect(adapter,
157                         SIGNAL(PropertyChanged(QString,QDBusVariant)),
158                         this,
159                         SLOT(adapterPropertyChanged(QString,QDBusVariant)));
160
161         connect(adapter,
162                 SIGNAL(DeviceRemoved(QDBusObjectPath)),
163                 this,
164                 SLOT(deviceRemoved(QDBusObjectPath)));
165
166         connect(adapter,
167                 SIGNAL(DeviceCreated(QDBusObjectPath)),
168                 this,
169                 SLOT(deviceCreated(QDBusObjectPath)));
170
171         adapterChanged(true);
172
173         QList<QDBusObjectPath> list = adapter->ListDevices();
174         foreach(QDBusObjectPath item, list)
175         {
176                 deviceCreated(item);
177         }
178 }
179
180 void BluetoothDevicesModel::adapterRemoved(QDBusObjectPath)
181 {
182         QDBusObjectPath adapterpath = manager->DefaultAdapter();
183
184         if(adapterpath.path() == "")
185         {
186                 beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
187                 foreach(BluetoothDevice* device, m_devices)
188                 {
189                         updateConnected(device->connected());
190                         device->deleteLater();
191                 }
192                 m_devices.clear();
193                 endRemoveRows();
194
195                 if(adapter) delete adapter;
196                 adapter = NULL;
197                 adapterChanged(false);
198                 return;
199         }
200 }
201
202 void BluetoothDevicesModel::deviceCreated(QDBusObjectPath devicepath)
203 {
204         BluetoothDevice* device = new BluetoothDevice(devicepath,this);
205
206         updateConnected(device->connected());
207         connect(device,SIGNAL(propertyChanged(QString,QVariant)),this,SLOT(devicePropertyChanged(QString,QVariant)));
208
209         beginInsertRows(QModelIndex(),m_devices.size(),m_devices.size());
210         m_devices.append(device);
211         endInsertRows();
212 }
213
214 void BluetoothDevicesModel::deviceRemoved(QDBusObjectPath devicepath)
215 {
216         for(int i=0; i<m_devices.size(); i++)
217         {
218
219                 if(m_devices[i]->path() == devicepath.path())
220                 {
221                         beginRemoveRows(QModelIndex(), i, i);
222                         m_devices[i]->deleteLater();
223                         m_devices.removeAt(i);
224                         endRemoveRows();
225                 }
226         }
227 }
228
229 void BluetoothDevicesModel::devicePropertyChanged(QString name, QVariant value)
230 {
231         BluetoothDevice* device = qobject_cast<BluetoothDevice*>(sender());
232
233         qDebug()<<"device property changed for "<<device->address()<<": "<<name<<" "<<value;
234
235         if(name == "Paired" && value.toBool() == true)
236         {
237                 emit devicePaired(device);
238         }
239
240         else if(name == "Connected")
241         {
242                 updateConnected(value.toBool());
243         }
244         int row = m_devices.indexOf(device);
245         if(row == -1) return; ///device doesn't exist.
246
247         dataChanged(createIndex(row, 0),createIndex(row, 0));
248 }
249
250 void BluetoothDevicesModel::adapterPropertyChanged(QString name, QDBusVariant value)
251 {
252         qDebug()<<"adapter property changed: "<<name<<" "<<value.variant();
253
254         if(name == "Powered")
255         {
256                 poweredChanged(value.variant().toBool());
257         }
258         else if(name == "Discoverable")
259         {
260                 discoverableChanged(value.variant().toBool());
261         }
262         else if(name == "DiscoverableTimeout")
263         {
264                 discoverableTimeoutChanged(value.variant().toInt());
265         }
266 }
267
268 void BluetoothDevicesModel::updateConnected(bool deviceconnectedStatus)
269 {
270         if(deviceconnectedStatus)
271         {
272                 if(!m_connected)
273                 {
274                         m_connected = true;
275                         connectedChanged(m_connected);
276                 }
277         }
278         else
279         {
280                 bool temp = false;
281                 foreach(BluetoothDevice* device, devices())
282                 {
283                         temp |= device->connected();
284                 }
285
286                 if(temp != m_connected)
287                 {
288                         m_connected = temp;
289                         connectedChanged(m_connected);
290                 }
291         }
292 }