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 g_signal_connect (account, "status-changed",
289 G_CALLBACK (contact_blocking_dialog_connection_status_changed), self);
291 conn = tp_account_get_connection (TP_ACCOUNT (account));
295 tp_proxy_prepare_async (conn, NULL,
296 contact_blocking_dialog_connection_prepared, self);
300 g_list_free (accounts);
303 static void contact_blocking_dialog_got_deny_channel (TpConnection *,
304 gboolean, const char *, GHashTable *, const GError *, gpointer, GObject *);
307 contact_blocking_dialog_connection_prepared (GObject *conn,
308 GAsyncResult *result,
311 EmpathyContactBlockingDialog *self = user_data;
313 GError *error = NULL;
315 if (!tp_proxy_prepare_finish (conn, result, &error))
317 DEBUG ("Failed to prepare connection: %s", error->message);
318 g_error_free (error);
322 /* request the deny channel */
323 request = tp_asv_new (
324 TP_PROP_CHANNEL_CHANNEL_TYPE,
326 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
328 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
332 TP_PROP_CHANNEL_TARGET_ID,
338 tp_cli_connection_interface_requests_call_ensure_channel (
339 TP_CONNECTION (conn), -1, request,
340 contact_blocking_dialog_got_deny_channel, NULL, NULL, G_OBJECT (self));
342 g_hash_table_destroy (request);
345 DECLARE_CALLBACK (contact_blocking_dialog_deny_channel_prepared);
348 contact_blocking_dialog_got_deny_channel (TpConnection *conn,
350 const char *channel_path,
352 const GError *in_error,
357 GError *error = NULL;
359 const GQuark features[] = {
360 TP_CHANNEL_FEATURE_CORE,
361 TP_CHANNEL_FEATURE_GROUP,
364 if (in_error != NULL)
366 DEBUG ("Failed to get 'deny' channel: %s", in_error->message);
370 channel = tp_channel_new_from_properties (conn, channel_path, props, &error);
374 DEBUG ("Failed to create channel proxy: %s", error->message);
375 g_error_free (error);
379 tp_proxy_prepare_async (channel, features,
380 contact_blocking_dialog_deny_channel_prepared, self);
384 contact_blocking_dialog_deny_channel_prepared (GObject *channel,
385 GAsyncResult *result,
388 EmpathyContactBlockingDialog *self = user_data;
390 GError *error = NULL;
392 if (!tp_proxy_prepare_finish (channel, result, &error))
394 DEBUG ("Failed to prepare channel: %s", error->message);
395 g_error_free (error);
399 conn = tp_channel_borrow_connection (TP_CHANNEL (channel));
401 DEBUG ("Channel prepared for connection %s", get_pretty_conn_name (conn));
403 g_hash_table_insert (self->priv->channels,
404 g_object_ref (conn), channel);
405 contact_blocking_dialog_refilter_account_chooser (self);
407 g_signal_connect (channel, "group-members-changed",
408 G_CALLBACK (contact_blocking_dialog_deny_channel_members_changed), self);
412 contact_blocking_dialog_set_error (EmpathyContactBlockingDialog *self,
415 const char *msg = NULL;
417 if (error->domain == TP_ERRORS)
419 if (error->code == TP_ERROR_INVALID_HANDLE)
420 msg = _("Unknown or invalid identifier");
421 else if (error->code == TP_ERROR_NOT_AVAILABLE)
422 msg = _("Contact blocking temporarily unavailable");
423 else if (error->code == TP_ERROR_NOT_CAPABLE)
424 msg = _("Contact blocking unavailable");
425 else if (error->code == TP_ERROR_PERMISSION_DENIED)
426 msg = _("Permission Denied");
430 msg = _("Could not block contact");
432 gtk_label_set_text (GTK_LABEL (self->priv->info_bar_label), msg);
433 gtk_widget_show (self->priv->info_bar);
436 static void contact_blocking_dialog_add_contact_got_handle (TpConnection *,
437 const GArray *, const GError *, gpointer, GObject *);
440 contact_blocking_dialog_add_contact (GtkWidget *widget,
441 EmpathyContactBlockingDialog *self)
443 TpConnection *conn = empathy_account_chooser_get_connection (
444 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
445 const char *identifiers[2] = { NULL, };
447 identifiers[0] = gtk_entry_get_text (
448 GTK_ENTRY (self->priv->add_contact_entry));
450 DEBUG ("Looking up handle for '%s'", identifiers[0]);
452 tp_cli_connection_call_request_handles (conn, -1,
453 TP_HANDLE_TYPE_CONTACT, identifiers,
454 contact_blocking_dialog_add_contact_got_handle,
455 NULL, NULL, G_OBJECT (self));
457 gtk_entry_set_text (GTK_ENTRY (self->priv->add_contact_entry), "");
458 gtk_widget_hide (self->priv->info_bar);
462 contact_blocking_dialog_added_contact (TpChannel *, const GError *,
463 gpointer, GObject *);
466 contact_blocking_dialog_add_contact_got_handle (TpConnection *conn,
467 const GArray *handles,
468 const GError *in_error,
472 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
473 TpChannel *channel = g_hash_table_lookup (priv->channels, conn);
475 if (in_error != NULL)
477 DEBUG ("Error getting handle: %s", in_error->message);
479 contact_blocking_dialog_set_error (
480 EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
485 g_return_if_fail (handles->len == 1);
487 DEBUG ("Adding handle %u to deny channel",
488 g_array_index (handles, TpHandle, 0));
490 tp_cli_channel_interface_group_call_add_members (channel, -1,
492 contact_blocking_dialog_added_contact, NULL, NULL, self);
496 contact_blocking_dialog_added_contact (TpChannel *channel,
497 const GError *in_error,
501 if (in_error != NULL)
503 DEBUG ("Error adding contact to deny list: %s", in_error->message);
505 contact_blocking_dialog_set_error (
506 EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
511 DEBUG ("Contact added");
515 contact_blocking_dialog_removed_contacts (TpChannel *,
516 const GError *, gpointer, GObject *);
519 contact_blocking_dialog_remove_contacts (GtkWidget *button,
520 EmpathyContactBlockingDialog *self)
522 TpConnection *conn = empathy_account_chooser_get_connection (
523 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
524 TpChannel *channel = g_hash_table_lookup (self->priv->channels, conn);
527 GArray *handles = g_array_new (FALSE, FALSE, sizeof (TpHandle));
529 rows = gtk_tree_selection_get_selected_rows (self->priv->selection, &model);
531 for (ptr = rows; ptr != NULL; ptr = ptr->next)
533 GtkTreePath *path = ptr->data;
537 if (!gtk_tree_model_get_iter (model, &iter, path))
540 gtk_tree_model_get (model, &iter,
544 g_array_append_val (handles, handle);
545 gtk_tree_path_free (path);
550 if (handles->len > 0)
552 DEBUG ("Removing %u handles", handles->len);
554 tp_cli_channel_interface_group_call_remove_members (channel, -1,
556 contact_blocking_dialog_removed_contacts,
557 NULL, NULL, G_OBJECT (self));
560 g_array_unref (handles);
564 contact_blocking_dialog_removed_contacts (TpChannel *channel,
565 const GError *in_error,
569 if (in_error != NULL)
571 DEBUG ("Error removing contacts from deny list: %s", in_error->message);
573 contact_blocking_dialog_set_error (
574 EMPATHY_CONTACT_BLOCKING_DIALOG (self), in_error);
579 DEBUG ("Contacts removed");
583 contact_blocking_dialog_account_changed (GtkWidget *account_chooser,
584 EmpathyContactBlockingDialog *self)
586 TpConnection *conn = empathy_account_chooser_get_connection (
587 EMPATHY_ACCOUNT_CHOOSER (account_chooser));
590 EmpathyContactManager *contact_manager;
591 EmpathyTpContactList *contact_list;
592 GList *members, *ptr;
594 if (self->priv->block_account_changed > 0)
597 /* clear the lists of contacts */
598 gtk_list_store_clear (self->priv->blocked_contacts);
599 gtk_list_store_clear (self->priv->completion_contacts);
604 DEBUG ("Account changed: %s", get_pretty_conn_name (conn));
606 /* load the deny list */
607 channel = g_hash_table_lookup (self->priv->channels, conn);
612 g_return_if_fail (TP_IS_CHANNEL (channel));
614 blocked = tp_intset_to_array (tp_channel_group_get_members (channel));
616 DEBUG ("%u contacts on blocked list", blocked->len);
618 contact_blocking_dialog_add_contacts_to_list (self, conn, blocked);
619 g_array_unref (blocked);
621 /* load the completion list */
622 g_return_if_fail (empathy_contact_manager_initialized ());
624 DEBUG ("Loading contacts");
626 contact_manager = empathy_contact_manager_dup_singleton ();
627 contact_list = empathy_contact_manager_get_list (contact_manager, conn);
628 members = empathy_contact_list_get_members (
629 EMPATHY_CONTACT_LIST (contact_list));
631 for (ptr = members; ptr != NULL; ptr = ptr->next)
633 EmpathyContact *contact = ptr->data;
635 gtk_list_store_insert_with_values (self->priv->completion_contacts,
637 COL_IDENTIFIER, empathy_contact_get_id (contact),
640 g_object_unref (contact);
643 g_list_free (members);
644 g_object_unref (contact_manager);
648 contact_blocking_dialog_view_selection_changed (GtkTreeSelection *selection,
649 EmpathyContactBlockingDialog *self)
651 GList *rows = gtk_tree_selection_get_selected_rows (selection, NULL);
653 /* update the sensitivity of the remove button */
654 gtk_widget_set_sensitive (self->priv->remove_button, rows != NULL);
656 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
661 contact_blocking_dialog_dispose (GObject *self)
663 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
665 tp_clear_pointer (&priv->channels, g_hash_table_destroy);
667 G_OBJECT_CLASS (empathy_contact_blocking_dialog_parent_class)->dispose (self);
671 empathy_contact_blocking_dialog_class_init (
672 EmpathyContactBlockingDialogClass *klass)
674 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
676 gobject_class->dispose = contact_blocking_dialog_dispose;
678 g_type_class_add_private (gobject_class,
679 sizeof (EmpathyContactBlockingDialogPrivate));
683 empathy_contact_blocking_dialog_init (EmpathyContactBlockingDialog *self)
688 GtkWidget *account_hbox, *blocked_contacts_view;
689 GtkEntryCompletion *completion;
690 TpAccountManager *am;
692 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
693 EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
694 EmpathyContactBlockingDialogPrivate);
696 self->priv->channels = g_hash_table_new_full (NULL, NULL,
697 g_object_unref, g_object_unref);
699 gtk_window_set_title (GTK_WINDOW (self), _("Edit Blocked Contacts"));
700 gtk_dialog_add_button (GTK_DIALOG (self),
701 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
703 filename = empathy_file_lookup ("empathy-contact-blocking-dialog.ui",
706 gui = empathy_builder_get_file (filename,
707 "contents", &contents,
708 "account-hbox", &account_hbox,
709 "add-button", &self->priv->add_button,
710 "add-contact-entry", &self->priv->add_contact_entry,
711 "blocked-contacts", &self->priv->blocked_contacts,
712 "blocked-contacts-view", &blocked_contacts_view,
713 "remove-button", &self->priv->remove_button,
716 empathy_builder_connect (gui, self,
717 "add-button", "clicked", contact_blocking_dialog_add_contact,
718 "add-contact-entry", "activate", contact_blocking_dialog_add_contact,
719 "remove-button", "clicked", contact_blocking_dialog_remove_contacts,
722 /* add the contents to the dialog */
724 GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (self))),
726 gtk_widget_show (contents);
728 /* set up the tree selection */
729 self->priv->selection = gtk_tree_view_get_selection (
730 GTK_TREE_VIEW (blocked_contacts_view));
731 gtk_tree_selection_set_mode (self->priv->selection, GTK_SELECTION_MULTIPLE);
732 g_signal_connect (self->priv->selection, "changed",
733 G_CALLBACK (contact_blocking_dialog_view_selection_changed), self);
735 /* build the contact entry */
736 self->priv->completion_contacts = gtk_list_store_new (1, G_TYPE_STRING);
737 completion = gtk_entry_completion_new ();
738 gtk_entry_completion_set_model (completion,
739 GTK_TREE_MODEL (self->priv->completion_contacts));
740 gtk_entry_completion_set_text_column (completion, COL_IDENTIFIER);
741 gtk_entry_set_completion (GTK_ENTRY (self->priv->add_contact_entry),
743 g_object_unref (completion);
744 g_object_unref (self->priv->completion_contacts);
746 /* add the account chooser */
747 self->priv->account_chooser = empathy_account_chooser_new ();
748 contact_blocking_dialog_refilter_account_chooser (self);
749 g_signal_connect (self->priv->account_chooser, "changed",
750 G_CALLBACK (contact_blocking_dialog_account_changed), self);
752 gtk_box_pack_start (GTK_BOX (account_hbox), self->priv->account_chooser,
754 gtk_widget_show (self->priv->account_chooser);
756 /* add an error warning info bar */
757 self->priv->info_bar = gtk_info_bar_new ();
758 gtk_box_pack_start (GTK_BOX (contents), self->priv->info_bar, FALSE, TRUE, 0);
759 gtk_info_bar_set_message_type (GTK_INFO_BAR (self->priv->info_bar),
762 self->priv->info_bar_label = gtk_label_new ("");
763 gtk_container_add (GTK_CONTAINER (
764 gtk_info_bar_get_content_area (GTK_INFO_BAR (self->priv->info_bar))),
765 self->priv->info_bar_label);
766 gtk_widget_show (self->priv->info_bar_label);
768 /* prepare the account manager */
769 am = tp_account_manager_dup ();
770 tp_proxy_prepare_async (am, NULL, contact_blocking_dialog_am_prepared, self);
774 g_object_unref (gui);
778 empathy_contact_blocking_dialog_new (GtkWindow *parent)
780 GtkWidget *self = g_object_new (EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
785 gtk_window_set_transient_for (GTK_WINDOW (self), parent);