1 #include "bluetoothdevicemodel.h"
3 BluetoothDeviceModel::BluetoothDeviceModel(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 BluetoothDeviceModel::rowCount(const QModelIndex &parent) const
24 return m_devices.size();
27 QVariant BluetoothDeviceModel::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 void BluetoothDeviceModel::adapterAdded(QDBusObjectPath path)
61 if(adapter && adapter->path() == path.path()) return;
63 QDBusObjectPath adapterpath = manager->DefaultAdapter();
65 if(adapterpath.path() == "")
68 adapter = new OrgBluezAdapterInterface(
71 QDBusConnection::systemBus(), this);
74 SIGNAL(DeviceRemoved(QDBusObjectPath)),
76 SIGNAL(deviceRemoved(QDBusObjectPath)));
79 SIGNAL(DeviceCreated(QDBusObjectPath)),
81 SIGNAL(deviceCreated(QDBusObjectPath)));
83 QList<QDBusObjectPath> list = adapter->ListDevices();
84 foreach(QDBusObjectPath item, list)
86 m_devices.append(new BluetoothDevice(item));
90 void BluetoothDeviceModel::adapterRemoved(QDBusObjectPath)
92 QDBusObjectPath adapterpath = manager->DefaultAdapter();
94 if(adapterpath.path() == "")
96 beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
97 foreach(BluetoothDevice* device, m_devices)
104 if(adapter) delete adapter;
109 void BluetoothDeviceModel::deviceCreated(QDBusObjectPath devicepath)
112 beginInsertRows(QModelIndex(),m_devices.size()+1,m_devices.size()+1);
113 m_devices.append(new BluetoothDevice(devicepath));
117 void BluetoothDeviceModel::deviceRemoved(QDBusObjectPath devicepath)
119 for(int i=0; i<m_devices.size(); i++)
122 if(m_devices[i]->path() == devicepath.path())
124 beginRemoveRows(QModelIndex(), i, i);
126 m_devices.removeAt(i);