Fixed project. version bump
[profile/ivi/bluetooth-qt.git] / bluetoothdevicemodel.cpp
1 #include "bluetoothdevicemodel.h"
2
3 BluetoothDevicesModel::BluetoothDevicesModel(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 BluetoothDevicesModel::rowCount(const QModelIndex &) const
23 {
24         return m_devices.size();
25 }
26
27 QVariant BluetoothDevicesModel::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 QString BluetoothDevicesModel::devicePath(QString devicename)
60 {
61         foreach(BluetoothDevice* device, m_devices)
62         {
63                 if(device->name() == devicename)
64                         return device->path();
65         }
66         return "";
67 }
68
69 BluetoothDevice* BluetoothDevicesModel::device(QString path)
70 {
71         foreach(BluetoothDevice* device, m_devices)
72         {
73                 if(device->path() == path)
74                         return device;
75         }
76 }
77
78 void BluetoothDevicesModel::adapterAdded(QDBusObjectPath path)
79 {
80         if(adapter && adapter->path() == path.path()) return;
81
82         QDBusObjectPath adapterpath = manager->DefaultAdapter();
83
84         if(adapterpath.path() == "")
85                 return;
86
87         adapter = new OrgBluezAdapterInterface(
88                         "org.bluez",
89                         adapterpath.path(),
90                         QDBusConnection::systemBus(), this);
91
92         connect(adapter,
93                 SIGNAL(DeviceRemoved(QDBusObjectPath)),
94                 this,
95                 SLOT(deviceRemoved(QDBusObjectPath)));
96
97         connect(adapter,
98                 SIGNAL(DeviceCreated(QDBusObjectPath)),
99                 this,
100                 SLOT(deviceCreated(QDBusObjectPath)));
101
102         QList<QDBusObjectPath> list = adapter->ListDevices();
103         foreach(QDBusObjectPath item, list)
104         {
105                 m_devices.append(new BluetoothDevice(item));
106         }
107 }
108
109 void BluetoothDevicesModel::adapterRemoved(QDBusObjectPath)
110 {
111         QDBusObjectPath adapterpath = manager->DefaultAdapter();
112
113         if(adapterpath.path() == "")
114         {
115                 beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
116                 foreach(BluetoothDevice* device, m_devices)
117                 {
118                         delete device;
119                 }
120                 m_devices.clear();
121                 endRemoveRows();
122
123                 if(adapter) delete adapter;
124                 return;
125         }
126 }
127
128 void BluetoothDevicesModel::deviceCreated(QDBusObjectPath devicepath)
129 {
130
131         beginInsertRows(QModelIndex(),m_devices.size()+1,m_devices.size()+1);
132         m_devices.append(new BluetoothDevice(devicepath));
133         endInsertRows();
134 }
135
136 void BluetoothDevicesModel::deviceRemoved(QDBusObjectPath devicepath)
137 {
138         for(int i=0; i<m_devices.size(); i++)
139         {
140
141                 if(m_devices[i]->path() == devicepath.path())
142                 {
143                         beginRemoveRows(QModelIndex(), i, i);
144                         delete m_devices[i];
145                         m_devices.removeAt(i);
146                         endRemoveRows();
147                 }
148         }
149 }