added conntected property to bluetoothdevicesmodel
authorKevron Rees <kevron_m_rees@linux.intel.com>
Wed, 18 May 2011 21:06:11 +0000 (14:06 -0700)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Wed, 18 May 2011 21:06:11 +0000 (14:06 -0700)
bluetoothdevicemodel.cpp
bluetoothdevicemodel.h

index 8fdfd9c..24015a5 100644 (file)
@@ -12,7 +12,7 @@
 #include "bluetoothdevicemodel.h"
 
 BluetoothDevicesModel::BluetoothDevicesModel(QObject *parent) :
-       QAbstractListModel(parent), adapter(NULL)
+       QAbstractListModel(parent), m_connected(false), adapter(NULL)
 {
        manager = new OrgBluezManagerInterface(
                        "org.bluez",
@@ -186,8 +186,8 @@ void BluetoothDevicesModel::adapterRemoved(QDBusObjectPath)
                beginRemoveRows(QModelIndex(), 0, m_devices.size()-1);
                foreach(BluetoothDevice* device, m_devices)
                {
-
-                       delete device;
+                       updateConnected(device->connected());
+                       device->deleteLater();
                }
                m_devices.clear();
                endRemoveRows();
@@ -203,6 +203,7 @@ void BluetoothDevicesModel::deviceCreated(QDBusObjectPath devicepath)
 {
        BluetoothDevice* device = new BluetoothDevice(devicepath,this);
 
+       updateConnected(device->connected());
        connect(device,SIGNAL(propertyChanged(QString,QVariant)),this,SLOT(devicePropertyChanged(QString,QVariant)));
 
        beginInsertRows(QModelIndex(),m_devices.size(),m_devices.size());
@@ -236,6 +237,10 @@ void BluetoothDevicesModel::devicePropertyChanged(QString name, QVariant value)
                emit devicePaired(device);
        }
 
+       else if(name == "Connected")
+       {
+               updateConnected(value.toBool());
+       }
        int row = m_devices.indexOf(device);
        if(row == -1) return; ///device doesn't exist.
 
@@ -259,3 +264,29 @@ void BluetoothDevicesModel::adapterPropertyChanged(QString name, QDBusVariant va
                discoverableTimeoutChanged(value.variant().toInt());
        }
 }
+
+void BluetoothDevicesModel::updateConnected(bool deviceconnectedStatus)
+{
+       if(deviceconnectedStatus)
+       {
+               if(!m_connected)
+               {
+                       m_connected = true;
+                       connectedChanged(m_connected);
+               }
+       }
+       else
+       {
+               bool temp = false;
+               foreach(BluetoothDevice* device, devices())
+               {
+                       temp |= device->connected();
+               }
+
+               if(temp != m_connected)
+               {
+                       m_connected = temp;
+                       connectedChanged(m_connected);
+               }
+       }
+}
index b58a7c1..8a393cd 100644 (file)
@@ -14,6 +14,8 @@ class BluetoothDevicesModel : public QAbstractListModel
        Q_PROPERTY(bool discoverable READ discoverable WRITE makeDiscoverable NOTIFY discoverableChanged);
        Q_PROPERTY(int discoverableTimeout READ discoverableTimeout WRITE setDiscoverableTimeout NOTIFY discoverableTimeoutChanged);
        Q_PROPERTY(bool adapterPresent READ adapterPresent NOTIFY adapterChanged);
+       Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged);
+
 public:
        explicit BluetoothDevicesModel(QObject *parent = 0);
 
@@ -29,6 +31,8 @@ public slots:
 
        QList<BluetoothDevice*> devices(){ return m_devices; }
 
+       bool connected() { return m_connected; }
+
        BluetoothDevice* device(QString path);
        void makePowered(bool poweredValue);
        bool powered();
@@ -52,8 +56,12 @@ signals:
        void devicePaired(BluetoothDevice* device);
        void discoverableChanged(bool discoverable);
        void adapterChanged(bool adapterPresent);
+       void connectedChanged(bool isConnected);
 
 private:
+       void updateConnected(bool deviceconnectedStatus);
+
+       bool m_connected;
        OrgBluezManagerInterface *manager;
        OrgBluezAdapterInterface *adapter;
        QList<BluetoothDevice*> m_devices;