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-gtk/empathy-account-chooser.h>
30 #include <libempathy-gtk/empathy-ui-utils.h>
32 #include "empathy-contact-blocking-dialog.h"
34 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
35 #include <libempathy/empathy-debug.h>
37 #define GET_PRIVATE(o) (EMPATHY_CONTACT_BLOCKING_DIALOG (o)->priv)
38 #define DECLARE_CALLBACK(func) \
39 static void func (GObject *, GAsyncResult *, gpointer);
41 G_DEFINE_TYPE (EmpathyContactBlockingDialog, empathy_contact_blocking_dialog,
44 struct _EmpathyContactBlockingDialogPrivate
46 GHashTable *channels; /* TpConnection* -> TpChannel* */
47 GtkListStore *blocked_contacts;
48 GtkTreeSelection *selection;
50 GtkWidget *account_chooser;
51 GtkWidget *add_contact_entry;
52 GtkWidget *remove_button;
55 enum /* blocked-contacts columns */
63 get_pretty_conn_name (TpConnection *conn)
65 return tp_proxy_get_object_path (conn) + strlen (TP_CONN_OBJECT_PATH_BASE);
69 contact_blocking_dialog_filter_account_chooser (TpAccount *account,
70 EmpathyAccountChooserFilterResultCallback callback,
71 gpointer callback_data,
74 EmpathyContactBlockingDialog *self = user_data;
75 TpConnection *conn = tp_account_get_connection (account);
80 g_hash_table_lookup (self->priv->channels, conn) != NULL;
82 callback (enable, callback_data);
85 static void contact_blocking_dialog_inspected_handles (TpConnection *,
86 const char **, const GError *, gpointer, GObject *);
89 contact_blocking_dialog_add_contacts_to_list (
90 EmpathyContactBlockingDialog *self,
95 tp_cli_connection_call_inspect_handles (conn, -1,
96 TP_HANDLE_TYPE_CONTACT, handles,
97 contact_blocking_dialog_inspected_handles,
98 g_boxed_copy (DBUS_TYPE_G_UINT_ARRAY, handles),
99 (GDestroyNotify) g_array_unref, G_OBJECT (self));
103 contact_blocking_dialog_inspected_handles (TpConnection *conn,
104 const char **identifiers,
105 const GError *in_error,
109 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
110 GArray *handles = user_data;
113 if (in_error != NULL)
115 DEBUG ("Failed to inspect handles: %s", in_error->message);
119 DEBUG ("Adding %u identifiers", handles->len);
121 for (i = 0; i < handles->len; i++)
123 const char *identifier = identifiers[i];
124 TpHandle handle = g_array_index (handles, TpHandle, i);
126 gtk_list_store_insert_with_values (priv->blocked_contacts, NULL, -1,
127 COL_IDENTIFIER, identifier,
133 DECLARE_CALLBACK (contact_blocking_dialog_connection_prepared);
136 contact_blocking_dialog_connection_status_changed (TpAccount *account,
140 const char *dbus_reason,
142 EmpathyContactBlockingDialog *self)
144 TpConnection *conn = tp_account_get_connection (account);
148 case TP_CONNECTION_STATUS_DISCONNECTED:
149 DEBUG ("Connection %s invalidated", get_pretty_conn_name (conn));
151 /* remove the channel from the hash table */
152 g_hash_table_remove (self->priv->channels, conn);
154 /* set the filter again to refilter the account list */
155 empathy_account_chooser_set_filter (
156 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser),
157 contact_blocking_dialog_filter_account_chooser, self);
160 case TP_CONNECTION_STATUS_CONNECTING:
163 case TP_CONNECTION_STATUS_CONNECTED:
164 DEBUG ("Connection %s reconnected", get_pretty_conn_name (conn));
166 tp_proxy_prepare_async (conn, NULL,
167 contact_blocking_dialog_connection_prepared, self);
172 contact_blocking_dialog_deny_channel_members_changed (TpChannel *channel,
176 GArray *local_pending,
177 GArray *remote_pending,
180 EmpathyContactBlockingDialog *self)
182 TpConnection *conn = tp_channel_borrow_connection (channel);
183 GtkTreeModel *model = GTK_TREE_MODEL (self->priv->blocked_contacts);
185 TpIntset *removed_set;
188 /* we only care about changes to the selected connection */
189 /* FIXME: can we compare proxy pointers directly? */
191 tp_proxy_get_object_path (tp_channel_borrow_connection (channel)),
192 tp_proxy_get_object_path (empathy_account_chooser_get_connection (
193 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser)))))
196 DEBUG ("deny list changed: %u added, %u removed", added->len, removed->len);
199 contact_blocking_dialog_add_contacts_to_list (self, conn, added);
201 /* remove contacts */
202 removed_set = tp_intset_from_array (removed);
204 valid = gtk_tree_model_get_iter_first (model, &iter);
209 gtk_tree_model_get (model, &iter,
213 if (tp_intset_is_member (removed_set, handle))
214 valid = gtk_list_store_remove (self->priv->blocked_contacts, &iter);
216 valid = gtk_tree_model_iter_next (model, &iter);
219 tp_intset_destroy (removed_set);
222 DECLARE_CALLBACK (contact_blocking_dialog_account_prepared);
225 contact_blocking_dialog_am_prepared (GObject *am,
226 GAsyncResult *result,
229 EmpathyContactBlockingDialog *self = user_data;
230 GList *accounts, *ptr;
231 GError *error = NULL;
233 if (!tp_proxy_prepare_finish (am, result, &error))
235 g_critical ("Could not prepare Account Manager: %s", error->message);
236 g_error_free (error);
240 accounts = tp_account_manager_get_valid_accounts (TP_ACCOUNT_MANAGER (am));
242 for (ptr = accounts; ptr != NULL; ptr = ptr->next)
244 TpAccount *account = ptr->data;
246 tp_proxy_prepare_async (account, NULL,
247 contact_blocking_dialog_account_prepared, self);
250 g_list_free (accounts);
254 contact_blocking_dialog_account_prepared (GObject *account,
255 GAsyncResult *result,
258 EmpathyContactBlockingDialog *self = user_data;
260 GError *error = NULL;
262 if (!tp_proxy_prepare_finish (account, result, &error))
264 DEBUG ("Could not prepare Account: %s", error->message);
265 g_error_free (error);
269 g_signal_connect (account, "status-changed",
270 G_CALLBACK (contact_blocking_dialog_connection_status_changed), self);
272 conn = tp_account_get_connection (TP_ACCOUNT (account));
276 tp_proxy_prepare_async (conn, NULL,
277 contact_blocking_dialog_connection_prepared, self);
281 static void contact_blocking_dialog_got_deny_channel (TpConnection *,
282 gboolean, const char *, GHashTable *, const GError *, gpointer, GObject *);
285 contact_blocking_dialog_connection_prepared (GObject *conn,
286 GAsyncResult *result,
289 EmpathyContactBlockingDialog *self = user_data;
291 GError *error = NULL;
293 if (!tp_proxy_prepare_finish (conn, result, &error))
295 DEBUG ("Failed to prepare connection: %s", error->message);
296 g_error_free (error);
300 /* request the deny channel */
301 request = tp_asv_new (
302 TP_PROP_CHANNEL_CHANNEL_TYPE,
304 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
306 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
310 TP_PROP_CHANNEL_TARGET_ID,
316 tp_cli_connection_interface_requests_call_ensure_channel (
317 TP_CONNECTION (conn), -1, request,
318 contact_blocking_dialog_got_deny_channel, NULL, NULL, G_OBJECT (self));
320 g_hash_table_destroy (request);
323 DECLARE_CALLBACK (contact_blocking_dialog_deny_channel_prepared);
326 contact_blocking_dialog_got_deny_channel (TpConnection *conn,
328 const char *channel_path,
330 const GError *in_error,
335 GError *error = NULL;
337 const GQuark features[] = {
338 TP_CHANNEL_FEATURE_CORE,
339 TP_CHANNEL_FEATURE_GROUP,
342 if (in_error != NULL)
344 DEBUG ("Failed to get 'deny' channel: %s", in_error->message);
348 channel = tp_channel_new_from_properties (conn, channel_path, props, &error);
352 DEBUG ("Failed to create channel proxy: %s", error->message);
353 g_error_free (error);
357 tp_proxy_prepare_async (channel, features,
358 contact_blocking_dialog_deny_channel_prepared, self);
362 contact_blocking_dialog_deny_channel_prepared (GObject *channel,
363 GAsyncResult *result,
366 EmpathyContactBlockingDialog *self = user_data;
368 GError *error = NULL;
370 if (!tp_proxy_prepare_finish (channel, result, &error))
372 DEBUG ("Failed to prepare channel: %s", error->message);
373 g_error_free (error);
377 conn = tp_channel_borrow_connection (TP_CHANNEL (channel));
379 DEBUG ("Channel prepared for connection %s", get_pretty_conn_name (conn));
381 g_hash_table_insert (self->priv->channels,
382 g_object_ref (conn), channel);
384 /* set the filter again to refilter the account list */
385 empathy_account_chooser_set_filter (
386 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser),
387 contact_blocking_dialog_filter_account_chooser, self);
389 g_signal_connect (channel, "group-members-changed",
390 G_CALLBACK (contact_blocking_dialog_deny_channel_members_changed), self);
393 static void contact_blocking_dialog_add_contact_got_handle (TpConnection *,
394 const GArray *, const GError *, gpointer, GObject *);
397 contact_blocking_dialog_add_contact (GtkWidget *widget,
398 EmpathyContactBlockingDialog *self)
400 TpConnection *conn = empathy_account_chooser_get_connection (
401 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
402 const char *identifiers[2] = { NULL, };
404 identifiers[0] = gtk_entry_get_text (
405 GTK_ENTRY (self->priv->add_contact_entry));
407 DEBUG ("Looking up handle for '%s'", identifiers[0]);
409 tp_cli_connection_call_request_handles (conn, -1,
410 TP_HANDLE_TYPE_CONTACT, identifiers,
411 contact_blocking_dialog_add_contact_got_handle,
412 NULL, NULL, G_OBJECT (self));
414 gtk_entry_set_text (GTK_ENTRY (self->priv->add_contact_entry), "");
418 contact_blocking_dialog_added_contact (TpChannel *, const GError *,
419 gpointer, GObject *);
422 contact_blocking_dialog_add_contact_got_handle (TpConnection *conn,
423 const GArray *handles,
424 const GError *in_error,
428 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
429 TpChannel *channel = g_hash_table_lookup (priv->channels, conn);
431 if (in_error != NULL)
433 DEBUG ("Error getting handle: %s", in_error->message);
434 /* FIXME: expose error to user */
438 g_return_if_fail (handles->len == 1);
440 DEBUG ("Adding handle %u to deny channel",
441 g_array_index (handles, TpHandle, 0));
443 tp_cli_channel_interface_group_call_add_members (channel, -1,
445 contact_blocking_dialog_added_contact, NULL, NULL, self);
449 contact_blocking_dialog_added_contact (TpChannel *channel,
450 const GError *in_error,
454 if (in_error != NULL)
456 DEBUG ("Error adding contact to deny list: %s", in_error->message);
457 /* FIXME: expose error to user */
461 DEBUG ("Contact added");
465 contact_blocking_dialog_removed_contacts (TpChannel *,
466 const GError *, gpointer, GObject *);
469 contact_blocking_dialog_remove_contacts (GtkWidget *button,
470 EmpathyContactBlockingDialog *self)
472 TpConnection *conn = empathy_account_chooser_get_connection (
473 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
474 TpChannel *channel = g_hash_table_lookup (self->priv->channels, conn);
477 GArray *handles = g_array_new (FALSE, FALSE, sizeof (TpHandle));
479 rows = gtk_tree_selection_get_selected_rows (self->priv->selection, &model);
481 for (ptr = rows; ptr != NULL; ptr = ptr->next)
483 GtkTreePath *path = ptr->data;
487 if (!gtk_tree_model_get_iter (model, &iter, path))
490 gtk_tree_model_get (model, &iter,
494 g_array_append_val (handles, handle);
495 gtk_tree_path_free (path);
500 if (handles->len > 0)
502 DEBUG ("Removing %u handles", handles->len);
504 tp_cli_channel_interface_group_call_remove_members (channel, -1,
506 contact_blocking_dialog_removed_contacts,
507 NULL, NULL, G_OBJECT (self));
510 g_array_unref (handles);
514 contact_blocking_dialog_removed_contacts (TpChannel *channel,
515 const GError *in_error,
519 if (in_error != NULL)
521 DEBUG ("Error removing contacts from deny list: %s", in_error->message);
522 /* FIXME: expose error to user */
526 DEBUG ("Contacts removed");
530 contact_blocking_dialog_account_changed (GtkWidget *account_chooser,
531 EmpathyContactBlockingDialog *self)
533 TpConnection *conn = empathy_account_chooser_get_connection (
534 EMPATHY_ACCOUNT_CHOOSER (account_chooser));
541 DEBUG ("Account changed: %s", get_pretty_conn_name (conn));
543 /* FIXME: clear the completion, get the new blocked list */
545 /* clear the list of blocked contacts */
546 gtk_list_store_clear (self->priv->blocked_contacts);
548 /* load the deny list */
549 channel = g_hash_table_lookup (self->priv->channels, conn);
551 g_return_if_fail (TP_IS_CHANNEL (channel));
553 blocked = tp_intset_to_array (tp_channel_group_get_members (channel));
555 DEBUG ("%u contacts on blocked list", blocked->len);
557 contact_blocking_dialog_add_contacts_to_list (self, conn, blocked);
559 g_array_unref (blocked);
563 contact_blocking_dialog_view_selection_changed (GtkTreeSelection *selection,
564 EmpathyContactBlockingDialog *self)
566 GList *rows = gtk_tree_selection_get_selected_rows (selection, NULL);
568 /* update the sensitivity of the remove button */
569 gtk_widget_set_sensitive (self->priv->remove_button, rows != NULL);
571 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
576 contact_blocking_dialog_dispose (GObject *self)
578 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
580 tp_clear_pointer (&priv->channels, g_hash_table_destroy);
584 empathy_contact_blocking_dialog_class_init (
585 EmpathyContactBlockingDialogClass *klass)
587 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
589 gobject_class->dispose = contact_blocking_dialog_dispose;
591 g_type_class_add_private (gobject_class,
592 sizeof (EmpathyContactBlockingDialogPrivate));
596 empathy_contact_blocking_dialog_init (EmpathyContactBlockingDialog *self)
601 GtkWidget *account_hbox, *blocked_contacts_view;
602 TpAccountManager *am;
604 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
605 EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
606 EmpathyContactBlockingDialogPrivate);
608 self->priv->channels = g_hash_table_new_full (NULL, NULL,
609 g_object_unref, g_object_unref);
611 gtk_window_set_title (GTK_WINDOW (self), _("Edit Blocked Contacts"));
612 gtk_dialog_add_button (GTK_DIALOG (self),
613 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
615 filename = empathy_file_lookup ("empathy-contact-blocking-dialog.ui",
618 gui = empathy_builder_get_file (filename,
619 "contents", &contents,
620 "account-hbox", &account_hbox,
621 "add-contact-entry", &self->priv->add_contact_entry,
622 "blocked-contacts", &self->priv->blocked_contacts,
623 "blocked-contacts-view", &blocked_contacts_view,
624 "remove-button", &self->priv->remove_button,
627 empathy_builder_connect (gui, self,
628 "add-button", "clicked", contact_blocking_dialog_add_contact,
629 "add-contact-entry", "activate", contact_blocking_dialog_add_contact,
630 "remove-button", "clicked", contact_blocking_dialog_remove_contacts,
633 /* add the contents to the dialog */
635 GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (self))),
637 gtk_widget_show (contents);
639 /* add the account chooser */
640 self->priv->account_chooser = empathy_account_chooser_new ();
641 empathy_account_chooser_set_filter (
642 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser),
643 contact_blocking_dialog_filter_account_chooser, self);
644 g_signal_connect (self->priv->account_chooser, "changed",
645 G_CALLBACK (contact_blocking_dialog_account_changed), self);
647 gtk_box_pack_start (GTK_BOX (account_hbox), self->priv->account_chooser,
649 gtk_widget_show (self->priv->account_chooser);
651 /* set up the tree selection */
652 self->priv->selection = gtk_tree_view_get_selection (
653 GTK_TREE_VIEW (blocked_contacts_view));
654 gtk_tree_selection_set_mode (self->priv->selection, GTK_SELECTION_MULTIPLE);
655 g_signal_connect (self->priv->selection, "changed",
656 G_CALLBACK (contact_blocking_dialog_view_selection_changed), self);
658 /* build the contact entry */
661 /* prepare the account manager */
662 am = tp_account_manager_dup ();
663 tp_proxy_prepare_async (am, NULL, contact_blocking_dialog_am_prepared, self);
666 g_object_unref (gui);
670 empathy_contact_blocking_dialog_new (GtkWindow *parent)
672 GtkWidget *self = g_object_new (EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
677 gtk_window_set_transient_for (GTK_WINDOW (self), parent);