added some properties and made roles dynamic based on them
[profile/ivi/bluetooth-qt.git] / nearbydevicesmodel.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 "nearbydevicesmodel.h"
13 #include "bluetoothbaseagent.h"
14
15 NearbyDevicesModel::NearbyDevicesModel(QObject *parent) :
16         QAbstractListModel(parent), adapter(NULL), agent(NULL)
17 {
18         manager = new OrgBluezManagerInterface(
19                         "org.bluez",
20                         "/", QDBusConnection::systemBus(), this);
21
22         connect(manager,SIGNAL(AdapterAdded(QDBusObjectPath)),this,SLOT(adapterAdded(QDBusObjectPath)));
23         connect(manager,SIGNAL(AdapterRemoved(QDBusObjectPath)),this,SLOT(adapterRemoved(QDBusObjectPath)));
24         adapterAdded(QDBusObjectPath());
25
26         QHash<int,QByteArray> roles;
27         QMetaObject properties = NearbyItem::staticMetaObject;
28         for(int i=0; i<properties.propertyCount();i++)
29         {
30                 roles[i]=properties.property(i).name();
31         }
32         setRoleNames(roles);
33 }
34
35
36 int NearbyDevicesModel::rowCount(const QModelIndex &parent) const
37 {
38         Q_UNUSED(parent);
39         return devices.size();
40 }
41
42 QVariant NearbyDevicesModel::data(const QModelIndex &index, int role) const
43 {
44
45         if(!index.isValid() || index.row() < 0)
46         {
47                 qDebug()<<"invalid index"<<index.row();
48                 return QVariant();
49         }
50
51         QString roleName = roleNames()[role];
52         QMetaObject object = NearbyItem::staticMetaObject;
53
54         for(int i=0; i<object.propertyCount(); i++)
55         {
56                 if(object.property(i).name() == roleName)
57                 {
58
59                         return object.property(i).read(devices[index.row()]);
60                 }
61         }
62
63         return QVariant();
64 }
65
66 void NearbyDevicesModel::pair(QString hwaddy)
67 {
68
69         qDebug()<<"attempting to pair with "<<hwaddy;
70         if(!adapter) return;
71         if(!agent) agent = new AsyncAgent("/temp/agent", this);
72
73         adapter->CreatePairedDevice(hwaddy,
74                                    QDBusObjectPath("/temp/agent"),"");
75
76         //qDebug()<<"new object created: "<<object.path();
77 }
78
79 void NearbyDevicesModel::discover(bool start)
80 {
81         if(!adapter) return;
82
83         if(start)
84                 adapter->StartDiscovery();
85         else adapter->StopDiscovery();
86 }
87
88 void NearbyDevicesModel::removeAll(bool)
89 {
90         for(int i=0;i<devices.size();i++)
91         {
92                 devices.removeAt(i);
93         }
94 }
95
96 void NearbyDevicesModel::replyRequestConfirmation(bool confirmed)
97 {
98         qDebug()<<"reply to RequestConfirmation:"<<confirmed;
99         if(agent) agent->replyRequestConfirmation(confirmed);
100 }
101
102 void NearbyDevicesModel::replyPasskey(uint passkey)
103 {
104         if(agent) agent->replyPasskey(passkey);
105 }
106
107 void NearbyDevicesModel::replyRequestPidCode(QString pidCode)
108 {
109         if(agent) agent->replyRequestPidCode(pidCode);
110 }
111
112 void NearbyDevicesModel::setAdapterProperty(QString name, QVariant value)
113 {
114         if(adapter) adapter->setProperty(name.toAscii().data(),value);
115 }
116
117 void NearbyDevicesModel::deviceCreated(QString hwaddy, QVariantMap properties)
118 {
119         bool found = false;
120         foreach(NearbyItem* path, devices)
121         {
122                 if(path->address() == hwaddy)
123                 {
124                         found=true;
125                         break;
126                 }
127         }
128
129         if(!found)
130         {
131                 beginInsertRows(QModelIndex(), devices.size(), devices.size());
132
133                 NearbyItem* item = new NearbyItem(properties["Name"].toString(),
134                                hwaddy,properties["Icon"].toString(),properties["LegacyPairing"].toBool(),this);
135
136                 devices.append(item);
137                 emit nearbyDeviceFound(devices.indexOf(item));
138                 endInsertRows();
139         }
140 }
141
142 void NearbyDevicesModel::deviceRemoved(QString hwaddy)
143 {
144         foreach(NearbyItem* device, devices)
145         {
146                 if(device->address() == hwaddy)
147                 {
148                         int i=devices.indexOf(device);
149                         //if(i == -1) continue;
150                         beginRemoveRows(QModelIndex(),i,i);
151                         devices.removeAt(i);
152                         emit nearbyDeviceRemoved(i);
153                         endRemoveRows();
154                 }
155         }
156 }
157
158 void NearbyDevicesModel::adapterAdded(QDBusObjectPath path)
159 {
160         if(adapter && adapter->path() == path.path()) return;
161
162         QDBusObjectPath adapterpath = manager->DefaultAdapter();
163
164         if(adapterpath.path() == "")
165                 return;
166
167         adapter = new OrgBluezAdapterInterface(
168                         "org.bluez",
169                         adapterpath.path(),
170                         QDBusConnection::systemBus(), this);
171
172         connect(adapter,
173                 SIGNAL(DeviceFound(QString, QVariantMap)),
174                 this,
175                 SLOT(deviceCreated(QString, QVariantMap)));
176         connect(adapter,
177                 SIGNAL(DeviceDisappeared(QString)),
178                 this,
179                 SLOT(deviceRemoved(QString)));
180         connect(adapter,
181                         SIGNAL(PropertyChanged(QString,QDBusVariant)),
182                         this,
183                         SLOT(adapterPropertiesChangedSlot(QString,QDBusVariant)));
184 }
185
186 void NearbyDevicesModel::adapterRemoved(QDBusObjectPath)
187 {
188         QDBusObjectPath adapterpath = manager->DefaultAdapter();
189
190         if(adapterpath.path() == "")
191         {
192                 removeAll(true);
193                 if(adapter){ delete adapter; adapter = NULL; }
194                 return;
195         }
196 }
197
198 void NearbyDevicesModel::adapterPropertiesChangedSlot(QString n,QDBusVariant v)
199 {
200         adapterPropertiesChanged(n,v.variant());
201 }