created NearbyItem with icon property addition
authorKevron Rees <kevron_m_rees@linux.intel.com>
Tue, 8 Feb 2011 22:38:50 +0000 (14:38 -0800)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Wed, 9 Feb 2011 00:13:48 +0000 (16:13 -0800)
agentadaptor.cpp
agentadaptor.h
asyncagent.cpp
asyncagent.h
bluetoothbaseagent.cpp
bluetoothbaseagent.h
nearbydevicesmodel.cpp
nearbydevicesmodel.h

index 7967f13..96d1487 100644 (file)
@@ -54,7 +54,7 @@ void AgentAdaptor::ConfirmModeChange(const QString &mode)
        agent->confirmModeChange(mode);
 }
 
-void AgentAdaptor::DisplayPasskey(const QDBusObjectPath &deviceObject, uint passkey)
+void AgentAdaptor::DisplayPasskey(const QDBusObjectPath &deviceObject, uint passkey, uint entered)
 {
        if(!agent)
        {
@@ -63,7 +63,7 @@ void AgentAdaptor::DisplayPasskey(const QDBusObjectPath &deviceObject, uint pass
 
        OrgBluezDeviceInterface device("org.bluez", deviceObject.path(), QDBusConnection::systemBus());
 
-       agent->displayPasskey(device, passkey);
+       agent->displayPasskey(device, passkey, entered);
 }
 
 void AgentAdaptor::Release()
index a071c8d..bec276f 100644 (file)
@@ -31,7 +31,7 @@ public slots:
        void Authorize(const QDBusObjectPath &device, const QString &uuid);
        void Cancel();
        void ConfirmModeChange(const QString &mode);
-       void DisplayPasskey(const QDBusObjectPath &device, uint passkey);
+       void DisplayPasskey(const QDBusObjectPath &device, uint passkey, uint entered);
        void Release();
        void RequestConfirmation(const QDBusObjectPath &device, uint passkey);
        uint RequestPasskey(const QDBusObjectPath &device);
index 453969d..ef8ddbd 100644 (file)
@@ -28,6 +28,8 @@ void AsyncAgent::requestConfirmation(OrgBluezDeviceInterface &device, uint key)
 
        QString alias = props["Alias"].toString();
 
+       deviceToPair = new BluetoothDevice(QDBusObjectPath(device.path()),this);
+
        QMetaObject::invokeMethod(parent(),"requestConfirmation",
                                                          Qt::QueuedConnection, Q_ARG(QString, alias), Q_ARG(uint,key));
 
@@ -45,6 +47,8 @@ uint AsyncAgent::requestPasskey(OrgBluezDeviceInterface &device)
 
        QString alias = props["Alias"].toString();
 
+       deviceToPair = new BluetoothDevice(QDBusObjectPath(device.path()),this);
+
        QMetaObject::invokeMethod(parent(), "requestPasskey", Qt::QueuedConnection, Q_ARG(QString, alias));
 
        return 0;
@@ -61,6 +65,8 @@ QString AsyncAgent::requestPidCode(OrgBluezDeviceInterface &device)
 
        QString alias = props["Alias"].toString();
 
+       deviceToPair = new BluetoothDevice(QDBusObjectPath(device.path()),this);
+
        QMetaObject::invokeMethod(parent(), "requestPidCode", Qt::QueuedConnection, Q_ARG(QString, alias));
 
        return "";
index f9fdcef..568e2c9 100644 (file)
@@ -13,6 +13,7 @@
 #define ASYNCAGENT_H
 
 #include "bluetoothbaseagent.h"
+#include "bluetoothdevice.h"
 
 class AsyncAgent : public BluetoothBaseAgent
 {
@@ -20,6 +21,8 @@ class AsyncAgent : public BluetoothBaseAgent
 public:
        explicit AsyncAgent(QString path, QObject *parent = 0);
 
+       BluetoothDevice* device() { return deviceToPair; }
+
        void requestConfirmation(OrgBluezDeviceInterface &device, uint key);
        uint requestPasskey(OrgBluezDeviceInterface &device);
        QString requestPidCode(OrgBluezDeviceInterface &device);
@@ -36,6 +39,8 @@ private:
        QDBusMessage pendingMessage;
        QDBusConnection m_connection;
 
+       BluetoothDevice* deviceToPair;
+
 };
 
 #endif // ASYNCAGENT_H
index 2c8d8bc..8677b26 100644 (file)
 #include "blueadapter.h"
 #include <QTimer>
 
-BluetoothBaseAgent::BluetoothBaseAgent(QString path, QObject *parent):QObject(parent),requestAccepted(false)
+BluetoothBaseAgent::BluetoothBaseAgent(QString path, QObject *parent):QObject(parent),requestAccepted(false),m_path(path)
 {
        new AgentAdaptor(this);
        QDBusConnection::systemBus().registerObject(path,this);
+}
 
-       OrgBluezManagerInterface manager(
-                       "org.bluez",
-                       "/", QDBusConnection::systemBus());
-
-       QDBusObjectPath adapterpath = manager.DefaultAdapter();
-
-       OrgBluezAdapterInterface adapter(
-                       "org.bluez",
-                       adapterpath.path(),
-                       QDBusConnection::systemBus());
+void BluetoothBaseAgent::registerAgent()
+{
+    OrgBluezManagerInterface manager(
+                    "org.bluez",
+                    "/", QDBusConnection::systemBus());
 
-       adapter.RegisterAgent(QDBusObjectPath(path),"");
+    QDBusObjectPath adapterpath = manager.DefaultAdapter();
 
-       qDebug()<<"last error: "<<adapter.lastError().message();
+    OrgBluezAdapterInterface adapter(
+                    "org.bluez",
+                    adapterpath.path(),
+                    QDBusConnection::systemBus());
 
+    adapter.RegisterAgent(QDBusObjectPath(m_path),"");
+    qDebug()<<"last error: "<<adapter.lastError().message();
 }
 
 void BluetoothBaseAgent::authorize(OrgBluezDeviceInterface &device, QString uuid)
@@ -50,9 +51,13 @@ void BluetoothBaseAgent::confirmModeChange(QString mode)
        qDebug()<<"mode changed "<<mode;
 }
 
-void BluetoothBaseAgent::displayPasskey(OrgBluezDeviceInterface &device, uint key)
+void BluetoothBaseAgent::displayPasskey(OrgBluezDeviceInterface &device, uint key, uint entered)
 {
        qDebug()<<"display key "<<device.path()<<" "<<key;
+
+       ///create and return back an empty reply:
+       QDBusMessage reply = message().createReply();
+       connection().send(reply);
 }
 
 void BluetoothBaseAgent::release()
index 8b65bd3..fcf1873 100644 (file)
@@ -20,15 +20,16 @@ class BluetoothBaseAgent: public QObject, public QDBusContext
 {
        Q_OBJECT
 public:
-       BluetoothBaseAgent(QString path="/pairing/agent", QObject *parent=0);
+       BluetoothBaseAgent(QString path="", QObject *parent=0);
 public slots:
        virtual void authorize(OrgBluezDeviceInterface &device, QString);
+       virtual void registerAgent();
        virtual void cancel()
        {
                sendErrorReply("org.bluez.Error.Canceled","The request was canceled");
        }
        virtual void confirmModeChange(QString);
-       virtual void displayPasskey(OrgBluezDeviceInterface &device, uint key);
+       virtual void displayPasskey(OrgBluezDeviceInterface &device, uint key, uint entered);
        virtual void release();
        virtual void requestConfirmation(OrgBluezDeviceInterface &device, uint key);
        virtual uint requestPasskey(OrgBluezDeviceInterface &device);
@@ -36,6 +37,7 @@ public slots:
 
 private:
        bool requestAccepted;
+       QString m_path;
 };
 
 #endif // BLUETOOTHBASEAGENT_H
index 6bdda1f..1112645 100644 (file)
@@ -24,8 +24,11 @@ NearbyDevicesModel::NearbyDevicesModel(QObject *parent) :
        adapterAdded(QDBusObjectPath());
 
        QHash<int,QByteArray> roles;
-       roles[NearbyDevicesModelRoles::name]="name";
-       roles[NearbyDevicesModelRoles::address]="address";
+       QMetaObject properties = NearbyItem::staticMetaObject;
+       for(int i=0; i<properties.propertyCount();i++)
+       {
+               roles[i]=properties.property(i).name();
+       }
        setRoleNames(roles);
 }
 
