added release signal and fixed bug in ayncagent that was causing crashes
[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         roles[NearbyDevicesModelRoles::name]="name";
28         roles[NearbyDevicesModelRoles::address]="address";
29         setRoleNames(roles);
30 }
31
32
33 int NearbyDevicesModel::rowCount(const QModelIndex &parent) const
34 {
35         Q_UNUSED(parent);
36         return devicepathlist.size();
37 }
38
39 QVariant NearbyDevicesModel::data(const QModelIndex &index, int role) const
40 {
41         if (role == NearbyDevicesModelRoles::name)
42         {
43                 QString rowData;
44                 if(index.row() < devicepathlist.size())
45                 {
46                         rowData = deviceAliasMap[devicepathlist[index.row()]];
47                 }
48                 return QVariant(rowData);
49         }
50         else if (role == NearbyDevicesModelRoles::address)
51         {
52                 QString rowData;
53                 if(index.row() < devicepathlist.size())
54                 {
55                         rowData = devicepathlist[index.row()];
56                 }
57                 return QVariant(rowData);
58         }
59         return QVariant();
60 }
61
62 void NearbyDevicesModel::pair(QString hwaddy)
63 {
64
65         qDebug()<<"attempting to pair with "<<hwaddy;
66         if(!adapter) return;
67         agent = new AsyncAgent("/temp/agent", this);
68
69         adapter->CreatePairedDevice(hwaddy,
70                                    QDBusObjectPath("/temp/agent"),"");
71
72         //qDebug()<<"new object created: "<<object.path();
73 }
74
75 void NearbyDevicesModel::discover(bool start)
76 {
77         if(!adapter) return;
78
79         if(start)
80                 adapter->StartDiscovery();
81         else adapter->StopDiscovery();
82 }
83
84 void NearbyDevicesModel::removeAll(bool)
85 {
86         for(int i=0;i<devicepathlist.size();i++)
87         {
88                 devicepathlist.removeAt(i);
89         }
90 }
91
92 void NearbyDevicesModel::replyRequestConfirmation(bool confirmed)
93 {
94         qDebug()<<"reply to RequestConfirmation:"<<confirmed;
95         if(agent) agent->replyRequestConfirmation(confirmed);
96 }
97
98 void NearbyDevicesModel::replyPasskey(uint passkey)
99 {
100         if(agent) agent->replyPasskey(passkey);
101 }
102
103 void NearbyDevicesModel::replyRequestPidCode(QString pidCode)
104 {
105         if(agent) agent->replyRequestPidCode(pidCode);
106 }
107
108 void NearbyDevicesModel::setAdapterProperty(QString name, QVariant value)
109 {
110         if(adapter) adapter->setProperty(name.toAscii().data(),value);
111 }
112
113 void NearbyDevicesModel::deviceCreated(QString hwaddy, QVariantMap properties)
114 {
115         bool found = false;
116         foreach(QString path, devicepathlist)
117         {
118                 if(path == hwaddy)
119                 {
120                         found=true;
121                         break;
122                 }
123         }
124
125         if(!found)
126         {
127                 beginInsertRows(QModelIndex(), devicepathlist.size()+1, devicepathlist.size()+1);
128                 devicepathlist.append(hwaddy);
129                 deviceAliasMap[hwaddy] = properties["Alias"].toString();
130                 emit nearbyDeviceFound(devicepathlist.indexOf(hwaddy));
131                 endInsertRows();
132         }
133 }
134
135 void NearbyDevicesModel::deviceRemoved(QString hwaddy)
136 {
137         int i=-1;
138         if((i = devicepathlist.indexOf(hwaddy)) >=0)
139         {
140                 beginRemoveRows(QModelIndex(),i,i);
141                 devicepathlist.removeAt(i);
142                 emit nearbyDeviceRemoved(i);
143                 endRemoveRows();
144         }
145
146 }
147
148 void NearbyDevicesModel::adapterAdded(QDBusObjectPath path)
149 {
150         if(adapter && adapter->path() == path.path()) return;
151
152         QDBusObjectPath adapterpath = manager->DefaultAdapter();
153
154         if(adapterpath.path() == "")
155                 return;
156
157         adapter = new OrgBluezAdapterInterface(
158                         "org.bluez",
159                         adapterpath.path(),
160                         QDBusConnection::systemBus(), this);
161
162         connect(adapter,
163                 SIGNAL(DeviceFound(QString, QVariantMap)),
164                 this,
165                 SLOT(deviceCreated(QString, QVariantMap)));
166         connect(adapter,
167                 SIGNAL(DeviceDisappeared(QString)),
168                 this,
169                 SLOT(deviceRemoved(QString)));
170         connect(adapter,
171                         SIGNAL(PropertyChanged(QString,QDBusVariant)),
172                         this,
173                         SLOT(adapterPropertiesChangedSlot(QString,QDBusVariant)));
174 }
175
176 void NearbyDevicesModel::adapterRemoved(QDBusObjectPath)
177 {
178         QDBusObjectPath adapterpath = manager->DefaultAdapter();
179
180         if(adapterpath.path() == "")
181         {
182                 removeAll(true);
183                 if(adapter){ delete adapter; adapter = NULL; }
184                 return;
185         }
186 }
187
188 void NearbyDevicesModel::adapterPropertiesChangedSlot(QString n,QDBusVariant v)
189 {
190         adapterPropertiesChanged(n,v.variant());
191 }