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;
62 GtkWidget *info_bar_label;
63 GtkWidget *remove_button;
66 enum /* blocked-contacts columns */
74 get_pretty_conn_name (TpConnection *conn)
76 return tp_proxy_get_object_path (conn) + strlen (TP_CONN_OBJECT_PATH_BASE);
80 contact_blocking_dialog_filter_account_chooser (TpAccount *account,
81 EmpathyAccountChooserFilterResultCallback callback,
82 gpointer callback_data,
85 EmpathyContactBlockingDialog *self = user_data;
86 TpConnection *conn = tp_account_get_connection (account);
91 g_hash_table_lookup (self->priv->channels, conn) != NULL;
93 callback (enable, callback_data);
96 static void contact_blocking_dialog_account_changed (GtkWidget *,
97 EmpathyContactBlockingDialog *);
100 contact_blocking_dialog_refilter_account_chooser (
101 EmpathyContactBlockingDialog *self)
103 EmpathyAccountChooser *chooser =
104 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser);
108 DEBUG ("Refiltering account chooser");
110 /* set the filter to refilter the account chooser */
111 self->priv->block_account_changed++;
112 empathy_account_chooser_set_filter (chooser,
113 contact_blocking_dialog_filter_account_chooser, self);
114 self->priv->block_account_changed--;
116 conn = empathy_account_chooser_get_connection (chooser);
117 enabled = (empathy_account_chooser_get_account (chooser) != NULL &&
119 g_hash_table_lookup (self->priv->channels, conn) != NULL);
122 DEBUG ("No account selected");
124 gtk_widget_set_sensitive (self->priv->add_button, enabled);
125 gtk_widget_set_sensitive (self->priv->add_contact_entry, enabled);
127 contact_blocking_dialog_account_changed (self->priv->account_chooser, self);
130 static void contact_blocking_dialog_inspected_handles (TpConnection *,
131 const char **, const GError *, gpointer, GObject *);
134 contact_blocking_dialog_add_contacts_to_list (
135 EmpathyContactBlockingDialog *self,
139 if (handles->len > 0)
140 tp_cli_connection_call_inspect_handles (conn, -1,
141 TP_HANDLE_TYPE_CONTACT, handles,
142 contact_blocking_dialog_inspected_handles,
143 g_boxed_copy (DBUS_TYPE_G_UINT_ARRAY, handles),
144 (GDestroyNotify) g_array_unref, G_OBJECT (self));
148 contact_blocking_dialog_inspected_handles (TpConnection *conn,
149 const char **identifiers,
150 const GError *in_error,
154 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
155 GArray *handles = user_data;
158 if (in_error != NULL)
160 DEBUG ("Failed to inspect handles: %s", in_error->message);
164 DEBUG ("Adding %u identifiers", handles->len);
166 for (i = 0; i < handles->len; i++)
168 const char *identifier = identifiers[i];
169 TpHandle handle = g_array_index (handles, TpHandle, i);
171 gtk_list_store_insert_with_values (priv->blocked_contacts, NULL, -1,
172 COL_IDENTIFIER, identifier,
178 DECLARE_CALLBACK (contact_blocking_dialog_connection_prepared);
181 contact_blocking_dialog_connection_status_changed (TpAccount *account,
185 const char *dbus_reason,
187 EmpathyContactBlockingDialog *self)
189 TpConnection *conn = tp_account_get_connection (account);
193 case TP_CONNECTION_STATUS_DISCONNECTED:
194 DEBUG ("Connection %s invalidated", get_pretty_conn_name (conn));
196 /* remove the channel from the hash table */
197 g_hash_table_remove (self->priv->channels, conn);
198 contact_blocking_dialog_refilter_account_chooser (self);
201 case TP_CONNECTION_STATUS_CONNECTING:
204 case TP_CONNECTION_STATUS_CONNECTED:
205 DEBUG ("Connection %s reconnected", get_pretty_conn_name (conn));
207 tp_proxy_prepare_async (conn, NULL,
208 contact_blocking_dialog_connection_prepared, self);
213 contact_blocking_dialog_deny_channel_members_changed (TpChannel *channel,
217 GArray *local_pending,
218 GArray *remote_pending,
221 EmpathyContactBlockingDialog *self)
223 TpConnection *conn = tp_channel_borrow_connection (channel);
224 GtkTreeModel *model = GTK_TREE_MODEL (self->priv->blocked_contacts);
226 TpIntset *removed_set;
229 /* we only care about changes to the selected connection */
230 /* FIXME: can we compare proxy pointers directly? */
232 tp_proxy_get_object_path (tp_channel_borrow_connection (channel)),
233 tp_proxy_get_object_path (empathy_account_chooser_get_connection (
234 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser)))))
237 DEBUG ("deny list changed: %u added, %u removed", added->len, removed->len);
240 contact_blocking_dialog_add_contacts_to_list (self, conn, added);
242 /* remove contacts */
243 removed_set = tp_intset_from_array (removed);
245 valid = gtk_tree_model_get_iter_first (model, &iter);
250 gtk_tree_model_get (model, &iter,
254 if (tp_intset_is_member (removed_set, handle))
255 valid = gtk_list_store_remove (self->priv->blocked_contacts, &iter);
257 valid = gtk_tree_model_iter_next (model, &iter);
260 tp_intset_destroy (removed_set);
263 DECLARE_CALLBACK (contact_blocking_dialog_connection_prepared);
266 contact_blocking_dialog_am_prepared (GObject *am,
267 GAsyncResult *result,
270 EmpathyContactBlockingDialog *self = user_data;
271 GList *accounts, *ptr;
272 GError *error = NULL;
274 if (!tp_proxy_prepare_finish (am, result, &error))
276 g_critical ("Could not prepare Account Manager: %s", error->message);
277 g_error_free (error);
281 accounts = tp_account_manager_get_valid_accounts (TP_ACCOUNT_MANAGER (am));
283 for (ptr = accounts; ptr != NULL; ptr = ptr->next)
285 TpAccount *account = ptr->data;
288 tp_g_signal_connect_object (account, "status-changed",
289 G_CALLBACK (contact_blocking_dialog_connection_status_changed),
292 conn = tp_account_get_connection (TP_ACCOUNT (account));
296 tp_proxy_prepare_async (conn, NULL,
297 contact_blocking_dialog_connection_prepared, self);
301 g_list_free (accounts);
304 static void contact_blocking_dialog_got_deny_channel (TpConnection *,
305 gboolean, const char *, GHashTable *, const GError *, gpointer, GObject *);
308 contact_blocking_dialog_connection_prepared (GObject *conn,
309 GAsyncResult *result,
312 EmpathyContactBlockingDialog *self = user_data;
314 GError *error = NULL;
316 if (!tp_proxy_prepare_finish (conn, result, &error))
318 DEBUG ("Failed to prepare connection: %s", error->message);
319 g_error_free (error);
323 /* request the deny channel */
324 request = tp_asv_new (
325 TP_PROP_CHANNEL_CHANNEL_TYPE,
327 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
329 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
333 TP_PROP_CHANNEL_TARGET_ID,
339 tp_cli_connection_interface_requests_call_ensure_channel (
340 TP_CONNECTION (conn), -1, request,
341 contact_blocking_dialog_got_deny_channel, NULL, NULL, G_OBJECT (self));
343 g_hash_table_destroy (request);
346 DECLARE_CALLBACK (contact_blocking_dialog_deny_channel_prepared);
349 contact_blocking_dialog_got_deny_channel (TpConnection *conn,
351 const char *channel_path,
353 const GError *in_error,
358 GError *error = NULL;
360 const GQuark features[] = {
361 TP_CHANNEL_FEATURE_CORE,
362 TP_CHANNEL_FEATURE_GROUP,
365 if (in_error != NULL)
367 DEBUG ("Failed to get 'deny' channel: %s", in_error->message);
371 channel = tp_channel_new_from_properties (conn, channel_path, props, &error);
375 DEBUG ("Failed to create channel proxy: %s", error->message);
376 g_error_free (error);
380 tp_proxy_prepare_async (channel, features,
381 contact_blocking_dialog_deny_channel_prepared, self);
385 contact_blocking_dialog_deny_channel_prepared (GObject *channel,
386 GAsyncResult *result,
389 EmpathyContactBlockingDialog *self = user_data;
391 GError *error = NULL;
393 if (!tp_proxy_prepare_finish (channel, result, &error))
395 DEBUG ("Failed to prepare channel: %s", error->message);
396 g_error_free (error);
400 conn = tp_channel_borrow_connection (TP_CHANNEL (channel));
402 DEBUG ("Channel prepared for connection %s", get_pretty_conn_name (conn));
404 g_hash_table_insert (self->priv->channels,
405 g_object_ref (conn), channel);
406 contact_blocking_dialog_refilter_account_chooser (self);
408 tp_g_signal_connect_object (channel, "group-members-changed",
409 G_CALLBACK (contact_blocking_dialog_deny_channel_members_changed),
414 contact_blocking_dialog_set_error (EmpathyContactBlockingDialog *self,
417 const char *msg = NULL;
419 if (error->domain == TP_ERRORS)
421 if (error->code == TP_ERROR_INVALID_HANDLE)
422 msg = _("Unknown or invalid identifier");
423 else if (error->code == TP_ERROR_NOT_AVAILABLE)
424 msg = _("Contact blocking temporarily unavailable");
425 else if (error->code == TP_ERROR_NOT_CAPABLE)
426 msg = _("Contact blocking unavailable");
427 else if (error->code == TP_ERROR_PERMISSION_DENIED)
428 msg = _("Permission Denied");
432 msg = _("Could not block contact");
434 gtk_label_set_text (GTK_LABEL (self->priv->info_bar_label), msg);
435 gtk_widget_show (self->priv->info_bar);
438 static void contact_blocking_dialog_add_contact_got_handle (TpConnection *,
439 const GArray *, const GError *, gpointer, GObject *);
442 contact_blocking_dialog_add_contact (GtkWidget *widget,
443 EmpathyContactBlockingDialog *self)
445 TpConnection *conn = empathy_account_chooser_get_connection (
446 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
447 const char *identifiers[2] = { NULL, };
449 identifiers[0] = gtk_entry_get_text (
450 GTK_ENTRY (self->priv->add_contact_entry));
452 DEBUG ("Looking up handle for '%s'", identifiers[0]);
454 tp_cli_connection_call_request_handles (conn, -1,
455 TP_HANDLE_TYPE_CONTACT, identifiers,
456 contact_blocking_dialog_add_contact_got_handle,
457 NULL, NULL, G_OBJECT (self));
459 gtk_entry_set_text (GTK_ENTRY (self->priv->add_contact_entry), "");
460 gtk_widget_hide (self->priv->info_bar);
464 contact_blocking_dialog_added_contact (TpChannel *, const GError *,
465 gpointer, GObject *);
468 contact_blocking_dialog_add_contact_got_handle (TpConnection *conn,
469 const GArray *handles,
470 const GError *in_error,
474 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
475 TpChannel *channel = g_hash_table_lookup (priv->channels, conn);
477 if (in_error != NULL)
479 DEBUG ("Error getting handle: %s", in_error->message);
481 contact_blocking_dialog_set_error (
482 EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
487 g_return_if_fail (handles->len == 1);
489 DEBUG ("Adding handle %u to deny channel",
490 g_array_index (handles, TpHandle, 0));
492 tp_cli_channel_interface_group_call_add_members (channel, -1,
494 contact_blocking_dialog_added_contact, NULL, NULL, self);
498 contact_blocking_dialog_added_contact (TpChannel *channel,
499 const GError *in_error,
503 if (in_error != NULL)
505 DEBUG ("Error adding contact to deny list: %s", in_error->message);
507 contact_blocking_dialog_set_error (
508 EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
513 DEBUG ("Contact added");
517 contact_blocking_dialog_removed_contacts (TpChannel *,
518 const GError *, gpointer, GObject *);
521 contact_blocking_dialog_remove_contacts (GtkWidget *button,
522 EmpathyContactBlockingDialog *self)
524 TpConnection *conn = empathy_account_chooser_get_connection (
525 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
526 TpChannel *channel = g_hash_table_lookup (self->priv->channels, conn);
529 GArray *handles = g_array_new (FALSE, FALSE, sizeof (TpHandle));
531 rows = gtk_tree_selection_get_selected_rows (self->priv->selection, &model);
533 for (ptr = rows; ptr != NULL; ptr = ptr->next)
535 GtkTreePath *path = ptr->data;
539 if (!gtk_tree_model_get_iter (model, &iter, path))
542 gtk_tree_model_get (model, &iter,
546 g_array_append_val (handles, handle);
547 gtk_tree_path_free (path);
552 if (handles->len > 0)
554 DEBUG ("Removing %u handles", handles->len);
556 tp_cli_channel_interface_group_call_remove_members (channel, -1,
558 contact_blocking_dialog_removed_contacts,
559 NULL, NULL, G_OBJECT (self));
562 g_array_unref (handles);
566 contact_blocking_dialog_removed_contacts (TpChannel *channel,
567 const GError *in_error,
571 if (in_error != NULL)
573 DEBUG ("Error removing contacts from deny list: %s", in_error->message);
575 contact_blocking_dialog_set_error (
576 EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
581 DEBUG ("Contacts removed");
585 contact_blocking_dialog_account_changed (GtkWidget *account_chooser,
586 EmpathyContactBlockingDialog *self)
588 TpConnection *conn = empathy_account_chooser_get_connection (
589 EMPATHY_ACCOUNT_CHOOSER (account_chooser));
592 EmpathyContactManager *contact_manager;
593 EmpathyTpContactList *contact_list;
594 GList *members, *ptr;
596 if (self->priv->block_account_changed > 0)
599 /* clear the lists of contacts */
600 gtk_list_store_clear (self->priv->blocked_contacts);
601 gtk_list_store_clear (self->priv->completion_contacts);
606 DEBUG ("Account changed: %s", get_pretty_conn_name (conn));
608 /* load the deny list */
609 channel = g_hash_table_lookup (self->priv->channels, conn);
614 g_return_if_fail (TP_IS_CHANNEL (channel));
616 blocked = tp_intset_to_array (tp_channel_group_get_members (channel));
618 DEBUG ("%u contacts on blocked list", blocked->len);
620 contact_blocking_dialog_add_contacts_to_list (self, conn, blocked);
621 g_array_unref (blocked);
623 /* load the completion list */
624 g_return_if_fail (empathy_contact_manager_initialized ());
626 DEBUG ("Loading contacts");
628 contact_manager = empathy_contact_manager_dup_singleton ();
629 contact_list = empathy_contact_manager_get_list (contact_manager, conn);
630 members = empathy_contact_list_get_members (
631 EMPATHY_CONTACT_LIST (contact_list));
633 for (ptr = members; ptr != NULL; ptr = ptr->next)
635 EmpathyContact *contact = ptr->data;
637 gtk_list_store_insert_with_values (self->priv->completion_contacts,
639 COL_IDENTIFIER, empathy_contact_get_id (contact),
642 g_object_unref (contact);
645 g_list_free (members);
646 g_object_unref (contact_manager);
650 contact_blocking_dialog_view_selection_changed (GtkTreeSelection *selection,
651 EmpathyContactBlockingDialog *self)
653 GList *rows = gtk_tree_selection_get_selected_rows (selection, NULL);
655 /* update the sensitivity of the remove button */
656 gtk_widget_set_sensitive (self->priv->remove_button, rows != NULL);
658 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
663 contact_blocking_dialog_dispose (GObject *self)
665 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
667 tp_clear_pointer (&priv->channels, g_hash_table_destroy);
669 G_OBJECT_CLASS (empathy_contact_blocking_dialog_parent_class)->dispose (self);
673 empathy_contact_blocking_dialog_class_init (
674 EmpathyContactBlockingDialogClass *klass)
676 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
678 gobject_class->dispose = contact_blocking_dialog_dispose;
680 g_type_class_add_private (gobject_class,
681 sizeof (EmpathyContactBlockingDialogPrivate));
685 empathy_contact_blocking_dialog_init (EmpathyContactBlockingDialog *self)
690 GtkWidget *account_hbox, *blocked_contacts_view;
691 GtkEntryCompletion *completion;
692 TpAccountManager *am;
694 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
695 EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
696 EmpathyContactBlockingDialogPrivate);
698 self->priv->channels = g_hash_table_new_full (NULL, NULL,
699 g_object_unref, g_object_unref);
701 gtk_window_set_title (GTK_WINDOW (self), _("Edit Blocked Contacts"));
702 gtk_dialog_add_button (GTK_DIALOG (self),
703 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
705 filename = empathy_file_lookup ("empathy-contact-blocking-dialog.ui",
708 gui = empathy_builder_get_file (filename,
709 "contents", &contents,
710 "account-hbox", &account_hbox,
711 "add-button", &self->priv->add_button,
712 "add-contact-entry", &self->priv->add_contact_entry,
713 "blocked-contacts", &self->priv->blocked_contacts,
714 "blocked-contacts-view", &blocked_contacts_view,
715 "remove-button", &self->priv->remove_button,
718 empathy_builder_connect (gui, self,
719 "add-button", "clicked", contact_blocking_dialog_add_contact,
720 "add-contact-entry", "activate", contact_blocking_dialog_add_contact,
721 "remove-button", "clicked", contact_blocking_dialog_remove_contacts,
724 /* add the contents to the dialog */
726 GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (self))),
728 gtk_widget_show (contents);
730 /* set up the tree selection */
731 self->priv->selection = gtk_tree_view_get_selection (
732 GTK_TREE_VIEW (blocked_contacts_view));
733 gtk_tree_selection_set_mode (self->priv->selection, GTK_SELECTION_MULTIPLE);
734 g_signal_connect (self->priv->selection, "changed",
735 G_CALLBACK (contact_blocking_dialog_view_selection_changed), self);
737 /* build the contact entry */
738 self->priv->completion_contacts = gtk_list_store_new (1, G_TYPE_STRING);
739 completion = gtk_entry_completion_new ();
740 gtk_entry_completion_set_model (completion,
741 GTK_TREE_MODEL (self->priv->completion_contacts));
742 gtk_entry_completion_set_text_column (completion, COL_IDENTIFIER);
743 gtk_entry_set_completion (GTK_ENTRY (self->priv->add_contact_entry),
745 g_object_unref (completion);
746 g_object_unref (self->priv->completion_contacts);
748 /* add the account chooser */
749 self->priv->account_chooser = empathy_account_chooser_new ();
750 contact_blocking_dialog_refilter_account_chooser (self);
751 g_signal_connect (self->priv->account_chooser, "changed",
752 G_CALLBACK (contact_blocking_dialog_account_changed), self);
754 gtk_box_pack_start (GTK_BOX (account_hbox), self->priv->account_chooser,
756 gtk_widget_show (self->priv->account_chooser);
758 /* add an error warning info bar */
759 self->priv->info_bar = gtk_info_bar_new ();
760 gtk_box_pack_start (GTK_BOX (contents), self->priv->info_bar, FALSE, TRUE, 0);
761 gtk_info_bar_set_message_type (GTK_INFO_BAR (self->priv->info_bar),
764 self->priv->info_bar_label = gtk_label_new ("");
765 gtk_container_add (GTK_CONTAINER (
766 gtk_info_bar_get_content_area (GTK_INFO_BAR (self->priv->info_bar))),
767 self->priv->info_bar_label);
768 gtk_widget_show (self->priv->info_bar_label);
770 /* prepare the account manager */
771 am = tp_account_manager_dup ();
772 tp_proxy_prepare_async (am, NULL, contact_blocking_dialog_am_prepared, self);
776 g_object_unref (gui);
780 empathy_contact_blocking_dialog_new (GtkWindow *parent)
782 GtkWidget *self = g_object_new (EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
787 gtk_window_set_transient_for (GTK_WINDOW (self), parent);