@@ -33,29 +36,41 @@ NearbyDevicesModel::NearbyDevicesModel(QObject *parent) :
 int NearbyDevicesModel::rowCount(const QModelIndex &parent) const
 {
        Q_UNUSED(parent);
-       return devicepathlist.size();
+       return devices.size();
 }
 
 QVariant NearbyDevicesModel::data(const QModelIndex &index, int role) const
 {
-       if (role == NearbyDevicesModelRoles::name)
+       /**if (role == NearbyDevicesModelRoles::name)
        {
                QString rowData;
-               if(index.row() < devicepathlist.size())
+               if(index.row() < devices.size())
                {
-                       rowData = deviceAliasMap[devicepathlist[index.row()]];
+                       rowData = deviceAliasMap[devices[index.row()]];
                }
                return QVariant(rowData);
        }
        else if (role == NearbyDevicesModelRoles::address)
        {
                QString rowData;
-               if(index.row() < devicepathlist.size())
+               if(index.row() < devices.size())
                {
-                       rowData = devicepathlist[index.row()];
+                       rowData = devices[index.row()];
                }
                return QVariant(rowData);
+       }*/
+
+       QString roleName = roleNames()[role];
+       QMetaObject object = NearbyItem::staticMetaObject;
+
+       for(int i=0; i<object.propertyCount(); i++)
+       {
+               if(object.property(i).name() == roleName)
+               {
+                       return object.property(i).read(devices[index.row()]);
+               }
        }
+
        return QVariant();
 }
 
