initial Bluetooth Device Model addition
[profile/ivi/bluetooth-qt.git] / nearbydevicesmodel.cpp
1 #include "nearbydevicesmodel.h"
2 #include "bluetoothbaseagent.h"
3
4 NearbyDevicesModel::NearbyDevicesModel(QObject *parent) :
5         QAbstractListModel(parent), adapter(NULL), agent(NULL)
6 {
7         manager = new OrgBluezManagerInterface(
8                         "org.bluez",
9                         "/", QDBusConnection::systemBus(), this);
10
11         connect(manager,SIGNAL(AdapterAdded(QDBusObjectPath)),this,SLOT(adapterAdded(QDBusObjectPath)));
12         connect(manager,SIGNAL(AdapterRemoved(QDBusObjectPath)),this,SLOT(adapterRemoved(QDBusObjectPath)));
13         adapterAdded(QDBusObjectPath());
14
15         QHash<int,QByteArray> roles;
16         roles[Qt::DisplayRole]="name";
17
18         setRoleNames(roles);
19 }
20
21
22 int NearbyDevicesModel::rowCount(const QModelIndex &parent) const
23 {
24         Q_UNUSED(parent);
25         return devicepathlist.size();
26 }
27
28 QVariant NearbyDevicesModel::data(const QModelIndex &index, int role) const
29 {
30         if (role == Qt::DisplayRole)
31         {
32                 QString rowData;
33                 if(index.row() < devicepathlist.size())
34                 {
35                         rowData = deviceAliasMap[devicepathlist[index.row()]];
36                 }
37                 return QVariant(rowData);
38         }
39
40         return QVariant();
41 }
42
43 void NearbyDevicesModel::pair(QString hwaddy)
44 {
45
46         qDebug()<<"attempting to pair with "<<hwaddy;
47         if(!adapter) return;
48         agent = new AsyncAgent("/temp/agent",this);
49
50         adapter->CreatePairedDevice(hwaddy,
51                                    QDBusObjectPath("/temp/agent"),"");
52
53         //qDebug()<<"new object created: "<<object.path();
54 }
55
56 void NearbyDevicesModel::discover(bool start)
57 {
58         if(start)
59                 adapter->StartDiscovery();
60         else adapter->StopDiscovery();
61 }
62
63 void NearbyDevicesModel::removeAll(bool)
64 {
65         for(int i=0;i<devicepathlist.size();i++)
66         {
67                 devicepathlist.removeAt(i);
68         }
69 }
70
71 void NearbyDevicesModel::replyRequestConfirmation(bool confirmed)
72 {
73         if(agent) agent->replyRequestConfirmation(confirmed);
74 }
75
76 void NearbyDevicesModel::replyPasskey(uint passkey)
77 {
78         if(agent) agent->replyPasskey(passkey);
79 }
80
81 void NearbyDevicesModel::replyRequestPidCode(QString pidCode)
82 {
83         if(agent) agent->replyRequestPidCode(pidCode);
84 }
85
86 void NearbyDevicesModel::deviceCreated(QString hwaddy, QVariantMap properties)
87 {
88         bool found = false;
89         foreach(QString path, devicepathlist)
90         {
91                 if(path == hwaddy)
92                 {
93                         found=true;
94                         break;
95                 }
96         }
97
98         if(!found)
99         {
100                 beginInsertRows(QModelIndex(), devicepathlist.size()+1, devicepathlist.size()+1);
101                 devicepathlist.append(hwaddy);
102                 deviceAliasMap[hwaddy] = properties["Alias"].toString();
103                 emit nearbyDeviceFound(devicepathlist.indexOf(hwaddy));
104                 endInsertRows();
105         }
106 }
107
108 void NearbyDevicesModel::deviceRemoved(QString hwaddy)
109 {
110         int i=-1;
111         if((i = devicepathlist.indexOf(hwaddy)) >=0)
112         {
113                 beginRemoveRows(QModelIndex(),i,i);
114                 devicepathlist.removeAt(i);
115                 emit nearbyDeviceRemoved(i);
116                 endRemoveRows();
117         }
118
119 }
120
121 void NearbyDevicesModel::adapterAdded(QDBusObjectPath path)
122 {
123         if(adapter && adapter->path() == path.path()) return;
124
125         QDBusObjectPath adapterpath = manager->DefaultAdapter();
126
127         if(adapterpath.path() == "")
128                 return;
129
130         adapter = new OrgBluezAdapterInterface(
131                         "org.bluez",
132                         adapterpath.path(),
133                         QDBusConnection::systemBus(), this);
134
135         connect(adapter,
136                 SIGNAL(DeviceFound(QString, QVariantMap)),
137                 this,
138                 SLOT(deviceCreated(QString, QVariantMap)));
139         connect(adapter,
140                 SIGNAL(DeviceDisappeared(QString)),
141                 this,
142                 SLOT(deviceRemoved(QString)));
143 }
144
145 void NearbyDevicesModel::adapterRemoved(QDBusObjectPath)
146 {
147         QDBusObjectPath adapterpath = manager->DefaultAdapter();
148
149         if(adapterpath.path() == "")
150         {
151                 removeAll(true);
152                 if(adapter) delete adapter;
153                 return;
154         }
155 }