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>
26 #include <glib/gi18n-lib.h>
28 #include <libempathy/empathy-utils.h>
30 #include <libempathy/empathy-contact-manager.h>
31 #include <libempathy/empathy-tp-contact-list.h>
33 #include <libempathy-gtk/empathy-account-chooser.h>
34 #include <libempathy-gtk/empathy-ui-utils.h>
36 #include "empathy-contact-blocking-dialog.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
39 #include <libempathy/empathy-debug.h>
41 #define GET_PRIVATE(o) (EMPATHY_CONTACT_BLOCKING_DIALOG (o)->priv)
42 #define DECLARE_CALLBACK(func) \
43 static void func (GObject *, GAsyncResult *, gpointer);
45 G_DEFINE_TYPE (EmpathyContactBlockingDialog, empathy_contact_blocking_dialog,
48 struct _EmpathyContactBlockingDialogPrivate
50 guint block_account_changed;
52 GtkListStore *blocked_contacts;
53 GtkListStore *completion_contacts;
54 GtkTreeSelection *selection;
56 GtkWidget *account_chooser;
57 GtkWidget *add_button;
58 GtkWidget *add_contact_entry;
60 GtkWidget *info_bar_label;
61 GtkWidget *remove_button;
63 TpConnection *current_conn;
66 enum /* blocked-contacts columns */
68 COL_BLOCKED_IDENTIFIER,
73 enum /* completion_contacts columns */
75 COL_COMPLETION_IDENTIFIER,
81 get_pretty_conn_name (TpConnection *conn)
83 return tp_proxy_get_object_path (conn) + strlen (TP_CONN_OBJECT_PATH_BASE);
87 contact_blocking_dialog_filter_account_chooser (TpAccount *account,
88 EmpathyAccountChooserFilterResultCallback callback,
89 gpointer callback_data,
92 TpConnection *conn = tp_account_get_connection (account);
97 tp_proxy_has_interface_by_id (conn,
98 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING);
100 callback (enable, callback_data);
103 static void contact_blocking_dialog_account_changed (GtkWidget *,
104 EmpathyContactBlockingDialog *);
107 contact_blocking_dialog_refilter_account_chooser (
108 EmpathyContactBlockingDialog *self)
110 EmpathyAccountChooser *chooser =
111 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser);
115 DEBUG ("Refiltering account chooser");
117 /* set the filter to refilter the account chooser */
118 self->priv->block_account_changed++;
119 empathy_account_chooser_set_filter (chooser,
120 contact_blocking_dialog_filter_account_chooser, self);
121 self->priv->block_account_changed--;
123 conn = empathy_account_chooser_get_connection (chooser);
124 enabled = (empathy_account_chooser_get_account (chooser) != NULL &&
126 tp_proxy_has_interface_by_id (conn,
127 TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING));
130 DEBUG ("No account selected");
132 gtk_widget_set_sensitive (self->priv->add_button, enabled);
133 gtk_widget_set_sensitive (self->priv->add_contact_entry, enabled);
135 contact_blocking_dialog_account_changed (self->priv->account_chooser, self);
139 contact_blocking_dialog_add_blocked (
140 EmpathyContactBlockingDialog *self,
143 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
149 for (i = 0; i < blocked->len; i++)
151 TpContact *contact = g_ptr_array_index (blocked, i);
153 gtk_list_store_insert_with_values (priv->blocked_contacts, NULL, -1,
154 COL_BLOCKED_IDENTIFIER, tp_contact_get_identifier (contact),
155 COL_BLOCKED_CONTACT, contact,
161 blocked_contacts_changed_cb (TpConnection *conn,
164 EmpathyContactBlockingDialog *self)
166 GtkTreeModel *model = GTK_TREE_MODEL (self->priv->blocked_contacts);
170 DEBUG ("blocked contacts changed on %s: %u added, %u removed",
171 get_pretty_conn_name (conn), added->len, removed->len);
174 contact_blocking_dialog_add_blocked (self, added);
176 /* remove contacts */
177 valid = gtk_tree_model_get_iter_first (model, &iter);
182 gtk_tree_model_get (model, &iter,
183 COL_BLOCKED_CONTACT, &contact,
186 if (tp_g_ptr_array_contains (removed, contact))
187 valid = gtk_list_store_remove (self->priv->blocked_contacts, &iter);
189 valid = gtk_tree_model_iter_next (model, &iter);
191 g_object_unref (contact);
196 contact_blocking_dialog_connection_status_changed (TpAccount *account,
200 const char *dbus_reason,
202 EmpathyContactBlockingDialog *self)
204 TpConnection *conn = tp_account_get_connection (account);
208 case TP_CONNECTION_STATUS_DISCONNECTED:
209 DEBUG ("Connection %s invalidated", get_pretty_conn_name (conn));
211 contact_blocking_dialog_refilter_account_chooser (self);
214 case TP_CONNECTION_STATUS_CONNECTING:
217 case TP_CONNECTION_STATUS_CONNECTED:
218 DEBUG ("Connection %s reconnected", get_pretty_conn_name (conn));
220 contact_blocking_dialog_refilter_account_chooser (self);
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_g_signal_connect_object (account, "status-changed",
247 G_CALLBACK (contact_blocking_dialog_connection_status_changed),
250 contact_blocking_dialog_refilter_account_chooser (self);
253 g_list_free (accounts);
257 contact_blocking_dialog_set_error (EmpathyContactBlockingDialog *self,
260 const char *msg = NULL;
262 if (error->domain == TP_ERRORS)
264 if (error->code == TP_ERROR_INVALID_HANDLE)
265 msg = _("Unknown or invalid identifier");
266 else if (error->code == TP_ERROR_NOT_AVAILABLE)
267 msg = _("Contact blocking temporarily unavailable");
268 else if (error->code == TP_ERROR_NOT_CAPABLE)
269 msg = _("Contact blocking unavailable");
270 else if (error->code == TP_ERROR_PERMISSION_DENIED)
271 msg = _("Permission Denied");
275 msg = _("Could not block contact");
277 gtk_label_set_text (GTK_LABEL (self->priv->info_bar_label), msg);
278 gtk_widget_show (self->priv->info_bar);
282 block_cb (GObject *source,
283 GAsyncResult *result,
286 EmpathyContactBlockingDialog *self = user_data;
287 GError *error = NULL;
289 if (!tp_contact_block_finish (TP_CONTACT (source), result,
292 DEBUG ("Error blocking contacts: %s", error->message);
294 contact_blocking_dialog_set_error (
295 EMPATHY_CONTACT_BLOCKING_DIALOG (self), error);
297 g_error_free (error);
301 DEBUG ("Contact blocked");
305 block_contact_got_contact(TpConnection *conn,
307 TpContact * const *contacts,
308 const gchar * const *requested_ids,
309 GHashTable *failed_id_errors,
312 GObject *weak_object)
314 EmpathyContactBlockingDialog *self =
315 EMPATHY_CONTACT_BLOCKING_DIALOG (weak_object);
316 gchar *id = user_data;
321 error = g_hash_table_lookup (failed_id_errors, id);
325 tp_contact_block_async (contacts[0], FALSE, block_cb, self);
329 DEBUG ("Error getting contact on %s: %s",
330 get_pretty_conn_name (conn), error->message);
332 contact_blocking_dialog_set_error (
333 EMPATHY_CONTACT_BLOCKING_DIALOG (self), error);
340 contact_blocking_dialog_add_contact (GtkWidget *widget,
341 EmpathyContactBlockingDialog *self)
343 TpConnection *conn = empathy_account_chooser_get_connection (
344 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
345 const char *identifiers[2] = { NULL, };
347 identifiers[0] = gtk_entry_get_text (
348 GTK_ENTRY (self->priv->add_contact_entry));
350 DEBUG ("Looking up handle for '%s' on %s",
351 identifiers[0], get_pretty_conn_name (conn));
353 tp_connection_get_contacts_by_id (conn, 1, identifiers,
354 0, NULL, block_contact_got_contact,
355 g_strdup (identifiers[0]), NULL, G_OBJECT (self));
357 gtk_entry_set_text (GTK_ENTRY (self->priv->add_contact_entry), "");
358 gtk_widget_hide (self->priv->info_bar);
362 unblock_cb (GObject *source,
363 GAsyncResult *result,
366 EmpathyContactBlockingDialog *self = user_data;
367 GError *error = NULL;
369 if (!tp_connection_unblock_contacts_finish (TP_CONNECTION (source), result,
372 DEBUG ("Error unblocking contacts: %s", error->message);
374 contact_blocking_dialog_set_error (
375 EMPATHY_CONTACT_BLOCKING_DIALOG (self), error);
377 g_error_free (error);
381 DEBUG ("Contacts unblocked");
385 contact_blocking_dialog_remove_contacts (GtkWidget *button,
386 EmpathyContactBlockingDialog *self)
388 TpConnection *conn = empathy_account_chooser_get_connection (
389 EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser));
394 rows = gtk_tree_selection_get_selected_rows (self->priv->selection, &model);
396 contacts = g_ptr_array_new_with_free_func (g_object_unref);
398 for (ptr = rows; ptr != NULL; ptr = ptr->next)
400 GtkTreePath *path = ptr->data;
404 if (!gtk_tree_model_get_iter (model, &iter, path))
407 gtk_tree_model_get (model, &iter,
408 COL_BLOCKED_CONTACT, &contact,
411 g_ptr_array_add (contacts, contact);
413 gtk_tree_path_free (path);
418 if (contacts->len > 0)
420 DEBUG ("Unblocking %u contacts", contacts->len);
422 tp_connection_unblock_contacts_async (conn, contacts->len,
423 (TpContact * const *) contacts->pdata, unblock_cb, self);
426 g_ptr_array_unref (contacts);
430 contact_blocking_dialog_account_changed (GtkWidget *account_chooser,
431 EmpathyContactBlockingDialog *self)
433 TpConnection *conn = empathy_account_chooser_get_connection (
434 EMPATHY_ACCOUNT_CHOOSER (account_chooser));
436 EmpathyContactManager *contact_manager;
437 EmpathyTpContactList *contact_list;
438 GList *members, *ptr;
440 if (self->priv->block_account_changed > 0)
443 if (conn == self->priv->current_conn)
446 /* clear the lists of contacts */
447 gtk_list_store_clear (self->priv->blocked_contacts);
448 gtk_list_store_clear (self->priv->completion_contacts);
450 if (self->priv->current_conn != NULL)
452 g_signal_handlers_disconnect_by_func (self->priv->current_conn,
453 blocked_contacts_changed_cb, self);
455 g_clear_object (&self->priv->current_conn);
461 DEBUG ("Account changed: %s", get_pretty_conn_name (conn));
463 self->priv->current_conn = g_object_ref (conn);
465 tp_g_signal_connect_object (conn, "blocked-contacts-changed",
466 G_CALLBACK (blocked_contacts_changed_cb), self, 0);
468 blocked = tp_connection_get_blocked_contacts (conn);
470 DEBUG ("%u contacts blocked on %s",
471 blocked != NULL ? blocked->len : 0, get_pretty_conn_name (conn));
473 contact_blocking_dialog_add_blocked (self, blocked);
475 /* load the completion list */
476 g_return_if_fail (empathy_contact_manager_initialized ());
478 DEBUG ("Loading contacts");
480 contact_manager = empathy_contact_manager_dup_singleton ();
481 contact_list = empathy_contact_manager_get_list (contact_manager, conn);
482 members = empathy_contact_list_get_members (
483 EMPATHY_CONTACT_LIST (contact_list));
485 for (ptr = members; ptr != NULL; ptr = ptr->next)
487 EmpathyContact *contact = ptr->data;
490 tmpstr = g_strdup_printf ("%s (%s)",
491 empathy_contact_get_alias (contact),
492 empathy_contact_get_id (contact));
494 gtk_list_store_insert_with_values (self->priv->completion_contacts,
496 COL_COMPLETION_IDENTIFIER, empathy_contact_get_id (contact),
497 COL_COMPLETION_TEXT, tmpstr,
501 g_object_unref (contact);
504 g_list_free (members);
505 g_object_unref (contact_manager);
509 contact_blocking_dialog_view_selection_changed (GtkTreeSelection *selection,
510 EmpathyContactBlockingDialog *self)
512 GList *rows = gtk_tree_selection_get_selected_rows (selection, NULL);
514 /* update the sensitivity of the remove button */
515 gtk_widget_set_sensitive (self->priv->remove_button, rows != NULL);
517 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
522 contact_selector_dialog_match_func (GtkEntryCompletion *completion,
531 model = gtk_entry_completion_get_model (completion);
532 if (model == NULL || iter == NULL)
535 gtk_tree_model_get (model, iter, COL_COMPLETION_TEXT, &str, -1);
536 lower = g_utf8_strdown (str, -1);
537 if (strstr (lower, key))
539 DEBUG ("Key %s is matching name **%s**", key, str);
546 gtk_tree_model_get (model, iter, COL_COMPLETION_IDENTIFIER, &str, -1);
547 lower = g_utf8_strdown (str, -1);
548 if (strstr (lower, key))
550 DEBUG ("Key %s is matching ID **%s**", key, str);
563 contact_selector_dialog_match_selected_cb (GtkEntryCompletion *widget,
566 EmpathyContactBlockingDialog *self)
570 if (iter == NULL || model == NULL)
573 gtk_tree_model_get (model, iter, COL_COMPLETION_IDENTIFIER, &id, -1);
574 gtk_entry_set_text (GTK_ENTRY (self->priv->add_contact_entry), id);
576 DEBUG ("Got selected match **%s**", id);
584 contact_blocking_dialog_dispose (GObject *self)
586 EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
588 g_clear_object (&priv->current_conn);
590 G_OBJECT_CLASS (empathy_contact_blocking_dialog_parent_class)->dispose (self);
594 empathy_contact_blocking_dialog_class_init (
595 EmpathyContactBlockingDialogClass *klass)
597 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
599 gobject_class->dispose = contact_blocking_dialog_dispose;
601 g_type_class_add_private (gobject_class,
602 sizeof (EmpathyContactBlockingDialogPrivate));
606 empathy_contact_blocking_dialog_init (EmpathyContactBlockingDialog *self)
611 GtkWidget *account_hbox, *blocked_contacts_view, *blocked_contacts_sw,
613 GtkEntryCompletion *completion;
614 TpAccountManager *am;
615 GtkStyleContext *context;
616 TpSimpleClientFactory *factory;
618 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
619 EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
620 EmpathyContactBlockingDialogPrivate);
622 gtk_window_set_title (GTK_WINDOW (self), _("Edit Blocked Contacts"));
623 gtk_dialog_add_button (GTK_DIALOG (self),
624 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
626 filename = empathy_file_lookup ("empathy-contact-blocking-dialog.ui",
629 gui = empathy_builder_get_file (filename,
630 "contents", &contents,
631 "account-hbox", &account_hbox,
632 "add-button", &self->priv->add_button,
633 "add-contact-entry", &self->priv->add_contact_entry,
634 "blocked-contacts", &self->priv->blocked_contacts,
635 "blocked-contacts-sw", &blocked_contacts_sw,
636 "blocked-contacts-view", &blocked_contacts_view,
637 "remove-button", &self->priv->remove_button,
638 "remove-toolbar", &remove_toolbar,
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 /* join the remove toolbar to the treeview */
648 context = gtk_widget_get_style_context (blocked_contacts_sw);
649 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
650 context = gtk_widget_get_style_context (remove_toolbar);
651 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
653 /* add the contents to the dialog */
655 GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (self))),
657 gtk_widget_show (contents);
659 /* set up the tree selection */
660 self->priv->selection = gtk_tree_view_get_selection (
661 GTK_TREE_VIEW (blocked_contacts_view));
662 gtk_tree_selection_set_mode (self->priv->selection, GTK_SELECTION_MULTIPLE);
663 g_signal_connect (self->priv->selection, "changed",
664 G_CALLBACK (contact_blocking_dialog_view_selection_changed), self);
666 /* build the contact entry */
667 self->priv->completion_contacts = gtk_list_store_new (N_COMPLETION_COLUMNS,
668 G_TYPE_STRING, /* id */
669 G_TYPE_STRING, /* text */
670 TP_TYPE_CONTACT); /* contact */
672 completion = gtk_entry_completion_new ();
673 gtk_entry_completion_set_model (completion,
674 GTK_TREE_MODEL (self->priv->completion_contacts));
675 gtk_entry_completion_set_text_column (completion, COL_COMPLETION_TEXT);
676 gtk_entry_completion_set_match_func (completion,
677 contact_selector_dialog_match_func,
679 g_signal_connect (completion, "match-selected",
680 G_CALLBACK (contact_selector_dialog_match_selected_cb),
682 gtk_entry_set_completion (GTK_ENTRY (self->priv->add_contact_entry),
684 g_object_unref (completion);
685 g_object_unref (self->priv->completion_contacts);
687 /* add the account chooser */
688 self->priv->account_chooser = empathy_account_chooser_new ();
689 contact_blocking_dialog_refilter_account_chooser (self);
690 g_signal_connect (self->priv->account_chooser, "changed",
691 G_CALLBACK (contact_blocking_dialog_account_changed), self);
693 gtk_box_pack_start (GTK_BOX (account_hbox), self->priv->account_chooser,
695 gtk_widget_show (self->priv->account_chooser);
697 /* add an error warning info bar */
698 self->priv->info_bar = gtk_info_bar_new ();
699 gtk_box_pack_start (GTK_BOX (contents), self->priv->info_bar, FALSE, TRUE, 0);
700 gtk_info_bar_set_message_type (GTK_INFO_BAR (self->priv->info_bar),
703 self->priv->info_bar_label = gtk_label_new ("");
704 gtk_container_add (GTK_CONTAINER (
705 gtk_info_bar_get_content_area (GTK_INFO_BAR (self->priv->info_bar))),
706 self->priv->info_bar_label);
707 gtk_widget_show (self->priv->info_bar_label);
709 /* prepare the account manager */
710 am = tp_account_manager_dup ();
712 factory = tp_proxy_get_factory (am);
713 tp_simple_client_factory_add_connection_features_varargs (factory,
714 TP_CONNECTION_FEATURE_CONTACT_BLOCKING, NULL);
716 tp_proxy_prepare_async (am, NULL, contact_blocking_dialog_am_prepared, self);
720 g_object_unref (gui);
724 empathy_contact_blocking_dialog_new (GtkWindow *parent)
726 GtkWidget *self = g_object_new (EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
731 gtk_window_set_transient_for (GTK_WINDOW (self), parent);