- fixed base agent ignoring pass in object path
[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         roles[name]="name";
27         roles[address]="address";
28         roles[path]="path";
29
30         setRoleNames(roles);
31 }
32
33 int BluetoothDevicesModel::rowCount(const QModelIndex &) const
34 {
35         return m_devices.size();
36 }
37
38 QVariant BluetoothDevicesModel::data(const QModelIndex &index, int role) const
39 {
40         qDebug()<<"requested role: "<<roleNames()[role];
41
42         if(!index.isValid() || index.row() < 0)
43         {
44                 qDebug()<<"index is not valid: "<<index.row()<<","<<index.column();
45                 return QVariant(); ///this is retarded but it has to be done.
46         }
47
48         if(role == name)
49         {
50                 QString rowData;
51                 if(index.row() < m_devices.size())
52                 {
53                         rowData = m_devices[index.row()]->name();
54                 }
55                 return QVariant(rowData);
56         }
57         else if(role == address)
58         {
59                 QString rowData;
60                 if(index.row() < m_devices.size())
61                 {
62                         rowData = m_devices[index.row()]->address();
63                 }
64                 return QVariant(rowData);
65         }
66         else if(role == path)
67         {
68                 QString rowData;
69                 if(index.row() < m_devices.size())
70                 {
71                         rowData = m_devices[index.row()]->path();
72                 }
73                 return QVariant(rowData);
74         }
75         return QVariant();
76 }
77
78 QString BluetoothDevicesModel::devicePath(QString devicename)
79 {
80         foreach(BluetoothDevice* device, m_devices)
81         {
82                 if(device->name() == devicename)
83                         return device->path();
84         }
85         return "";
86 }
87
88 BluetoothDevice* BluetoothDevicesModel::device(QString path)
89 {
90         foreach(BluetoothDevice* device, m_devices)
91         {
92                 if(device->path() == path)
93                         return device;
94         }
95         qDebug()<<"Device not found for path: "<<path;
96         return NULL;
97 }
98
99 void BluetoothDevicesModel::makeDiscoverable(bool discoverableValue)
100 {
101         if(adapter) adapter->SetProperty("Discoverable", QDBusVariant(discoverableValue));
102 }
103
104 bool BluetoothDevicesModel::discoverable()
105 {
106         if(adapter)
107         {
108                 QVariantMap props = adapter->GetProperties();
109                 return props["Discoverable"].toBool();
110         }
111
112         return false;
113 }
114
115 void BluetoothDevicesModel::adapterAdded(QDBusObjectPath path)
116 {
117         if(adapter && adapter->path() == path.path()) return;
118
119         QDBusObjectPath adapterpath = manager->DefaultAdapter();
120
121         if(adapterpath.path() == "")
122         {
123                 ///we actually shouldn't ever get here.
124                 return;
125         }
126
127         adapter = new OrgBluezAdapterInterface(
128                         "org.bluez",
129                         adapterpath.path(),
130                         QDBusConnection::systemBus(), this);
131
132         connect(adapter,
133                         SIGNAL(PropertyChanged(QString,QDBusVariant)),
134                         this,
135                         SLOT(adapterPropertyChanged(QString,QDBusVariant)));
136
137         connect(adapter,
138                 SIGNAL(DeviceRemoved(QDBusObjectPath)),
139                 this,
140                 SLOT(deviceRemoved(QDBusObjectPath)));
141
142         connect(adapter,
143                 SIGNAL(DeviceCreated(QDBusObjectPath)),
144                 this,
145                 SLOT(deviceCreated(QDBusObjectPath)));
146
147         adapterChanged(true);
148
149         QList<QDBusObjectPath> list = adapter->ListDevices();
150         foreach(QDBusObjectPath item, list)
151         {
152                 deviceCreated(item);
153         }
154 }
155
156 void BluetoothDevicesModel::adapterRemoved(QDBusObjectPath)
157 {
158         QDBusObjectPath adapterpath = manager->DefaultAdapter();
159
160         if(adapterpath.path() == "")
161         {
162                 beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
163                 foreach(BluetoothDevice* device, m_devices)
164                 {
165
166                         delete device;
167                 }
168                 m_devices.clear();
169                 endRemoveRows();
170
171                 if(adapter) delete adapter;
172                 adapter = NULL;
173                 adapterChanged(false);
174                 return;
175         }
176 }
177
178 void BluetoothDevicesModel::deviceCreated(QDBusObjectPath devicepath)
179 {
180         BluetoothDevice* device = new BluetoothDevice(devicepath,this);
181
182         connect(device,SIGNAL(propertyChanged(QString,QVariant)),this,SLOT(devicePropertyChanged(QString,QVariant)));
183
184         beginInsertRows(QModelIndex(),m_devices.size(),m_devices.size());
185         m_devices.append(device);
186         endInsertRows();
187 }
188
189 void BluetoothDevicesModel::deviceRemoved(QDBusObjectPath devicepath)
190 {
191         for(int i=0; i<m_devices.size(); i++)
192         {
193
194                 if(m_devices[i]->path() == devicepath.path())
195                 {
196                         beginRemoveRows(QModelIndex(), i, i);
197                         m_devices[i]->deleteLater();
198                         m_devices.removeAt(i);
199                         endRemoveRows();
200                 }
201         }
202 }
203
204 void BluetoothDevicesModel::devicePropertyChanged(QString name, QVariant value)
205 {
206         BluetoothDevice* device = qobject_cast<BluetoothDevice*>(sender());
207
208         qDebug()<<"device property changed for "<<device->address()<<": "<<name<<" "<<value;
209
210         if(name == "Paired" && value.toBool() == true)
211         {
212                 emit devicePaired(device);
213         }
214
215         int row = m_devices.indexOf(device);
216         if(row == -1) return; ///device doesn't exist.
217
218         dataChanged(createIndex(row, 0),createIndex(row, 0));
219 }
220
221
222 void BluetoothDevicesModel::adapterPropertyChanged(QString name, QDBusVariant value)
223 {
224         qDebug()<<"adapter property changed: "<<name<<" "<<value.variant();
225
226         if(name == "Discoverable")
227         {
228                 discoverableChanged(value.variant().toBool());
229         }
230 }