@@ -64,7 +79,7 @@ void NearbyDevicesModel::pair(QString hwaddy)
 
        qDebug()<<"attempting to pair with "<<hwaddy;
        if(!adapter) return;
-       agent = new AsyncAgent("/temp/agent", this);
+       if(!agent) agent = new AsyncAgent("/temp/agent", this);
 
        adapter->CreatePairedDevice(hwaddy,
                                   QDBusObjectPath("/temp/agent"),"");
@@ -83,9 +98,9 @@ void NearbyDevicesModel::discover(bool start)
 
 void NearbyDevicesModel::removeAll(bool)
 {
-       for(int i=0;i<devicepathlist.size();i++)
+       for(int i=0;i<devices.size();i++)
        {
-               devicepathlist.removeAt(i);
+               devices.removeAt(i);
        }
 }
 
@@ -113,9 +128,9 @@ void NearbyDevicesModel::setAdapterProperty(QString name, QVariant value)
 void NearbyDevicesModel::deviceCreated(QString hwaddy, QVariantMap properties)
 {
        bool found = false;
-       foreach(QString path, devicepathlist)
+       foreach(NearbyItem* path, devices)
        {
-               if(path == hwaddy)
+               if(path->address() == hwaddy)
                {
                        found=true;
                        break;
@@ -124,25 +139,30 @@ void NearbyDevicesModel::deviceCreated(QString hwaddy, QVariantMap properties)
 
        if(!found)
        {
-               beginInsertRows(QModelIndex(), devicepathlist.size()+1, devicepathlist.size()+1);
-               devicepathlist.append(hwaddy);
-               deviceAliasMap[hwaddy] = properties["Alias"].toString();
-               emit nearbyDeviceFound(devicepathlist.indexOf(hwaddy));
+               beginInsertRows(QModelIndex(), devices.size()+1, devices.size()+1);
+
+               NearbyItem* item = new NearbyItem(properties["Alias"].toString(),
+                              hwaddy,properties["Icon"].toString(),this);
+
+               devices.append(item);
+               emit nearbyDeviceFound(devices.indexOf(item));
                endInsertRows();
        }
 }
 
 void NearbyDevicesModel::deviceRemoved(QString hwaddy)
 {
-       int i=-1;
-       if((i = devicepathlist.indexOf(hwaddy)) >=0)
+       foreach(NearbyItem* device, devices)
        {
-               beginRemoveRows(QModelIndex(),i,i);
-               devicepathlist.removeAt(i);
-               emit nearbyDeviceRemoved(i);
-               endRemoveRows();
+               if(device->address() == hwaddy)
+               {
+                       int i=devices.indexOf(device);
+                       beginRemoveRows(QModelIndex(),i,i);
+                       devices.removeAll(device);
+                       emit nearbyDeviceRemoved(i);
+                       endRemoveRows();
+               }
        }
-
 }
 
 void NearbyDevicesModel::adapterAdded(QDBusObjectPath path)
index 7e3197f..3bc9ab6 100644 (file)
@@ -8,31 +8,61 @@
 #include "bluemanager.h"
 #include "blueadapter.h"
 #include "asyncagent.h"
+#include "bluetoothdevice.h"
 
 namespace NearbyDevicesModelRoles
 {
        enum
        {
                name = Qt::UserRole + 1,
-               address
+               address,
+               icon
        };
 }
 
+class NearbyItem: public QObject
+{
+       Q_OBJECT
+       Q_PROPERTY(QString name READ name)
+       Q_PROPERTY(QString address READ address)
+       Q_PROPERTY(QString icon READ icon)
+public:
+       NearbyItem(QString name="", QString address="", QString icon="", QObject* parent = 0)
+       :QObject(parent)
+       {
+           m_name=name;
+           m_addy=address;
+           m_icon=icon;
+       }
+
+       QString name() { return m_name; }
+       QString address() { return m_addy; }
+       QString icon() { return m_icon; }
+
+private:
+       QString m_name;
+       QString m_addy;
+       QString m_icon;
+};
+
 class NearbyDevicesModel : public QAbstractListModel
 {
        Q_OBJECT
+       Q_PROPERTY(BluetoothDevice* pairingDevice READ pairingDevice)
 
 public:
        NearbyDevicesModel(QObject *parent = 0);
 
+       BluetoothDevice* pairingDevice() { if(agent) return agent->device();  else return NULL; }
+
 public slots:
 
        int columnCount(const QModelIndex &) const { return 1;}
        int rowCount(const QModelIndex &parent = QModelIndex()) const;
        QVariant data(const QModelIndex &index, int role) const;
 
-       QString hwAddress(int index) { return devicepathlist[index]; }
-       QString alias(int index){ return deviceAliasMap[devicepathlist[index]]; }
+       QString hwAddress(int index) { return devices[index]->address(); }
+       QString alias(int index){ return devices[index]->name(); }
        void pair(QString hwaddy);
        void discover(bool start);
        void removeAll(bool);
@@ -63,11 +93,10 @@ signals:
        void adapterPropertiesChanged(QString name, QVariant value);
 
 private:
-       QList<QString> devicepathlist;
-       QMap<QString,QString> deviceAliasMap;
+       QList<NearbyItem*> devices;
        OrgBluezManagerInterface *manager;
        OrgBluezAdapterInterface *adapter;
-       AsyncAgent* agent;
+       AsyncAgent* agent;      
 };
 
 #endif // NEARBYDEVICESMODEL_H