From d8420bc54e52440d6d3efda4828b64be7d75f02a Mon Sep 17 00:00:00 2001 From: "Jasper St. Pierre" Date: Sat, 4 Jun 2011 17:47:34 -0400 Subject: [PATCH] Initial work on supporting contact blocking. --- src/contact-list.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 82 insertions(+), 1 deletions(-) diff --git a/src/contact-list.c b/src/contact-list.c index 8cee5a7..e333850 100644 --- a/src/contact-list.c +++ b/src/contact-list.c @@ -80,6 +80,7 @@ static void haze_contact_list_mutable_init (TpMutableContactListInterface *); static void haze_contact_list_groups_init (TpContactGroupListInterface *); static void haze_contact_list_mutable_groups_init ( TpMutableContactGroupListInterface *); +static void haze_contact_list_blockable_init (TpBlockableContactListInterface *); G_DEFINE_TYPE_WITH_CODE(HazeContactList, haze_contact_list, @@ -89,7 +90,9 @@ G_DEFINE_TYPE_WITH_CODE(HazeContactList, G_IMPLEMENT_INTERFACE (TP_TYPE_CONTACT_GROUP_LIST, haze_contact_list_groups_init); G_IMPLEMENT_INTERFACE (TP_TYPE_MUTABLE_CONTACT_GROUP_LIST, - haze_contact_list_mutable_groups_init)) + haze_contact_list_mutable_groups_init); + G_IMPLEMENT_INTERFACE (TP_TYPE_BLOCKABLE_CONTACT_LIST, + haze_contact_list_blockable_init)) static void haze_contact_list_init (HazeContactList *self) @@ -1223,3 +1226,81 @@ haze_contact_list_mutable_groups_init ( vtable->rename_group_async = haze_contact_list_rename_group_async; /* assume default: groups are stored persistently */ } + +static TpHandleSet * +dup_blocked_contacts (TpBaseContactList *cl) +{ + HazeContactList *self = HAZE_CONTACT_LIST (cl); + PurpleAccount *account = self->priv->conn->account; + TpBaseConnection *base_conn = TP_BASE_CONNECTION (self->priv->conn); + TpHandleRepoIface *contact_repo = tp_base_connection_get_handles (base_conn, + TP_HANDLE_TYPE_CONTACT); + TpHandleSet *blocked = tp_handle_set_new (contact_repo); + GSList *l; + + for (l = account->deny; l != NULL; l = l->next) { + TpHandle handle = tp_handle_ensure (contact_repo, l->data, NULL, NULL); + + if (G_LIKELY (handle != 0)) + tp_handle_set_add (blocked, handle); + } + + return blocked; +} + +static void +set_contacts_privacy (TpBaseContactList *cl, + TpHandleSet *contacts, + gboolean block) +{ + HazeContactList *self = HAZE_CONTACT_LIST (cl); + PurpleAccount *account = self->priv->conn->account; + TpIntsetFastIter iter; + TpHandle handle; + + tp_intset_fast_iter_init (&iter, tp_handle_set_peek (contacts)); + + while (tp_intset_fast_iter_next (&iter, &handle)) + { + const gchar *bname = haze_connection_handle_inspect (self->priv->conn, + TP_HANDLE_TYPE_CONTACT, handle); + + if (block) + purple_privacy_deny (account, bname, FALSE, FALSE); + else + purple_privacy_allow (account, bname, FALSE, FALSE); + } +} + +static void +block_contacts_async(TpBaseContactList *cl, + TpHandleSet *contacts, + GAsyncReadyCallback callback, + gpointer user_data) +{ + set_contacts_privacy (cl, contacts, TRUE); + + tp_simple_async_report_success_in_idle ((GObject *) cl, callback, + user_data, block_contacts_async); +} + +static void +unblock_contacts_async(TpBaseContactList *cl, + TpHandleSet *contacts, + GAsyncReadyCallback callback, + gpointer user_data) +{ + set_contacts_privacy (cl, contacts, FALSE); + + tp_simple_async_report_success_in_idle ((GObject *) cl, callback, + user_data, unblock_contacts_async); +} + +static void +haze_contact_list_blockable_init( + TpBlockableContactListInterface *vtable) +{ + vtable->dup_blocked_contacts = dup_blocked_contacts; + vtable->block_contacts_async = block_contacts_async; + vtable->unblock_contacts_async = unblock_contacts_async; +} -- 1.7.5.2