1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Martyn Russell <martyn@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
29 #include <glib/gi18n-lib.h>
32 #include <telepathy-glib/account-manager.h>
34 #include <libempathy/empathy-utils.h>
36 #include "empathy-ui-utils.h"
37 #include "empathy-account-chooser.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include <libempathy/empathy-debug.h>
43 * SECTION:empathy-account-chooser
44 * @title:EmpathyAccountChooser
45 * @short_description: A widget used to choose from a list of accounts
46 * @include: libempathy-gtk/empathy-account-chooser.h
48 * #EmpathyAccountChooser is a widget which extends #GtkComboBox to provide
49 * a chooser of available accounts.
53 * EmpathyAccountChooser:
54 * @parent: parent object
56 * Widget which extends #GtkComboBox to provide a chooser of available accounts.
59 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountChooser)
61 TpAccountManager *manager;
62 gboolean set_active_item;
63 gboolean account_manually_set;
64 gboolean has_all_option;
65 EmpathyAccountChooserFilterFunc filter;
68 } EmpathyAccountChooserPriv;
71 EmpathyAccountChooser *chooser;
79 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
84 static void account_chooser_finalize (GObject *object);
85 static void account_chooser_get_property (GObject *object,
89 static void account_chooser_set_property (GObject *object,
93 static void account_chooser_setup (EmpathyAccountChooser *chooser);
94 static void account_chooser_account_validity_changed_cb (TpAccountManager *manager,
97 EmpathyAccountChooser *chooser);
98 static void account_chooser_account_add_foreach (TpAccount *account,
99 EmpathyAccountChooser *chooser);
100 static void account_chooser_account_removed_cb (TpAccountManager *manager,
102 EmpathyAccountChooser *chooser);
103 static void account_chooser_account_remove_foreach (TpAccount *account,
104 EmpathyAccountChooser *chooser);
105 static void account_chooser_update_iter (EmpathyAccountChooser *chooser,
107 static void account_chooser_status_changed_cb (TpAccount *account,
111 gchar *dbus_error_name,
114 static gboolean account_chooser_separator_func (GtkTreeModel *model,
116 EmpathyAccountChooser *chooser);
117 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
120 SetAccountData *data);
132 static guint signals[LAST_SIGNAL] = { 0 };
134 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
137 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
139 GObjectClass *object_class = G_OBJECT_CLASS (klass);
141 object_class->finalize = account_chooser_finalize;
142 object_class->get_property = account_chooser_get_property;
143 object_class->set_property = account_chooser_set_property;
146 * EmpathyAccountChooser:has-all-option:
148 * Have an additional option in the list to mean all accounts.
150 g_object_class_install_property (object_class,
152 g_param_spec_boolean ("has-all-option",
154 "Have a separate option in the list to mean ALL accounts",
159 g_signal_new ("ready",
160 G_OBJECT_CLASS_TYPE (object_class),
164 g_cclosure_marshal_VOID__VOID,
168 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
172 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
174 EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
175 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
177 chooser->priv = priv;
178 priv->set_active_item = FALSE;
179 priv->account_manually_set = FALSE;
181 priv->filter_data = NULL;
183 priv->manager = tp_account_manager_dup ();
185 g_signal_connect (priv->manager, "account-validity-changed",
186 G_CALLBACK (account_chooser_account_validity_changed_cb),
188 g_signal_connect (priv->manager, "account-removed",
189 G_CALLBACK (account_chooser_account_removed_cb),
192 account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
196 account_chooser_finalize (GObject *object)
198 EmpathyAccountChooserPriv *priv = GET_PRIV (object);
200 g_signal_handlers_disconnect_by_func (priv->manager,
201 account_chooser_account_validity_changed_cb,
203 g_signal_handlers_disconnect_by_func (priv->manager,
204 account_chooser_account_removed_cb,
206 g_object_unref (priv->manager);
208 G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
212 account_chooser_get_property (GObject *object,
217 EmpathyAccountChooserPriv *priv;
219 priv = GET_PRIV (object);
222 case PROP_HAS_ALL_OPTION:
223 g_value_set_boolean (value, priv->has_all_option);
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
232 account_chooser_set_property (GObject *object,
237 EmpathyAccountChooserPriv *priv;
239 priv = GET_PRIV (object);
242 case PROP_HAS_ALL_OPTION:
243 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
244 g_value_get_boolean (value));
247 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
253 * empathy_account_chooser_new:
255 * Creates a new #EmpathyAccountChooser.
257 * Return value: A new #EmpathyAccountChooser
260 empathy_account_chooser_new (void)
264 chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
270 * empathy_account_chooser_dup_account:
271 * @chooser: an #EmpathyAccountChooser
273 * Returns the account which is currently selected in the chooser or %NULL
274 * if there is no account selected. The #TpAccount returned should be
275 * unrefed with g_object_unref() when finished with.
277 * Return value: a new ref to the #TpAccount currently selected, or %NULL.
280 empathy_account_chooser_dup_account (EmpathyAccountChooser *chooser)
282 EmpathyAccountChooserPriv *priv;
287 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
289 priv = GET_PRIV (chooser);
291 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
292 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
296 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
302 * empathy_account_chooser_get_connection:
303 * @chooser: an #EmpathyAccountChooser
305 * Returns a borrowed reference to the #TpConnection associated with the
306 * account currently selected. The caller must reference the returned object with
307 * g_object_ref() if it will be kept
309 * Return value: a borrowed reference to the #TpConnection associated with the
310 * account curently selected.
313 empathy_account_chooser_get_connection (EmpathyAccountChooser *chooser)
315 EmpathyAccountChooserPriv *priv;
317 TpConnection *connection;
319 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
321 priv = GET_PRIV (chooser);
323 account = empathy_account_chooser_dup_account (chooser);
325 /* if the returned account is NULL, then the account manager probably
326 * hasn't been prepared yet. It should be safe to return NULL here
328 if (account == NULL) {
332 connection = tp_account_get_connection (account);
333 g_object_unref (account);
339 * empathy_account_chooser_set_account:
340 * @chooser: an #EmpathyAccountChooser
341 * @account: a #TpAccount
343 * Sets the currently selected account to @account, if it exists in the list.
345 * Return value: whether the chooser was set to @account.
348 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
351 EmpathyAccountChooserPriv *priv;
352 GtkComboBox *combobox;
357 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
359 priv = GET_PRIV (chooser);
361 combobox = GTK_COMBO_BOX (chooser);
362 model = gtk_combo_box_get_model (combobox);
363 gtk_combo_box_get_active_iter (combobox, &iter);
365 data.chooser = chooser;
366 data.account = account;
368 gtk_tree_model_foreach (model,
369 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
372 priv->account_manually_set = data.set;
378 * empathy_account_chooser_get_has_all_option:
379 * @chooser: an #EmpathyAccountChooser
381 * Returns whether @chooser has the #EmpathyAccountChooser:has-all-option property
384 * Return value: whether @chooser has the #EmpathyAccountChooser:has-all-option property
388 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
390 EmpathyAccountChooserPriv *priv;
392 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
394 priv = GET_PRIV (chooser);
396 return priv->has_all_option;
400 * empathy_account_chooser_set_has_all_option:
401 * @chooser: an #EmpathyAccountChooser
402 * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option property
404 * Sets the #EmpathyAccountChooser:has-all-option property.
407 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
408 gboolean has_all_option)
410 EmpathyAccountChooserPriv *priv;
411 GtkComboBox *combobox;
416 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
418 priv = GET_PRIV (chooser);
420 if (priv->has_all_option == has_all_option) {
424 combobox = GTK_COMBO_BOX (chooser);
425 model = gtk_combo_box_get_model (combobox);
426 store = GTK_LIST_STORE (model);
428 priv->has_all_option = has_all_option;
431 * The first 2 options are the ALL and separator
434 if (has_all_option) {
435 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
436 (GtkTreeViewRowSeparatorFunc)
437 account_chooser_separator_func,
441 gtk_list_store_prepend (store, &iter);
442 gtk_list_store_set (store, &iter,
443 COL_ACCOUNT_TEXT, NULL,
444 COL_ACCOUNT_ENABLED, TRUE,
445 COL_ACCOUNT_POINTER, NULL,
448 gtk_list_store_prepend (store, &iter);
449 gtk_list_store_set (store, &iter,
450 COL_ACCOUNT_TEXT, _("All"),
451 COL_ACCOUNT_ENABLED, TRUE,
452 COL_ACCOUNT_POINTER, NULL,
455 if (gtk_tree_model_get_iter_first (model, &iter)) {
456 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
457 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
461 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
462 (GtkTreeViewRowSeparatorFunc)
468 g_object_notify (G_OBJECT (chooser), "has-all-option");
472 account_manager_prepared_cb (GObject *source_object,
473 GAsyncResult *result,
477 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
478 EmpathyAccountChooser *chooser = user_data;
479 EmpathyAccountChooserPriv *priv = GET_PRIV (chooser);
480 GError *error = NULL;
482 if (!tp_account_manager_prepare_finish (manager, result, &error)) {
483 DEBUG ("Failed to prepare account manager: %s", error->message);
484 g_error_free (error);
488 accounts = tp_account_manager_get_valid_accounts (manager);
490 for (l = accounts; l != NULL; l = l->next) {
491 TpAccount *account = l->data;
493 account_chooser_account_add_foreach (account, chooser);
495 empathy_signal_connect_weak (account, "status-changed",
496 G_CALLBACK (account_chooser_status_changed_cb),
500 g_list_free (accounts);
503 g_signal_emit (chooser, signals[READY], 0);
507 account_cmp (GtkTreeModel *model,
512 gboolean a_enabled, b_enabled;
513 gchar *a_text, *b_text;
516 gtk_tree_model_get (model, a, COL_ACCOUNT_ENABLED, &a_enabled, -1);
517 gtk_tree_model_get (model, b, COL_ACCOUNT_ENABLED, &b_enabled, -1);
519 /* Enabled accounts are displayed first */
520 if (a_enabled != b_enabled)
521 return a_enabled ? -1: 1;
523 gtk_tree_model_get (model, a, COL_ACCOUNT_TEXT, &a_text, -1);
524 gtk_tree_model_get (model, b, COL_ACCOUNT_TEXT, &b_text, -1);
526 if (a_text == b_text)
528 else if (a_text == NULL)
530 else if (b_text == NULL)
533 result = g_ascii_strcasecmp (a_text, b_text);
542 account_chooser_setup (EmpathyAccountChooser *chooser)
544 EmpathyAccountChooserPriv *priv;
546 GtkCellRenderer *renderer;
547 GtkComboBox *combobox;
549 priv = GET_PRIV (chooser);
551 /* Set up combo box with new store */
552 combobox = GTK_COMBO_BOX (chooser);
554 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
556 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
557 G_TYPE_STRING, /* Image */
558 G_TYPE_STRING, /* Name */
559 G_TYPE_BOOLEAN, /* Enabled */
562 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
563 account_cmp, chooser, NULL);
564 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
565 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
567 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
569 renderer = gtk_cell_renderer_pixbuf_new ();
570 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
571 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
572 "icon-name", COL_ACCOUNT_IMAGE,
573 "sensitive", COL_ACCOUNT_ENABLED,
575 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
577 renderer = gtk_cell_renderer_text_new ();
578 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
579 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
580 "text", COL_ACCOUNT_TEXT,
581 "sensitive", COL_ACCOUNT_ENABLED,
584 /* Populate accounts */
585 tp_account_manager_prepare_async (priv->manager, NULL,
586 account_manager_prepared_cb, chooser);
588 g_object_unref (store);
592 account_chooser_account_validity_changed_cb (TpAccountManager *manager,
595 EmpathyAccountChooser *chooser)
598 account_chooser_account_add_foreach (account, chooser);
600 account_chooser_account_remove_foreach (account, chooser);
605 account_chooser_account_add_foreach (TpAccount *account,
606 EmpathyAccountChooser *chooser)
609 GtkComboBox *combobox;
613 combobox = GTK_COMBO_BOX (chooser);
614 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
616 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
617 gtk_list_store_insert_with_values (store, &iter, position,
618 COL_ACCOUNT_POINTER, account,
620 account_chooser_update_iter (chooser, &iter);
624 account_chooser_account_removed_cb (TpAccountManager *manager,
626 EmpathyAccountChooser *chooser)
628 account_chooser_account_remove_foreach (account, chooser);
638 account_chooser_find_account_foreach (GtkTreeModel *model,
643 FindAccountData *data = user_data;
646 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
648 if (account == data->account) {
650 *(data->iter) = *iter;
651 g_object_unref (account);
656 g_object_unref (account);
662 account_chooser_find_account (EmpathyAccountChooser *chooser,
667 GtkComboBox *combobox;
668 FindAccountData data;
670 combobox = GTK_COMBO_BOX (chooser);
671 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
673 data.account = account;
675 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
676 account_chooser_find_account_foreach,
683 account_chooser_account_remove_foreach (TpAccount *account,
684 EmpathyAccountChooser *chooser)
687 GtkComboBox *combobox;
690 combobox = GTK_COMBO_BOX (chooser);
691 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
693 if (account_chooser_find_account (chooser, account, &iter)) {
694 gtk_list_store_remove (store, &iter);
699 account_chooser_update_iter (EmpathyAccountChooser *chooser,
702 EmpathyAccountChooserPriv *priv;
704 GtkComboBox *combobox;
706 const gchar *icon_name;
707 gboolean is_enabled = TRUE;
709 priv = GET_PRIV (chooser);
711 combobox = GTK_COMBO_BOX (chooser);
712 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
714 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
715 COL_ACCOUNT_POINTER, &account,
718 icon_name = tp_account_get_icon_name (account);
720 is_enabled = priv->filter (account, priv->filter_data);
723 gtk_list_store_set (store, iter,
724 COL_ACCOUNT_IMAGE, icon_name,
725 COL_ACCOUNT_TEXT, tp_account_get_display_name (account),
726 COL_ACCOUNT_ENABLED, is_enabled,
729 /* set first connected account as active account */
730 if (priv->account_manually_set == FALSE &&
731 priv->set_active_item == FALSE && is_enabled) {
732 priv->set_active_item = TRUE;
733 gtk_combo_box_set_active_iter (combobox, iter);
736 g_object_unref (account);
740 account_chooser_status_changed_cb (TpAccount *account,
744 gchar *dbus_error_name,
748 EmpathyAccountChooser *chooser = user_data;
751 if (account_chooser_find_account (chooser, account, &iter)) {
752 account_chooser_update_iter (chooser, &iter);
757 account_chooser_separator_func (GtkTreeModel *model,
759 EmpathyAccountChooser *chooser)
761 EmpathyAccountChooserPriv *priv;
763 gboolean is_separator;
765 priv = GET_PRIV (chooser);
767 if (!priv->has_all_option) {
771 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
772 is_separator = text == NULL;
779 account_chooser_set_account_foreach (GtkTreeModel *model,
782 SetAccountData *data)
787 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
789 /* Special case so we can make it possible to select the All option */
790 if ((data->account == NULL) != (account == NULL)) {
794 equal = (data->account == account);
798 g_object_unref (account);
802 GtkComboBox *combobox;
804 combobox = GTK_COMBO_BOX (data->chooser);
805 gtk_combo_box_set_active_iter (combobox, iter);
814 account_chooser_filter_foreach (GtkTreeModel *model,
819 account_chooser_update_iter (chooser, iter);
824 * empathy_account_chooser_set_filter:
825 * @chooser: an #EmpathyAccountChooser
827 * @user_data: data to pass to @filter, or %NULL
829 * Sets a filter on the @chooser so only accounts that are %TRUE in the eyes
830 * of the filter are visible in the @chooser.
833 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
834 EmpathyAccountChooserFilterFunc filter,
837 EmpathyAccountChooserPriv *priv;
840 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
842 priv = GET_PRIV (chooser);
844 priv->filter = filter;
845 priv->filter_data = user_data;
847 /* Refilter existing data */
848 priv->set_active_item = FALSE;
849 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
850 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
854 * EmpathyAccountChooserFilterFunc:
855 * @account: a #TpAccount
856 * @user_data: user data, or %NULL
858 * A function which decides whether the account indicated by @account
861 * Return value: whether the account indicated by @account is visible.
865 * empathy_account_chooser_filter_is_connected:
866 * @account: a #TpAccount
867 * @user_data: user data or %NULL
869 * A useful #EmpathyAccountChooserFilterFunc that one could pass into
870 * empathy_account_chooser_set_filter() and only show connected accounts.
872 * Return value: Whether @account is connected
875 empathy_account_chooser_filter_is_connected (TpAccount *account,
878 return (tp_account_get_connection_status (account, NULL)
879 == TP_CONNECTION_STATUS_CONNECTED);
883 empathy_account_chooser_is_ready (EmpathyAccountChooser *self)
885 EmpathyAccountChooserPriv *priv = GET_PRIV (self);