3 * meego handset bluetooth
4 * Copyright © 2010, Intel Corporation.
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
12 #include "nearbydevicesmodel.h"
13 #include "bluetoothbaseagent.h"
15 NearbyDevicesModel::NearbyDevicesModel(QObject *parent) :
16 QAbstractListModel(parent), adapter(NULL), agent(NULL)
18 manager = new OrgBluezManagerInterface(
20 "/", QDBusConnection::systemBus(), this);
22 connect(manager,SIGNAL(AdapterAdded(QDBusObjectPath)),this,SLOT(adapterAdded(QDBusObjectPath)));
23 connect(manager,SIGNAL(AdapterRemoved(QDBusObjectPath)),this,SLOT(adapterRemoved(QDBusObjectPath)));
24 adapterAdded(QDBusObjectPath());
26 QHash<int,QByteArray> roles;
27 roles[NearbyDevicesModelRoles::name]="name";
28 roles[NearbyDevicesModelRoles::address]="address";
33 int NearbyDevicesModel::rowCount(const QModelIndex &parent) const
36 return devicepathlist.size();
39 QVariant NearbyDevicesModel::data(const QModelIndex &index, int role) const
41 if (role == NearbyDevicesModelRoles::name)
44 if(index.row() < devicepathlist.size())
46 rowData = deviceAliasMap[devicepathlist[index.row()]];
48 return QVariant(rowData);
50 else if (role == NearbyDevicesModelRoles::address)
53 if(index.row() < devicepathlist.size())
55 rowData = devicepathlist[index.row()];
57 return QVariant(rowData);
62 void NearbyDevicesModel::pair(QString hwaddy)
65 qDebug()<<"attempting to pair with "<<hwaddy;
67 agent = new AsyncAgent("/temp/agent", this);
69 adapter->CreatePairedDevice(hwaddy,
70 QDBusObjectPath("/temp/agent"),"");
72 //qDebug()<<"new object created: "<<object.path();
75 void NearbyDevicesModel::discover(bool start)
80 adapter->StartDiscovery();
81 else adapter->StopDiscovery();
84 void NearbyDevicesModel::removeAll(bool)
86 for(int i=0;i<devicepathlist.size();i++)
88 devicepathlist.removeAt(i);
92 void NearbyDevicesModel::replyRequestConfirmation(bool confirmed)
94 qDebug()<<"reply to RequestConfirmation:"<<confirmed;
95 if(agent) agent->replyRequestConfirmation(confirmed);
98 void NearbyDevicesModel::replyPasskey(uint passkey)
100 if(agent) agent->replyPasskey(passkey);
103 void NearbyDevicesModel::replyRequestPidCode(QString pidCode)
105 if(agent) agent->replyRequestPidCode(pidCode);
108 void NearbyDevicesModel::setAdapterProperty(QString name, QVariant value)
110 if(adapter) adapter->setProperty(name.toAscii().data(),value);
113 void NearbyDevicesModel::deviceCreated(QString hwaddy, QVariantMap properties)
116 foreach(QString path, devicepathlist)
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));
135 void NearbyDevicesModel::deviceRemoved(QString hwaddy)
138 if((i = devicepathlist.indexOf(hwaddy)) >=0)
140 beginRemoveRows(QModelIndex(),i,i);
141 devicepathlist.removeAt(i);
142 emit nearbyDeviceRemoved(i);
148 void NearbyDevicesModel::adapterAdded(QDBusObjectPath path)
150 if(adapter && adapter->path() == path.path()) return;
152 QDBusObjectPath adapterpath = manager->DefaultAdapter();
154 if(adapterpath.path() == "")
157 adapter = new OrgBluezAdapterInterface(
160 QDBusConnection::systemBus(), this);
163 SIGNAL(DeviceFound(QString, QVariantMap)),
165 SLOT(deviceCreated(QString, QVariantMap)));
167 SIGNAL(DeviceDisappeared(QString)),
169 SLOT(deviceRemoved(QString)));
171 SIGNAL(PropertyChanged(QString,QDBusVariant)),
173 SLOT(adapterPropertiesChangedSlot(QString,QDBusVariant)));
176 void NearbyDevicesModel::adapterRemoved(QDBusObjectPath)
178 QDBusObjectPath adapterpath = manager->DefaultAdapter();
180 if(adapterpath.path() == "")
183 if(adapter){ delete adapter; adapter = NULL; }
188 void NearbyDevicesModel::adapterPropertiesChangedSlot(QString n,QDBusVariant v)
190 adapterPropertiesChanged(n,v.variant());