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>
26 #include <glib/gi18n-lib.h>
28 #include <libempathy/empathy-utils.h>
30 #include <libempathy/empathy-contact-manager.h>
31 #include <libempathy/empathy-tp-contact-list.h>
33 #include <libempathy-gtk/empathy-account-chooser.h>
34 #include <libempathy-gtk/empathy-ui-utils.h>
36 #include "empathy-contact-blocking-dialog.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
39 #include <libempathy/empathy-debug.h>
41 #define GET_PRIVATE(o) (EMPATHY_CONTACT_BLOCKING_DIALOG (o)->priv)
42 #define DECLARE_CALLBACK(func) \
43 static void func (GObject *, GAsyncResult *, gpointer);
45 G_DEFINE_TYPE (EmpathyContactBlockingDialog, empathy_contact_blocking_dialog,
48 struct _EmpathyContactBlockingDialogPrivate
50 /* a map of all active connections to their 'deny' channel */
51 GHashTable *channels; /* reffed TpConnection* -> reffed TpChannel* */
53 guint block_account_changed;
55 GtkListStore *blocked_contacts;
56 GtkListStore *completion_contacts;
57 GtkTreeSelection *selection;
59 GtkWidget *account_chooser;
60 GtkWidget *add_button;
61 GtkWidget *add_contact_entry;
63 GtkWidget *info_bar_label;
64 GtkWidget *remove_button;
67 enum /* blocked-contacts columns */
75 get_pretty_conn_name (TpConnection *conn)
77 return tp_proxy_get_object_path (conn) + strlen (TP_CONN_OBJECT_PATH_BASE);
81 contact_blocking_dialog_filter_account_chooser (TpAccount *account,
82 EmpathyAccountChooserFilterResultCallback callback,
83 gpointer callback_data,
86 EmpathyContactBlockingDialog *self = user_data;
87 TpConnection *conn = tp_account_get_connection (account);
92 g_hash_table_lookup (self->priv->channels, conn) != NULL;
94 callback (enable, callback_data);
97 static void contact_blocking_dialog_account_changed (GtkWidget *,
98 EmpathyContactBlockingDialog *);
101 contact_blocking_dialog_refilter_account_chooser (
102 EmpathyContactBlockingDialog *self)
104 EmpathyAccountChooser *chooser =
105 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser);
109 DEBUG ("Refiltering account chooser");
111 /* set the filter to refilter the account chooser */
112 self->priv->block_account_changed++;
113 empathy_account_chooser_set_filter (chooser,
114 contact_blocking_dialog_filter_account_chooser, self);
115 self->priv->block_account_changed--;
117 conn = empathy_account_chooser_get_connection (chooser);
118 enabled = (empathy_account_chooser_get_account (chooser) != NULL &&
120 g_hash_table_lookup (self->priv->channels, conn) != NULL);
123 DEBUG ("No account selected");
125 gtk_widget_set_sensitive (self->priv->add_button, enabled);
126 gtk_widget_set_sensitive (self->priv->add_contact_entry, enabled);
128 contact_blocking_dialog_account_changed (self->priv->account_chooser, self);
131 static void contact_blocking_dialog_inspected_handles (TpConnection *,
132 const char **, const GError *, gpointer, GObject *);
135 contact_blocking_dialog_add_contacts_to_list (
136 EmpathyContactBlockingDialog *self,
140 if (handles->len > 0)
141 tp_cli_connection_call_inspect_handles (conn, -1,
142 TP_HANDLE_TYPE_CONTACT, handles,
143 contact_blocking_dialog_inspected_handles,
144 g_boxed_copy (DBUS_TYPE_G_UINT_ARRAY, handles),
145 (GDestroyNotify) g_array_unref, G_OBJECT (self));
149 contact_blocking_dialog_inspected_handles (TpConnection *conn,
150 const char **identifiers,
151 const GError *in_error,
155 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
156 GArray *handles = user_data;
159 if (in_error != NULL)
161 DEBUG ("Failed to inspect handles: %s", in_error->message);
165 DEBUG ("Adding %u identifiers", handles->len);
167 for (i = 0; i < handles->len; i++)
169 const char *identifier = identifiers[i];
170 TpHandle handle = g_array_index (handles, TpHandle, i);
172 gtk_list_store_insert_with_values (priv->blocked_contacts, NULL, -1,
173 COL_IDENTIFIER, identifier,
179 DECLARE_CALLBACK (contact_blocking_dialog_connection_prepared);
182 contact_blocking_dialog_connection_status_changed (TpAccount *account,
186 const char *dbus_reason,
188 EmpathyContactBlockingDialog *self)
190 TpConnection *conn = tp_account_get_connection (account);
194 case TP_CONNECTION_STATUS_DISCONNECTED:
195 DEBUG ("Connection %s invalidated", get_pretty_conn_name (conn));
197 /* remove the channel from the hash table */
198 g_hash_table_remove (self->priv->channels, conn);
199 contact_blocking_dialog_refilter_account_chooser (self);
202 case TP_CONNECTION_STATUS_CONNECTING:
205 case TP_CONNECTION_STATUS_CONNECTED:
206 DEBUG ("Connection %s reconnected", get_pretty_conn_name (conn));
208 tp_proxy_prepare_async (conn, NULL,
209 contact_blocking_dialog_connection_prepared, self);
214 contact_blocking_dialog_deny_channel_members_changed (TpChannel *channel,
218 GArray *local_pending,
219 GArray *remote_pending,
222 EmpathyContactBlockingDialog *self)
224 TpConnection *conn = tp_channel_borrow_connection (channel);
225 GtkTreeModel *model = GTK_TREE_MODEL (self->priv->blocked_contacts);
227 TpIntset *removed_set;
230 /* we only care about changes to the selected connection */
231 /* FIXME: can we compare proxy pointers directly? */
233 tp_proxy_get_object_path (tp_channel_borrow_connection (channel)),
234 tp_proxy_get_object_path (empathy_account_chooser_get_connection (
235 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser)))))
238 DEBUG ("deny list changed: %u added, %u removed", added->len, removed->len);
241 contact_blocking_dialog_add_contacts_to_list (self, conn, added);
243 /* remove contacts */
244 removed_set = tp_intset_from_array (removed);
246 valid = gtk_tree_model_get_iter_first (model, &iter);
251 gtk_tree_model_get (model, &iter,
255 if (tp_intset_is_member (removed_set, handle))
256 valid = gtk_list_store_remove (self->priv->blocked_contacts, &iter);
258 valid = gtk_tree_model_iter_next (model, &iter);
261 tp_intset_destroy (removed_set);
264 DECLARE_CALLBACK (contact_blocking_dialog_connection_prepared);
267 contact_blocking_dialog_am_prepared (GObject *am,
268 GAsyncResult *result,
271 EmpathyContactBlockingDialog *self = user_data;
272 GList *accounts, *ptr;
273 GError *error = NULL;
275 if (!tp_proxy_prepare_finish (am, result, &error))
277 g_critical ("Could not prepare Account Manager: %s", error->message);
278 g_error_free (error);
282 accounts = tp_account_manager_get_valid_accounts (TP_ACCOUNT_MANAGER (am));
284 for (ptr = accounts; ptr != NULL; ptr = ptr->next)
286 TpAccount *account = ptr->data;
289 tp_g_signal_connect_object (account, "status-changed",
290 G_CALLBACK (contact_blocking_dialog_connection_status_changed),
293 conn = tp_account_get_connection (TP_ACCOUNT (account));
297 tp_proxy_prepare_async (conn, NULL,
298 contact_blocking_dialog_connection_prepared, self);
302 g_list_free (accounts);
305 static void contact_blocking_dialog_got_deny_channel (TpConnection *,
306 gboolean, const char *, GHashTable *, const GError *, gpointer, GObject *);
309 contact_blocking_dialog_connection_prepared (GObject *conn,
310 GAsyncResult *result,
313 EmpathyContactBlockingDialog *self = user_data;
315 GError *error = NULL;
317 if (!tp_proxy_prepare_finish (conn, result, &error))
319 DEBUG ("Failed to prepare connection: %s", error->message);
320 g_error_free (error);
324 /* request the deny channel */
325 request = tp_asv_new (
326 TP_PROP_CHANNEL_CHANNEL_TYPE,
328 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
330 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
334 TP_PROP_CHANNEL_TARGET_ID,
340 tp_cli_connection_interface_requests_call_ensure_channel (
341 TP_CONNECTION (conn), -1, request,
342 contact_blocking_dialog_got_deny_channel, NULL, NULL, G_OBJECT (self));
344 g_hash_table_destroy (request);
347 DECLARE_CALLBACK (contact_blocking_dialog_deny_channel_prepared);
350 contact_blocking_dialog_got_deny_channel (TpConnection *conn,
352 const char *channel_path,
354 const GError *in_error,
359 GError *error = NULL;
361 const GQuark features[] = {
362 TP_CHANNEL_FEATURE_CORE,
363 TP_CHANNEL_FEATURE_GROUP,
366 if (in_error != NULL)
368 DEBUG ("Failed to get 'deny' channel: %s", in_error->message);
372 channel = tp_channel_new_from_properties (conn, channel_path, props, &error);
376 DEBUG ("Failed to create channel proxy: %s", error->message);
377 g_error_free (error);
381 tp_proxy_prepare_async (channel, features,
382 contact_blocking_dialog_deny_channel_prepared, self);
386 contact_blocking_dialog_deny_channel_prepared (GObject *channel,
387 GAsyncResult *result,
390 EmpathyContactBlockingDialog *self = user_data;
392 GError *error = NULL;
394 if (!tp_proxy_prepare_finish (channel, result, &error))
396 DEBUG ("Failed to prepare channel: %s", error->message);
397 g_error_free (error);
401 conn = tp_channel_borrow_connection (TP_CHANNEL (channel));
403 DEBUG ("Channel prepared for connection %s", get_pretty_conn_name (conn));
405 g_hash_table_insert (self->priv->channels,
406 g_object_ref (conn), channel);
407 contact_blocking_dialog_refilter_account_chooser (self);
409 tp_g_signal_connect_object (channel, "group-members-changed",
410 G_CALLBACK (contact_blocking_dialog_deny_channel_members_changed),
415 contact_blocking_dialog_set_error (EmpathyContactBlockingDialog *self,
418 const char *msg = NULL;
420 if (error->domain == TP_ERRORS)
422 if (error->code == TP_ERROR_INVALID_HANDLE)
423 msg = _("Unknown or invalid identifier");
424 else if (error->code == TP_ERROR_NOT_AVAILABLE)
425 msg = _("Contact blocking temporarily unavailable");
426 else if (error->code == TP_ERROR_NOT_CAPABLE)
427 msg = _("Contact blocking unavailable");
428 else if (error->code == TP_ERROR_PERMISSION_DENIED)
429 msg = _("Permission Denied");
433 msg = _("Could not block contact");
435 gtk_label_set_text (GTK_LABEL (self->priv->info_bar_label), msg);
436 gtk_widget_show (self->priv->info_bar);
439 static void contact_blocking_dialog_add_contact_got_handle (TpConnection *,
440 const GArray *, const GError *, gpointer, GObject *);
443 contact_blocking_dialog_add_contact (GtkWidget *widget,
444 EmpathyContactBlockingDialog *self)
446 TpConnection *conn = empathy_account_chooser_get_connection (
447 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
448 const char *identifiers[2] = { NULL, };
450 identifiers[0] = gtk_entry_get_text (
451 GTK_ENTRY (self->priv->add_contact_entry));
453 DEBUG ("Looking up handle for '%s'", identifiers[0]);
455 tp_cli_connection_call_request_handles (conn, -1,
456 TP_HANDLE_TYPE_CONTACT, identifiers,
457 contact_blocking_dialog_add_contact_got_handle,
458 NULL, NULL, G_OBJECT (self));
460 gtk_entry_set_text (GTK_ENTRY (self->priv->add_contact_entry), "");
461 gtk_widget_hide (self->priv->info_bar);
465 contact_blocking_dialog_added_contact (TpChannel *, const GError *,
466 gpointer, GObject *);
469 contact_blocking_dialog_add_contact_got_handle (TpConnection *conn,
470 const GArray *handles,
471 const GError *in_error,
475 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
476 TpChannel *channel = g_hash_table_lookup (priv->channels, conn);
478 if (in_error != NULL)
480 DEBUG ("Error getting handle: %s", in_error->message);
482 contact_blocking_dialog_set_error (
483 EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
488 g_return_if_fail (handles->len == 1);
490 DEBUG ("Adding handle %u to deny channel",
491 g_array_index (handles, TpHandle, 0));
493 tp_cli_channel_interface_group_call_add_members (channel, -1,
495 contact_blocking_dialog_added_contact, NULL, NULL, self);
499 contact_blocking_dialog_added_contact (TpChannel *channel,
500 const GError *in_error,
504 if (in_error != NULL)
506 DEBUG ("Error adding contact to deny list: %s", in_error->message);
508 contact_blocking_dialog_set_error (
509 EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
514 DEBUG ("Contact added");
518 contact_blocking_dialog_removed_contacts (TpChannel *,
519 const GError *, gpointer, GObject *);
522 contact_blocking_dialog_remove_contacts (GtkWidget *button,
523 EmpathyContactBlockingDialog *self)
525 TpConnection *conn = empathy_account_chooser_get_connection (
526 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
527 TpChannel *channel = g_hash_table_lookup (self->priv->channels, conn);
530 GArray *handles = g_array_new (FALSE, FALSE, sizeof (TpHandle));
532 rows = gtk_tree_selection_get_selected_rows (self->priv->selection, &model);
534 for (ptr = rows; ptr != NULL; ptr = ptr->next)
536 GtkTreePath *path = ptr->data;
540 if (!gtk_tree_model_get_iter (model, &iter, path))
543 gtk_tree_model_get (model, &iter,
547 g_array_append_val (handles, handle);
548 gtk_tree_path_free (path);
553 if (handles->len > 0)
555 DEBUG ("Removing %u handles", handles->len);
557 tp_cli_channel_interface_group_call_remove_members (channel, -1,
559 contact_blocking_dialog_removed_contacts,
560 NULL, NULL, G_OBJECT (self));
563 g_array_unref (handles);
567 contact_blocking_dialog_removed_contacts (TpChannel *channel,
568 const GError *in_error,
572 if (in_error != NULL)
574 DEBUG ("Error removing contacts from deny list: %s", in_error->message);
576 contact_blocking_dialog_set_error (
577 EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
582 DEBUG ("Contacts removed");
586 contact_blocking_dialog_account_changed (GtkWidget *account_chooser,
587 EmpathyContactBlockingDialog *self)
589 TpConnection *conn = empathy_account_chooser_get_connection (
590 EMPATHY_ACCOUNT_CHOOSER (account_chooser));
593 EmpathyContactManager *contact_manager;
594 EmpathyTpContactList *contact_list;
595 GList *members, *ptr;
597 if (self->priv->block_account_changed > 0)
600 /* clear the lists of contacts */
601 gtk_list_store_clear (self->priv->blocked_contacts);
602 gtk_list_store_clear (self->priv->completion_contacts);
607 DEBUG ("Account changed: %s", get_pretty_conn_name (conn));
609 /* load the deny list */
610 channel = g_hash_table_lookup (self->priv->channels, conn);
615 g_return_if_fail (TP_IS_CHANNEL (channel));
617 blocked = tp_intset_to_array (tp_channel_group_get_members (channel));
619 DEBUG ("%u contacts on blocked list", blocked->len);
621 contact_blocking_dialog_add_contacts_to_list (self, conn, blocked);
622 g_array_unref (blocked);
624 /* load the completion list */
625 g_return_if_fail (empathy_contact_manager_initialized ());
627 DEBUG ("Loading contacts");
629 contact_manager = empathy_contact_manager_dup_singleton ();
630 contact_list = empathy_contact_manager_get_list (contact_manager, conn);
631 members = empathy_contact_list_get_members (
632 EMPATHY_CONTACT_LIST (contact_list));
634 for (ptr = members; ptr != NULL; ptr = ptr->next)
636 EmpathyContact *contact = ptr->data;
638 gtk_list_store_insert_with_values (self->priv->completion_contacts,
640 COL_IDENTIFIER, empathy_contact_get_id (contact),
643 g_object_unref (contact);
646 g_list_free (members);
647 g_object_unref (contact_manager);
651 contact_blocking_dialog_view_selection_changed (GtkTreeSelection *selection,
652 EmpathyContactBlockingDialog *self)
654 GList *rows = gtk_tree_selection_get_selected_rows (selection, NULL);
656 /* update the sensitivity of the remove button */
657 gtk_widget_set_sensitive (self->priv->remove_button, rows != NULL);
659 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
664 contact_blocking_dialog_dispose (GObject *self)
666 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
668 tp_clear_pointer (&priv->channels, g_hash_table_destroy);
670 G_OBJECT_CLASS (empathy_contact_blocking_dialog_parent_class)->dispose (self);
674 empathy_contact_blocking_dialog_class_init (
675 EmpathyContactBlockingDialogClass *klass)
677 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
679 gobject_class->dispose = contact_blocking_dialog_dispose;
681 g_type_class_add_private (gobject_class,
682 sizeof (EmpathyContactBlockingDialogPrivate));
686 empathy_contact_blocking_dialog_init (EmpathyContactBlockingDialog *self)
691 GtkWidget *account_hbox, *blocked_contacts_view;
692 GtkEntryCompletion *completion;
693 TpAccountManager *am;
695 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
696 EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
697 EmpathyContactBlockingDialogPrivate);
699 self->priv->channels = g_hash_table_new_full (NULL, NULL,
700 g_object_unref, g_object_unref);
702 gtk_window_set_title (GTK_WINDOW (self), _("Edit Blocked Contacts"));
703 gtk_dialog_add_button (GTK_DIALOG (self),
704 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
706 filename = empathy_file_lookup ("empathy-contact-blocking-dialog.ui",
709 gui = empathy_builder_get_file (filename,
710 "contents", &contents,
711 "account-hbox", &account_hbox,
712 "add-button", &self->priv->add_button,
713 "add-contact-entry", &self->priv->add_contact_entry,
714 "blocked-contacts", &self->priv->blocked_contacts,
715 "blocked-contacts-view", &blocked_contacts_view,
716 "remove-button", &self->priv->remove_button,
719 empathy_builder_connect (gui, self,
720 "add-button", "clicked", contact_blocking_dialog_add_contact,
721 "add-contact-entry", "activate", contact_blocking_dialog_add_contact,
722 "remove-button", "clicked", contact_blocking_dialog_remove_contacts,
725 /* add the contents to the dialog */
727 GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (self))),
729 gtk_widget_show (contents);
731 /* set up the tree selection */
732 self->priv->selection = gtk_tree_view_get_selection (
733 GTK_TREE_VIEW (blocked_contacts_view));
734 gtk_tree_selection_set_mode (self->priv->selection, GTK_SELECTION_MULTIPLE);
735 g_signal_connect (self->priv->selection, "changed",
736 G_CALLBACK (contact_blocking_dialog_view_selection_changed), self);
738 /* build the contact entry */
739 self->priv->completion_contacts = gtk_list_store_new (1, G_TYPE_STRING);
740 completion = gtk_entry_completion_new ();
741 gtk_entry_completion_set_model (completion,
742 GTK_TREE_MODEL (self->priv->completion_contacts));
743 gtk_entry_completion_set_text_column (completion, COL_IDENTIFIER);
744 gtk_entry_set_completion (GTK_ENTRY (self->priv->add_contact_entry),
746 g_object_unref (completion);
747 g_object_unref (self->priv->completion_contacts);
749 /* add the account chooser */
750 self->priv->account_chooser = empathy_account_chooser_new ();
751 contact_blocking_dialog_refilter_account_chooser (self);
752 g_signal_connect (self->priv->account_chooser, "changed",
753 G_CALLBACK (contact_blocking_dialog_account_changed), self);
755 gtk_box_pack_start (GTK_BOX (account_hbox), self->priv->account_chooser,
757 gtk_widget_show (self->priv->account_chooser);
759 /* add an error warning info bar */
760 self->priv->info_bar = gtk_info_bar_new ();
761 gtk_box_pack_start (GTK_BOX (contents), self->priv->info_bar, FALSE, TRUE, 0);
762 gtk_info_bar_set_message_type (GTK_INFO_BAR (self->priv->info_bar),
765 self->priv->info_bar_label = gtk_label_new ("");
766 gtk_container_add (GTK_CONTAINER (
767 gtk_info_bar_get_content_area (GTK_INFO_BAR (self->priv->info_bar))),
768 self->priv->info_bar_label);
769 gtk_widget_show (self->priv->info_bar_label);
771 /* prepare the account manager */
772 am = tp_account_manager_dup ();
773 tp_proxy_prepare_async (am, NULL, contact_blocking_dialog_am_prepared, self);
777 g_object_unref (gui);
781 empathy_contact_blocking_dialog_new (GtkWindow *parent)
783 GtkWidget *self = g_object_new (EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
788 gtk_window_set_transient_for (GTK_WINDOW (self), parent);