[Telepathy-commits] [telepathy-qt4/master] Simplify ContactManager code by adding a lookupContactByHandle utility function

Olli Salli olli.salli at collabora.co.uk
Tue Feb 3 01:06:13 PST 2009


---
 TelepathyQt4/Client/contact-manager.cpp |   25 +++++++++++++++++++------
 1 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/TelepathyQt4/Client/contact-manager.cpp b/TelepathyQt4/Client/contact-manager.cpp
index a5328e5..b3dbf8b 100644
--- a/TelepathyQt4/Client/contact-manager.cpp
+++ b/TelepathyQt4/Client/contact-manager.cpp
@@ -68,6 +68,7 @@ struct ContactManager::Private
 {
     Connection *conn;
     QMap<uint, QWeakPointer<Contact> > contacts;
+    QSharedPointer<Contact> lookupContactByHandle(uint handle);
 
     QMap<Contact::Feature, bool> tracking;
     void ensureTracking(Contact::Feature feature);
@@ -117,7 +118,7 @@ PendingContacts *ContactManager::contactsForHandles(const UIntList &handles,
     QSet<Contact::Feature> missingFeatures;
 
     foreach (uint handle, handles) {
-        QSharedPointer<Contact> contact = mPriv->contacts[handle].toStrongRef();
+        QSharedPointer<Contact> contact = mPriv->lookupContactByHandle(handle);
         if (contact) {
             if ((features - contact->requestedFeatures()).isEmpty()) {
                 // Contact exists and has all the requested features
@@ -130,9 +131,6 @@ PendingContacts *ContactManager::contactsForHandles(const UIntList &handles,
         } else {
             // Contact doesn't exist - we need to get all of the features (same as unite(features))
             missingFeatures = features;
-            // It might be a weak pointer with 0 refs, make sure to remove it
-            mPriv->contacts.remove(handle);
-            // In either case, the contact needs to be fetched
             otherContacts.insert(handle);
         }
     }
@@ -205,7 +203,7 @@ void ContactManager::onPresencesChanged(const Telepathy::SimpleContactPresences
     debug() << "Got PresencesChanged for" << presences.size() << "contacts";
 
     foreach (uint handle, presences.keys()) {
-        QSharedPointer<Contact> contact = mPriv->contacts.value(handle).toStrongRef();
+        QSharedPointer<Contact> contact = mPriv->lookupContactByHandle(handle);
 
         if (contact) {
             contact->receiveSimplePresence(presences[handle]);
@@ -227,7 +225,7 @@ ContactManager::~ContactManager()
 QSharedPointer<Contact> ContactManager::ensureContact(const ReferencedHandles &handle,
         const QSet<Contact::Feature> &features, const QVariantMap &attributes) {
     uint bareHandle = handle[0];
-    QSharedPointer<Contact> contact = mPriv->contacts.value(bareHandle).toStrongRef();
+    QSharedPointer<Contact> contact = mPriv->lookupContactByHandle(bareHandle);
 
     if (!contact) {
         contact = QSharedPointer<Contact>(new Contact(this, handle, features, attributes));
@@ -239,6 +237,21 @@ QSharedPointer<Contact> ContactManager::ensureContact(const ReferencedHandles &h
     return contact;
 }
 
+QSharedPointer<Contact> ContactManager::Private::lookupContactByHandle(uint handle)
+{
+    QSharedPointer<Contact> contact;
+
+    if (contacts.contains(handle)) {
+        contact = contacts.value(handle).toStrongRef();
+        if (!contact) {
+            // Dangling weak pointer, remove it
+            contacts.remove(handle);
+        }
+    }
+
+    return contact;
+}
+
 void ContactManager::Private::ensureTracking(Contact::Feature feature)
 {
     if (tracking[feature])
-- 
1.5.6.5




More information about the telepathy-commits mailing list