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 */
76 get_pretty_conn_name (TpConnection *conn)
78 return tp_proxy_get_object_path (conn) + strlen (TP_CONN_OBJECT_PATH_BASE);
82 contact_blocking_dialog_filter_account_chooser (TpAccount *account,
83 EmpathyAccountChooserFilterResultCallback callback,
84 gpointer callback_data,
87 EmpathyContactBlockingDialog *self = user_data;
88 TpConnection *conn = tp_account_get_connection (account);
93 g_hash_table_lookup (self->priv->channels, conn) != NULL;
95 callback (enable, callback_data);
98 static void contact_blocking_dialog_account_changed (GtkWidget *,
99 EmpathyContactBlockingDialog *);
102 contact_blocking_dialog_refilter_account_chooser (
103 EmpathyContactBlockingDialog *self)
105 EmpathyAccountChooser *chooser =
106 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser);
110 DEBUG ("Refiltering account chooser");
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--;
118 conn = empathy_account_chooser_get_connection (chooser);
119 enabled = (empathy_account_chooser_get_account (chooser) != NULL &&
121 g_hash_table_lookup (self->priv->channels, conn) != NULL);
124 DEBUG ("No account selected");
126 gtk_widget_set_sensitive (self->priv->add_button, enabled);
127 gtk_widget_set_sensitive (self->priv->add_contact_entry, enabled);
129 contact_blocking_dialog_account_changed (self->priv->account_chooser, self);
132 static void contact_blocking_dialog_inspected_handles (TpConnection *,
133 const char **, const GError *, gpointer, GObject *);
136 contact_blocking_dialog_add_contacts_to_list (
137 EmpathyContactBlockingDialog *self,
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));
150 contact_blocking_dialog_inspected_handles (TpConnection *conn,
151 const char **identifiers,
152 const GError *in_error,
156 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
157 GArray *handles = user_data;
160 if (in_error != NULL)
162 DEBUG ("Failed to inspect handles: %s", in_error->message);
166 DEBUG ("Adding %u identifiers", handles->len);
168 for (i = 0; i < handles->len; i++)
170 const char *identifier = identifiers[i];
171 TpHandle handle = g_array_index (handles, TpHandle, i);
173 gtk_list_store_insert_with_values (priv->blocked_contacts, NULL, -1,
174 COL_IDENTIFIER, identifier,
180 DECLARE_CALLBACK (contact_blocking_dialog_connection_prepared);
183 contact_blocking_dialog_connection_status_changed (TpAccount *account,
187 const char *dbus_reason,
189 EmpathyContactBlockingDialog *self)
191 TpConnection *conn = tp_account_get_connection (account);
195 case TP_CONNECTION_STATUS_DISCONNECTED:
196 DEBUG ("Connection %s invalidated", get_pretty_conn_name (conn));
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);
203 case TP_CONNECTION_STATUS_CONNECTING:
206 case TP_CONNECTION_STATUS_CONNECTED:
207 DEBUG ("Connection %s reconnected", get_pretty_conn_name (conn));
209 tp_proxy_prepare_async (conn, NULL,
210 contact_blocking_dialog_connection_prepared, self);
215 contact_blocking_dialog_deny_channel_members_changed (TpChannel *channel,
219 GArray *local_pending,
220 GArray *remote_pending,
223 EmpathyContactBlockingDialog *self)
225 TpConnection *conn = tp_channel_borrow_connection (channel);
226 GtkTreeModel *model = GTK_TREE_MODEL (self->priv->blocked_contacts);
228 TpIntset *removed_set;
231 /* we only care about changes to the selected connection */
232 /* FIXME: can we compare proxy pointers directly? */
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)))))
239 DEBUG ("deny list changed: %u added, %u removed", added->len, removed->len);
242 contact_blocking_dialog_add_contacts_to_list (self, conn, added);
244 /* remove contacts */
245 removed_set = tp_intset_from_array (removed);
247 valid = gtk_tree_model_get_iter_first (model, &iter);
252 gtk_tree_model_get (model, &iter,
256 if (tp_intset_is_member (removed_set, handle))
257 valid = gtk_list_store_remove (self->priv->blocked_contacts, &iter);
259 valid = gtk_tree_model_iter_next (model, &iter);
262 tp_intset_destroy (removed_set);
265 DECLARE_CALLBACK (contact_blocking_dialog_connection_prepared);
268 contact_blocking_dialog_am_prepared (GObject *am,
269 GAsyncResult *result,
272 EmpathyContactBlockingDialog *self = user_data;
273 GList *accounts, *ptr;
274 GError *error = NULL;
276 if (!tp_proxy_prepare_finish (am, result, &error))
278 g_critical ("Could not prepare Account Manager: %s", error->message);
279 g_error_free (error);
283 accounts = tp_account_manager_get_valid_accounts (TP_ACCOUNT_MANAGER (am));
285 for (ptr = accounts; ptr != NULL; ptr = ptr->next)
287 TpAccount *account = ptr->data;
290 tp_g_signal_connect_object (account, "status-changed",
291 G_CALLBACK (contact_blocking_dialog_connection_status_changed),
294 conn = tp_account_get_connection (TP_ACCOUNT (account));
298 tp_proxy_prepare_async (conn, NULL,
299 contact_blocking_dialog_connection_prepared, self);
303 g_list_free (accounts);
306 static void contact_blocking_dialog_got_deny_channel (TpConnection *,
307 gboolean, const char *, GHashTable *, const GError *, gpointer, GObject *);
310 contact_blocking_dialog_connection_prepared (GObject *conn,
311 GAsyncResult *result,
314 EmpathyContactBlockingDialog *self = user_data;
316 GError *error = NULL;
318 if (!tp_proxy_prepare_finish (conn, result, &error))
320 DEBUG ("Failed to prepare connection: %s", error->message);
321 g_error_free (error);
325 /* request the deny channel */
326 request = tp_asv_new (
327 TP_PROP_CHANNEL_CHANNEL_TYPE,
329 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
331 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
335 TP_PROP_CHANNEL_TARGET_ID,
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));
345 g_hash_table_destroy (request);
348 DECLARE_CALLBACK (contact_blocking_dialog_deny_channel_prepared);
351 contact_blocking_dialog_got_deny_channel (TpConnection *conn,
353 const char *channel_path,
355 const GError *in_error,
360 GError *error = NULL;
362 const GQuark features[] = {
363 TP_CHANNEL_FEATURE_CORE,
364 TP_CHANNEL_FEATURE_GROUP,
367 if (in_error != NULL)
369 DEBUG ("Failed to get 'deny' channel: %s", in_error->message);
373 channel = tp_channel_new_from_properties (conn, channel_path, props, &error);
377 DEBUG ("Failed to create channel proxy: %s", error->message);
378 g_error_free (error);
382 tp_proxy_prepare_async (channel, features,
383 contact_blocking_dialog_deny_channel_prepared, self);
387 contact_blocking_dialog_deny_channel_prepared (GObject *channel,
388 GAsyncResult *result,
391 EmpathyContactBlockingDialog *self = user_data;
393 GError *error = NULL;
395 if (!tp_proxy_prepare_finish (channel, result, &error))
397 DEBUG ("Failed to prepare channel: %s", error->message);
398 g_error_free (error);
402 conn = tp_channel_borrow_connection (TP_CHANNEL (channel));
404 DEBUG ("Channel prepared for connection %s", get_pretty_conn_name (conn));
406 g_hash_table_insert (self->priv->channels,
407 g_object_ref (conn), channel);
408 contact_blocking_dialog_refilter_account_chooser (self);
410 tp_g_signal_connect_object (channel, "group-members-changed",
411 G_CALLBACK (contact_blocking_dialog_deny_channel_members_changed),
416 contact_blocking_dialog_set_error (EmpathyContactBlockingDialog *self,
419 const char *msg = NULL;
421 if (error->domain == TP_ERRORS)
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");
434 msg = _("Could not block contact");
436 gtk_label_set_text (GTK_LABEL (self->priv->info_bar_label), msg);
437 gtk_widget_show (self->priv->info_bar);
440 static void contact_blocking_dialog_add_contact_got_handle (TpConnection *,
441 const GArray *, const GError *, gpointer, GObject *);
444 contact_blocking_dialog_add_contact (GtkWidget *widget,
445 EmpathyContactBlockingDialog *self)
447 TpConnection *conn = empathy_account_chooser_get_connection (
448 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
449 const char *identifiers[2] = { NULL, };
451 identifiers[0] = gtk_entry_get_text (
452 GTK_ENTRY (self->priv->add_contact_entry));
454 DEBUG ("Looking up handle for '%s'", identifiers[0]);
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));
461 gtk_entry_set_text (GTK_ENTRY (self->priv->add_contact_entry), "");
462 gtk_widget_hide (self->priv->info_bar);
466 contact_blocking_dialog_added_contact (TpChannel *, const GError *,
467 gpointer, GObject *);
470 contact_blocking_dialog_add_contact_got_handle (TpConnection *conn,
471 const GArray *handles,
472 const GError *in_error,
476 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
477 TpChannel *channel = g_hash_table_lookup (priv->channels, conn);
479 if (in_error != NULL)
481 DEBUG ("Error getting handle: %s", in_error->message);
483 contact_blocking_dialog_set_error (
484 EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
489 g_return_if_fail (handles->len == 1);
491 DEBUG ("Adding handle %u to deny channel",
492 g_array_index (handles, TpHandle, 0));
494 tp_cli_channel_interface_group_call_add_members (channel, -1,
496 contact_blocking_dialog_added_contact, NULL, NULL, self);
500 contact_blocking_dialog_added_contact (TpChannel *channel,
501 const GError *in_error,
505 if (in_error != NULL)
507 DEBUG ("Error adding contact to deny list: %s", in_error->message);
509 contact_blocking_dialog_set_error (
510 EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
515 DEBUG ("Contact added");
519 contact_blocking_dialog_removed_contacts (TpChannel *,
520 const GError *, gpointer, GObject *);
523 contact_blocking_dialog_remove_contacts (GtkWidget *button,
524 EmpathyContactBlockingDialog *self)
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);
531 GArray *handles = g_array_new (FALSE, FALSE, sizeof (TpHandle));
533 rows = gtk_tree_selection_get_selected_rows (self->priv->selection, &model);
535 for (ptr = rows; ptr != NULL; ptr = ptr->next)
537 GtkTreePath *path = ptr->data;
541 if (!gtk_tree_model_get_iter (model, &iter, path))
544 gtk_tree_model_get (model, &iter,
548 g_array_append_val (handles, handle);
549 gtk_tree_path_free (path);
554 if (handles->len > 0)
556 DEBUG ("Removing %u handles", handles->len);
558 tp_cli_channel_interface_group_call_remove_members (channel, -1,
560 contact_blocking_dialog_removed_contacts,
561 NULL, NULL, G_OBJECT (self));
564 g_array_unref (handles);
568 contact_blocking_dialog_removed_contacts (TpChannel *channel,
569 const GError *in_error,
573 if (in_error != NULL)
575 DEBUG ("Error removing contacts from deny list: %s", in_error->message);
577 contact_blocking_dialog_set_error (
578 EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
583 DEBUG ("Contacts removed");
587 contact_blocking_dialog_account_changed (GtkWidget *account_chooser,
588 EmpathyContactBlockingDialog *self)
590 TpConnection *conn = empathy_account_chooser_get_connection (
591 EMPATHY_ACCOUNT_CHOOSER (account_chooser));
594 EmpathyContactManager *contact_manager;
595 EmpathyTpContactList *contact_list;
596 GList *members, *ptr;
598 if (self->priv->block_account_changed > 0)
601 /* clear the lists of contacts */
602 gtk_list_store_clear (self->priv->blocked_contacts);
603 gtk_list_store_clear (self->priv->completion_contacts);
608 DEBUG ("Account changed: %s", get_pretty_conn_name (conn));
610 /* load the deny list */
611 channel = g_hash_table_lookup (self->priv->channels, conn);
616 g_return_if_fail (TP_IS_CHANNEL (channel));
618 blocked = tp_intset_to_array (tp_channel_group_get_members (channel));
620 DEBUG ("%u contacts on blocked list", blocked->len);
622 contact_blocking_dialog_add_contacts_to_list (self, conn, blocked);
623 g_array_unref (blocked);
625 /* load the completion list */
626 g_return_if_fail (empathy_contact_manager_initialized ());
628 DEBUG ("Loading contacts");
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));
635 for (ptr = members; ptr != NULL; ptr = ptr->next)
637 EmpathyContact *contact = ptr->data;
640 tmpstr = g_strdup_printf ("%s (%s)",
641 empathy_contact_get_alias (contact),
642 empathy_contact_get_id (contact));
644 gtk_list_store_insert_with_values (self->priv->completion_contacts,
646 COL_IDENTIFIER, empathy_contact_get_id (contact),
651 g_object_unref (contact);
654 g_list_free (members);
655 g_object_unref (contact_manager);
659 contact_blocking_dialog_view_selection_changed (GtkTreeSelection *selection,
660 EmpathyContactBlockingDialog *self)
662 GList *rows = gtk_tree_selection_get_selected_rows (selection, NULL);
664 /* update the sensitivity of the remove button */
665 gtk_widget_set_sensitive (self->priv->remove_button, rows != NULL);
667 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
672 contact_selector_dialog_match_func (GtkEntryCompletion *completion,
681 model = gtk_entry_completion_get_model (completion);
682 if (model == NULL || iter == NULL)
685 gtk_tree_model_get (model, iter, COL_TEXT, &str, -1);
686 lower = g_utf8_strdown (str, -1);
687 if (strstr (lower, key))
689 DEBUG ("Key %s is matching name **%s**", key, str);
696 gtk_tree_model_get (model, iter, COL_IDENTIFIER, &str, -1);
697 lower = g_utf8_strdown (str, -1);
698 if (strstr (lower, key))
700 DEBUG ("Key %s is matching ID **%s**", key, str);
713 contact_selector_dialog_match_selected_cb (GtkEntryCompletion *widget,
716 EmpathyContactBlockingDialog *self)
720 if (iter == NULL || model == NULL)
723 gtk_tree_model_get (model, iter, COL_IDENTIFIER, &id, -1);
724 gtk_entry_set_text (GTK_ENTRY (self->priv->add_contact_entry), id);
726 DEBUG ("Got selected match **%s**", id);
734 contact_blocking_dialog_dispose (GObject *self)
736 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
738 tp_clear_pointer (&priv->channels, g_hash_table_destroy);
740 G_OBJECT_CLASS (empathy_contact_blocking_dialog_parent_class)->dispose (self);
744 empathy_contact_blocking_dialog_class_init (
745 EmpathyContactBlockingDialogClass *klass)
747 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
749 gobject_class->dispose = contact_blocking_dialog_dispose;
751 g_type_class_add_private (gobject_class,
752 sizeof (EmpathyContactBlockingDialogPrivate));
756 empathy_contact_blocking_dialog_init (EmpathyContactBlockingDialog *self)
761 GtkWidget *account_hbox, *blocked_contacts_view;
762 GtkEntryCompletion *completion;
763 TpAccountManager *am;
765 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
766 EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
767 EmpathyContactBlockingDialogPrivate);
769 self->priv->channels = g_hash_table_new_full (NULL, NULL,
770 g_object_unref, g_object_unref);
772 gtk_window_set_title (GTK_WINDOW (self), _("Edit Blocked Contacts"));
773 gtk_dialog_add_button (GTK_DIALOG (self),
774 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
776 filename = empathy_file_lookup ("empathy-contact-blocking-dialog.ui",
779 gui = empathy_builder_get_file (filename,
780 "contents", &contents,
781 "account-hbox", &account_hbox,
782 "add-button", &self->priv->add_button,
783 "add-contact-entry", &self->priv->add_contact_entry,
784 "blocked-contacts", &self->priv->blocked_contacts,
785 "blocked-contacts-view", &blocked_contacts_view,
786 "remove-button", &self->priv->remove_button,
789 empathy_builder_connect (gui, self,
790 "add-button", "clicked", contact_blocking_dialog_add_contact,
791 "add-contact-entry", "activate", contact_blocking_dialog_add_contact,
792 "remove-button", "clicked", contact_blocking_dialog_remove_contacts,
795 /* add the contents to the dialog */
797 GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (self))),
799 gtk_widget_show (contents);
801 /* set up the tree selection */
802 self->priv->selection = gtk_tree_view_get_selection (
803 GTK_TREE_VIEW (blocked_contacts_view));
804 gtk_tree_selection_set_mode (self->priv->selection, GTK_SELECTION_MULTIPLE);
805 g_signal_connect (self->priv->selection, "changed",
806 G_CALLBACK (contact_blocking_dialog_view_selection_changed), self);
808 /* build the contact entry */
809 self->priv->completion_contacts = gtk_list_store_new (N_COLUMNS,
810 G_TYPE_STRING, /* id */
811 G_TYPE_UINT, /* handle */
812 G_TYPE_STRING); /* text */
813 completion = gtk_entry_completion_new ();
814 gtk_entry_completion_set_model (completion,
815 GTK_TREE_MODEL (self->priv->completion_contacts));
816 gtk_entry_completion_set_text_column (completion, COL_TEXT);
817 gtk_entry_completion_set_match_func (completion,
818 contact_selector_dialog_match_func,
820 g_signal_connect (completion, "match-selected",
821 G_CALLBACK (contact_selector_dialog_match_selected_cb),
823 gtk_entry_set_completion (GTK_ENTRY (self->priv->add_contact_entry),
825 g_object_unref (completion);
826 g_object_unref (self->priv->completion_contacts);
828 /* add the account chooser */
829 self->priv->account_chooser = empathy_account_chooser_new ();
830 contact_blocking_dialog_refilter_account_chooser (self);
831 g_signal_connect (self->priv->account_chooser, "changed",
832 G_CALLBACK (contact_blocking_dialog_account_changed), self);
834 gtk_box_pack_start (GTK_BOX (account_hbox), self->priv->account_chooser,
836 gtk_widget_show (self->priv->account_chooser);
838 /* add an error warning info bar */
839 self->priv->info_bar = gtk_info_bar_new ();
840 gtk_box_pack_start (GTK_BOX (contents), self->priv->info_bar, FALSE, TRUE, 0);
841 gtk_info_bar_set_message_type (GTK_INFO_BAR (self->priv->info_bar),
844 self->priv->info_bar_label = gtk_label_new ("");
845 gtk_container_add (GTK_CONTAINER (
846 gtk_info_bar_get_content_area (GTK_INFO_BAR (self->priv->info_bar))),
847 self->priv->info_bar_label);
848 gtk_widget_show (self->priv->info_bar_label);
850 /* prepare the account manager */
851 am = tp_account_manager_dup ();
852 tp_proxy_prepare_async (am, NULL, contact_blocking_dialog_am_prepared, self);
856 g_object_unref (gui);
860 empathy_contact_blocking_dialog_new (GtkWindow *parent)
862 GtkWidget *self = g_object_new (EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
867 gtk_window_set_transient_for (GTK_WINDOW (self), parent);