b2e6215672714f30ab35239b9fbd34e73cd872c8
[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 void BluetoothDevicesModel::adapterAdded(QDBusObjectPath path)
104 {
105         if(adapter && adapter->path() == path.path()) return;
106
107         QDBusObjectPath adapterpath = manager->DefaultAdapter();
108
109         if(adapterpath.path() == "")
110         {
111                 ///we actually shouldn't ever get here.
112                 return;
113         }
114
115         adapter = new OrgBluezAdapterInterface(
116                         "org.bluez",
117                         adapterpath.path(),
118                         QDBusConnection::systemBus(), this);
119
120         connect(adapter,
121                         SIGNAL(PropertyChanged(QString,QDBusVariant)),
122                         this,
123                         SLOT(adapterPropertyChanged(QString,QDBusVariant)));
124
125         connect(adapter,
126                 SIGNAL(DeviceRemoved(QDBusObjectPath)),
127                 this,
128                 SLOT(deviceRemoved(QDBusObjectPath)));
129
130         connect(adapter,
131                 SIGNAL(DeviceCreated(QDBusObjectPath)),
132                 this,
133                 SLOT(deviceCreated(QDBusObjectPath)));
134
135         adapterChanged(true);
136
137         QList<QDBusObjectPath> list = adapter->ListDevices();
138         foreach(QDBusObjectPath item, list)
139         {
140                 deviceCreated(item);
141         }
142 }
143
144 void BluetoothDevicesModel::adapterRemoved(QDBusObjectPath)
145 {
146         QDBusObjectPath adapterpath = manager->DefaultAdapter();
147
148         if(adapterpath.path() == "")
149         {
150                 beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
151                 foreach(BluetoothDevice* device, m_devices)
152                 {
153
154                         delete device;
155                 }
156                 m_devices.clear();
157                 endRemoveRows();
158
159                 if(adapter) delete adapter;
160                 adapter = NULL;
161                 adapterChanged(false);
162                 return;
163         }
164 }
165
166 void BluetoothDevicesModel::deviceCreated(QDBusObjectPath devicepath)
167 {
168         BluetoothDevice* device = new BluetoothDevice(devicepath,this);
169
170         connect(device,SIGNAL(propertyChanged(QString,QVariant)),this,SLOT(devicePropertyChanged(QString,QVariant)));
171
172         beginInsertRows(QModelIndex(),m_devices.size(),m_devices.size());
173         m_devices.append(device);
174         endInsertRows();
175 }
176
177 void BluetoothDevicesModel::deviceRemoved(QDBusObjectPath devicepath)
178 {
179         for(int i=0; i<m_devices.size(); i++)
180         {
181
182                 if(m_devices[i]->path() == devicepath.path())
183                 {
184                         beginRemoveRows(QModelIndex(), i, i);
185                         m_devices[i]->deleteLater();
186                         m_devices.removeAt(i);
187                         endRemoveRows();
188                 }
189         }
190 }
191
192 void BluetoothDevicesModel::devicePropertyChanged(QString name, QVariant value)
193 {
194         BluetoothDevice* device = qobject_cast<BluetoothDevice*>(sender());
195
196         qDebug()<<"device property changed for "<<device->address()<<": "<<name<<" "<<value;
197
198         if(name == "Paired" && value.toBool() == true)
199         {
200                 emit devicePaired(device);
201         }
202
203         int row = m_devices.indexOf(device);
204         if(row == -1) return; ///device doesn't exist.
205
206         dataChanged(createIndex(row, 0),createIndex(row, 0));
207 }
208
209
210 void BluetoothDevicesModel::adapterPropertyChanged(QString name, QDBusVariant value)
211 {
212         qDebug()<<"adapter property changed: "<<name<<" "<<value.variant();
213
214         if(name == "Discoverable")
215         {
216                 discoverableChanged(value.variant().toBool());
217         }
218 }