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 "bluetoothdevicemodel.h"
14 BluetoothDevicesModel::BluetoothDevicesModel(QObject *parent) :
15 QAbstractListModel(parent), m_connected(false), adapter(NULL)
17 manager = new OrgBluezManagerInterface(
19 "/", QDBusConnection::systemBus(), this);
21 connect(manager,SIGNAL(AdapterAdded(QDBusObjectPath)),this,SLOT(adapterAdded(QDBusObjectPath)));
22 connect(manager,SIGNAL(AdapterRemoved(QDBusObjectPath)),this,SLOT(adapterRemoved(QDBusObjectPath)));
23 adapterAdded(QDBusObjectPath());
25 QHash<int, QByteArray> roles;
27 QMetaObject properties = BluetoothDevice::staticMetaObject;
29 for(int i=0; i<properties.propertyCount();i++)
31 roles[i]=properties.property(i).name();
37 int BluetoothDevicesModel::rowCount(const QModelIndex &) const
39 return m_devices.size();
42 QVariant BluetoothDevicesModel::data(const QModelIndex &index, int role) const
44 qDebug()<<"requested role: "<<roleNames()[role];
46 if(!index.isValid() || index.row() < 0)
48 qDebug()<<"index is not valid: "<<index.row()<<","<<index.column();
49 return QVariant(); ///this is retarded but it has to be done.
52 QString roleName = roleNames()[role];
53 QMetaObject object = BluetoothDevice::staticMetaObject;
55 for(int i=0; i<object.propertyCount(); i++)
57 if(object.property(i).name() == roleName)
60 return object.property(i).read(m_devices[index.row()]);
66 QString BluetoothDevicesModel::devicePath(QString devicename)
68 foreach(BluetoothDevice* device, m_devices)
70 if(device->name() == devicename)
71 return device->path();
76 BluetoothDevice* BluetoothDevicesModel::device(QString path)
78 foreach(BluetoothDevice* device, m_devices)
80 if(device->path() == path)
83 qDebug()<<"Device not found for path: "<<path;
87 void BluetoothDevicesModel::makePowered(bool poweredValue)
89 if(adapter) adapter->SetProperty("Powered", QDBusVariant(poweredValue));
92 bool BluetoothDevicesModel::powered()
96 QVariantMap props = adapter->GetProperties();
97 return props["Powered"].toBool();
103 void BluetoothDevicesModel::makeDiscoverable(bool discoverableValue)
105 if(adapter) adapter->SetProperty("Discoverable", QDBusVariant(discoverableValue));
108 bool BluetoothDevicesModel::discoverable()
112 QVariantMap props = adapter->GetProperties();
113 return props["Discoverable"].toBool();
119 int BluetoothDevicesModel::discoverableTimeout()
123 QVariantMap props = adapter->GetProperties();
124 return props["DiscoverableTimeout"].toInt();
130 void BluetoothDevicesModel::setDiscoverableTimeout(int timeout)
134 bool success = adapter->setProperty("DiscoverableTimeout", timeout);
135 qDebug()<<"Setting discoverable timeout to "<<timeout<<": "<<success;
139 void BluetoothDevicesModel::adapterAdded(QDBusObjectPath path)
141 if(adapter && adapter->path() == path.path()) return;
143 QDBusObjectPath adapterpath = manager->DefaultAdapter();
145 if(adapterpath.path() == "")
147 ///we actually shouldn't ever get here.
151 adapter = new OrgBluezAdapterInterface(
154 QDBusConnection::systemBus(), this);
157 SIGNAL(PropertyChanged(QString,QDBusVariant)),
159 SLOT(adapterPropertyChanged(QString,QDBusVariant)));
162 SIGNAL(DeviceRemoved(QDBusObjectPath)),
164 SLOT(deviceRemoved(QDBusObjectPath)));
167 SIGNAL(DeviceCreated(QDBusObjectPath)),
169 SLOT(deviceCreated(QDBusObjectPath)));
171 adapterChanged(true);
173 QList<QDBusObjectPath> list = adapter->ListDevices();
174 foreach(QDBusObjectPath item, list)
180 void BluetoothDevicesModel::adapterRemoved(QDBusObjectPath)
182 QDBusObjectPath adapterpath = manager->DefaultAdapter();
184 if(adapterpath.path() == "")
186 beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
187 foreach(BluetoothDevice* device, m_devices)
189 updateConnected(device->connected());
190 device->deleteLater();
195 if(adapter) delete adapter;
197 adapterChanged(false);
202 void BluetoothDevicesModel::deviceCreated(QDBusObjectPath devicepath)
204 BluetoothDevice* device = new BluetoothDevice(devicepath,this);
206 updateConnected(device->connected());
207 connect(device,SIGNAL(propertyChanged(QString,QVariant)),this,SLOT(devicePropertyChanged(QString,QVariant)));
209 beginInsertRows(QModelIndex(),m_devices.size(),m_devices.size());
210 m_devices.append(device);
214 void BluetoothDevicesModel::deviceRemoved(QDBusObjectPath devicepath)
216 for(int i=0; i<m_devices.size(); i++)
219 if(m_devices[i]->path() == devicepath.path())
221 beginRemoveRows(QModelIndex(), i, i);
222 m_devices[i]->deleteLater();
223 m_devices.removeAt(i);
229 void BluetoothDevicesModel::devicePropertyChanged(QString name, QVariant value)
231 BluetoothDevice* device = qobject_cast<BluetoothDevice*>(sender());
233 qDebug()<<"device property changed for "<<device->address()<<": "<<name<<" "<<value;
235 if(name == "Paired" && value.toBool() == true)
237 emit devicePaired(device);
240 else if(name == "Connected")
242 updateConnected(value.toBool());
244 int row = m_devices.indexOf(device);
245 if(row == -1) return; ///device doesn't exist.
247 dataChanged(createIndex(row, 0),createIndex(row, 0));
250 void BluetoothDevicesModel::adapterPropertyChanged(QString name, QDBusVariant value)
252 qDebug()<<"adapter property changed: "<<name<<" "<<value.variant();
254 if(name == "Powered")
256 poweredChanged(value.variant().toBool());
258 else if(name == "Discoverable")
260 discoverableChanged(value.variant().toBool());
262 else if(name == "DiscoverableTimeout")
264 discoverableTimeoutChanged(value.variant().toInt());
268 void BluetoothDevicesModel::updateConnected(bool deviceconnectedStatus)
270 if(deviceconnectedStatus)
275 connectedChanged(m_connected);
281 foreach(BluetoothDevice* device, devices())
283 temp |= device->connected();
286 if(temp != m_connected)
289 connectedChanged(m_connected);