[Telepathy-commits] [telepathy-qt4/master] Add support for tracking SimplePresence changes on contacts (needs a test)

Olli Salli olli.salli at collabora.co.uk
Mon Feb 2 07:13:49 PST 2009


---
 TelepathyQt4/Client/contact-manager.cpp |   40 +++++++++++++++++++++++++++++++
 TelepathyQt4/Client/contact-manager.h   |    3 ++
 TelepathyQt4/Client/contact.cpp         |   17 +++++++++++-
 TelepathyQt4/Client/contact.h           |    4 +++
 4 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/TelepathyQt4/Client/contact-manager.cpp b/TelepathyQt4/Client/contact-manager.cpp
index 1a459ad..a5328e5 100644
--- a/TelepathyQt4/Client/contact-manager.cpp
+++ b/TelepathyQt4/Client/contact-manager.cpp
@@ -68,6 +68,9 @@ struct ContactManager::Private
 {
     Connection *conn;
     QMap<uint, QWeakPointer<Contact> > contacts;
+
+    QMap<Contact::Feature, bool> tracking;
+    void ensureTracking(Contact::Feature feature);
 };
 
 Connection *ContactManager::connection() const
@@ -141,6 +144,7 @@ PendingContacts *ContactManager::contactsForHandles(const UIntList &handles,
     QSet<QString> interfaces;
     foreach (Contact::Feature feature, missingFeatures) {
         interfaces.insert(featureToInterface(feature));
+        mPriv->ensureTracking(feature);
     }
 
     // TODO: filter using conn->contactAttributeInterfaces() when I add that
@@ -196,6 +200,19 @@ PendingContacts *ContactManager::upgradeContacts(const QList<QSharedPointer<Cont
     return new PendingContacts(this, contacts, features);
 }
 
+void ContactManager::onPresencesChanged(const Telepathy::SimpleContactPresences &presences)
+{
+    debug() << "Got PresencesChanged for" << presences.size() << "contacts";
+
+    foreach (uint handle, presences.keys()) {
+        QSharedPointer<Contact> contact = mPriv->contacts.value(handle).toStrongRef();
+
+        if (contact) {
+            contact->receiveSimplePresence(presences[handle]);
+        }
+    }
+}
+
 ContactManager::ContactManager(Connection *parent)
     : QObject(parent), mPriv(new Private)
 {
@@ -222,5 +239,28 @@ QSharedPointer<Contact> ContactManager::ensureContact(const ReferencedHandles &h
     return contact;
 }
 
+void ContactManager::Private::ensureTracking(Contact::Feature feature)
+{
+    if (tracking[feature])
+        return;
+
+    switch (feature) {
+        case Contact::FeatureSimplePresence:
+            QObject::connect(
+                    conn->simplePresenceInterface(),
+                    SIGNAL(PresencesChanged(const Telepathy::SimpleContactPresences &)),
+                    conn->contactManager(),
+                    SLOT(onPresencesChanged(const Telepathy::SimpleContactPresences &)));
+            break;
+
+        default:
+            warning() << " Unknown feature" << feature
+                << "when trying to figure out how to connect change notification!";
+            return;
+    }
+
+    tracking[feature] = true;
+}
+
 }
 }
diff --git a/TelepathyQt4/Client/contact-manager.h b/TelepathyQt4/Client/contact-manager.h
index 9c12198..e325219 100644
--- a/TelepathyQt4/Client/contact-manager.h
+++ b/TelepathyQt4/Client/contact-manager.h
@@ -65,6 +65,9 @@ class ContactManager : public QObject
         PendingContacts *upgradeContacts(const QList<QSharedPointer<Contact> > &contacts,
                 const QSet<Contact::Feature> &features);
 
+    private Q_SLOTS:
+        void onPresencesChanged(const Telepathy::SimpleContactPresences &);
+
     private:
         ContactManager(Connection *parent);
         ~ContactManager();
diff --git a/TelepathyQt4/Client/contact.cpp b/TelepathyQt4/Client/contact.cpp
index 4a4c976..dafa78c 100644
--- a/TelepathyQt4/Client/contact.cpp
+++ b/TelepathyQt4/Client/contact.cpp
@@ -139,8 +139,7 @@ void Contact::augment(const QSet<Feature> &requestedFeatures, const QVariantMap
                             TELEPATHY_INTERFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE "/presence"));
 
                 if (!maybePresence.status.isEmpty()) {
-                    mPriv->simplePresence = maybePresence;
-                    mPriv->actualFeatures.insert(FeatureSimplePresence);
+                    receiveSimplePresence(maybePresence);
                 }
                 break;
 
@@ -151,5 +150,19 @@ void Contact::augment(const QSet<Feature> &requestedFeatures, const QVariantMap
     }
 }
 
+void Contact::receiveSimplePresence(const SimplePresence &presence)
+{
+    if (!mPriv->requestedFeatures.contains(FeatureSimplePresence))
+        return;
+
+    mPriv->actualFeatures.insert(FeatureSimplePresence);
+
+    if (mPriv->simplePresence.status != presence.status
+            || mPriv->simplePresence.statusMessage != presence.statusMessage) {
+        mPriv->simplePresence = presence;
+        emit simplePresenceChanged(presenceStatus(), presenceType(), presenceMessage());
+    }
+}
+
 } // Telepathy::Client
 } // Telepathy
diff --git a/TelepathyQt4/Client/contact.h b/TelepathyQt4/Client/contact.h
index f1593e3..750b076 100644
--- a/TelepathyQt4/Client/contact.h
+++ b/TelepathyQt4/Client/contact.h
@@ -63,6 +63,9 @@ public:
 
     ~Contact();
 
+Q_SIGNALS:
+    void simplePresenceChanged(const QString &status, uint type, const QString &presenceMessage);
+
 private:
     Q_DISABLE_COPY(Contact);
 
@@ -70,6 +73,7 @@ private:
             const QSet<Feature> &requestedFeatures, const QVariantMap &attributes);
 
     void augment(const QSet<Feature> &requestedFeatures, const QVariantMap &attributes);
+    void receiveSimplePresence(const SimplePresence &presence);
 
     struct Private;
     friend class ContactManager;
-- 
1.5.6.5




More information about the Telepathy-commits mailing list