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_connection_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;
251 g_signal_connect (account, "status-changed",
252 G_CALLBACK (contact_blocking_dialog_connection_status_changed), self);
254 conn = tp_account_get_connection (TP_ACCOUNT (account));
258 tp_proxy_prepare_async (conn, NULL,
259 contact_blocking_dialog_connection_prepared, self);
263 g_list_free (accounts);
266 static void contact_blocking_dialog_got_deny_channel (TpConnection *,
267 gboolean, const char *, GHashTable *, const GError *, gpointer, GObject *);
270 contact_blocking_dialog_connection_prepared (GObject *conn,
271 GAsyncResult *result,
274 EmpathyContactBlockingDialog *self = user_data;
276 GError *error = NULL;
278 if (!tp_proxy_prepare_finish (conn, result, &error))
280 DEBUG ("Failed to prepare connection: %s", error->message);
281 g_error_free (error);
285 /* request the deny channel */
286 request = tp_asv_new (
287 TP_PROP_CHANNEL_CHANNEL_TYPE,
289 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
291 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
295 TP_PROP_CHANNEL_TARGET_ID,
301 tp_cli_connection_interface_requests_call_ensure_channel (
302 TP_CONNECTION (conn), -1, request,
303 contact_blocking_dialog_got_deny_channel, NULL, NULL, G_OBJECT (self));
305 g_hash_table_destroy (request);
308 DECLARE_CALLBACK (contact_blocking_dialog_deny_channel_prepared);
311 contact_blocking_dialog_got_deny_channel (TpConnection *conn,
313 const char *channel_path,
315 const GError *in_error,
320 GError *error = NULL;
322 const GQuark features[] = {
323 TP_CHANNEL_FEATURE_CORE,
324 TP_CHANNEL_FEATURE_GROUP,
327 if (in_error != NULL)
329 DEBUG ("Failed to get 'deny' channel: %s", in_error->message);
333 channel = tp_channel_new_from_properties (conn, channel_path, props, &error);
337 DEBUG ("Failed to create channel proxy: %s", error->message);
338 g_error_free (error);
342 tp_proxy_prepare_async (channel, features,
343 contact_blocking_dialog_deny_channel_prepared, self);
347 contact_blocking_dialog_deny_channel_prepared (GObject *channel,
348 GAsyncResult *result,
351 EmpathyContactBlockingDialog *self = user_data;
353 GError *error = NULL;
355 if (!tp_proxy_prepare_finish (channel, result, &error))
357 DEBUG ("Failed to prepare channel: %s", error->message);
358 g_error_free (error);
362 conn = tp_channel_borrow_connection (TP_CHANNEL (channel));
364 DEBUG ("Channel prepared for connection %s", get_pretty_conn_name (conn));
366 g_hash_table_insert (self->priv->channels,
367 g_object_ref (conn), channel);
369 /* set the filter again to refilter the account list */
370 empathy_account_chooser_set_filter (
371 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser),
372 contact_blocking_dialog_filter_account_chooser, self);
374 g_signal_connect (channel, "group-members-changed",
375 G_CALLBACK (contact_blocking_dialog_deny_channel_members_changed), self);
378 static void contact_blocking_dialog_add_contact_got_handle (TpConnection *,
379 const GArray *, const GError *, gpointer, GObject *);
382 contact_blocking_dialog_add_contact (GtkWidget *widget,
383 EmpathyContactBlockingDialog *self)
385 TpConnection *conn = empathy_account_chooser_get_connection (
386 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
387 const char *identifiers[2] = { NULL, };
389 identifiers[0] = gtk_entry_get_text (
390 GTK_ENTRY (self->priv->add_contact_entry));
392 DEBUG ("Looking up handle for '%s'", identifiers[0]);
394 tp_cli_connection_call_request_handles (conn, -1,
395 TP_HANDLE_TYPE_CONTACT, identifiers,
396 contact_blocking_dialog_add_contact_got_handle,
397 NULL, NULL, G_OBJECT (self));
399 gtk_entry_set_text (GTK_ENTRY (self->priv->add_contact_entry), "");
403 contact_blocking_dialog_added_contact (TpChannel *, const GError *,
404 gpointer, GObject *);
407 contact_blocking_dialog_add_contact_got_handle (TpConnection *conn,
408 const GArray *handles,
409 const GError *in_error,
413 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
414 TpChannel *channel = g_hash_table_lookup (priv->channels, conn);
416 if (in_error != NULL)
418 DEBUG ("Error getting handle: %s", in_error->message);
419 /* FIXME: expose error to user */
423 g_return_if_fail (handles->len == 1);
425 DEBUG ("Adding handle %u to deny channel",
426 g_array_index (handles, TpHandle, 0));
428 tp_cli_channel_interface_group_call_add_members (channel, -1,
430 contact_blocking_dialog_added_contact, NULL, NULL, self);
434 contact_blocking_dialog_added_contact (TpChannel *channel,
435 const GError *in_error,
439 if (in_error != NULL)
441 DEBUG ("Error adding contact to deny list: %s", in_error->message);
442 /* FIXME: expose error to user */
446 DEBUG ("Contact added");
450 contact_blocking_dialog_removed_contacts (TpChannel *,
451 const GError *, gpointer, GObject *);
454 contact_blocking_dialog_remove_contacts (GtkWidget *button,
455 EmpathyContactBlockingDialog *self)
457 TpConnection *conn = empathy_account_chooser_get_connection (
458 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
459 TpChannel *channel = g_hash_table_lookup (self->priv->channels, conn);
462 GArray *handles = g_array_new (FALSE, FALSE, sizeof (TpHandle));
464 rows = gtk_tree_selection_get_selected_rows (self->priv->selection, &model);
466 for (ptr = rows; ptr != NULL; ptr = ptr->next)
468 GtkTreePath *path = ptr->data;
472 if (!gtk_tree_model_get_iter (model, &iter, path))
475 gtk_tree_model_get (model, &iter,
479 g_array_append_val (handles, handle);
480 gtk_tree_path_free (path);
485 if (handles->len > 0)
487 DEBUG ("Removing %u handles", handles->len);
489 tp_cli_channel_interface_group_call_remove_members (channel, -1,
491 contact_blocking_dialog_removed_contacts,
492 NULL, NULL, G_OBJECT (self));
495 g_array_unref (handles);
499 contact_blocking_dialog_removed_contacts (TpChannel *channel,
500 const GError *in_error,
504 if (in_error != NULL)
506 DEBUG ("Error removing contacts from deny list: %s", in_error->message);
507 /* FIXME: expose error to user */
511 DEBUG ("Contacts removed");
515 contact_blocking_dialog_account_changed (GtkWidget *account_chooser,
516 EmpathyContactBlockingDialog *self)
518 TpConnection *conn = empathy_account_chooser_get_connection (
519 EMPATHY_ACCOUNT_CHOOSER (account_chooser));
522 EmpathyContactManager *contact_manager;
523 EmpathyTpContactList *contact_list;
524 GList *members, *ptr;
529 DEBUG ("Account changed: %s", get_pretty_conn_name (conn));
531 /* clear the lists of contacts */
532 gtk_list_store_clear (self->priv->blocked_contacts);
533 gtk_list_store_clear (self->priv->completion_contacts);
535 /* load the deny list */
536 channel = g_hash_table_lookup (self->priv->channels, conn);
538 g_return_if_fail (TP_IS_CHANNEL (channel));
540 blocked = tp_intset_to_array (tp_channel_group_get_members (channel));
542 DEBUG ("%u contacts on blocked list", blocked->len);
544 contact_blocking_dialog_add_contacts_to_list (self, conn, blocked);
545 g_array_unref (blocked);
547 /* load the completion list */
548 g_return_if_fail (empathy_contact_manager_initialized ());
550 DEBUG ("Loading contacts");
552 contact_manager = empathy_contact_manager_dup_singleton ();
553 contact_list = empathy_contact_manager_get_list (contact_manager, conn);
554 members = empathy_contact_list_get_members (
555 EMPATHY_CONTACT_LIST (contact_list));
557 for (ptr = members; ptr != NULL; ptr = ptr->next)
559 EmpathyContact *contact = ptr->data;
561 gtk_list_store_insert_with_values (self->priv->completion_contacts,
563 COL_IDENTIFIER, empathy_contact_get_id (contact),
566 g_object_unref (contact);
569 g_list_free (members);
570 g_object_unref (contact_manager);
574 contact_blocking_dialog_view_selection_changed (GtkTreeSelection *selection,
575 EmpathyContactBlockingDialog *self)
577 GList *rows = gtk_tree_selection_get_selected_rows (selection, NULL);
579 /* update the sensitivity of the remove button */
580 gtk_widget_set_sensitive (self->priv->remove_button, rows != NULL);
582 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
587 contact_blocking_dialog_dispose (GObject *self)
589 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
591 tp_clear_pointer (&priv->channels, g_hash_table_destroy);
593 G_OBJECT_CLASS (empathy_contact_blocking_dialog_parent_class)->dispose (self);
597 empathy_contact_blocking_dialog_class_init (
598 EmpathyContactBlockingDialogClass *klass)
600 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
602 gobject_class->dispose = contact_blocking_dialog_dispose;
604 g_type_class_add_private (gobject_class,
605 sizeof (EmpathyContactBlockingDialogPrivate));
609 empathy_contact_blocking_dialog_init (EmpathyContactBlockingDialog *self)
614 GtkWidget *account_hbox, *blocked_contacts_view;
615 GtkEntryCompletion *completion;
616 TpAccountManager *am;
618 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
619 EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
620 EmpathyContactBlockingDialogPrivate);
622 self->priv->channels = g_hash_table_new_full (NULL, NULL,
623 g_object_unref, g_object_unref);
625 gtk_window_set_title (GTK_WINDOW (self), _("Edit Blocked Contacts"));
626 gtk_dialog_add_button (GTK_DIALOG (self),
627 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
629 filename = empathy_file_lookup ("empathy-contact-blocking-dialog.ui",
632 gui = empathy_builder_get_file (filename,
633 "contents", &contents,
634 "account-hbox", &account_hbox,
635 "add-contact-entry", &self->priv->add_contact_entry,
636 "blocked-contacts", &self->priv->blocked_contacts,
637 "blocked-contacts-view", &blocked_contacts_view,
638 "remove-button", &self->priv->remove_button,
641 empathy_builder_connect (gui, self,
642 "add-button", "clicked", contact_blocking_dialog_add_contact,
643 "add-contact-entry", "activate", contact_blocking_dialog_add_contact,
644 "remove-button", "clicked", contact_blocking_dialog_remove_contacts,
647 /* add the contents to the dialog */
649 GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (self))),
651 gtk_widget_show (contents);
653 /* add the account chooser */
654 self->priv->account_chooser = empathy_account_chooser_new ();
655 empathy_account_chooser_set_filter (
656 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser),
657 contact_blocking_dialog_filter_account_chooser, self);
658 g_signal_connect (self->priv->account_chooser, "changed",
659 G_CALLBACK (contact_blocking_dialog_account_changed), self);
661 gtk_box_pack_start (GTK_BOX (account_hbox), self->priv->account_chooser,
663 gtk_widget_show (self->priv->account_chooser);
665 /* set up the tree selection */
666 self->priv->selection = gtk_tree_view_get_selection (
667 GTK_TREE_VIEW (blocked_contacts_view));
668 gtk_tree_selection_set_mode (self->priv->selection, GTK_SELECTION_MULTIPLE);
669 g_signal_connect (self->priv->selection, "changed",
670 G_CALLBACK (contact_blocking_dialog_view_selection_changed), self);
672 /* build the contact entry */
673 self->priv->completion_contacts = gtk_list_store_new (1, G_TYPE_STRING);
674 completion = gtk_entry_completion_new ();
675 gtk_entry_completion_set_model (completion,
676 GTK_TREE_MODEL (self->priv->completion_contacts));
677 gtk_entry_completion_set_text_column (completion, COL_IDENTIFIER);
678 gtk_entry_set_completion (GTK_ENTRY (self->priv->add_contact_entry),
680 g_object_unref (completion);
681 g_object_unref (self->priv->completion_contacts);
683 /* prepare the account manager */
684 am = tp_account_manager_dup ();
685 tp_proxy_prepare_async (am, NULL, contact_blocking_dialog_am_prepared, self);
689 g_object_unref (gui);
693 empathy_contact_blocking_dialog_new (GtkWindow *parent)
695 GtkWidget *self = g_object_new (EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
700 gtk_window_set_transient_for (GTK_WINDOW (self), parent);