[Telepathy-commits] [telepathy-qt4/master] ContactManager: Added support for blocking contacts.

Andre Moreira Magalhaes (andrunko) andre.magalhaes at collabora.co.uk
Fri Feb 27 14:38:36 PST 2009


---
 TelepathyQt4/Client/contact-manager.cpp |   83 ++++++++++++++++++++++++++++++-
 TelepathyQt4/Client/contact-manager.h   |   12 ++++-
 TelepathyQt4/Client/contact.cpp         |   27 ++++++++++-
 TelepathyQt4/Client/contact.h           |    5 ++
 4 files changed, 124 insertions(+), 3 deletions(-)

diff --git a/TelepathyQt4/Client/contact-manager.cpp b/TelepathyQt4/Client/contact-manager.cpp
index 82fc113..6df5f49 100644
--- a/TelepathyQt4/Client/contact-manager.cpp
+++ b/TelepathyQt4/Client/contact-manager.cpp
@@ -248,6 +248,35 @@ PendingOperation *ContactManager::removeContactsPresencePublication(
     return publishChannel->groupRemoveContacts(contacts);
 }
 
+bool ContactManager::canBlockContacts() const
+{
+    QSharedPointer<Channel> denyChannel;
+    if (mPriv->contactListsChannels.contains(ContactListChannel::TypeDeny)) {
+        denyChannel = mPriv->contactListsChannels[ContactListChannel::TypeDeny].channel;
+    }
+    return (bool) denyChannel;
+}
+
+PendingOperation *ContactManager::blockContacts(
+        const QList<QSharedPointer<Contact> > &contacts, bool value)
+{
+    if (!canBlockContacts()) {
+        warning() << "Contact blocking requested, "
+            "but unable to block contacts";
+        return new PendingFailure(this, TELEPATHY_ERROR_NOT_IMPLEMENTED,
+                "Cannot block contacts");
+    }
+
+    QSharedPointer<Channel> denyChannel =
+        mPriv->contactListsChannels[ContactListChannel::TypeDeny].channel;
+    if (value) {
+        return denyChannel->groupAddContacts(contacts);
+    }
+    else {
+        return denyChannel->groupRemoveContacts(contacts);
+    }
+}
+
 PendingContacts *ContactManager::contactsForHandles(const UIntList &handles,
         const QSet<Contact::Feature> &features)
 {
@@ -440,6 +469,34 @@ void ContactManager::onPublishChannelMembersChanged(
     }
 }
 
+void ContactManager::onDenyChannelMembersChanged(
+        const QSet<QSharedPointer<Contact> > &groupMembersAdded,
+        const QSet<QSharedPointer<Contact> > &groupLocalPendingMembersAdded,
+        const QSet<QSharedPointer<Contact> > &groupRemotePendingMembersAdded,
+        const QSet<QSharedPointer<Contact> > &groupMembersRemoved,
+        const Channel::GroupMemberChangeDetails &details)
+{
+    Q_UNUSED(details);
+
+    if (!groupLocalPendingMembersAdded.isEmpty()) {
+        warning() << "Found local pending contacts on deny list";
+    }
+
+    if (!groupRemotePendingMembersAdded.isEmpty()) {
+        warning() << "Found remote pending contacts on deny list";
+    }
+
+    foreach (QSharedPointer<Contact> contact, groupMembersAdded) {
+        debug() << "Contact" << contact->id() << "added to deny list";
+        contact->setBlocked(true);
+    }
+
+    foreach (QSharedPointer<Contact> contact, groupMembersRemoved) {
+        debug() << "Contact" << contact->id() << "removed from deny list";
+        contact->setBlocked(false);
+    }
+}
+
 ContactManager::ContactManager(Connection *parent)
     : QObject(parent), mPriv(new Private)
 {
@@ -501,6 +558,13 @@ void ContactManager::setContactListChannels(
                         const QSet<QSharedPointer<Telepathy::Client::Contact> > &,
                         const QSet<QSharedPointer<Telepathy::Client::Contact> > &,
                         const Telepathy::Client::Channel::GroupMemberChangeDetails &));
