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>
33 #include <telepathy-glib/util.h>
35 #include <libempathy/empathy-utils.h>
37 #include "empathy-ui-utils.h"
38 #include "empathy-account-chooser.h"
40 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
41 #include <libempathy/empathy-debug.h>
44 * SECTION:empathy-account-chooser
45 * @title:EmpathyAccountChooser
46 * @short_description: A widget used to choose from a list of accounts
47 * @include: libempathy-gtk/empathy-account-chooser.h
49 * #EmpathyAccountChooser is a widget which extends #GtkComboBox to provide
50 * a chooser of available accounts.
54 * EmpathyAccountChooser:
55 * @parent: parent object
57 * Widget which extends #GtkComboBox to provide a chooser of available accounts.
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountChooser)
62 TpAccountManager *manager;
63 gboolean set_active_item;
64 gboolean account_manually_set;
65 gboolean has_all_option;
66 EmpathyAccountChooserFilterFunc filter;
69 } EmpathyAccountChooserPriv;
72 EmpathyAccountChooser *chooser;
77 /* Distinguishes between store entries which are actually accounts, and special
78 * items like the "All" entry and the separator below it, so they can be sorted
79 * correctly. Higher-numbered entries will sort earlier.
90 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
96 static void account_chooser_finalize (GObject *object);
97 static void account_chooser_get_property (GObject *object,
101 static void account_chooser_set_property (GObject *object,
105 static void account_chooser_setup (EmpathyAccountChooser *chooser);
106 static void account_chooser_account_validity_changed_cb (TpAccountManager *manager,
109 EmpathyAccountChooser *chooser);
110 static void account_chooser_account_add_foreach (TpAccount *account,
111 EmpathyAccountChooser *chooser);
112 static void account_chooser_account_removed_cb (TpAccountManager *manager,
114 EmpathyAccountChooser *chooser);
115 static void account_chooser_account_remove_foreach (TpAccount *account,
116 EmpathyAccountChooser *chooser);
117 static void account_chooser_update_iter (EmpathyAccountChooser *chooser,
119 static void account_chooser_status_changed_cb (TpAccount *account,
123 gchar *dbus_error_name,
126 static gboolean account_chooser_separator_func (GtkTreeModel *model,
128 EmpathyAccountChooser *chooser);
129 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
132 SetAccountData *data);
144 static guint signals[LAST_SIGNAL] = { 0 };
146 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
149 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
151 GObjectClass *object_class = G_OBJECT_CLASS (klass);
153 object_class->finalize = account_chooser_finalize;
154 object_class->get_property = account_chooser_get_property;
155 object_class->set_property = account_chooser_set_property;
158 * EmpathyAccountChooser:has-all-option:
160 * Have an additional option in the list to mean all accounts.
162 g_object_class_install_property (object_class,
164 g_param_spec_boolean ("has-all-option",
166 "Have a separate option in the list to mean ALL accounts",
171 g_signal_new ("ready",
172 G_OBJECT_CLASS_TYPE (object_class),
176 g_cclosure_marshal_VOID__VOID,
180 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
184 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
186 EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
187 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
189 chooser->priv = priv;
190 priv->set_active_item = FALSE;
191 priv->account_manually_set = FALSE;
193 priv->filter_data = NULL;
195 priv->manager = tp_account_manager_dup ();
197 g_signal_connect (priv->manager, "account-validity-changed",
198 G_CALLBACK (account_chooser_account_validity_changed_cb),
200 g_signal_connect (priv->manager, "account-removed",
201 G_CALLBACK (account_chooser_account_removed_cb),
204 account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
208 account_chooser_finalize (GObject *object)
210 EmpathyAccountChooserPriv *priv = GET_PRIV (object);
212 g_signal_handlers_disconnect_by_func (priv->manager,
213 account_chooser_account_validity_changed_cb,
215 g_signal_handlers_disconnect_by_func (priv->manager,
216 account_chooser_account_removed_cb,
218 g_object_unref (priv->manager);
220 G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
224 account_chooser_get_property (GObject *object,
229 EmpathyAccountChooserPriv *priv;
231 priv = GET_PRIV (object);
234 case PROP_HAS_ALL_OPTION:
235 g_value_set_boolean (value, priv->has_all_option);
238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
244 account_chooser_set_property (GObject *object,
249 EmpathyAccountChooserPriv *priv;
251 priv = GET_PRIV (object);
254 case PROP_HAS_ALL_OPTION:
255 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
256 g_value_get_boolean (value));
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
265 * empathy_account_chooser_new:
267 * Creates a new #EmpathyAccountChooser.
269 * Return value: A new #EmpathyAccountChooser
272 empathy_account_chooser_new (void)
276 chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
282 * empathy_account_chooser_dup_account:
283 * @chooser: an #EmpathyAccountChooser
285 * Returns the account which is currently selected in the chooser or %NULL
286 * if there is no account selected. The #TpAccount returned should be
287 * unrefed with g_object_unref() when finished with.
289 * Return value: a new ref to the #TpAccount currently selected, or %NULL.
292 empathy_account_chooser_dup_account (EmpathyAccountChooser *chooser)
294 EmpathyAccountChooserPriv *priv;
299 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
301 priv = GET_PRIV (chooser);
303 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
304 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
308 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
314 * empathy_account_chooser_get_connection:
315 * @chooser: an #EmpathyAccountChooser
317 * Returns a borrowed reference to the #TpConnection associated with the
318 * account currently selected. The caller must reference the returned object with
319 * g_object_ref() if it will be kept
321 * Return value: a borrowed reference to the #TpConnection associated with the
322 * account curently selected.
325 empathy_account_chooser_get_connection (EmpathyAccountChooser *chooser)
327 EmpathyAccountChooserPriv *priv;
329 TpConnection *connection;
331 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
333 priv = GET_PRIV (chooser);
335 account = empathy_account_chooser_dup_account (chooser);
337 /* if the returned account is NULL, then the account manager probably
338 * hasn't been prepared yet. It should be safe to return NULL here
340 if (account == NULL) {
344 connection = tp_account_get_connection (account);
345 g_object_unref (account);
351 * empathy_account_chooser_set_account:
352 * @chooser: an #EmpathyAccountChooser
353 * @account: a #TpAccount
355 * Sets the currently selected account to @account, if it exists in the list.
357 * Return value: whether the chooser was set to @account.
360 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
363 EmpathyAccountChooserPriv *priv;
364 GtkComboBox *combobox;
369 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
371 priv = GET_PRIV (chooser);
373 combobox = GTK_COMBO_BOX (chooser);
374 model = gtk_combo_box_get_model (combobox);
375 gtk_combo_box_get_active_iter (combobox, &iter);
377 data.chooser = chooser;
378 data.account = account;
380 gtk_tree_model_foreach (model,
381 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
384 priv->account_manually_set = data.set;
390 * empathy_account_chooser_get_has_all_option:
391 * @chooser: an #EmpathyAccountChooser
393 * Returns whether @chooser has the #EmpathyAccountChooser:has-all-option property
396 * Return value: whether @chooser has the #EmpathyAccountChooser:has-all-option property
400 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
402 EmpathyAccountChooserPriv *priv;
404 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
406 priv = GET_PRIV (chooser);
408 return priv->has_all_option;
412 * empathy_account_chooser_set_has_all_option:
413 * @chooser: an #EmpathyAccountChooser
414 * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option property
416 * Sets the #EmpathyAccountChooser:has-all-option property.
419 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
420 gboolean has_all_option)
422 EmpathyAccountChooserPriv *priv;
423 GtkComboBox *combobox;
428 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
430 priv = GET_PRIV (chooser);
432 if (priv->has_all_option == has_all_option) {
436 combobox = GTK_COMBO_BOX (chooser);
437 model = gtk_combo_box_get_model (combobox);
438 store = GTK_LIST_STORE (model);
440 priv->has_all_option = has_all_option;
443 * The first 2 options are the ALL and separator
446 if (has_all_option) {
447 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
448 (GtkTreeViewRowSeparatorFunc)
449 account_chooser_separator_func,
453 gtk_list_store_prepend (store, &iter);
454 gtk_list_store_set (store, &iter,
455 COL_ACCOUNT_TEXT, NULL,
456 COL_ACCOUNT_ENABLED, TRUE,
457 COL_ACCOUNT_POINTER, NULL,
458 COL_ACCOUNT_ROW_TYPE, ROW_SEPARATOR,
461 gtk_list_store_prepend (store, &iter);
462 gtk_list_store_set (store, &iter,
463 COL_ACCOUNT_TEXT, _("All"),
464 COL_ACCOUNT_ENABLED, TRUE,
465 COL_ACCOUNT_POINTER, NULL,
466 COL_ACCOUNT_ROW_TYPE, ROW_ALL,
469 if (gtk_tree_model_get_iter_first (model, &iter)) {
470 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
471 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
475 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
476 (GtkTreeViewRowSeparatorFunc)
482 g_object_notify (G_OBJECT (chooser), "has-all-option");
486 account_manager_prepared_cb (GObject *source_object,
487 GAsyncResult *result,
491 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
492 EmpathyAccountChooser *chooser = user_data;
493 EmpathyAccountChooserPriv *priv = GET_PRIV (chooser);
494 GError *error = NULL;
496 if (!tp_account_manager_prepare_finish (manager, result, &error)) {
497 DEBUG ("Failed to prepare account manager: %s", error->message);
498 g_error_free (error);
502 accounts = tp_account_manager_get_valid_accounts (manager);
504 for (l = accounts; l != NULL; l = l->next) {
505 TpAccount *account = l->data;
507 account_chooser_account_add_foreach (account, chooser);
509 tp_g_signal_connect_object (account, "status-changed",
510 G_CALLBACK (account_chooser_status_changed_cb),
514 g_list_free (accounts);
517 g_signal_emit (chooser, signals[READY], 0);
521 account_cmp (GtkTreeModel *model,
526 RowType a_type, b_type;
527 gboolean a_enabled, b_enabled;
528 gchar *a_text, *b_text;
531 gtk_tree_model_get (model, a,
532 COL_ACCOUNT_ENABLED, &a_enabled,
533 COL_ACCOUNT_ROW_TYPE, &a_type,
535 gtk_tree_model_get (model, b,
536 COL_ACCOUNT_ENABLED, &b_enabled,
537 COL_ACCOUNT_ROW_TYPE, &b_type,
540 /* This assumes that we have at most one of each special row type. */
541 if (a_type != b_type) {
542 /* Display higher-numbered special row types first. */
543 return (b_type - a_type);
546 /* Enabled accounts are displayed first */
547 if (a_enabled != b_enabled)
548 return a_enabled ? -1: 1;
550 gtk_tree_model_get (model, a, COL_ACCOUNT_TEXT, &a_text, -1);
551 gtk_tree_model_get (model, b, COL_ACCOUNT_TEXT, &b_text, -1);
553 if (a_text == b_text)
555 else if (a_text == NULL)
557 else if (b_text == NULL)
560 result = g_ascii_strcasecmp (a_text, b_text);
569 account_chooser_setup (EmpathyAccountChooser *chooser)
571 EmpathyAccountChooserPriv *priv;
573 GtkCellRenderer *renderer;
574 GtkComboBox *combobox;
576 priv = GET_PRIV (chooser);
578 /* Set up combo box with new store */
579 combobox = GTK_COMBO_BOX (chooser);
581 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
583 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
584 G_TYPE_STRING, /* Image */
585 G_TYPE_STRING, /* Name */
586 G_TYPE_BOOLEAN, /* Enabled */
587 G_TYPE_UINT, /* Row type */
590 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
591 account_cmp, chooser, NULL);
592 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
593 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
595 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
597 renderer = gtk_cell_renderer_pixbuf_new ();
598 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
599 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
600 "icon-name", COL_ACCOUNT_IMAGE,
601 "sensitive", COL_ACCOUNT_ENABLED,
603 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
605 renderer = gtk_cell_renderer_text_new ();
606 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
607 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
608 "text", COL_ACCOUNT_TEXT,
609 "sensitive", COL_ACCOUNT_ENABLED,
612 /* Populate accounts */
613 tp_account_manager_prepare_async (priv->manager, NULL,
614 account_manager_prepared_cb, chooser);
616 g_object_unref (store);
620 account_chooser_account_validity_changed_cb (TpAccountManager *manager,
623 EmpathyAccountChooser *chooser)
626 account_chooser_account_add_foreach (account, chooser);
628 account_chooser_account_remove_foreach (account, chooser);
633 account_chooser_account_add_foreach (TpAccount *account,
634 EmpathyAccountChooser *chooser)
637 GtkComboBox *combobox;
641 combobox = GTK_COMBO_BOX (chooser);
642 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
644 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
645 gtk_list_store_insert_with_values (store, &iter, position,
646 COL_ACCOUNT_POINTER, account,
648 account_chooser_update_iter (chooser, &iter);
652 account_chooser_account_removed_cb (TpAccountManager *manager,
654 EmpathyAccountChooser *chooser)
656 account_chooser_account_remove_foreach (account, chooser);
666 account_chooser_find_account_foreach (GtkTreeModel *model,
671 FindAccountData *data = user_data;
674 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
676 if (account == data->account) {
678 *(data->iter) = *iter;
679 g_object_unref (account);
684 g_object_unref (account);
690 account_chooser_find_account (EmpathyAccountChooser *chooser,
695 GtkComboBox *combobox;
696 FindAccountData data;
698 combobox = GTK_COMBO_BOX (chooser);
699 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
701 data.account = account;
703 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
704 account_chooser_find_account_foreach,
711 account_chooser_account_remove_foreach (TpAccount *account,
712 EmpathyAccountChooser *chooser)
715 GtkComboBox *combobox;
718 combobox = GTK_COMBO_BOX (chooser);
719 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
721 if (account_chooser_find_account (chooser, account, &iter)) {
722 gtk_list_store_remove (store, &iter);
727 account_chooser_update_iter (EmpathyAccountChooser *chooser,
730 EmpathyAccountChooserPriv *priv;
732 GtkComboBox *combobox;
734 const gchar *icon_name;
735 gboolean is_enabled = TRUE;
737 priv = GET_PRIV (chooser);
739 combobox = GTK_COMBO_BOX (chooser);
740 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
742 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
743 COL_ACCOUNT_POINTER, &account,
746 /* Skip rows without account associated */
750 icon_name = tp_account_get_icon_name (account);
752 is_enabled = priv->filter (account, priv->filter_data);
755 gtk_list_store_set (store, iter,
756 COL_ACCOUNT_IMAGE, icon_name,
757 COL_ACCOUNT_TEXT, tp_account_get_display_name (account),
758 COL_ACCOUNT_ENABLED, is_enabled,
761 /* set first connected account as active account */
762 if (priv->account_manually_set == FALSE &&
763 priv->set_active_item == FALSE && is_enabled) {
764 priv->set_active_item = TRUE;
765 gtk_combo_box_set_active_iter (combobox, iter);
768 g_object_unref (account);
772 account_chooser_status_changed_cb (TpAccount *account,
776 gchar *dbus_error_name,
780 EmpathyAccountChooser *chooser = user_data;
783 if (account_chooser_find_account (chooser, account, &iter)) {
784 account_chooser_update_iter (chooser, &iter);
789 account_chooser_separator_func (GtkTreeModel *model,
791 EmpathyAccountChooser *chooser)
795 gtk_tree_model_get (model, iter, COL_ACCOUNT_ROW_TYPE, &row_type, -1);
796 return (row_type == ROW_SEPARATOR);
800 account_chooser_set_account_foreach (GtkTreeModel *model,
803 SetAccountData *data)
808 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
810 equal = (data->account == account);
813 g_object_unref (account);
817 GtkComboBox *combobox;
819 combobox = GTK_COMBO_BOX (data->chooser);
820 gtk_combo_box_set_active_iter (combobox, iter);
829 account_chooser_filter_foreach (GtkTreeModel *model,
834 account_chooser_update_iter (chooser, iter);
839 * empathy_account_chooser_set_filter:
840 * @chooser: an #EmpathyAccountChooser
842 * @user_data: data to pass to @filter, or %NULL
844 * Sets a filter on the @chooser so only accounts that are %TRUE in the eyes
845 * of the filter are visible in the @chooser.
848 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
849 EmpathyAccountChooserFilterFunc filter,
852 EmpathyAccountChooserPriv *priv;
855 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
857 priv = GET_PRIV (chooser);
859 priv->filter = filter;
860 priv->filter_data = user_data;
862 /* Refilter existing data */
863 priv->set_active_item = FALSE;
864 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
865 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
869 * EmpathyAccountChooserFilterFunc:
870 * @account: a #TpAccount
871 * @user_data: user data, or %NULL
873 * A function which decides whether the account indicated by @account
876 * Return value: whether the account indicated by @account is visible.
880 * empathy_account_chooser_filter_is_connected:
881 * @account: a #TpAccount
882 * @user_data: user data or %NULL
884 * A useful #EmpathyAccountChooserFilterFunc that one could pass into
885 * empathy_account_chooser_set_filter() and only show connected accounts.
887 * Return value: Whether @account is connected
890 empathy_account_chooser_filter_is_connected (TpAccount *account,
893 return (tp_account_get_connection_status (account, NULL)
894 == TP_CONNECTION_STATUS_CONNECTED);
898 empathy_account_chooser_is_ready (EmpathyAccountChooser *self)
900 EmpathyAccountChooserPriv *priv = GET_PRIV (self);
906 empathy_account_chooser_get_account (EmpathyAccountChooser *chooser)
910 account = empathy_account_chooser_dup_account (chooser);
914 g_object_unref (account);