From f563069e83ce11f089c017266e4184a5e3c4ad18 Mon Sep 17 00:00:00 2001 From: Dario Freddi Date: Tue, 30 Mar 2010 22:35:59 +0200 Subject: [PATCH] Add a new signal, allKnownContactsChanged, to monitor contact addition/removal Signed-off-by: Dario Freddi --- TelepathyQt4/contact-manager.cpp | 46 ++++++++++++++++++++++++++++++++++++++ TelepathyQt4/contact-manager.h | 2 + 2 files changed, 48 insertions(+), 0 deletions(-) diff --git a/TelepathyQt4/contact-manager.cpp b/TelepathyQt4/contact-manager.cpp index bb52f9d..2485d1b 100644 --- a/TelepathyQt4/contact-manager.cpp +++ b/TelepathyQt4/contact-manager.cpp @@ -56,6 +56,8 @@ struct TELEPATHY_QT4_NO_EXPORT ContactManager::Private Private(ContactManager *parent, const ConnectionPtr &connection) : parent(parent), connection(connection) { + // Make the cache start from the current known contacts + cachedAllKnownContacts = allKnownContacts(); } QString addContactListGroupChannel(const ChannelPtr &contactListGroupChannel) @@ -91,6 +93,7 @@ struct TELEPATHY_QT4_NO_EXPORT ContactManager::Private ContactManager *parent; WeakPtr connection; QMap > contacts; + Contacts cachedAllKnownContacts; QMap tracking; void ensureTracking(Contact::Feature feature); @@ -106,6 +109,7 @@ struct TELEPATHY_QT4_NO_EXPORT ContactManager::Private Contacts allKnownContacts() const; void updateContactsPresenceState(); + void computeKnownContactsChanges(const Contacts &added, const Contacts &removed); }; ConnectionPtr ContactManager::connection() const @@ -797,6 +801,13 @@ void ContactManager::onSubscribeChannelMembersChanged( debug() << "Contact" << contact->id() << "removed from subscribe list"; contact->setSubscriptionState(Contact::PresenceStateNo); } + + // Perform the needed computation for allKnownContactsChanged + Contacts allAdded; + allAdded.unite(groupMembersAdded); + allAdded.unite(groupLocalPendingMembersAdded); + allAdded.unite(groupRemotePendingMembersAdded); + mPriv->computeKnownContactsChanges(allAdded, groupMembersRemoved); } void ContactManager::onPublishChannelMembersChanged( @@ -830,6 +841,13 @@ void ContactManager::onPublishChannelMembersChanged( if (!groupLocalPendingMembersAdded.isEmpty()) { emit presencePublicationRequested(groupLocalPendingMembersAdded); } + + // Perform the needed computation for allKnownContactsChanged + Contacts allAdded; + allAdded.unite(groupMembersAdded); + allAdded.unite(groupLocalPendingMembersAdded); + allAdded.unite(groupRemotePendingMembersAdded); + mPriv->computeKnownContactsChanges(allAdded, groupMembersRemoved); } void ContactManager::onDenyChannelMembersChanged( @@ -1095,6 +1113,34 @@ Contacts ContactManager::Private::allKnownContacts() const return contacts; } +void ContactManager::Private::computeKnownContactsChanges(const Tp::Contacts& added, const Tp::Contacts& removed) +{ + // First of all, compute the real additions/removals based upon our cache + Tp::Contacts realAdded = added; + realAdded.subtract(cachedAllKnownContacts); + Tp::Contacts realRemoved = removed; + realRemoved.intersect(cachedAllKnownContacts); + + // Check if realRemoved have been _really_ removed from all lists + foreach (const ContactListChannel &contactListChannel, contactListChannels) { + ChannelPtr channel = contactListChannel.channel; + if (!channel) { + continue; + } + realRemoved.subtract(channel->groupContacts()); + realRemoved.subtract(channel->groupLocalPendingContacts()); + realRemoved.subtract(channel->groupRemotePendingContacts()); + } + + // Are there any real changes? + if (!realAdded.isEmpty() || !realRemoved.isEmpty()) { + // Yes, update our "cache" and emit the signal + cachedAllKnownContacts.unite(realAdded); + cachedAllKnownContacts.subtract(realRemoved); + emit parent->allKnownContactsChanged(realAdded, realRemoved); + } +} + void ContactManager::Private::updateContactsPresenceState() { Contacts subscribeContacts; diff --git a/TelepathyQt4/contact-manager.h b/TelepathyQt4/contact-manager.h index 834916e..34c825c 100644 --- a/TelepathyQt4/contact-manager.h +++ b/TelepathyQt4/contact-manager.h @@ -113,6 +113,8 @@ Q_SIGNALS: void groupMembersChanged(const QString &group, const Tp::Contacts &groupMembersAdded, const Tp::Contacts &groupMembersRemoved); + void allKnownContactsChanged(const Tp::Contacts &contactsAdded, + const Tp::Contacts &contactsRemoved); private Q_SLOTS: void onAliasesChanged(const Tp::AliasPairList &); -- 1.7.0.3