+        } else if (type == ContactListChannel::TypeDeny) {
+            method = SLOT(onDenyChannelMembersChanged(
+                        const QSet<QSharedPointer<Telepathy::Client::Contact> > &,
+                        const QSet<QSharedPointer<Telepathy::Client::Contact> > &,
+                        const QSet<QSharedPointer<Telepathy::Client::Contact> > &,
+                        const QSet<QSharedPointer<Telepathy::Client::Contact> > &,
+                        const Telepathy::Client::Channel::GroupMemberChangeDetails &));
         } else {
             continue;
         }
@@ -609,7 +673,16 @@ void ContactManager::Private::updateContactsPresenceState()
         }
     }
 
-    if (!subscribeChannel && !publishChannel) {
+    QSharedPointer<Channel> denyChannel;
+    QSet<QSharedPointer<Contact> > denyContacts;
+    if (contactListsChannels.contains(ContactListChannel::TypeDeny)) {
+        denyChannel = contactListsChannels[ContactListChannel::TypeDeny].channel;
+        if (denyChannel) {
+            denyContacts = denyChannel->groupContacts();
+        }
+    }
+
+    if (!subscribeChannel && !publishChannel && !denyChannel) {
         return;
     }
 
@@ -636,6 +709,12 @@ void ContactManager::Private::updateContactsPresenceState()
                 contact->setPublishState(Contact::PresenceStateNo);
             }
         }
+
+        if (denyChannel) {
+            if (denyContacts.contains(contact)) {
+                contact->setBlocked(true);
+            }
+        }
     }
 }
 
@@ -645,6 +724,7 @@ QString ContactManager::ContactListChannel::identifierForType(Type type)
         QLatin1String("subscribe"),
         QLatin1String("publish"),
         QLatin1String("stored"),
+        QLatin1String("deny"),
     };
     return identifiers[type];
 }
@@ -656,6 +736,7 @@ uint ContactManager::ContactListChannel::typeForIdentifier(const QString &identi
         types.insert(QLatin1String("subscribe"), TypeSubscribe);
         types.insert(QLatin1String("publish"), TypePublish);
         types.insert(QLatin1String("stored"), TypeStored);
+        types.insert(QLatin1String("deny"), TypeDeny);
     }
     if (types.contains(identifier)) {
         return types[identifier];
diff --git a/TelepathyQt4/Client/contact-manager.h b/TelepathyQt4/Client/contact-manager.h
index aa38100..b09e4e4 100644
--- a/TelepathyQt4/Client/contact-manager.h
+++ b/TelepathyQt4/Client/contact-manager.h
@@ -72,6 +72,10 @@ class ContactManager : public QObject
         PendingOperation *removeContactsPresencePublication(
                 const QList<QSharedPointer<Contact> > &contacts, const QString &message = QString());
 
+        bool canBlockContacts() const;
+        PendingOperation *blockContacts(
+                const QList<QSharedPointer<Contact> > &contacts, bool value = true);
+
         PendingContacts *contactsForHandles(const UIntList &handles,
                 const QSet<Contact::Feature> &features = QSet<Contact::Feature>());
         PendingContacts *contactsForHandles(const ReferencedHandles &handles,
@@ -103,6 +107,12 @@ class ContactManager : public QObject
             const QSet<QSharedPointer<Telepathy::Client::Contact> > &groupRemotePendingMembersAdded,
             const QSet<QSharedPointer<Telepathy::Client::Contact> > &groupMembersRemoved,
             const Telepathy::Client::Channel::GroupMemberChangeDetails &details);
+        void onDenyChannelMembersChanged(
+            const QSet<QSharedPointer<Telepathy::Client::Contact> > &groupMembersAdded,
+            const QSet<QSharedPointer<Telepathy::Client::Contact> > &groupLocalPendingMembersAdded,
+            const QSet<QSharedPointer<Telepathy::Client::Contact> > &groupRemotePendingMembersAdded,
+            const QSet<QSharedPointer<Telepathy::Client::Contact> > &groupMembersRemoved,
+            const Telepathy::Client::Channel::GroupMemberChangeDetails &details);
 
     private:
         struct ContactListChannel
@@ -111,6 +121,7 @@ class ContactManager : public QObject
                 TypeSubscribe = 0,
                 TypePublish,
                 TypeStored,
+                TypeDeny,
                 LastType
             };
 
