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), 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::makeDiscoverable(bool discoverableValue)
89 if(adapter) adapter->SetProperty("Discoverable", QDBusVariant(discoverableValue));
92 bool BluetoothDevicesModel::discoverable()
96 QVariantMap props = adapter->GetProperties();
97 return props["Discoverable"].toBool();
103 int BluetoothDevicesModel::discoverableTimeout()
107 QVariantMap props = adapter->GetProperties();
108 return props["DiscoverableTimeout"].toInt();
114 void BluetoothDevicesModel::setDiscoverableTimeout(int timeout)
118 bool success = adapter->setProperty("DiscoverableTimeout", timeout);
119 qDebug()<<"Setting discoverable timeout to "<<timeout<<": "<<success;
123 void BluetoothDevicesModel::adapterAdded(QDBusObjectPath path)
125 if(adapter && adapter->path() == path.path()) return;
127 QDBusObjectPath adapterpath = manager->DefaultAdapter();
129 if(adapterpath.path() == "")
131 ///we actually shouldn't ever get here.
135 adapter = new OrgBluezAdapterInterface(
138 QDBusConnection::systemBus(), this);
141 SIGNAL(PropertyChanged(QString,QDBusVariant)),
143 SLOT(adapterPropertyChanged(QString,QDBusVariant)));
146 SIGNAL(DeviceRemoved(QDBusObjectPath)),
148 SLOT(deviceRemoved(QDBusObjectPath)));
151 SIGNAL(DeviceCreated(QDBusObjectPath)),
153 SLOT(deviceCreated(QDBusObjectPath)));
155 adapterChanged(true);
157 QList<QDBusObjectPath> list = adapter->ListDevices();
158 foreach(QDBusObjectPath item, list)
164 void BluetoothDevicesModel::adapterRemoved(QDBusObjectPath)
166 QDBusObjectPath adapterpath = manager->DefaultAdapter();
168 if(adapterpath.path() == "")
170 beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
171 foreach(BluetoothDevice* device, m_devices)
179 if(adapter) delete adapter;
181 adapterChanged(false);
186 void BluetoothDevicesModel::deviceCreated(QDBusObjectPath devicepath)
188 BluetoothDevice* device = new BluetoothDevice(devicepath,this);
190 connect(device,SIGNAL(propertyChanged(QString,QVariant)),this,SLOT(devicePropertyChanged(QString,QVariant)));
192 beginInsertRows(QModelIndex(),m_devices.size(),m_devices.size());
193 m_devices.append(device);
197 void BluetoothDevicesModel::deviceRemoved(QDBusObjectPath devicepath)
199 for(int i=0; i<m_devices.size(); i++)
202 if(m_devices[i]->path() == devicepath.path())
204 beginRemoveRows(QModelIndex(), i, i);
205 m_devices[i]->deleteLater();
206 m_devices.removeAt(i);
212 void BluetoothDevicesModel::devicePropertyChanged(QString name, QVariant value)
214 BluetoothDevice* device = qobject_cast<BluetoothDevice*>(sender());
216 qDebug()<<"device property changed for "<<device->address()<<": "<<name<<" "<<value;
218 if(name == "Paired" && value.toBool() == true)
220 emit devicePaired(device);
223 int row = m_devices.indexOf(device);
224 if(row == -1) return; ///device doesn't exist.
226 dataChanged(createIndex(row, 0),createIndex(row, 0));
230 void BluetoothDevicesModel::adapterPropertyChanged(QString name, QDBusVariant value)
232 qDebug()<<"adapter property changed: "<<name<<" "<<value.variant();
234 if(name == "Discoverable")
236 discoverableChanged(value.variant().toBool());
238 else if(name == "DiscoverableTimeout")
240 discoverableTimeoutChanged(value.variant().toInt());