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;
49 GtkWidget *account_chooser;
52 enum /* blocked-contacts columns */
60 get_pretty_conn_name (TpConnection *conn)
62 return tp_proxy_get_object_path (conn) + strlen (TP_CONN_OBJECT_PATH_BASE);
66 contact_blocking_dialog_filter_account_chooser (TpAccount *account,
67 EmpathyAccountChooserFilterResultCallback callback,
68 gpointer callback_data,
71 EmpathyContactBlockingDialog *self = user_data;
72 TpConnection *conn = tp_account_get_connection (account);
77 g_hash_table_lookup (self->priv->channels, conn) != NULL;
79 callback (enable, callback_data);
82 static void contact_blocking_dialog_inspected_handles (TpConnection *,
83 const char **, const GError *, gpointer, GObject *);
86 contact_blocking_dialog_add_contacts_to_list (
87 EmpathyContactBlockingDialog *self,
92 tp_cli_connection_call_inspect_handles (conn, -1,
93 TP_HANDLE_TYPE_CONTACT, handles,
94 contact_blocking_dialog_inspected_handles,
95 g_boxed_copy (DBUS_TYPE_G_UINT_ARRAY, handles),
96 (GDestroyNotify) g_array_unref, G_OBJECT (self));
100 contact_blocking_dialog_inspected_handles (TpConnection *conn,
101 const char **identifiers,
102 const GError *in_error,
106 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
107 GArray *handles = user_data;
110 if (in_error != NULL)
112 DEBUG ("Failed to inspect handles: %s", in_error->message);
116 DEBUG ("Adding %u identifiers", handles->len);
118 for (i = 0; i < handles->len; i++)
120 const char *identifier = identifiers[i];
121 TpHandle handle = g_array_index (handles, TpHandle, i);
123 gtk_list_store_insert_with_values (priv->blocked_contacts, NULL, -1,
124 COL_IDENTIFIER, identifier,
130 DECLARE_CALLBACK (contact_blocking_dialog_connection_prepared);
133 contact_blocking_dialog_connection_status_changed (TpAccount *account,
137 const char *dbus_reason,
139 EmpathyContactBlockingDialog *self)
141 TpConnection *conn = tp_account_get_connection (account);
145 case TP_CONNECTION_STATUS_DISCONNECTED:
146 DEBUG ("Connection %s invalidated", get_pretty_conn_name (conn));
148 /* remove the channel from the hash table */
149 g_hash_table_remove (self->priv->channels, conn);
151 /* set the filter again to refilter the account list */
152 empathy_account_chooser_set_filter (
153 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser),
154 contact_blocking_dialog_filter_account_chooser, self);
157 case TP_CONNECTION_STATUS_CONNECTING:
160 case TP_CONNECTION_STATUS_CONNECTED:
161 DEBUG ("Connection %s reconnected", get_pretty_conn_name (conn));
163 tp_proxy_prepare_async (conn, NULL,
164 contact_blocking_dialog_connection_prepared, self);
169 contact_blocking_dialog_deny_channel_members_changed (TpChannel *channel,
173 GArray *local_pending,
174 GArray *remote_pending,
177 EmpathyContactBlockingDialog *self)
179 TpConnection *conn = tp_channel_borrow_connection (channel);
180 GtkTreeModel *model = GTK_TREE_MODEL (self->priv->blocked_contacts);
182 TpIntset *removed_set;
185 /* we only care about changes to the selected connection */
186 /* FIXME: can we compare proxy pointers directly? */
188 tp_proxy_get_object_path (tp_channel_borrow_connection (channel)),
189 tp_proxy_get_object_path (empathy_account_chooser_get_connection (
190 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser)))))
193 DEBUG ("deny list changed: %u added, %u removed", added->len, removed->len);
196 contact_blocking_dialog_add_contacts_to_list (self, conn, added);
198 /* remove contacts */
199 removed_set = tp_intset_from_array (removed);
201 valid = gtk_tree_model_get_iter_first (model, &iter);
206 gtk_tree_model_get (model, &iter,
210 if (tp_intset_is_member (removed_set, handle))
211 valid = gtk_list_store_remove (self->priv->blocked_contacts, &iter);
213 valid = gtk_tree_model_iter_next (model, &iter);
216 tp_intset_destroy (removed_set);
219 DECLARE_CALLBACK (contact_blocking_dialog_account_prepared);
222 contact_blocking_dialog_am_prepared (GObject *am,
223 GAsyncResult *result,
226 EmpathyContactBlockingDialog *self = user_data;
227 GList *accounts, *ptr;
228 GError *error = NULL;
230 if (!tp_proxy_prepare_finish (am, result, &error))
232 g_critical ("Could not prepare Account Manager: %s", error->message);
233 g_error_free (error);
237 accounts = tp_account_manager_get_valid_accounts (TP_ACCOUNT_MANAGER (am));
239 for (ptr = accounts; ptr != NULL; ptr = ptr->next)
241 TpAccount *account = ptr->data;
243 tp_proxy_prepare_async (account, NULL,
244 contact_blocking_dialog_account_prepared, self);
247 g_list_free (accounts);
251 contact_blocking_dialog_account_prepared (GObject *account,
252 GAsyncResult *result,
255 EmpathyContactBlockingDialog *self = user_data;
257 GError *error = NULL;
259 if (!tp_proxy_prepare_finish (account, result, &error))
261 DEBUG ("Could not prepare Account: %s", error->message);
262 g_error_free (error);
266 g_signal_connect (account, "status-changed",
267 G_CALLBACK (contact_blocking_dialog_connection_status_changed), self);
269 conn = tp_account_get_connection (TP_ACCOUNT (account));
273 tp_proxy_prepare_async (conn, NULL,
274 contact_blocking_dialog_connection_prepared, self);
278 static void contact_blocking_dialog_got_deny_channel (TpConnection *,
279 gboolean, const char *, GHashTable *, const GError *, gpointer, GObject *);
282 contact_blocking_dialog_connection_prepared (GObject *conn,
283 GAsyncResult *result,
286 EmpathyContactBlockingDialog *self = user_data;
288 GError *error = NULL;
290 if (!tp_proxy_prepare_finish (conn, result, &error))
292 DEBUG ("Failed to prepare connection: %s", error->message);
293 g_error_free (error);
297 /* request the deny channel */
298 request = tp_asv_new (
299 TP_PROP_CHANNEL_CHANNEL_TYPE,
301 TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
303 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
307 TP_PROP_CHANNEL_TARGET_ID,
313 tp_cli_connection_interface_requests_call_ensure_channel (
314 TP_CONNECTION (conn), -1, request,
315 contact_blocking_dialog_got_deny_channel, NULL, NULL, G_OBJECT (self));
317 g_hash_table_destroy (request);
320 DECLARE_CALLBACK (contact_blocking_dialog_deny_channel_prepared);
323 contact_blocking_dialog_got_deny_channel (TpConnection *conn,
325 const char *channel_path,
327 const GError *in_error,
332 GError *error = NULL;
334 const GQuark features[] = {
335 TP_CHANNEL_FEATURE_CORE,
336 TP_CHANNEL_FEATURE_GROUP,
339 if (in_error != NULL)
341 DEBUG ("Failed to get 'deny' channel: %s", in_error->message);
345 channel = tp_channel_new_from_properties (conn, channel_path, props, &error);
349 DEBUG ("Failed to create channel proxy: %s", error->message);
350 g_error_free (error);
354 tp_proxy_prepare_async (channel, features,
355 contact_blocking_dialog_deny_channel_prepared, self);
359 contact_blocking_dialog_deny_channel_prepared (GObject *channel,
360 GAsyncResult *result,
363 EmpathyContactBlockingDialog *self = user_data;
365 GError *error = NULL;
367 if (!tp_proxy_prepare_finish (channel, result, &error))
369 DEBUG ("Failed to prepare channel: %s", error->message);
370 g_error_free (error);
374 conn = tp_channel_borrow_connection (TP_CHANNEL (channel));
376 DEBUG ("Channel prepared for connection %s", get_pretty_conn_name (conn));
378 g_hash_table_insert (self->priv->channels,
379 g_object_ref (conn), channel);
381 /* set the filter again to refilter the account list */
382 empathy_account_chooser_set_filter (
383 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser),
384 contact_blocking_dialog_filter_account_chooser, self);
386 g_signal_connect (channel, "group-members-changed",
387 G_CALLBACK (contact_blocking_dialog_deny_channel_members_changed), self);
391 contact_blocking_dialog_account_changed (GtkWidget *account_chooser,
392 EmpathyContactBlockingDialog *self)
394 TpConnection *conn = empathy_account_chooser_get_connection (
395 EMPATHY_ACCOUNT_CHOOSER (account_chooser));
402 DEBUG ("Account changed: %s", get_pretty_conn_name (conn));
404 /* FIXME: clear the completion, get the new blocked list */
406 /* clear the list of blocked contacts */
407 gtk_list_store_clear (self->priv->blocked_contacts);
409 /* load the deny list */
410 channel = g_hash_table_lookup (self->priv->channels, conn);
412 g_return_if_fail (TP_IS_CHANNEL (channel));
414 blocked = tp_intset_to_array (tp_channel_group_get_members (channel));
416 DEBUG ("%u contacts on blocked list", blocked->len);
418 contact_blocking_dialog_add_contacts_to_list (self, conn, blocked);
420 g_array_unref (blocked);
424 contact_blocking_dialog_dispose (GObject *self)
426 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
428 tp_clear_pointer (&priv->channels, g_hash_table_destroy);
432 empathy_contact_blocking_dialog_class_init (
433 EmpathyContactBlockingDialogClass *klass)
435 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
437 gobject_class->dispose = contact_blocking_dialog_dispose;
439 g_type_class_add_private (gobject_class,
440 sizeof (EmpathyContactBlockingDialogPrivate));
444 empathy_contact_blocking_dialog_init (EmpathyContactBlockingDialog *self)
449 GtkWidget *account_hbox, *add_contact_entry;
450 TpAccountManager *am;
452 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
453 EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
454 EmpathyContactBlockingDialogPrivate);
456 self->priv->channels = g_hash_table_new_full (NULL, NULL,
457 g_object_unref, g_object_unref);
459 gtk_window_set_title (GTK_WINDOW (self), _("Edit Blocked Contacts"));
460 gtk_dialog_add_button (GTK_DIALOG (self),
461 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
463 filename = empathy_file_lookup ("empathy-contact-blocking-dialog.ui",
466 gui = empathy_builder_get_file (filename,
467 "contents", &contents,
468 "account-hbox", &account_hbox,
469 "add-contact-entry", &add_contact_entry,
470 "blocked-contacts", &self->priv->blocked_contacts,
473 /* add the contents to the dialog */
475 GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (self))),
477 gtk_widget_show (contents);
479 /* add the account chooser */
480 self->priv->account_chooser = empathy_account_chooser_new ();
481 empathy_account_chooser_set_filter (
482 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser),
483 contact_blocking_dialog_filter_account_chooser, self);
484 g_signal_connect (self->priv->account_chooser, "changed",
485 G_CALLBACK (contact_blocking_dialog_account_changed), self);
487 gtk_box_pack_start (GTK_BOX (account_hbox), self->priv->account_chooser,
489 gtk_widget_show (self->priv->account_chooser);
491 /* build the contact entry */
494 /* prepare the account manager */
495 am = tp_account_manager_dup ();
496 tp_proxy_prepare_async (am, NULL, contact_blocking_dialog_am_prepared, self);
499 g_object_unref (gui);
503 empathy_contact_blocking_dialog_new (GtkWindow *parent)
505 GtkWidget *self = g_object_new (EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
510 gtk_window_set_transient_for (GTK_WINDOW (self), parent);