1 #include "bluetoothdevicemodel.h"
3 BluetoothDevicesModel::BluetoothDevicesModel(QObject *parent) :
4 QAbstractListModel(parent), adapter(NULL)
6 manager = new OrgBluezManagerInterface(
8 "/", QDBusConnection::systemBus(), this);
10 connect(manager,SIGNAL(AdapterAdded(QDBusObjectPath)),this,SLOT(adapterAdded(QDBusObjectPath)));
11 connect(manager,SIGNAL(AdapterRemoved(QDBusObjectPath)),this,SLOT(adapterRemoved(QDBusObjectPath)));
12 adapterAdded(QDBusObjectPath());
14 QHash<int, QByteArray> roles;
16 roles[address]="address";
22 int BluetoothDevicesModel::rowCount(const QModelIndex &) const
24 return m_devices.size();
27 QVariant BluetoothDevicesModel::data(const QModelIndex &index, int role) const
32 if(index.row() < m_devices.size())
34 rowData = m_devices[index.row()]->name();
36 return QVariant(rowData);
38 else if(role == address)
41 if(index.row() < m_devices.size())
43 rowData = m_devices[index.row()]->address();
45 return QVariant(rowData);
50 if(index.row() < m_devices.size())
52 rowData = m_devices[index.row()]->path();
54 return QVariant(rowData);
59 QString BluetoothDevicesModel::devicePath(QString devicename)
61 foreach(BluetoothDevice* device, m_devices)
63 if(device->name() == devicename)
64 return device->path();
69 BluetoothDevice* BluetoothDevicesModel::device(QString path)
71 foreach(BluetoothDevice* device, m_devices)
73 if(device->path() == path)
78 void BluetoothDevicesModel::adapterAdded(QDBusObjectPath path)
80 if(adapter && adapter->path() == path.path()) return;
82 QDBusObjectPath adapterpath = manager->DefaultAdapter();
84 if(adapterpath.path() == "")
87 adapter = new OrgBluezAdapterInterface(
90 QDBusConnection::systemBus(), this);
93 SIGNAL(DeviceRemoved(QDBusObjectPath)),
95 SLOT(deviceRemoved(QDBusObjectPath)));
98 SIGNAL(DeviceCreated(QDBusObjectPath)),
100 SLOT(deviceCreated(QDBusObjectPath)));
102 QList<QDBusObjectPath> list = adapter->ListDevices();
103 foreach(QDBusObjectPath item, list)
105 m_devices.append(new BluetoothDevice(item));
109 void BluetoothDevicesModel::adapterRemoved(QDBusObjectPath)
111 QDBusObjectPath adapterpath = manager->DefaultAdapter();
113 if(adapterpath.path() == "")
115 beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
116 foreach(BluetoothDevice* device, m_devices)
123 if(adapter) delete adapter;
128 void BluetoothDevicesModel::deviceCreated(QDBusObjectPath devicepath)
131 beginInsertRows(QModelIndex(),m_devices.size()+1,m_devices.size()+1);
132 m_devices.append(new BluetoothDevice(devicepath));
136 void BluetoothDevicesModel::deviceRemoved(QDBusObjectPath devicepath)
138 for(int i=0; i<m_devices.size(); i++)
141 if(m_devices[i]->path() == devicepath.path())
143 beginRemoveRows(QModelIndex(), i, i);
145 m_devices.removeAt(i);