initial Bluetooth Device Model addition
[profile/ivi/bluetooth-qt.git] / bluetoothdevicemodel.cpp
1 #include "bluetoothdevicemodel.h"
2
3 BluetoothDeviceModel::BluetoothDeviceModel(QObject *parent) :
4         QAbstractListModel(parent), adapter(NULL)
5 {
6         manager = new OrgBluezManagerInterface(
7                         "org.bluez",
8                         "/", QDBusConnection::systemBus(), this);
9
10         connect(manager,SIGNAL(AdapterAdded(QDBusObjectPath)),this,SLOT(adapterAdded(QDBusObjectPath)));
11         connect(manager,SIGNAL(AdapterRemoved(QDBusObjectPath)),this,SLOT(adapterRemoved(QDBusObjectPath)));
12         adapterAdded(QDBusObjectPath());
13
14         QHash<int, QByteArray> roles;
15         roles[name]="name";
16         roles[address]="address";
17         roles[path]="path";
18
19         setRoleNames(roles);
20 }
21
22 int BluetoothDeviceModel::rowCount(const QModelIndex &parent) const
23 {
24         return m_devices.size();
25 }
26
27 QVariant BluetoothDeviceModel::data(const QModelIndex &index, int role) const
28 {
29         if(role == name)
30         {
31                 QString rowData;
32                 if(index.row() < m_devices.size())
33                 {
34                         rowData = m_devices[index.row()]->name();
35                 }
36                 return QVariant(rowData);
37         }
38         else if(role == address)
39         {
40                 QString rowData;
41                 if(index.row() < m_devices.size())
42                 {
43                         rowData = m_devices[index.row()]->address();
44                 }
45                 return QVariant(rowData);
46         }
47         else if(role == path)
48         {
49                 QString rowData;
50                 if(index.row() < m_devices.size())
51                 {
52                         rowData = m_devices[index.row()]->path();
53                 }
54                 return QVariant(rowData);
55         }
56         return QVariant();
57 }
58
59 void BluetoothDeviceModel::adapterAdded(QDBusObjectPath path)
60 {
61         if(adapter && adapter->path() == path.path()) return;
62
63         QDBusObjectPath adapterpath = manager->DefaultAdapter();
64
65         if(adapterpath.path() == "")
66                 return;
67
68         adapter = new OrgBluezAdapterInterface(
69                         "org.bluez",
70                         adapterpath.path(),
71                         QDBusConnection::systemBus(), this);
72
73         connect(adapter,
74                 SIGNAL(DeviceRemoved(QDBusObjectPath)),
75                 this,
76                 SIGNAL(deviceRemoved(QDBusObjectPath)));
77
78         connect(adapter,
79                 SIGNAL(DeviceCreated(QDBusObjectPath)),
80                 this,
81                 SIGNAL(deviceCreated(QDBusObjectPath)));
82
83         QList<QDBusObjectPath> list = adapter->ListDevices();
84         foreach(QDBusObjectPath item, list)
85         {
86                 m_devices.append(new BluetoothDevice(item));
87         }
88 }
89
90 void BluetoothDeviceModel::adapterRemoved(QDBusObjectPath)
91 {
92         QDBusObjectPath adapterpath = manager->DefaultAdapter();
93
94         if(adapterpath.path() == "")
95         {
96                 beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
97                 foreach(BluetoothDevice* device, m_devices)
98                 {
99                         delete device;
100                 }
101                 m_devices.clear();
102                 endRemoveRows();
103
104                 if(adapter) delete adapter;
105                 return;
106         }
107 }
108
109 void BluetoothDeviceModel::deviceCreated(QDBusObjectPath devicepath)
110 {
111
112         beginInsertRows(QModelIndex(),m_devices.size()+1,m_devices.size()+1);
113         m_devices.append(new BluetoothDevice(devicepath));
114         endInsertRows();
115 }
116
117 void BluetoothDeviceModel::deviceRemoved(QDBusObjectPath devicepath)
118 {
119         for(int i=0; i<m_devices.size(); i++)
120         {
121
122                 if(m_devices[i]->path() == devicepath.path())
123                 {
124                         beginRemoveRows(QModelIndex(), i, i);
125                         delete m_devices[i];
126                         m_devices.removeAt(i);
127                         endRemoveRows();
128                 }
129         }
130 }