added debug message for device removal
[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), 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::makeDiscoverable(bool discoverableValue)
88 {
89         if(adapter) adapter->SetProperty("Discoverable", QDBusVariant(discoverableValue));
90 }
91
92 bool BluetoothDevicesModel::discoverable()
93 {
94         if(adapter)
95         {
96                 QVariantMap props = adapter->GetProperties();
97                 return props["Discoverable"].toBool();
98         }
99
100         return false;
101 }
102
103 int BluetoothDevicesModel::discoverableTimeout()
104 {
105         if(adapter)
106         {
107                 QVariantMap props = adapter->GetProperties();
108                 return props["DiscoverableTimeout"].toInt();
109         }
110
111         return -1;
112 }
113
114 void BluetoothDevicesModel::setDiscoverableTimeout(int timeout)
115 {
116         if(adapter)
117         {
118                 bool success = adapter->setProperty("DiscoverableTimeout", timeout);
119                 qDebug()<<"Setting discoverable timeout to "<<timeout<<": "<<success;
120         }
121 }
122
123 void BluetoothDevicesModel::adapterAdded(QDBusObjectPath path)
124 {
125         if(adapter && adapter->path() == path.path()) return;
126
127         QDBusObjectPath adapterpath = manager->DefaultAdapter();
128
129         if(adapterpath.path() == "")
130         {
131                 ///we actually shouldn't ever get here.
132                 return;
133         }
134
135         adapter = new OrgBluezAdapterInterface(
136                         "org.bluez",
137                         adapterpath.path(),
138                         QDBusConnection::systemBus(), this);
139
140         connect(adapter,
141                         SIGNAL(PropertyChanged(QString,QDBusVariant)),
142                         this,
143                         SLOT(adapterPropertyChanged(QString,QDBusVariant)));
144
145         connect(adapter,
146                 SIGNAL(DeviceRemoved(QDBusObjectPath)),
147                 this,
148                 SLOT(deviceRemoved(QDBusObjectPath)));
149
150         connect(adapter,
151                 SIGNAL(DeviceCreated(QDBusObjectPath)),
152                 this,
153                 SLOT(deviceCreated(QDBusObjectPath)));
154
155         adapterChanged(true);
156
157         QList<QDBusObjectPath> list = adapter->ListDevices();
158         foreach(QDBusObjectPath item, list)
159         {
160                 deviceCreated(item);
161         }
162 }
163
164 void BluetoothDevicesModel::adapterRemoved(QDBusObjectPath)
165 {
166         QDBusObjectPath adapterpath = manager->DefaultAdapter();
167
168         if(adapterpath.path() == "")
169         {
170                 beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
171                 foreach(BluetoothDevice* device, m_devices)
172                 {
173
174                         delete device;
175                 }
176                 m_devices.clear();
177                 endRemoveRows();
178
179                 if(adapter) delete adapter;
180                 adapter = NULL;
181                 adapterChanged(false);
182                 return;
183         }
184 }
185
186 void BluetoothDevicesModel::deviceCreated(QDBusObjectPath devicepath)
187 {
188         BluetoothDevice* device = new BluetoothDevice(devicepath,this);
189
190         connect(device,SIGNAL(propertyChanged(QString,QVariant)),this,SLOT(devicePropertyChanged(QString,QVariant)));
191
192         beginInsertRows(QModelIndex(),m_devices.size(),m_devices.size());
193         m_devices.append(device);
194         endInsertRows();
195 }
196
197 void BluetoothDevicesModel::deviceRemoved(QDBusObjectPath devicepath)
198 {
199         for(int i=0; i<m_devices.size(); i++)
200         {
201
202                 if(m_devices[i]->path() == devicepath.path())
203                 {
204                         beginRemoveRows(QModelIndex(), i, i);
205                         m_devices[i]->deleteLater();
206                         m_devices.removeAt(i);
207                         endRemoveRows();
208                 }
209         }
210 }
211
212 void BluetoothDevicesModel::devicePropertyChanged(QString name, QVariant value)
213 {
214         BluetoothDevice* device = qobject_cast<BluetoothDevice*>(sender());
215
216         qDebug()<<"device property changed for "<<device->address()<<": "<<name<<" "<<value;
217
218         if(name == "Paired" && value.toBool() == true)
219         {
220                 emit devicePaired(device);
221         }
222
223         int row = m_devices.indexOf(device);
224         if(row == -1) return; ///device doesn't exist.
225
226         dataChanged(createIndex(row, 0),createIndex(row, 0));
227 }
228
229
230 void BluetoothDevicesModel::adapterPropertyChanged(QString name, QDBusVariant value)
231 {
232         qDebug()<<"adapter property changed: "<<name<<" "<<value.variant();
233
234         if(name == "Discoverable")
235         {
236                 discoverableChanged(value.variant().toBool());
237         }
238         else if(name == "DiscoverableTimeout")
239         {
240                 discoverableTimeoutChanged(value.variant().toInt());
241         }
242 }