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