added release signal and fixed bug in ayncagent that was causing crashes
[profile/ivi/bluetooth-qt.git] / bluetoothdevicemodel.cpp
1 /*  -*- Mode: C++ -*-
2  *
3  * meego handset bluetooth
4  * Copyright © 2010, Intel Corporation.
5  *
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
9  *
10  */
11
12 #include "bluetoothdevicemodel.h"
13
14 BluetoothDevicesModel::BluetoothDevicesModel(QObject *parent) :
15         QAbstractListModel(parent), adapter(NULL)
16 {
17         manager = new OrgBluezManagerInterface(
18                         "org.bluez",
19                         "/", QDBusConnection::systemBus(), this);
20
21         connect(manager,SIGNAL(AdapterAdded(QDBusObjectPath)),this,SLOT(adapterAdded(QDBusObjectPath)));
22         connect(manager,SIGNAL(AdapterRemoved(QDBusObjectPath)),this,SLOT(adapterRemoved(QDBusObjectPath)));
23         adapterAdded(QDBusObjectPath());
24
25         QHash<int, QByteArray> roles;
26         roles[name]="name";
27         roles[address]="address";
28         roles[path]="path";
29
30         setRoleNames(roles);
31 }
32
33 int BluetoothDevicesModel::rowCount(const QModelIndex &) const
34 {
35         return m_devices.size();
36 }
37
38 QVariant BluetoothDevicesModel::data(const QModelIndex &index, int role) const
39 {
40         if(role == name)
41         {
42                 QString rowData;
43                 if(index.row() < m_devices.size())
44                 {
45                         rowData = m_devices[index.row()]->name();
46                 }
47                 return QVariant(rowData);
48         }
49         else if(role == address)
50         {
51                 QString rowData;
52                 if(index.row() < m_devices.size())
53                 {
54                         rowData = m_devices[index.row()]->address();
55                 }
56                 return QVariant(rowData);
57         }
58         else if(role == path)
59         {
60                 QString rowData;
61                 if(index.row() < m_devices.size())
62                 {
63                         rowData = m_devices[index.row()]->path();
64                 }
65                 return QVariant(rowData);
66         }
67         return QVariant();
68 }
69
70 QString BluetoothDevicesModel::devicePath(QString devicename)
71 {
72         foreach(BluetoothDevice* device, m_devices)
73         {
74                 if(device->name() == devicename)
75                         return device->path();
76         }
77         return "";
78 }
79
80 BluetoothDevice* BluetoothDevicesModel::device(QString path)
81 {
82         foreach(BluetoothDevice* device, m_devices)
83         {
84                 if(device->path() == path)
85                         return device;
86         }
87         return NULL;
88 }
89
90 void BluetoothDevicesModel::adapterAdded(QDBusObjectPath path)
91 {
92         if(adapter && adapter->path() == path.path()) return;
93
94         QDBusObjectPath adapterpath = manager->DefaultAdapter();
95
96         if(adapterpath.path() == "")
97                 return;
98
99         adapter = new OrgBluezAdapterInterface(
100                         "org.bluez",
101                         adapterpath.path(),
102                         QDBusConnection::systemBus(), this);
103
104         connect(adapter,
105                 SIGNAL(DeviceRemoved(QDBusObjectPath)),
106                 this,
107                 SLOT(deviceRemoved(QDBusObjectPath)));
108
109         connect(adapter,
110                 SIGNAL(DeviceCreated(QDBusObjectPath)),
111                 this,
112                 SLOT(deviceCreated(QDBusObjectPath)));
113
114         QList<QDBusObjectPath> list = adapter->ListDevices();
115         foreach(QDBusObjectPath item, list)
116         {
117                 deviceCreated(item);
118         }
119 }
120
121 void BluetoothDevicesModel::adapterRemoved(QDBusObjectPath)
122 {
123         QDBusObjectPath adapterpath = manager->DefaultAdapter();
124
125         if(adapterpath.path() == "")
126         {
127                 beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
128                 foreach(BluetoothDevice* device, m_devices)
129                 {
130                         delete device;
131                 }
132                 m_devices.clear();
133                 endRemoveRows();
134
135                 if(adapter) delete adapter;
136                 return;
137         }
138 }
139
140 void BluetoothDevicesModel::deviceCreated(QDBusObjectPath devicepath)
141 {
142         BluetoothDevice* device = new BluetoothDevice(devicepath,this);
143
144         connect(device,SIGNAL(propertyChanged(QString,QVariant)),this,SLOT(devicePropertyChanged(QString,QVariant)));
145
146         beginInsertRows(QModelIndex(),m_devices.size()+1,m_devices.size()+1);
147         m_devices.append(device);
148         endInsertRows();
149 }
150
151 void BluetoothDevicesModel::deviceRemoved(QDBusObjectPath devicepath)
152 {
153         for(int i=0; i<m_devices.size(); i++)
154         {
155
156                 if(m_devices[i]->path() == devicepath.path())
157                 {
158                         beginRemoveRows(QModelIndex(), i, i);
159                         m_devices[i]->deleteLater();
160                         m_devices.removeAt(i);
161                         endRemoveRows();
162                 }
163         }
164 }
165
166 void BluetoothDevicesModel::devicePropertyChanged(QString name, QVariant value)
167 {
168         if(name == "Paired" && value.toBool() == true)
169         {
170                 qDebug()<<"device property changed: "<<name<<" "<<value;
171                 BluetoothDevice* device = qobject_cast<BluetoothDevice*>(sender());
172                 emit devicePaired(device);
173         }
174 }