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