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
43 if(index.row() < m_devices.size())
45 rowData = m_devices[index.row()]->name();
47 return QVariant(rowData);
49 else if(role == address)
52 if(index.row() < m_devices.size())
54 rowData = m_devices[index.row()]->address();
56 return QVariant(rowData);
61 if(index.row() < m_devices.size())
63 rowData = m_devices[index.row()]->path();
65 return QVariant(rowData);
70 QString BluetoothDevicesModel::devicePath(QString devicename)
72 foreach(BluetoothDevice* device, m_devices)
74 if(device->name() == devicename)
75 return device->path();
80 BluetoothDevice* BluetoothDevicesModel::device(QString path)
82 foreach(BluetoothDevice* device, m_devices)
84 if(device->path() == path)
90 void BluetoothDevicesModel::adapterAdded(QDBusObjectPath path)
92 if(adapter && adapter->path() == path.path()) return;
94 QDBusObjectPath adapterpath = manager->DefaultAdapter();
96 if(adapterpath.path() == "")
99 adapter = new OrgBluezAdapterInterface(
102 QDBusConnection::systemBus(), this);
105 SIGNAL(DeviceRemoved(QDBusObjectPath)),
107 SLOT(deviceRemoved(QDBusObjectPath)));
110 SIGNAL(DeviceCreated(QDBusObjectPath)),
112 SLOT(deviceCreated(QDBusObjectPath)));
114 QList<QDBusObjectPath> list = adapter->ListDevices();
115 foreach(QDBusObjectPath item, list)
121 void BluetoothDevicesModel::adapterRemoved(QDBusObjectPath)
123 QDBusObjectPath adapterpath = manager->DefaultAdapter();
125 if(adapterpath.path() == "")
127 beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
128 foreach(BluetoothDevice* device, m_devices)
135 if(adapter) delete adapter;
140 void BluetoothDevicesModel::deviceCreated(QDBusObjectPath devicepath)
142 BluetoothDevice* device = new BluetoothDevice(devicepath,this);
144 connect(device,SIGNAL(propertyChanged(QString,QVariant)),this,SLOT(devicePropertyChanged(QString,QVariant)));
146 beginInsertRows(QModelIndex(),m_devices.size()+1,m_devices.size()+1);
147 m_devices.append(device);
151 void BluetoothDevicesModel::deviceRemoved(QDBusObjectPath devicepath)
153 for(int i=0; i<m_devices.size(); i++)
156 if(m_devices[i]->path() == devicepath.path())
158 beginRemoveRows(QModelIndex(), i, i);
159 m_devices[i]->deleteLater();
160 m_devices.removeAt(i);
166 void BluetoothDevicesModel::devicePropertyChanged(QString name, QVariant value)
168 if(name == "Paired" && value.toBool() == true)
170 qDebug()<<"device property changed: "<<name<<" "<<value;
171 BluetoothDevice* device = qobject_cast<BluetoothDevice*>(sender());
172 emit devicePaired(device);