@@ -126,7 +137,6 @@ class ContactManager : public QObject
 
             ~ContactListChannel()
             {
-                channel.clear();
             }
 
             static QString identifierForType(Type type);
diff --git a/TelepathyQt4/Client/contact.cpp b/TelepathyQt4/Client/contact.cpp
index fd958d4..8123892 100644
--- a/TelepathyQt4/Client/contact.cpp
+++ b/TelepathyQt4/Client/contact.cpp
@@ -38,7 +38,8 @@ struct Contact::Private
 {
     Private(ContactManager *manager, const ReferencedHandles &handle)
         : manager(manager), handle(handle), isAvatarTokenKnown(false),
-          subscriptionState(PresenceStateNo), publishState(PresenceStateNo)
+          subscriptionState(PresenceStateNo), publishState(PresenceStateNo),
+          blocked(false)
     {
     }
 
@@ -56,6 +57,7 @@ struct Contact::Private
 
     PresenceState subscriptionState;
     PresenceState publishState;
+    bool blocked;
 };
 
 ContactManager *Contact::manager() const
@@ -199,6 +201,20 @@ PendingOperation *Contact::removePresencePublication(const QString &message)
             message);
 }
 
+bool Contact::isBlocked() const
+{
+    return mPriv->blocked;
+}
+
+PendingOperation *Contact::block(bool value)
+{
+    QSharedPointer<Contact> self =
+        mPriv->manager->lookupContactByHandle(mPriv->handle[0]);
+    return mPriv->manager->blockContacts(
+            QList<QSharedPointer<Contact> >() << self,
+            value);
+}
+
 Contact::~Contact()
 {
     debug() << "Contact" << id() << "destroyed";
@@ -332,5 +348,14 @@ void Contact::setPublishState(Contact::PresenceState state)
     emit publishStateChanged(state);
 }
 
+void Contact::setBlocked(bool value)
+{
+    if (mPriv->blocked == value) {
+        return;
+    }
+    mPriv->blocked = value;
+    emit blockStatusChanged(value);
+}
+
 } // Telepathy::Client
 } // Telepathy
diff --git a/TelepathyQt4/Client/contact.h b/TelepathyQt4/Client/contact.h
index 848c8f2..f38c0e5 100644
--- a/TelepathyQt4/Client/contact.h
+++ b/TelepathyQt4/Client/contact.h
@@ -85,6 +85,9 @@ public:
     PendingOperation *authorizePresencePublication(const QString &message = QString());
     PendingOperation *removePresencePublication(const QString &message = QString());
 
+    bool isBlocked() const;
+    PendingOperation *block(bool value = true);
+
     ~Contact();
 
 Q_SIGNALS:
@@ -94,6 +97,7 @@ Q_SIGNALS:
 
     void subscriptionStateChanged(PresenceState state);
     void publishStateChanged(PresenceState state);
+    void blockStatusChanged(bool blocked);
 
     // TODO: consider how the Renaming interface should work and map to Contacts
     // I guess it would be something like:
@@ -115,6 +119,7 @@ private:
 
     void setSubscriptionState(PresenceState state);
     void setPublishState(PresenceState state);
+    void setBlocked(bool value);
 
     struct Private;
     friend class ContactManager;
-- 
1.5.6.5




More information about the telepathy-commits mailing list