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 roles[address]="address";
33 int BluetoothDevicesModel::rowCount(const QModelIndex &) const
35 return m_devices.size();
38 QVariant BluetoothDevicesModel::data(const QModelIndex &index, int role) const
40 qDebug()<<"requested role: "<<roleNames()[role];
42 if(!index.isValid() || index.row() < 0)
44 qDebug()<<"index is not valid: "<<index.row()<<","<<index.column();
45 return QVariant(); ///this is retarded but it has to be done.
51 if(index.row() < m_devices.size())
53 rowData = m_devices[index.row()]->name();
55 return QVariant(rowData);
57 else if(role == address)
60 if(index.row() < m_devices.size())
62 rowData = m_devices[index.row()]->address();
64 return QVariant(rowData);
69 if(index.row() < m_devices.size())
71 rowData = m_devices[index.row()]->path();
73 return QVariant(rowData);
78 QString BluetoothDevicesModel::devicePath(QString devicename)
80 foreach(BluetoothDevice* device, m_devices)
82 if(device->name() == devicename)
83 return device->path();
88 BluetoothDevice* BluetoothDevicesModel::device(QString path)
90 foreach(BluetoothDevice* device, m_devices)
92 if(device->path() == path)
95 qDebug()<<"Device not found for path: "<<path;
99 void BluetoothDevicesModel::makeDiscoverable(bool discoverableValue)
101 if(adapter) adapter->SetProperty("Discoverable", QDBusVariant(discoverableValue));
104 bool BluetoothDevicesModel::discoverable()
108 QVariantMap props = adapter->GetProperties();
109 return props["Discoverable"].toBool();
115 void BluetoothDevicesModel::adapterAdded(QDBusObjectPath path)
117 if(adapter && adapter->path() == path.path()) return;
119 QDBusObjectPath adapterpath = manager->DefaultAdapter();
121 if(adapterpath.path() == "")
123 ///we actually shouldn't ever get here.
127 adapter = new OrgBluezAdapterInterface(
130 QDBusConnection::systemBus(), this);
133 SIGNAL(PropertyChanged(QString,QDBusVariant)),
135 SLOT(adapterPropertyChanged(QString,QDBusVariant)));
138 SIGNAL(DeviceRemoved(QDBusObjectPath)),
140 SLOT(deviceRemoved(QDBusObjectPath)));
143 SIGNAL(DeviceCreated(QDBusObjectPath)),
145 SLOT(deviceCreated(QDBusObjectPath)));
147 adapterChanged(true);
149 QList<QDBusObjectPath> list = adapter->ListDevices();
150 foreach(QDBusObjectPath item, list)
156 void BluetoothDevicesModel::adapterRemoved(QDBusObjectPath)
158 QDBusObjectPath adapterpath = manager->DefaultAdapter();
160 if(adapterpath.path() == "")
162 beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
163 foreach(BluetoothDevice* device, m_devices)
171 if(adapter) delete adapter;
173 adapterChanged(false);
178 void BluetoothDevicesModel::deviceCreated(QDBusObjectPath devicepath)
180 BluetoothDevice* device = new BluetoothDevice(devicepath,this);
182 connect(device,SIGNAL(propertyChanged(QString,QVariant)),this,SLOT(devicePropertyChanged(QString,QVariant)));
184 beginInsertRows(QModelIndex(),m_devices.size(),m_devices.size());
185 m_devices.append(device);
189 void BluetoothDevicesModel::deviceRemoved(QDBusObjectPath devicepath)
191 for(int i=0; i<m_devices.size(); i++)
194 if(m_devices[i]->path() == devicepath.path())
196 beginRemoveRows(QModelIndex(), i, i);
197 m_devices[i]->deleteLater();
198 m_devices.removeAt(i);
204 void BluetoothDevicesModel::devicePropertyChanged(QString name, QVariant value)
206 BluetoothDevice* device = qobject_cast<BluetoothDevice*>(sender());
208 qDebug()<<"device property changed for "<<device->address()<<": "<<name<<" "<<value;
210 if(name == "Paired" && value.toBool() == true)
212 emit devicePaired(device);
215 int row = m_devices.indexOf(device);
216 if(row == -1) return; ///device doesn't exist.
218 dataChanged(createIndex(row, 0),createIndex(row, 0));
222 void BluetoothDevicesModel::adapterPropertyChanged(QString name, QDBusVariant value)
224 qDebug()<<"adapter property changed: "<<name<<" "<<value.variant();
226 if(name == "Discoverable")
228 discoverableChanged(value.variant().toBool());