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 void BluetoothDevicesModel::adapterAdded(QDBusObjectPath path)
105 if(adapter && adapter->path() == path.path()) return;
107 QDBusObjectPath adapterpath = manager->DefaultAdapter();
109 if(adapterpath.path() == "")
111 ///we actually shouldn't ever get here.
115 adapter = new OrgBluezAdapterInterface(
118 QDBusConnection::systemBus(), this);
121 SIGNAL(PropertyChanged(QString,QDBusVariant)),
123 SLOT(adapterPropertyChanged(QString,QDBusVariant)));
126 SIGNAL(DeviceRemoved(QDBusObjectPath)),
128 SLOT(deviceRemoved(QDBusObjectPath)));
131 SIGNAL(DeviceCreated(QDBusObjectPath)),
133 SLOT(deviceCreated(QDBusObjectPath)));
135 adapterChanged(true);
137 QList<QDBusObjectPath> list = adapter->ListDevices();
138 foreach(QDBusObjectPath item, list)
144 void BluetoothDevicesModel::adapterRemoved(QDBusObjectPath)
146 QDBusObjectPath adapterpath = manager->DefaultAdapter();
148 if(adapterpath.path() == "")
150 beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
151 foreach(BluetoothDevice* device, m_devices)
159 if(adapter) delete adapter;
161 adapterChanged(false);
166 void BluetoothDevicesModel::deviceCreated(QDBusObjectPath devicepath)
168 BluetoothDevice* device = new BluetoothDevice(devicepath,this);
170 connect(device,SIGNAL(propertyChanged(QString,QVariant)),this,SLOT(devicePropertyChanged(QString,QVariant)));
172 beginInsertRows(QModelIndex(),m_devices.size(),m_devices.size());
173 m_devices.append(device);
177 void BluetoothDevicesModel::deviceRemoved(QDBusObjectPath devicepath)
179 for(int i=0; i<m_devices.size(); i++)
182 if(m_devices[i]->path() == devicepath.path())
184 beginRemoveRows(QModelIndex(), i, i);
185 m_devices[i]->deleteLater();
186 m_devices.removeAt(i);
192 void BluetoothDevicesModel::devicePropertyChanged(QString name, QVariant value)
194 BluetoothDevice* device = qobject_cast<BluetoothDevice*>(sender());
196 qDebug()<<"device property changed for "<<device->address()<<": "<<name<<" "<<value;
198 if(name == "Paired" && value.toBool() == true)
200 emit devicePaired(device);
203 int row = m_devices.indexOf(device);
204 if(row == -1) return; ///device doesn't exist.
206 dataChanged(createIndex(row, 0),createIndex(row, 0));
210 void BluetoothDevicesModel::adapterPropertyChanged(QString name, QDBusVariant value)
212 qDebug()<<"adapter property changed: "<<name<<" "<<value.variant();
214 if(name == "Discoverable")
216 discoverableChanged(value.variant().toBool());