]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-blocking-dialog.c
Revert "contact-blocking-dialog: use tp-glib high level blocking API"
[empathy.git] / libempathy-gtk / empathy-contact-blocking-dialog.c
1 /*
2  * empathy-contact-blocking-dialog.c
3  *
4  * EmpathyContactBlockingDialog
5  *
6  * Copyright (C) 2011 Collabora Ltd.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  * Authors: Danielle Madeley <danielle.madeley@collabora.co.uk>
23  */
24 #include "config.h"
25
26 #include <glib/gi18n-lib.h>
27
28 #include <libempathy/empathy-utils.h>
29
30 #include <libempathy/empathy-contact-manager.h>
31 #include <libempathy/empathy-tp-contact-list.h>
32
33 #include <libempathy-gtk/empathy-account-chooser.h>
34 #include <libempathy-gtk/empathy-ui-utils.h>
35
36 #include "empathy-contact-blocking-dialog.h"
37
38 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
39 #include <libempathy/empathy-debug.h>
40
41 #define GET_PRIVATE(o) (EMPATHY_CONTACT_BLOCKING_DIALOG (o)->priv)
42 #define DECLARE_CALLBACK(func) \
43   static void func (GObject *, GAsyncResult *, gpointer);
44
45 G_DEFINE_TYPE (EmpathyContactBlockingDialog, empathy_contact_blocking_dialog,
46     GTK_TYPE_DIALOG);
47
48 struct _EmpathyContactBlockingDialogPrivate
49 {
50   /* a map of all active connections to their 'deny' channel */
51   GHashTable *channels; /* reffed TpConnection* -> reffed TpChannel* */
52
53   guint block_account_changed;
54
55   GtkListStore *blocked_contacts;
56   GtkListStore *completion_contacts;
57   GtkTreeSelection *selection;
58
59   GtkWidget *account_chooser;
60   GtkWidget *add_button;
61   GtkWidget *add_contact_entry;
62   GtkWidget *info_bar;
63   GtkWidget *info_bar_label;
64   GtkWidget *remove_button;
65 };
66
67 enum /* blocked-contacts columns */
68 {
69   COL_BLOCKED_IDENTIFIER,
70   COL_BLOCKED_HANDLE,
71   N_BLOCKED_COLUMNS
72 };
73
74 enum /* completion_contacts columns */
75 {
76   COL_COMPLETION_IDENTIFIER,
77   COL_COMPLETION_TEXT,
78   N_COMPLETION_COLUMNS
79 };
80
81 static const char *
82 get_pretty_conn_name (TpConnection *conn)
83 {
84   return tp_proxy_get_object_path (conn) + strlen (TP_CONN_OBJECT_PATH_BASE);
85 }
86
87 static void
88 contact_blocking_dialog_filter_account_chooser (TpAccount *account,
89     EmpathyAccountChooserFilterResultCallback callback,
90     gpointer callback_data,
91     gpointer user_data)
92 {
93   EmpathyContactBlockingDialog *self = user_data;
94   TpConnection *conn = tp_account_get_connection (account);
95   gboolean enable;
96
97   enable =
98     conn != NULL &&
99     g_hash_table_lookup (self->priv->channels, conn) != NULL;
100
101   callback (enable, callback_data);
102 }
103
104 static void contact_blocking_dialog_account_changed (GtkWidget *,
105     EmpathyContactBlockingDialog *);
106
107 static void
108 contact_blocking_dialog_refilter_account_chooser (
109     EmpathyContactBlockingDialog *self)
110 {
111   EmpathyAccountChooser *chooser =
112     EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser);
113   TpConnection *conn;
114   gboolean enabled;
115
116   DEBUG ("Refiltering account chooser");
117
118   /* set the filter to refilter the account chooser */
119   self->priv->block_account_changed++;
120   empathy_account_chooser_set_filter (chooser,
121       contact_blocking_dialog_filter_account_chooser, self);
122   self->priv->block_account_changed--;
123
124   conn = empathy_account_chooser_get_connection (chooser);
125   enabled = (empathy_account_chooser_get_account (chooser) != NULL &&
126              conn != NULL &&
127              g_hash_table_lookup (self->priv->channels, conn) != NULL);
128
129   if (!enabled)
130     DEBUG ("No account selected");
131
132   gtk_widget_set_sensitive (self->priv->add_button, enabled);
133   gtk_widget_set_sensitive (self->priv->add_contact_entry, enabled);
134
135   contact_blocking_dialog_account_changed (self->priv->account_chooser, self);
136 }
137
138 static void contact_blocking_dialog_inspected_handles (TpConnection *,
139     const char **, const GError *, gpointer, GObject *);
140
141 static void
142 contact_blocking_dialog_add_contacts_to_list (
143     EmpathyContactBlockingDialog *self,
144     TpConnection *conn,
145     GArray *handles)
146 {
147   if (handles->len > 0)
148     tp_cli_connection_call_inspect_handles (conn, -1,
149         TP_HANDLE_TYPE_CONTACT, handles,
150         contact_blocking_dialog_inspected_handles,
151         g_boxed_copy (DBUS_TYPE_G_UINT_ARRAY, handles),
152         (GDestroyNotify) g_array_unref, G_OBJECT (self));
153 }
154
155 static void
156 contact_blocking_dialog_inspected_handles (TpConnection *conn,
157     const char **identifiers,
158     const GError *in_error,
159     gpointer user_data,
160     GObject *self)
161 {
162   EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
163   GArray *handles = user_data;
164   guint i;
165
166   if (in_error != NULL)
167     {
168       DEBUG ("Failed to inspect handles: %s", in_error->message);
169       return;
170     }
171
172   DEBUG ("Adding %u identifiers", handles->len);
173
174   for (i = 0; i < handles->len; i++)
175     {
176       const char *identifier = identifiers[i];
177       TpHandle handle = g_array_index (handles, TpHandle, i);
178
179       gtk_list_store_insert_with_values (priv->blocked_contacts, NULL, -1,
180           COL_BLOCKED_IDENTIFIER, identifier,
181           COL_BLOCKED_HANDLE, handle,
182           -1);
183     }
184 }
185
186 DECLARE_CALLBACK (contact_blocking_dialog_connection_prepared);
187
188 static void
189 contact_blocking_dialog_connection_status_changed (TpAccount *account,
190     guint old_status,
191     guint new_status,
192     guint reason,
193     const char *dbus_reason,
194     GHashTable *details,
195     EmpathyContactBlockingDialog *self)
196 {
197   TpConnection *conn = tp_account_get_connection (account);
198
199   switch (new_status)
200     {
201       case TP_CONNECTION_STATUS_DISCONNECTED:
202         DEBUG ("Connection %s invalidated", get_pretty_conn_name (conn));
203
204         /* remove the channel from the hash table */
205         g_hash_table_remove (self->priv->channels, conn);
206         contact_blocking_dialog_refilter_account_chooser (self);
207         break;
208
209       case TP_CONNECTION_STATUS_CONNECTING:
210         break;
211
212       case TP_CONNECTION_STATUS_CONNECTED:
213         DEBUG ("Connection %s reconnected", get_pretty_conn_name (conn));
214
215         tp_proxy_prepare_async (conn, NULL,
216             contact_blocking_dialog_connection_prepared, self);
217     }
218 }
219
220 static void
221 contact_blocking_dialog_deny_channel_members_changed (TpChannel *channel,
222     const char *message,
223     GArray *added,
224     GArray *removed,
225     GArray *local_pending,
226     GArray *remote_pending,
227     TpHandle actor,
228     guint reason,
229     EmpathyContactBlockingDialog *self)
230 {
231   TpConnection *conn = tp_channel_borrow_connection (channel);
232   GtkTreeModel *model = GTK_TREE_MODEL (self->priv->blocked_contacts);
233   GtkTreeIter iter;
234   TpIntset *removed_set;
235   gboolean valid;
236
237   /* we only care about changes to the selected connection */
238   /* FIXME: can we compare proxy pointers directly? */
239   if (tp_strdiff (
240         tp_proxy_get_object_path (tp_channel_borrow_connection (channel)),
241         tp_proxy_get_object_path (empathy_account_chooser_get_connection (
242             EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser)))))
243     return;
244
245   DEBUG ("deny list changed on %s: %u added, %u removed",
246       get_pretty_conn_name (conn), added->len, removed->len);
247
248   /* add contacts */
249   contact_blocking_dialog_add_contacts_to_list (self, conn, added);
250
251   /* remove contacts */
252   removed_set = tp_intset_from_array (removed);
253
254   valid = gtk_tree_model_get_iter_first (model, &iter);
255   while (valid)
256     {
257       TpHandle handle;
258
259       gtk_tree_model_get (model, &iter,
260           COL_BLOCKED_HANDLE, &handle,
261           -1);
262
263       if (tp_intset_is_member (removed_set, handle))
264         valid = gtk_list_store_remove (self->priv->blocked_contacts, &iter);
265       else
266         valid = gtk_tree_model_iter_next (model, &iter);
267     }
268
269   tp_intset_destroy (removed_set);
270 }
271
272 DECLARE_CALLBACK (contact_blocking_dialog_connection_prepared);
273
274 static void
275 contact_blocking_dialog_am_prepared (GObject *am,
276     GAsyncResult *result,
277     gpointer user_data)
278 {
279   EmpathyContactBlockingDialog *self = user_data;
280   GList *accounts, *ptr;
281   GError *error = NULL;
282
283   if (!tp_proxy_prepare_finish (am, result, &error))
284     {
285       g_critical ("Could not prepare Account Manager: %s", error->message);
286       g_error_free (error);
287       return;
288     }
289
290   accounts = tp_account_manager_get_valid_accounts (TP_ACCOUNT_MANAGER (am));
291
292   for (ptr = accounts; ptr != NULL; ptr = ptr->next)
293     {
294       TpAccount *account = ptr->data;
295       TpConnection *conn;
296
297       tp_g_signal_connect_object (account, "status-changed",
298           G_CALLBACK (contact_blocking_dialog_connection_status_changed),
299           self, 0);
300
301       conn = tp_account_get_connection (TP_ACCOUNT (account));
302
303       if (conn != NULL)
304         {
305           tp_proxy_prepare_async (conn, NULL,
306               contact_blocking_dialog_connection_prepared, self);
307         }
308     }
309
310   g_list_free (accounts);
311 }
312
313 static void contact_blocking_dialog_got_deny_channel (TpConnection *,
314     gboolean, const char *, GHashTable *, const GError *, gpointer, GObject *);
315
316 static void
317 contact_blocking_dialog_connection_prepared (GObject *conn,
318     GAsyncResult *result,
319     gpointer user_data)
320 {
321   EmpathyContactBlockingDialog *self = user_data;
322   GHashTable *request;
323   GError *error = NULL;
324
325   if (!tp_proxy_prepare_finish (conn, result, &error))
326     {
327       DEBUG ("Failed to prepare connection %s: %s",
328           get_pretty_conn_name ((TpConnection *) conn), error->message);
329       g_error_free (error);
330       return;
331     }
332
333   /* request the deny channel */
334   request = tp_asv_new (
335       TP_PROP_CHANNEL_CHANNEL_TYPE,
336       G_TYPE_STRING,
337       TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
338
339       TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
340       G_TYPE_UINT,
341       TP_HANDLE_TYPE_LIST,
342
343       TP_PROP_CHANNEL_TARGET_ID,
344       G_TYPE_STRING,
345       "deny",
346
347       NULL);
348
349   tp_cli_connection_interface_requests_call_ensure_channel (
350       TP_CONNECTION (conn), -1, request,
351       contact_blocking_dialog_got_deny_channel, NULL, NULL, G_OBJECT (self));
352
353   g_hash_table_destroy (request);
354 }
355
356 DECLARE_CALLBACK (contact_blocking_dialog_deny_channel_prepared);
357
358 static void
359 contact_blocking_dialog_got_deny_channel (TpConnection *conn,
360     gboolean yours,
361     const char *channel_path,
362     GHashTable *props,
363     const GError *in_error,
364     gpointer user_data,
365     GObject *self)
366 {
367   TpChannel *channel;
368   GError *error = NULL;
369
370   const GQuark features[] = {
371       TP_CHANNEL_FEATURE_CORE,
372       TP_CHANNEL_FEATURE_GROUP,
373       0 };
374
375   if (in_error != NULL)
376     {
377       DEBUG ("Failed to get 'deny' channel on %s: %s",
378           get_pretty_conn_name (conn), in_error->message);
379       return;
380     }
381
382   channel = tp_channel_new_from_properties (conn, channel_path, props, &error);
383
384   if (error != NULL)
385     {
386       DEBUG ("Failed to create channel proxy on %s: %s",
387           get_pretty_conn_name (conn), in_error->message);
388       g_error_free (error);
389       return;
390     }
391
392   tp_proxy_prepare_async (channel, features,
393       contact_blocking_dialog_deny_channel_prepared, self);
394 }
395
396 static void
397 contact_blocking_dialog_deny_channel_prepared (GObject *channel,
398     GAsyncResult *result,
399     gpointer user_data)
400 {
401   EmpathyContactBlockingDialog *self = user_data;
402   TpConnection *conn;
403   GError *error = NULL;
404
405   if (!tp_proxy_prepare_finish (channel, result, &error))
406     {
407       DEBUG ("Failed to prepare channel %s: %s",
408           tp_proxy_get_object_path (channel), error->message);
409       g_error_free (error);
410       return;
411     }
412
413   conn = tp_channel_borrow_connection (TP_CHANNEL (channel));
414
415   DEBUG ("Channel %s prepared for connection %s",
416       tp_proxy_get_object_path (channel), get_pretty_conn_name (conn));
417
418   g_hash_table_insert (self->priv->channels,
419       g_object_ref (conn), channel);
420   contact_blocking_dialog_refilter_account_chooser (self);
421
422   tp_g_signal_connect_object (channel, "group-members-changed",
423       G_CALLBACK (contact_blocking_dialog_deny_channel_members_changed),
424       self, 0);
425 }
426
427 static void
428 contact_blocking_dialog_set_error (EmpathyContactBlockingDialog *self,
429     const GError *error)
430 {
431   const char *msg = NULL;
432
433   if (error->domain == TP_ERRORS)
434     {
435       if (error->code == TP_ERROR_INVALID_HANDLE)
436         msg = _("Unknown or invalid identifier");
437       else if (error->code == TP_ERROR_NOT_AVAILABLE)
438         msg = _("Contact blocking temporarily unavailable");
439       else if (error->code == TP_ERROR_NOT_CAPABLE)
440         msg = _("Contact blocking unavailable");
441       else if (error->code == TP_ERROR_PERMISSION_DENIED)
442         msg = _("Permission Denied");
443     }
444
445   if (msg == NULL)
446     msg = _("Could not block contact");
447
448   gtk_label_set_text (GTK_LABEL (self->priv->info_bar_label), msg);
449   gtk_widget_show (self->priv->info_bar);
450 }
451
452 static void contact_blocking_dialog_add_contact_got_handle (TpConnection *,
453     const GArray *, const GError *, gpointer, GObject *);
454
455 static void
456 contact_blocking_dialog_add_contact (GtkWidget *widget,
457     EmpathyContactBlockingDialog *self)
458 {
459   TpConnection *conn = empathy_account_chooser_get_connection (
460       EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
461   const char *identifiers[2] = { NULL, };
462
463   identifiers[0] = gtk_entry_get_text (
464       GTK_ENTRY (self->priv->add_contact_entry));
465
466   DEBUG ("Looking up handle for '%s' on %s",
467       identifiers[0], get_pretty_conn_name (conn));
468
469   tp_cli_connection_call_request_handles (conn, -1,
470       TP_HANDLE_TYPE_CONTACT, identifiers,
471       contact_blocking_dialog_add_contact_got_handle,
472       NULL, NULL, G_OBJECT (self));
473
474   gtk_entry_set_text (GTK_ENTRY (self->priv->add_contact_entry), "");
475   gtk_widget_hide (self->priv->info_bar);
476 }
477
478 static void
479 contact_blocking_dialog_added_contact (TpChannel *, const GError *,
480     gpointer, GObject *);
481
482 static void
483 contact_blocking_dialog_add_contact_got_handle (TpConnection *conn,
484     const GArray *handles,
485     const GError *in_error,
486     gpointer user_data,
487     GObject *self)
488 {
489   EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
490   TpChannel *channel = g_hash_table_lookup (priv->channels, conn);
491
492   if (in_error != NULL)
493     {
494       DEBUG ("Error getting handle on %s: %s",
495           get_pretty_conn_name (conn), in_error->message);
496
497       contact_blocking_dialog_set_error (
498           EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
499
500       return;
501     }
502
503   g_return_if_fail (handles->len == 1);
504
505   DEBUG ("Adding handle %u to deny channel on %s",
506       g_array_index (handles, TpHandle, 0), get_pretty_conn_name (conn));
507
508   tp_cli_channel_interface_group_call_add_members (channel, -1,
509       handles, "",
510       contact_blocking_dialog_added_contact, NULL, NULL, self);
511 }
512
513 static void
514 contact_blocking_dialog_added_contact (TpChannel *channel,
515     const GError *in_error,
516     gpointer user_data,
517     GObject *self)
518 {
519   if (in_error != NULL)
520     {
521       DEBUG ("Error adding contact to deny list %s: %s",
522           tp_proxy_get_object_path (channel), in_error->message);
523
524       contact_blocking_dialog_set_error (
525           EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
526
527       return;
528     }
529
530   DEBUG ("Contact added to %s", tp_proxy_get_object_path (channel));
531 }
532
533 static void
534 contact_blocking_dialog_removed_contacts (TpChannel *,
535     const GError *, gpointer, GObject *);
536
537 static void
538 contact_blocking_dialog_remove_contacts (GtkWidget *button,
539     EmpathyContactBlockingDialog *self)
540 {
541   TpConnection *conn = empathy_account_chooser_get_connection (
542       EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
543   TpChannel *channel = g_hash_table_lookup (self->priv->channels, conn);
544   GtkTreeModel *model;
545   GList *rows, *ptr;
546   GArray *handles = g_array_new (FALSE, FALSE, sizeof (TpHandle));
547
548   rows = gtk_tree_selection_get_selected_rows (self->priv->selection, &model);
549
550   for (ptr = rows; ptr != NULL; ptr = ptr->next)
551     {
552       GtkTreePath *path = ptr->data;
553       GtkTreeIter iter;
554       TpHandle handle;
555
556       if (!gtk_tree_model_get_iter (model, &iter, path))
557         continue;
558
559       gtk_tree_model_get (model, &iter,
560           COL_BLOCKED_HANDLE, &handle,
561           -1);
562
563       g_array_append_val (handles, handle);
564       gtk_tree_path_free (path);
565     }
566
567   g_list_free (rows);
568
569   if (handles->len > 0)
570     {
571       DEBUG ("Removing %u handles", handles->len);
572
573       tp_cli_channel_interface_group_call_remove_members (channel, -1,
574           handles, "",
575           contact_blocking_dialog_removed_contacts,
576           NULL, NULL, G_OBJECT (self));
577     }
578
579   g_array_unref (handles);
580 }
581
582 static void
583 contact_blocking_dialog_removed_contacts (TpChannel *channel,
584     const GError *in_error,
585     gpointer user_data,
586     GObject *self)
587 {
588   if (in_error != NULL)
589     {
590       DEBUG ("Error removing contacts from deny list: %s", in_error->message);
591
592       contact_blocking_dialog_set_error (
593           EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
594
595       return;
596     }
597
598   DEBUG ("Contacts removed");
599 }
600
601 static void
602 contact_blocking_dialog_account_changed (GtkWidget *account_chooser,
603     EmpathyContactBlockingDialog *self)
604 {
605   TpConnection *conn = empathy_account_chooser_get_connection (
606       EMPATHY_ACCOUNT_CHOOSER (account_chooser));
607   TpChannel *channel;
608   GArray *blocked;
609   EmpathyContactManager *contact_manager;
610   EmpathyTpContactList *contact_list;
611   GList *members, *ptr;
612
613   if (self->priv->block_account_changed > 0)
614     return;
615
616   /* clear the lists of contacts */
617   gtk_list_store_clear (self->priv->blocked_contacts);
618   gtk_list_store_clear (self->priv->completion_contacts);
619
620   if (conn == NULL)
621     return;
622
623   DEBUG ("Account changed: %s", get_pretty_conn_name (conn));
624
625   /* load the deny list */
626   channel = g_hash_table_lookup (self->priv->channels, conn);
627
628   if (channel == NULL)
629     return;
630
631   g_return_if_fail (TP_IS_CHANNEL (channel));
632
633   blocked = tp_intset_to_array (tp_channel_group_get_members (channel));
634
635   DEBUG ("%u contacts on blocked list", blocked->len);
636
637   contact_blocking_dialog_add_contacts_to_list (self, conn, blocked);
638   g_array_unref (blocked);
639
640   /* load the completion list */
641   g_return_if_fail (empathy_contact_manager_initialized ());
642
643   DEBUG ("Loading contacts");
644
645   contact_manager = empathy_contact_manager_dup_singleton ();
646   contact_list = empathy_contact_manager_get_list (contact_manager, conn);
647   members = empathy_contact_list_get_members (
648       EMPATHY_CONTACT_LIST (contact_list));
649
650   for (ptr = members; ptr != NULL; ptr = ptr->next)
651     {
652       EmpathyContact *contact = ptr->data;
653       gchar *tmpstr;
654
655       tmpstr = g_strdup_printf ("%s (%s)",
656           empathy_contact_get_alias (contact),
657           empathy_contact_get_id (contact));
658
659       gtk_list_store_insert_with_values (self->priv->completion_contacts,
660           NULL, -1,
661           COL_COMPLETION_IDENTIFIER, empathy_contact_get_id (contact),
662           COL_COMPLETION_TEXT, tmpstr,
663           -1);
664
665       g_free (tmpstr);
666       g_object_unref (contact);
667     }
668
669   g_list_free (members);
670   g_object_unref (contact_manager);
671 }
672
673 static void
674 contact_blocking_dialog_view_selection_changed (GtkTreeSelection *selection,
675     EmpathyContactBlockingDialog *self)
676 {
677   GList *rows = gtk_tree_selection_get_selected_rows (selection, NULL);
678
679   /* update the sensitivity of the remove button */
680   gtk_widget_set_sensitive (self->priv->remove_button, rows != NULL);
681
682   g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
683   g_list_free (rows);
684 }
685
686 static gboolean
687 contact_selector_dialog_match_func (GtkEntryCompletion *completion,
688     const gchar *key,
689     GtkTreeIter *iter,
690     gpointer user_data)
691 {
692   GtkTreeModel *model;
693   gchar *str, *lower;
694   gboolean v = FALSE;
695
696   model = gtk_entry_completion_get_model (completion);
697   if (model == NULL || iter == NULL)
698     return FALSE;
699
700   gtk_tree_model_get (model, iter, COL_COMPLETION_TEXT, &str, -1);
701   lower = g_utf8_strdown (str, -1);
702   if (strstr (lower, key))
703     {
704       DEBUG ("Key %s is matching name **%s**", key, str);
705       v = TRUE;
706       goto out;
707     }
708   g_free (str);
709   g_free (lower);
710
711   gtk_tree_model_get (model, iter, COL_COMPLETION_IDENTIFIER, &str, -1);
712   lower = g_utf8_strdown (str, -1);
713   if (strstr (lower, key))
714     {
715       DEBUG ("Key %s is matching ID **%s**", key, str);
716       v = TRUE;
717       goto out;
718     }
719
720 out:
721   g_free (str);
722   g_free (lower);
723
724   return v;
725 }
726
727 static gboolean
728 contact_selector_dialog_match_selected_cb (GtkEntryCompletion *widget,
729     GtkTreeModel *model,
730     GtkTreeIter *iter,
731     EmpathyContactBlockingDialog *self)
732 {
733   gchar *id;
734
735   if (iter == NULL || model == NULL)
736     return FALSE;
737
738   gtk_tree_model_get (model, iter, COL_COMPLETION_IDENTIFIER, &id, -1);
739   gtk_entry_set_text (GTK_ENTRY (self->priv->add_contact_entry), id);
740
741   DEBUG ("Got selected match **%s**", id);
742
743   g_free (id);
744
745   return TRUE;
746 }
747
748 static void
749 contact_blocking_dialog_dispose (GObject *self)
750 {
751   EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
752
753   tp_clear_pointer (&priv->channels, g_hash_table_destroy);
754
755   G_OBJECT_CLASS (empathy_contact_blocking_dialog_parent_class)->dispose (self);
756 }
757
758 static void
759 empathy_contact_blocking_dialog_class_init (
760     EmpathyContactBlockingDialogClass *klass)
761 {
762   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
763
764   gobject_class->dispose = contact_blocking_dialog_dispose;
765
766   g_type_class_add_private (gobject_class,
767       sizeof (EmpathyContactBlockingDialogPrivate));
768 }
769
770 static void
771 empathy_contact_blocking_dialog_init (EmpathyContactBlockingDialog *self)
772 {
773   GtkBuilder *gui;
774   char *filename;
775   GtkWidget *contents;
776   GtkWidget *account_hbox, *blocked_contacts_view, *blocked_contacts_sw,
777       *remove_toolbar;
778   GtkEntryCompletion *completion;
779   TpAccountManager *am;
780   GtkStyleContext *context;
781
782   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
783       EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
784       EmpathyContactBlockingDialogPrivate);
785
786   self->priv->channels = g_hash_table_new_full (NULL, NULL,
787       g_object_unref, g_object_unref);
788
789   gtk_window_set_title (GTK_WINDOW (self), _("Edit Blocked Contacts"));
790   gtk_dialog_add_button (GTK_DIALOG (self),
791       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
792
793   filename = empathy_file_lookup ("empathy-contact-blocking-dialog.ui",
794       "libempathy-gtk");
795
796   gui = empathy_builder_get_file (filename,
797       "contents", &contents,
798       "account-hbox", &account_hbox,
799       "add-button", &self->priv->add_button,
800       "add-contact-entry", &self->priv->add_contact_entry,
801       "blocked-contacts", &self->priv->blocked_contacts,
802       "blocked-contacts-sw", &blocked_contacts_sw,
803       "blocked-contacts-view", &blocked_contacts_view,
804       "remove-button", &self->priv->remove_button,
805       "remove-toolbar", &remove_toolbar,
806       NULL);
807
808   empathy_builder_connect (gui, self,
809       "add-button", "clicked", contact_blocking_dialog_add_contact,
810       "add-contact-entry", "activate", contact_blocking_dialog_add_contact,
811       "remove-button", "clicked", contact_blocking_dialog_remove_contacts,
812       NULL);
813
814   /* join the remove toolbar to the treeview */
815   context = gtk_widget_get_style_context (blocked_contacts_sw);
816   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
817   context = gtk_widget_get_style_context (remove_toolbar);
818   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
819
820   /* add the contents to the dialog */
821   gtk_container_add (
822       GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (self))),
823       contents);
824   gtk_widget_show (contents);
825
826   /* set up the tree selection */
827   self->priv->selection = gtk_tree_view_get_selection (
828       GTK_TREE_VIEW (blocked_contacts_view));
829   gtk_tree_selection_set_mode (self->priv->selection, GTK_SELECTION_MULTIPLE);
830   g_signal_connect (self->priv->selection, "changed",
831       G_CALLBACK (contact_blocking_dialog_view_selection_changed), self);
832
833   /* build the contact entry */
834   self->priv->completion_contacts = gtk_list_store_new (N_COMPLETION_COLUMNS,
835       G_TYPE_STRING, /* id */
836       G_TYPE_UINT, /* handle */
837       G_TYPE_STRING); /* text */
838   completion = gtk_entry_completion_new ();
839   gtk_entry_completion_set_model (completion,
840       GTK_TREE_MODEL (self->priv->completion_contacts));
841   gtk_entry_completion_set_text_column (completion, COL_COMPLETION_TEXT);
842   gtk_entry_completion_set_match_func (completion,
843       contact_selector_dialog_match_func,
844       NULL, NULL);
845   g_signal_connect (completion, "match-selected",
846         G_CALLBACK (contact_selector_dialog_match_selected_cb),
847         self);
848   gtk_entry_set_completion (GTK_ENTRY (self->priv->add_contact_entry),
849       completion);
850   g_object_unref (completion);
851   g_object_unref (self->priv->completion_contacts);
852
853   /* add the account chooser */
854   self->priv->account_chooser = empathy_account_chooser_new ();
855   contact_blocking_dialog_refilter_account_chooser (self);
856   g_signal_connect (self->priv->account_chooser, "changed",
857       G_CALLBACK (contact_blocking_dialog_account_changed), self);
858
859   gtk_box_pack_start (GTK_BOX (account_hbox), self->priv->account_chooser,
860       TRUE, TRUE, 0);
861   gtk_widget_show (self->priv->account_chooser);
862
863   /* add an error warning info bar */
864   self->priv->info_bar = gtk_info_bar_new ();
865   gtk_box_pack_start (GTK_BOX (contents), self->priv->info_bar, FALSE, TRUE, 0);
866   gtk_info_bar_set_message_type (GTK_INFO_BAR (self->priv->info_bar),
867       GTK_MESSAGE_ERROR);
868
869   self->priv->info_bar_label = gtk_label_new ("");
870   gtk_container_add (GTK_CONTAINER (
871         gtk_info_bar_get_content_area (GTK_INFO_BAR (self->priv->info_bar))),
872       self->priv->info_bar_label);
873   gtk_widget_show (self->priv->info_bar_label);
874
875   /* prepare the account manager */
876   am = tp_account_manager_dup ();
877   tp_proxy_prepare_async (am, NULL, contact_blocking_dialog_am_prepared, self);
878   g_object_unref (am);
879
880   g_free (filename);
881   g_object_unref (gui);
882 }
883
884 GtkWidget *
885 empathy_contact_blocking_dialog_new (GtkWindow *parent)
886 {
887   GtkWidget *self = g_object_new (EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
888       NULL);
889
890   if (parent != NULL)
891     {
892       gtk_window_set_transient_for (GTK_WINDOW (self), parent);
893     }
894
895   return self;
896 }