2 * empathy-contact-blocking-dialog.c
4 * EmpathyContactBlockingDialog
6 * Copyright (C) 2011 Collabora Ltd.
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.
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.
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
22 * Authors: Danielle Madeley <danielle.madeley@collabora.co.uk>
25 #include <glib/gi18n.h>
27 #include <libempathy/empathy-utils.h>
29 #include <libempathy/empathy-contact-manager.h>
30 #include <libempathy/empathy-tp-contact-list.h>
32 #include <libempathy-gtk/empathy-account-chooser.h>
33 #include <libempathy-gtk/empathy-ui-utils.h>
35 #include "empathy-contact-blocking-dialog.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include <libempathy/empathy-debug.h>
40 #define GET_PRIVATE(o) (EMPATHY_CONTACT_BLOCKING_DIALOG (o)->priv)
41 #define DECLARE_CALLBACK(func) \
42 static void func (GObject *, GAsyncResult *, gpointer);
44 G_DEFINE_TYPE (EmpathyContactBlockingDialog, empathy_contact_blocking_dialog,
47 struct _EmpathyContactBlockingDialogPrivate
49 /* a map of all active connections to their 'deny' channel */
50 GHashTable *channels; /* reffed TpConnection* -> reffed TpChannel* */
52 guint block_account_changed;
54 GtkListStore *blocked_contacts;
55 GtkListStore *completion_contacts;
56 GtkTreeSelection *selection;
58 GtkWidget *account_chooser;
59 GtkWidget *add_button;
60 GtkWidget *add_contact_entry;
61 GtkWidget *remove_button;
64 enum /* blocked-contacts columns */
72 get_pretty_conn_name (TpConnection *conn)
74 return tp_proxy_get_object_path (conn) + strlen (TP_CONN_OBJECT_PATH_BASE);
78 contact_blocking_dialog_filter_account_chooser (TpAccount *account,
79 EmpathyAccountChooserFilterResultCallback callback,
80 gpointer callback_data,
83 EmpathyContactBlockingDialog *self = user_data;
84 TpConnection *conn = tp_account_get_connection (account);
89 g_hash_table_lookup (self->priv->channels, conn) != NULL;
91 callback (enable, callback_data);
94 static void contact_blocking_dialog_account_changed (GtkWidget *,
95 EmpathyContactBlockingDialog *);
98 contact_blocking_dialog_refilter_account_chooser (
99 EmpathyContactBlockingDialog *self)
101 EmpathyAccountChooser *chooser =
102 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser);
106 DEBUG ("Refiltering account chooser");
108 /* set the filter to refilter the account chooser */
109 self->priv->block_account_changed++;
110 empathy_account_chooser_set_filter (chooser,
111 contact_blocking_dialog_filter_account_chooser, self);
112 self->priv->block_account_changed--;
114 conn = empathy_account_chooser_get_connection (chooser);
115 enabled = (empathy_account_chooser_get_account (chooser) != NULL &&
117 g_hash_table_lookup (self->priv->channels, conn) != NULL);
120 DEBUG ("No account selected");
122 gtk_widget_set_sensitive (self->priv->add_button, enabled);
123 gtk_widget_set_sensitive (self->priv->add_contact_entry, enabled);
125 contact_blocking_dialog_account_changed (self->priv->account_chooser, self);
128 static void contact_blocking_dialog_inspected_handles (TpConnection *,
129 const char **, const GError *, gpointer, GObject *);
132 contact_blocking_dialog_add_contacts_to_list (
133 EmpathyContactBlockingDialog *self,
137 if (handles->len > 0)
138 tp_cli_connection_call_inspect_handles (conn, -1,
139 TP_HANDLE_TYPE_CONTACT, handles,
140 contact_blocking_dialog_inspected_handles,
141 g_boxed_copy (DBUS_TYPE_G_UINT_ARRAY, handles),
142 (GDestroyNotify) g_array_unref, G_OBJECT (self));
146 contact_blocking_dialog_inspected_handles (TpConnection *conn,
147 const char **identifiers,
148 const GError *in_error,
152 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
153 GArray *handles = user_data;
156 if (in_error != NULL)
158 DEBUG ("Failed to inspect handles: %s", in_error->message);
162 DEBUG ("Adding %u identifiers", handles->len);
164 for (i = 0; i < handles->len; i++)
166 const char *identifier = identifiers[i];
167 TpHandle handle = g_array_index (handles, TpHandle, i);
169 gtk_list_store_insert_with_values (priv->blocked_contacts, NULL, -1,
170 COL_IDENTIFIER, identifier,
176 DECLARE_CALLBACK (contact_blocking_dialog_connection_prepared);
179 contact_blocking_dialog_connection_status_changed (TpAccount *account,
183 const char *dbus_reason,
185 EmpathyContactBlockingDialog *self)
187 TpConnection *conn = tp_account_get_connection (account);
191 case TP_CONNECTION_STATUS_DISCONNECTED:
192 DEBUG ("Connection %s invalidated", get_pretty_conn_name (conn));
194 /* remove the channel from the hash table */
195 g_hash_table_remove (self->priv->channels, conn);
196 contact_blocking_dialog_refilter_account_chooser (self);
199 case TP_CONNECTION_STATUS_CONNECTING:
202 case TP_CONNECTION_STATUS_CONNECTED:
203 DEBUG ("Connection %s reconnected", get_pretty_conn_name (conn));
205 tp_proxy_prepare_async (conn, NULL,
206 contact_blocking_dialog_connection_prepared, self);
211 contact_blocking_dialog_deny_channel_members_changed (TpChannel *channel,
215 GArray *local_pending,
216 GArray *remote_pending,
219 EmpathyContactBlockingDialog *self)
221 TpConnection *conn = tp_channel_borrow_connection (channel);
222 GtkTreeModel *model = GTK_TREE_MODEL (self->priv->blocked_contacts);
224 TpIntset *removed_set;
227 /* we only care about changes to the selected connection */
228 /* FIXME: can we compare proxy pointers directly? */
230 tp_proxy_get_object_path (tp_channel_borrow_connection (channel)),
231 tp_proxy_get_object_path (empathy_account_chooser_get_connection (
232 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser)))))
235 DEBUG ("deny list changed: %u added, %u removed", added->len, removed->len);
238 contact_blocking_dialog_add_contacts_to_list (self, conn, added);
240 /* remove contacts */
241 removed_set = tp_intset_from_array (removed);
243 valid = gtk_tree_model_get_iter_first (model, &iter);
248 gtk_tree_model_get (model, &iter,
252 if (tp_intset_is_member (removed_set, handle))
253 valid = gtk_list_store_remove (self->priv->blocked_contacts, &iter);
255 valid = gtk_tree_model_iter_next (model, &iter);
258 tp_intset_destroy (removed_set);
261 DECLARE_CALLBACK (contact_blocking_dialog_connection_prepared);
264 contact_blocking_dialog_am_prepared (GObject *am,
265 GAsyncResult *result,
268 EmpathyContactBlockingDialog *self = user_data;
269 GList *accounts, *ptr;
270 GError *error = NULL;
272 if (!tp_proxy_prepare_finish (am, result, &error))
274 g_critical ("Could not prepare Account Manager: %s", error->message);
275 g_error_free (error);
279 accounts = tp_account_manager_get_valid_accounts (TP_ACCOUNT_MANAGER (am));
281 for (ptr = accounts; ptr != NULL; ptr = ptr->next)
283 TpAccount *account = ptr->data;
286 g_signal_connect (account, "status-changed",
287 G_CALLBACK (contact_blocking_dialog_connection_status_changed), self);
289 conn = tp_account_get_connection (TP_ACCOUNT (account));
293 tp_proxy_prepare_async (conn, NULL,
294 contact_blocking_dialog_connection_prepared, self);
298 g_list_free (accounts);
301 static void contact_blocking_dialog_got_deny_channel (TpConnection *,
302 gboolean, const char *, GHashTable *, const GError *, gpointer, GObject *);
305 contact_blocking_dialog_connection_prepared (GObject *conn,
306 GAsyncResult *result,
309 EmpathyContactBlockingDialog *self = user_data;
311 GError *error = NULL;
313 if (!tp_proxy_prepare_finish (conn, result, &error))
315 DEBUG ("Failed to prepare connection: %s", error->message);
316 g_error_free (error);
320 /* request the deny channel */
321 request = tp_asv_new (
322 TP_PROP_CHANNEL_CHANNEL_TYPE,
324 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
326 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
330 TP_PROP_CHANNEL_TARGET_ID,
336 tp_cli_connection_interface_requests_call_ensure_channel (
337 TP_CONNECTION (conn), -1, request,
338 contact_blocking_dialog_got_deny_channel, NULL, NULL, G_OBJECT (self));
340 g_hash_table_destroy (request);
343 DECLARE_CALLBACK (contact_blocking_dialog_deny_channel_prepared);
346 contact_blocking_dialog_got_deny_channel (TpConnection *conn,
348 const char *channel_path,
350 const GError *in_error,
355 GError *error = NULL;
357 const GQuark features[] = {
358 TP_CHANNEL_FEATURE_CORE,
359 TP_CHANNEL_FEATURE_GROUP,
362 if (in_error != NULL)
364 DEBUG ("Failed to get 'deny' channel: %s", in_error->message);
368 channel = tp_channel_new_from_properties (conn, channel_path, props, &error);
372 DEBUG ("Failed to create channel proxy: %s", error->message);
373 g_error_free (error);
377 tp_proxy_prepare_async (channel, features,
378 contact_blocking_dialog_deny_channel_prepared, self);
382 contact_blocking_dialog_deny_channel_prepared (GObject *channel,
383 GAsyncResult *result,
386 EmpathyContactBlockingDialog *self = user_data;
388 GError *error = NULL;
390 if (!tp_proxy_prepare_finish (channel, result, &error))
392 DEBUG ("Failed to prepare channel: %s", error->message);
393 g_error_free (error);
397 conn = tp_channel_borrow_connection (TP_CHANNEL (channel));
399 DEBUG ("Channel prepared for connection %s", get_pretty_conn_name (conn));
401 g_hash_table_insert (self->priv->channels,
402 g_object_ref (conn), channel);
403 contact_blocking_dialog_refilter_account_chooser (self);
405 g_signal_connect (channel, "group-members-changed",
406 G_CALLBACK (contact_blocking_dialog_deny_channel_members_changed), self);
409 static void contact_blocking_dialog_add_contact_got_handle (TpConnection *,
410 const GArray *, const GError *, gpointer, GObject *);
413 contact_blocking_dialog_add_contact (GtkWidget *widget,
414 EmpathyContactBlockingDialog *self)
416 TpConnection *conn = empathy_account_chooser_get_connection (
417 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
418 const char *identifiers[2] = { NULL, };
420 identifiers[0] = gtk_entry_get_text (
421 GTK_ENTRY (self->priv->add_contact_entry));
423 DEBUG ("Looking up handle for '%s'", identifiers[0]);
425 tp_cli_connection_call_request_handles (conn, -1,
426 TP_HANDLE_TYPE_CONTACT, identifiers,
427 contact_blocking_dialog_add_contact_got_handle,
428 NULL, NULL, G_OBJECT (self));
430 gtk_entry_set_text (GTK_ENTRY (self->priv->add_contact_entry), "");
434 contact_blocking_dialog_added_contact (TpChannel *, const GError *,
435 gpointer, GObject *);
438 contact_blocking_dialog_add_contact_got_handle (TpConnection *conn,
439 const GArray *handles,
440 const GError *in_error,
444 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
445 TpChannel *channel = g_hash_table_lookup (priv->channels, conn);
447 if (in_error != NULL)
449 DEBUG ("Error getting handle: %s", in_error->message);
450 /* FIXME: expose error to user */
454 g_return_if_fail (handles->len == 1);
456 DEBUG ("Adding handle %u to deny channel",
457 g_array_index (handles, TpHandle, 0));
459 tp_cli_channel_interface_group_call_add_members (channel, -1,
461 contact_blocking_dialog_added_contact, NULL, NULL, self);
465 contact_blocking_dialog_added_contact (TpChannel *channel,
466 const GError *in_error,
470 if (in_error != NULL)
472 DEBUG ("Error adding contact to deny list: %s", in_error->message);
473 /* FIXME: expose error to user */
477 DEBUG ("Contact added");
481 contact_blocking_dialog_removed_contacts (TpChannel *,
482 const GError *, gpointer, GObject *);
485 contact_blocking_dialog_remove_contacts (GtkWidget *button,
486 EmpathyContactBlockingDialog *self)
488 TpConnection *conn = empathy_account_chooser_get_connection (
489 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
490 TpChannel *channel = g_hash_table_lookup (self->priv->channels, conn);
493 GArray *handles = g_array_new (FALSE, FALSE, sizeof (TpHandle));
495 rows = gtk_tree_selection_get_selected_rows (self->priv->selection, &model);
497 for (ptr = rows; ptr != NULL; ptr = ptr->next)
499 GtkTreePath *path = ptr->data;
503 if (!gtk_tree_model_get_iter (model, &iter, path))
506 gtk_tree_model_get (model, &iter,
510 g_array_append_val (handles, handle);
511 gtk_tree_path_free (path);
516 if (handles->len > 0)
518 DEBUG ("Removing %u handles", handles->len);
520 tp_cli_channel_interface_group_call_remove_members (channel, -1,
522 contact_blocking_dialog_removed_contacts,
523 NULL, NULL, G_OBJECT (self));
526 g_array_unref (handles);
530 contact_blocking_dialog_removed_contacts (TpChannel *channel,
531 const GError *in_error,
535 if (in_error != NULL)
537 DEBUG ("Error removing contacts from deny list: %s", in_error->message);
538 /* FIXME: expose error to user */
542 DEBUG ("Contacts removed");
546 contact_blocking_dialog_account_changed (GtkWidget *account_chooser,
547 EmpathyContactBlockingDialog *self)
549 TpConnection *conn = empathy_account_chooser_get_connection (
550 EMPATHY_ACCOUNT_CHOOSER (account_chooser));
553 EmpathyContactManager *contact_manager;
554 EmpathyTpContactList *contact_list;
555 GList *members, *ptr;
557 if (self->priv->block_account_changed > 0)
560 /* clear the lists of contacts */
561 gtk_list_store_clear (self->priv->blocked_contacts);
562 gtk_list_store_clear (self->priv->completion_contacts);
567 DEBUG ("Account changed: %s", get_pretty_conn_name (conn));
569 /* load the deny list */
570 channel = g_hash_table_lookup (self->priv->channels, conn);
575 g_return_if_fail (TP_IS_CHANNEL (channel));
577 blocked = tp_intset_to_array (tp_channel_group_get_members (channel));
579 DEBUG ("%u contacts on blocked list", blocked->len);
581 contact_blocking_dialog_add_contacts_to_list (self, conn, blocked);
582 g_array_unref (blocked);
584 /* load the completion list */
585 g_return_if_fail (empathy_contact_manager_initialized ());
587 DEBUG ("Loading contacts");
589 contact_manager = empathy_contact_manager_dup_singleton ();
590 contact_list = empathy_contact_manager_get_list (contact_manager, conn);
591 members = empathy_contact_list_get_members (
592 EMPATHY_CONTACT_LIST (contact_list));
594 for (ptr = members; ptr != NULL; ptr = ptr->next)
596 EmpathyContact *contact = ptr->data;
598 gtk_list_store_insert_with_values (self->priv->completion_contacts,
600 COL_IDENTIFIER, empathy_contact_get_id (contact),
603 g_object_unref (contact);
606 g_list_free (members);
607 g_object_unref (contact_manager);
611 contact_blocking_dialog_view_selection_changed (GtkTreeSelection *selection,
612 EmpathyContactBlockingDialog *self)
614 GList *rows = gtk_tree_selection_get_selected_rows (selection, NULL);
616 /* update the sensitivity of the remove button */
617 gtk_widget_set_sensitive (self->priv->remove_button, rows != NULL);
619 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
624 contact_blocking_dialog_dispose (GObject *self)
626 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
628 tp_clear_pointer (&priv->channels, g_hash_table_destroy);
630 G_OBJECT_CLASS (empathy_contact_blocking_dialog_parent_class)->dispose (self);
634 empathy_contact_blocking_dialog_class_init (
635 EmpathyContactBlockingDialogClass *klass)
637 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
639 gobject_class->dispose = contact_blocking_dialog_dispose;
641 g_type_class_add_private (gobject_class,
642 sizeof (EmpathyContactBlockingDialogPrivate));
646 empathy_contact_blocking_dialog_init (EmpathyContactBlockingDialog *self)
651 GtkWidget *account_hbox, *blocked_contacts_view;
652 GtkEntryCompletion *completion;
653 TpAccountManager *am;
655 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
656 EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
657 EmpathyContactBlockingDialogPrivate);
659 self->priv->channels = g_hash_table_new_full (NULL, NULL,
660 g_object_unref, g_object_unref);
662 gtk_window_set_title (GTK_WINDOW (self), _("Edit Blocked Contacts"));
663 gtk_dialog_add_button (GTK_DIALOG (self),
664 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
666 filename = empathy_file_lookup ("empathy-contact-blocking-dialog.ui",
669 gui = empathy_builder_get_file (filename,
670 "contents", &contents,
671 "account-hbox", &account_hbox,
672 "add-button", &self->priv->add_button,
673 "add-contact-entry", &self->priv->add_contact_entry,
674 "blocked-contacts", &self->priv->blocked_contacts,
675 "blocked-contacts-view", &blocked_contacts_view,
676 "remove-button", &self->priv->remove_button,
679 empathy_builder_connect (gui, self,
680 "add-button", "clicked", contact_blocking_dialog_add_contact,
681 "add-contact-entry", "activate", contact_blocking_dialog_add_contact,
682 "remove-button", "clicked", contact_blocking_dialog_remove_contacts,
685 /* add the contents to the dialog */
687 GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (self))),
689 gtk_widget_show (contents);
691 /* set up the tree selection */
692 self->priv->selection = gtk_tree_view_get_selection (
693 GTK_TREE_VIEW (blocked_contacts_view));
694 gtk_tree_selection_set_mode (self->priv->selection, GTK_SELECTION_MULTIPLE);
695 g_signal_connect (self->priv->selection, "changed",
696 G_CALLBACK (contact_blocking_dialog_view_selection_changed), self);
698 /* build the contact entry */
699 self->priv->completion_contacts = gtk_list_store_new (1, G_TYPE_STRING);
700 completion = gtk_entry_completion_new ();
701 gtk_entry_completion_set_model (completion,
702 GTK_TREE_MODEL (self->priv->completion_contacts));
703 gtk_entry_completion_set_text_column (completion, COL_IDENTIFIER);
704 gtk_entry_set_completion (GTK_ENTRY (self->priv->add_contact_entry),
706 g_object_unref (completion);
707 g_object_unref (self->priv->completion_contacts);
709 /* add the account chooser */
710 self->priv->account_chooser = empathy_account_chooser_new ();
711 contact_blocking_dialog_refilter_account_chooser (self);
712 g_signal_connect (self->priv->account_chooser, "changed",
713 G_CALLBACK (contact_blocking_dialog_account_changed), self);
715 gtk_box_pack_start (GTK_BOX (account_hbox), self->priv->account_chooser,
717 gtk_widget_show (self->priv->account_chooser);
719 /* prepare the account manager */
720 am = tp_account_manager_dup ();
721 tp_proxy_prepare_async (am, NULL, contact_blocking_dialog_am_prepared, self);
725 g_object_unref (gui);
729 empathy_contact_blocking_dialog_new (GtkWindow *parent)
731 GtkWidget *self = g_object_new (EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
736 gtk_window_set_transient_for (GTK_WINDOW (self), parent);