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 GHashTable *channels; /* TpConnection* -> TpChannel* */
50 GtkListStore *blocked_contacts;
51 GtkListStore *completion_contacts;
52 GtkTreeSelection *selection;
54 GtkWidget *account_chooser;
55 GtkWidget *add_contact_entry;
56 GtkWidget *remove_button;
59 enum /* blocked-contacts columns */
67 get_pretty_conn_name (TpConnection *conn)
69 return tp_proxy_get_object_path (conn) + strlen (TP_CONN_OBJECT_PATH_BASE);
73 contact_blocking_dialog_filter_account_chooser (TpAccount *account,
74 EmpathyAccountChooserFilterResultCallback callback,
75 gpointer callback_data,
78 EmpathyContactBlockingDialog *self = user_data;
79 TpConnection *conn = tp_account_get_connection (account);
84 g_hash_table_lookup (self->priv->channels, conn) != NULL;
86 callback (enable, callback_data);
89 static void contact_blocking_dialog_inspected_handles (TpConnection *,
90 const char **, const GError *, gpointer, GObject *);
93 contact_blocking_dialog_add_contacts_to_list (
94 EmpathyContactBlockingDialog *self,
99 tp_cli_connection_call_inspect_handles (conn, -1,
100 TP_HANDLE_TYPE_CONTACT, handles,
101 contact_blocking_dialog_inspected_handles,
102 g_boxed_copy (DBUS_TYPE_G_UINT_ARRAY, handles),
103 (GDestroyNotify) g_array_unref, G_OBJECT (self));
107 contact_blocking_dialog_inspected_handles (TpConnection *conn,
108 const char **identifiers,
109 const GError *in_error,
113 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
114 GArray *handles = user_data;
117 if (in_error != NULL)
119 DEBUG ("Failed to inspect handles: %s", in_error->message);
123 DEBUG ("Adding %u identifiers", handles->len);
125 for (i = 0; i < handles->len; i++)
127 const char *identifier = identifiers[i];
128 TpHandle handle = g_array_index (handles, TpHandle, i);
130 gtk_list_store_insert_with_values (priv->blocked_contacts, NULL, -1,
131 COL_IDENTIFIER, identifier,
137 DECLARE_CALLBACK (contact_blocking_dialog_connection_prepared);
140 contact_blocking_dialog_connection_status_changed (TpAccount *account,
144 const char *dbus_reason,
146 EmpathyContactBlockingDialog *self)
148 TpConnection *conn = tp_account_get_connection (account);
152 case TP_CONNECTION_STATUS_DISCONNECTED:
153 DEBUG ("Connection %s invalidated", get_pretty_conn_name (conn));
155 /* remove the channel from the hash table */
156 g_hash_table_remove (self->priv->channels, conn);
158 /* set the filter again to refilter the account list */
159 empathy_account_chooser_set_filter (
160 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser),
161 contact_blocking_dialog_filter_account_chooser, self);
164 case TP_CONNECTION_STATUS_CONNECTING:
167 case TP_CONNECTION_STATUS_CONNECTED:
168 DEBUG ("Connection %s reconnected", get_pretty_conn_name (conn));
170 tp_proxy_prepare_async (conn, NULL,
171 contact_blocking_dialog_connection_prepared, self);
176 contact_blocking_dialog_deny_channel_members_changed (TpChannel *channel,
180 GArray *local_pending,
181 GArray *remote_pending,
184 EmpathyContactBlockingDialog *self)
186 TpConnection *conn = tp_channel_borrow_connection (channel);
187 GtkTreeModel *model = GTK_TREE_MODEL (self->priv->blocked_contacts);
189 TpIntset *removed_set;
192 /* we only care about changes to the selected connection */
193 /* FIXME: can we compare proxy pointers directly? */
195 tp_proxy_get_object_path (tp_channel_borrow_connection (channel)),
196 tp_proxy_get_object_path (empathy_account_chooser_get_connection (
197 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser)))))
200 DEBUG ("deny list changed: %u added, %u removed", added->len, removed->len);
203 contact_blocking_dialog_add_contacts_to_list (self, conn, added);
205 /* remove contacts */
206 removed_set = tp_intset_from_array (removed);
208 valid = gtk_tree_model_get_iter_first (model, &iter);
213 gtk_tree_model_get (model, &iter,
217 if (tp_intset_is_member (removed_set, handle))
218 valid = gtk_list_store_remove (self->priv->blocked_contacts, &iter);
220 valid = gtk_tree_model_iter_next (model, &iter);
223 tp_intset_destroy (removed_set);
226 DECLARE_CALLBACK (contact_blocking_dialog_account_prepared);
229 contact_blocking_dialog_am_prepared (GObject *am,
230 GAsyncResult *result,
233 EmpathyContactBlockingDialog *self = user_data;
234 GList *accounts, *ptr;
235 GError *error = NULL;
237 if (!tp_proxy_prepare_finish (am, result, &error))
239 g_critical ("Could not prepare Account Manager: %s", error->message);
240 g_error_free (error);
244 accounts = tp_account_manager_get_valid_accounts (TP_ACCOUNT_MANAGER (am));
246 for (ptr = accounts; ptr != NULL; ptr = ptr->next)
248 TpAccount *account = ptr->data;
250 tp_proxy_prepare_async (account, NULL,
251 contact_blocking_dialog_account_prepared, self);
254 g_list_free (accounts);
258 contact_blocking_dialog_account_prepared (GObject *account,
259 GAsyncResult *result,
262 EmpathyContactBlockingDialog *self = user_data;
264 GError *error = NULL;
266 if (!tp_proxy_prepare_finish (account, result, &error))
268 DEBUG ("Could not prepare Account: %s", error->message);
269 g_error_free (error);
273 g_signal_connect (account, "status-changed",
274 G_CALLBACK (contact_blocking_dialog_connection_status_changed), self);
276 conn = tp_account_get_connection (TP_ACCOUNT (account));
280 tp_proxy_prepare_async (conn, NULL,
281 contact_blocking_dialog_connection_prepared, self);
285 static void contact_blocking_dialog_got_deny_channel (TpConnection *,
286 gboolean, const char *, GHashTable *, const GError *, gpointer, GObject *);
289 contact_blocking_dialog_connection_prepared (GObject *conn,
290 GAsyncResult *result,
293 EmpathyContactBlockingDialog *self = user_data;
295 GError *error = NULL;
297 if (!tp_proxy_prepare_finish (conn, result, &error))
299 DEBUG ("Failed to prepare connection: %s", error->message);
300 g_error_free (error);
304 /* request the deny channel */
305 request = tp_asv_new (
306 TP_PROP_CHANNEL_CHANNEL_TYPE,
308 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
310 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
314 TP_PROP_CHANNEL_TARGET_ID,
320 tp_cli_connection_interface_requests_call_ensure_channel (
321 TP_CONNECTION (conn), -1, request,
322 contact_blocking_dialog_got_deny_channel, NULL, NULL, G_OBJECT (self));
324 g_hash_table_destroy (request);
327 DECLARE_CALLBACK (contact_blocking_dialog_deny_channel_prepared);
330 contact_blocking_dialog_got_deny_channel (TpConnection *conn,
332 const char *channel_path,
334 const GError *in_error,
339 GError *error = NULL;
341 const GQuark features[] = {
342 TP_CHANNEL_FEATURE_CORE,
343 TP_CHANNEL_FEATURE_GROUP,
346 if (in_error != NULL)
348 DEBUG ("Failed to get 'deny' channel: %s", in_error->message);
352 channel = tp_channel_new_from_properties (conn, channel_path, props, &error);
356 DEBUG ("Failed to create channel proxy: %s", error->message);
357 g_error_free (error);
361 tp_proxy_prepare_async (channel, features,
362 contact_blocking_dialog_deny_channel_prepared, self);
366 contact_blocking_dialog_deny_channel_prepared (GObject *channel,
367 GAsyncResult *result,
370 EmpathyContactBlockingDialog *self = user_data;
372 GError *error = NULL;
374 if (!tp_proxy_prepare_finish (channel, result, &error))
376 DEBUG ("Failed to prepare channel: %s", error->message);
377 g_error_free (error);
381 conn = tp_channel_borrow_connection (TP_CHANNEL (channel));
383 DEBUG ("Channel prepared for connection %s", get_pretty_conn_name (conn));
385 g_hash_table_insert (self->priv->channels,
386 g_object_ref (conn), channel);
388 /* set the filter again to refilter the account list */
389 empathy_account_chooser_set_filter (
390 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser),
391 contact_blocking_dialog_filter_account_chooser, self);
393 g_signal_connect (channel, "group-members-changed",
394 G_CALLBACK (contact_blocking_dialog_deny_channel_members_changed), self);
397 static void contact_blocking_dialog_add_contact_got_handle (TpConnection *,
398 const GArray *, const GError *, gpointer, GObject *);
401 contact_blocking_dialog_add_contact (GtkWidget *widget,
402 EmpathyContactBlockingDialog *self)
404 TpConnection *conn = empathy_account_chooser_get_connection (
405 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
406 const char *identifiers[2] = { NULL, };
408 identifiers[0] = gtk_entry_get_text (
409 GTK_ENTRY (self->priv->add_contact_entry));
411 DEBUG ("Looking up handle for '%s'", identifiers[0]);
413 tp_cli_connection_call_request_handles (conn, -1,
414 TP_HANDLE_TYPE_CONTACT, identifiers,
415 contact_blocking_dialog_add_contact_got_handle,
416 NULL, NULL, G_OBJECT (self));
418 gtk_entry_set_text (GTK_ENTRY (self->priv->add_contact_entry), "");
422 contact_blocking_dialog_added_contact (TpChannel *, const GError *,
423 gpointer, GObject *);
426 contact_blocking_dialog_add_contact_got_handle (TpConnection *conn,
427 const GArray *handles,
428 const GError *in_error,
432 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
433 TpChannel *channel = g_hash_table_lookup (priv->channels, conn);
435 if (in_error != NULL)
437 DEBUG ("Error getting handle: %s", in_error->message);
438 /* FIXME: expose error to user */
442 g_return_if_fail (handles->len == 1);
444 DEBUG ("Adding handle %u to deny channel",
445 g_array_index (handles, TpHandle, 0));
447 tp_cli_channel_interface_group_call_add_members (channel, -1,
449 contact_blocking_dialog_added_contact, NULL, NULL, self);
453 contact_blocking_dialog_added_contact (TpChannel *channel,
454 const GError *in_error,
458 if (in_error != NULL)
460 DEBUG ("Error adding contact to deny list: %s", in_error->message);
461 /* FIXME: expose error to user */
465 DEBUG ("Contact added");
469 contact_blocking_dialog_removed_contacts (TpChannel *,
470 const GError *, gpointer, GObject *);
473 contact_blocking_dialog_remove_contacts (GtkWidget *button,
474 EmpathyContactBlockingDialog *self)
476 TpConnection *conn = empathy_account_chooser_get_connection (
477 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
478 TpChannel *channel = g_hash_table_lookup (self->priv->channels, conn);
481 GArray *handles = g_array_new (FALSE, FALSE, sizeof (TpHandle));
483 rows = gtk_tree_selection_get_selected_rows (self->priv->selection, &model);
485 for (ptr = rows; ptr != NULL; ptr = ptr->next)
487 GtkTreePath *path = ptr->data;
491 if (!gtk_tree_model_get_iter (model, &iter, path))
494 gtk_tree_model_get (model, &iter,
498 g_array_append_val (handles, handle);
499 gtk_tree_path_free (path);
504 if (handles->len > 0)
506 DEBUG ("Removing %u handles", handles->len);
508 tp_cli_channel_interface_group_call_remove_members (channel, -1,
510 contact_blocking_dialog_removed_contacts,
511 NULL, NULL, G_OBJECT (self));
514 g_array_unref (handles);
518 contact_blocking_dialog_removed_contacts (TpChannel *channel,
519 const GError *in_error,
523 if (in_error != NULL)
525 DEBUG ("Error removing contacts from deny list: %s", in_error->message);
526 /* FIXME: expose error to user */
530 DEBUG ("Contacts removed");
534 contact_blocking_dialog_account_changed (GtkWidget *account_chooser,
535 EmpathyContactBlockingDialog *self)
537 TpConnection *conn = empathy_account_chooser_get_connection (
538 EMPATHY_ACCOUNT_CHOOSER (account_chooser));
541 EmpathyContactManager *contact_manager;
542 EmpathyTpContactList *contact_list;
543 GList *members, *ptr;
548 DEBUG ("Account changed: %s", get_pretty_conn_name (conn));
550 /* clear the lists of contacts */
551 gtk_list_store_clear (self->priv->blocked_contacts);
552 gtk_list_store_clear (self->priv->completion_contacts);
554 /* load the deny list */
555 channel = g_hash_table_lookup (self->priv->channels, conn);
557 g_return_if_fail (TP_IS_CHANNEL (channel));
559 blocked = tp_intset_to_array (tp_channel_group_get_members (channel));
561 DEBUG ("%u contacts on blocked list", blocked->len);
563 contact_blocking_dialog_add_contacts_to_list (self, conn, blocked);
564 g_array_unref (blocked);
566 /* load the completion list */
567 g_return_if_fail (empathy_contact_manager_initialized ());
569 DEBUG ("Loading contacts");
571 contact_manager = empathy_contact_manager_dup_singleton ();
572 contact_list = empathy_contact_manager_get_list (contact_manager, conn);
573 members = empathy_contact_list_get_members (
574 EMPATHY_CONTACT_LIST (contact_list));
576 for (ptr = members; ptr != NULL; ptr = ptr->next)
578 EmpathyContact *contact = ptr->data;
580 gtk_list_store_insert_with_values (self->priv->completion_contacts,
582 COL_IDENTIFIER, empathy_contact_get_id (contact),
585 g_object_unref (contact);
588 g_list_free (members);
589 g_object_unref (contact_manager);
593 contact_blocking_dialog_view_selection_changed (GtkTreeSelection *selection,
594 EmpathyContactBlockingDialog *self)
596 GList *rows = gtk_tree_selection_get_selected_rows (selection, NULL);
598 /* update the sensitivity of the remove button */
599 gtk_widget_set_sensitive (self->priv->remove_button, rows != NULL);
601 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
606 contact_blocking_dialog_dispose (GObject *self)
608 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
610 tp_clear_pointer (&priv->channels, g_hash_table_destroy);
612 G_OBJECT_CLASS (empathy_contact_blocking_dialog_parent_class)->dispose (self);
616 empathy_contact_blocking_dialog_class_init (
617 EmpathyContactBlockingDialogClass *klass)
619 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
621 gobject_class->dispose = contact_blocking_dialog_dispose;
623 g_type_class_add_private (gobject_class,
624 sizeof (EmpathyContactBlockingDialogPrivate));
628 empathy_contact_blocking_dialog_init (EmpathyContactBlockingDialog *self)
633 GtkWidget *account_hbox, *blocked_contacts_view;
634 GtkEntryCompletion *completion;
635 TpAccountManager *am;
637 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
638 EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
639 EmpathyContactBlockingDialogPrivate);
641 self->priv->channels = g_hash_table_new_full (NULL, NULL,
642 g_object_unref, g_object_unref);
644 gtk_window_set_title (GTK_WINDOW (self), _("Edit Blocked Contacts"));
645 gtk_dialog_add_button (GTK_DIALOG (self),
646 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
648 filename = empathy_file_lookup ("empathy-contact-blocking-dialog.ui",
651 gui = empathy_builder_get_file (filename,
652 "contents", &contents,
653 "account-hbox", &account_hbox,
654 "add-contact-entry", &self->priv->add_contact_entry,
655 "blocked-contacts", &self->priv->blocked_contacts,
656 "blocked-contacts-view", &blocked_contacts_view,
657 "remove-button", &self->priv->remove_button,
660 empathy_builder_connect (gui, self,
661 "add-button", "clicked", contact_blocking_dialog_add_contact,
662 "add-contact-entry", "activate", contact_blocking_dialog_add_contact,
663 "remove-button", "clicked", contact_blocking_dialog_remove_contacts,
666 /* add the contents to the dialog */
668 GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (self))),
670 gtk_widget_show (contents);
672 /* add the account chooser */
673 self->priv->account_chooser = empathy_account_chooser_new ();
674 empathy_account_chooser_set_filter (
675 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser),
676 contact_blocking_dialog_filter_account_chooser, self);
677 g_signal_connect (self->priv->account_chooser, "changed",
678 G_CALLBACK (contact_blocking_dialog_account_changed), self);
680 gtk_box_pack_start (GTK_BOX (account_hbox), self->priv->account_chooser,
682 gtk_widget_show (self->priv->account_chooser);
684 /* set up the tree selection */
685 self->priv->selection = gtk_tree_view_get_selection (
686 GTK_TREE_VIEW (blocked_contacts_view));
687 gtk_tree_selection_set_mode (self->priv->selection, GTK_SELECTION_MULTIPLE);
688 g_signal_connect (self->priv->selection, "changed",
689 G_CALLBACK (contact_blocking_dialog_view_selection_changed), self);
691 /* build the contact entry */
692 self->priv->completion_contacts = gtk_list_store_new (1, G_TYPE_STRING);
693 completion = gtk_entry_completion_new ();
694 gtk_entry_completion_set_model (completion,
695 GTK_TREE_MODEL (self->priv->completion_contacts));
696 gtk_entry_completion_set_text_column (completion, COL_IDENTIFIER);
697 gtk_entry_set_completion (GTK_ENTRY (self->priv->add_contact_entry),
699 g_object_unref (completion);
700 g_object_unref (self->priv->completion_contacts);
702 /* prepare the account manager */
703 am = tp_account_manager_dup ();
704 tp_proxy_prepare_async (am, NULL, contact_blocking_dialog_am_prepared, self);
708 g_object_unref (gui);
712 empathy_contact_blocking_dialog_new (GtkWindow *parent)
714 GtkWidget *self = g_object_new (EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
719 gtk_window_set_transient_for (GTK_WINDOW (self), parent);