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;
76 /* Distinguishes between store entries which are actually accounts, and special
77 * items like the "All" entry and the separator below it, so they can be sorted
78 * correctly. Higher-numbered entries will sort earlier.
89 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
95 static void account_chooser_finalize (GObject *object);
96 static void account_chooser_get_property (GObject *object,
100 static void account_chooser_set_property (GObject *object,
104 static void account_chooser_setup (EmpathyAccountChooser *chooser);
105 static void account_chooser_account_validity_changed_cb (TpAccountManager *manager,
108 EmpathyAccountChooser *chooser);
109 static void account_chooser_account_add_foreach (TpAccount *account,
110 EmpathyAccountChooser *chooser);
111 static void account_chooser_account_removed_cb (TpAccountManager *manager,
113 EmpathyAccountChooser *chooser);
114 static void account_chooser_account_remove_foreach (TpAccount *account,
115 EmpathyAccountChooser *chooser);
116 static void account_chooser_update_iter (EmpathyAccountChooser *chooser,
118 static void account_chooser_status_changed_cb (TpAccount *account,
122 gchar *dbus_error_name,
125 static gboolean account_chooser_separator_func (GtkTreeModel *model,
127 EmpathyAccountChooser *chooser);
128 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
131 SetAccountData *data);
143 static guint signals[LAST_SIGNAL] = { 0 };
145 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
148 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
150 GObjectClass *object_class = G_OBJECT_CLASS (klass);
152 object_class->finalize = account_chooser_finalize;
153 object_class->get_property = account_chooser_get_property;
154 object_class->set_property = account_chooser_set_property;
157 * EmpathyAccountChooser:has-all-option:
159 * Have an additional option in the list to mean all accounts.
161 g_object_class_install_property (object_class,
163 g_param_spec_boolean ("has-all-option",
165 "Have a separate option in the list to mean ALL accounts",
170 g_signal_new ("ready",
171 G_OBJECT_CLASS_TYPE (object_class),
175 g_cclosure_marshal_VOID__VOID,
179 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
183 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
185 EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
186 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
188 chooser->priv = priv;
189 priv->set_active_item = FALSE;
190 priv->account_manually_set = FALSE;
192 priv->filter_data = NULL;
194 priv->manager = tp_account_manager_dup ();
196 g_signal_connect (priv->manager, "account-validity-changed",
197 G_CALLBACK (account_chooser_account_validity_changed_cb),
199 g_signal_connect (priv->manager, "account-removed",
200 G_CALLBACK (account_chooser_account_removed_cb),
203 account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
207 account_chooser_finalize (GObject *object)
209 EmpathyAccountChooserPriv *priv = GET_PRIV (object);
211 g_signal_handlers_disconnect_by_func (priv->manager,
212 account_chooser_account_validity_changed_cb,
214 g_signal_handlers_disconnect_by_func (priv->manager,
215 account_chooser_account_removed_cb,
217 g_object_unref (priv->manager);
219 G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
223 account_chooser_get_property (GObject *object,
228 EmpathyAccountChooserPriv *priv;
230 priv = GET_PRIV (object);
233 case PROP_HAS_ALL_OPTION:
234 g_value_set_boolean (value, priv->has_all_option);
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
243 account_chooser_set_property (GObject *object,
248 EmpathyAccountChooserPriv *priv;
250 priv = GET_PRIV (object);
253 case PROP_HAS_ALL_OPTION:
254 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
255 g_value_get_boolean (value));
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
264 * empathy_account_chooser_new:
266 * Creates a new #EmpathyAccountChooser.
268 * Return value: A new #EmpathyAccountChooser
271 empathy_account_chooser_new (void)
275 chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
281 * empathy_account_chooser_dup_account:
282 * @chooser: an #EmpathyAccountChooser
284 * Returns the account which is currently selected in the chooser or %NULL
285 * if there is no account selected. The #TpAccount returned should be
286 * unrefed with g_object_unref() when finished with.
288 * Return value: a new ref to the #TpAccount currently selected, or %NULL.
291 empathy_account_chooser_dup_account (EmpathyAccountChooser *chooser)
293 EmpathyAccountChooserPriv *priv;
298 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
300 priv = GET_PRIV (chooser);
302 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
303 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
307 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
313 * empathy_account_chooser_get_connection:
314 * @chooser: an #EmpathyAccountChooser
316 * Returns a borrowed reference to the #TpConnection associated with the
317 * account currently selected. The caller must reference the returned object with
318 * g_object_ref() if it will be kept
320 * Return value: a borrowed reference to the #TpConnection associated with the
321 * account curently selected.
324 empathy_account_chooser_get_connection (EmpathyAccountChooser *chooser)
326 EmpathyAccountChooserPriv *priv;
328 TpConnection *connection;
330 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
332 priv = GET_PRIV (chooser);
334 account = empathy_account_chooser_dup_account (chooser);
336 /* if the returned account is NULL, then the account manager probably
337 * hasn't been prepared yet. It should be safe to return NULL here
339 if (account == NULL) {
343 connection = tp_account_get_connection (account);
344 g_object_unref (account);
350 * empathy_account_chooser_set_account:
351 * @chooser: an #EmpathyAccountChooser
352 * @account: a #TpAccount
354 * Sets the currently selected account to @account, if it exists in the list.
356 * Return value: whether the chooser was set to @account.
359 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
362 EmpathyAccountChooserPriv *priv;
363 GtkComboBox *combobox;
368 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
370 priv = GET_PRIV (chooser);
372 combobox = GTK_COMBO_BOX (chooser);
373 model = gtk_combo_box_get_model (combobox);
374 gtk_combo_box_get_active_iter (combobox, &iter);
376 data.chooser = chooser;
377 data.account = account;
379 gtk_tree_model_foreach (model,
380 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
383 priv->account_manually_set = data.set;
389 * empathy_account_chooser_get_has_all_option:
390 * @chooser: an #EmpathyAccountChooser
392 * Returns whether @chooser has the #EmpathyAccountChooser:has-all-option property
395 * Return value: whether @chooser has the #EmpathyAccountChooser:has-all-option property
399 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
401 EmpathyAccountChooserPriv *priv;
403 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
405 priv = GET_PRIV (chooser);
407 return priv->has_all_option;
411 * empathy_account_chooser_set_has_all_option:
412 * @chooser: an #EmpathyAccountChooser
413 * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option property
415 * Sets the #EmpathyAccountChooser:has-all-option property.
418 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
419 gboolean has_all_option)
421 EmpathyAccountChooserPriv *priv;
422 GtkComboBox *combobox;
427 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
429 priv = GET_PRIV (chooser);
431 if (priv->has_all_option == has_all_option) {
435 combobox = GTK_COMBO_BOX (chooser);
436 model = gtk_combo_box_get_model (combobox);
437 store = GTK_LIST_STORE (model);
439 priv->has_all_option = has_all_option;
442 * The first 2 options are the ALL and separator
445 if (has_all_option) {
446 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
447 (GtkTreeViewRowSeparatorFunc)
448 account_chooser_separator_func,
452 gtk_list_store_prepend (store, &iter);
453 gtk_list_store_set (store, &iter,
454 COL_ACCOUNT_TEXT, NULL,
455 COL_ACCOUNT_ENABLED, TRUE,
456 COL_ACCOUNT_POINTER, NULL,
457 COL_ACCOUNT_ROW_TYPE, ROW_SEPARATOR,
460 gtk_list_store_prepend (store, &iter);
461 gtk_list_store_set (store, &iter,
462 COL_ACCOUNT_TEXT, _("All"),
463 COL_ACCOUNT_ENABLED, TRUE,
464 COL_ACCOUNT_POINTER, NULL,
465 COL_ACCOUNT_ROW_TYPE, ROW_ALL,
468 if (gtk_tree_model_get_iter_first (model, &iter)) {
469 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
470 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
474 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
475 (GtkTreeViewRowSeparatorFunc)
481 g_object_notify (G_OBJECT (chooser), "has-all-option");
485 account_manager_prepared_cb (GObject *source_object,
486 GAsyncResult *result,
490 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
491 EmpathyAccountChooser *chooser = user_data;
492 EmpathyAccountChooserPriv *priv = GET_PRIV (chooser);
493 GError *error = NULL;
495 if (!tp_account_manager_prepare_finish (manager, result, &error)) {
496 DEBUG ("Failed to prepare account manager: %s", error->message);
497 g_error_free (error);
501 accounts = tp_account_manager_get_valid_accounts (manager);
503 for (l = accounts; l != NULL; l = l->next) {
504 TpAccount *account = l->data;
506 account_chooser_account_add_foreach (account, chooser);
508 empathy_signal_connect_weak (account, "status-changed",
509 G_CALLBACK (account_chooser_status_changed_cb),
513 g_list_free (accounts);
516 g_signal_emit (chooser, signals[READY], 0);
520 account_cmp (GtkTreeModel *model,
525 RowType a_type, b_type;
526 gboolean a_enabled, b_enabled;
527 gchar *a_text, *b_text;
530 gtk_tree_model_get (model, a,
531 COL_ACCOUNT_ENABLED, &a_enabled,
532 COL_ACCOUNT_ROW_TYPE, &a_type,
534 gtk_tree_model_get (model, b,
535 COL_ACCOUNT_ENABLED, &b_enabled,
536 COL_ACCOUNT_ROW_TYPE, &b_type,
539 /* This assumes that we have at most one of each special row type. */
540 if (a_type != b_type) {
541 /* Display higher-numbered special row types first. */
542 return (b_type - a_type);
545 /* Enabled accounts are displayed first */
546 if (a_enabled != b_enabled)
547 return a_enabled ? -1: 1;
549 gtk_tree_model_get (model, a, COL_ACCOUNT_TEXT, &a_text, -1);
550 gtk_tree_model_get (model, b, COL_ACCOUNT_TEXT, &b_text, -1);
552 if (a_text == b_text)
554 else if (a_text == NULL)
556 else if (b_text == NULL)
559 result = g_ascii_strcasecmp (a_text, b_text);
568 account_chooser_setup (EmpathyAccountChooser *chooser)
570 EmpathyAccountChooserPriv *priv;
572 GtkCellRenderer *renderer;
573 GtkComboBox *combobox;
575 priv = GET_PRIV (chooser);
577 /* Set up combo box with new store */
578 combobox = GTK_COMBO_BOX (chooser);
580 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
582 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
583 G_TYPE_STRING, /* Image */
584 G_TYPE_STRING, /* Name */
585 G_TYPE_BOOLEAN, /* Enabled */
586 G_TYPE_UINT, /* Row type */
589 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
590 account_cmp, chooser, NULL);
591 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
592 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
594 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
596 renderer = gtk_cell_renderer_pixbuf_new ();
597 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
598 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
599 "icon-name", COL_ACCOUNT_IMAGE,
600 "sensitive", COL_ACCOUNT_ENABLED,
602 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
604 renderer = gtk_cell_renderer_text_new ();
605 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
606 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
607 "text", COL_ACCOUNT_TEXT,
608 "sensitive", COL_ACCOUNT_ENABLED,
611 /* Populate accounts */
612 tp_account_manager_prepare_async (priv->manager, NULL,
613 account_manager_prepared_cb, chooser);
615 g_object_unref (store);
619 account_chooser_account_validity_changed_cb (TpAccountManager *manager,
622 EmpathyAccountChooser *chooser)
625 account_chooser_account_add_foreach (account, chooser);
627 account_chooser_account_remove_foreach (account, chooser);
632 account_chooser_account_add_foreach (TpAccount *account,
633 EmpathyAccountChooser *chooser)
636 GtkComboBox *combobox;
640 combobox = GTK_COMBO_BOX (chooser);
641 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
643 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
644 gtk_list_store_insert_with_values (store, &iter, position,
645 COL_ACCOUNT_POINTER, account,
647 account_chooser_update_iter (chooser, &iter);
651 account_chooser_account_removed_cb (TpAccountManager *manager,
653 EmpathyAccountChooser *chooser)
655 account_chooser_account_remove_foreach (account, chooser);
665 account_chooser_find_account_foreach (GtkTreeModel *model,
670 FindAccountData *data = user_data;
673 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
675 if (account == data->account) {
677 *(data->iter) = *iter;
678 g_object_unref (account);
683 g_object_unref (account);
689 account_chooser_find_account (EmpathyAccountChooser *chooser,
694 GtkComboBox *combobox;
695 FindAccountData data;
697 combobox = GTK_COMBO_BOX (chooser);
698 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
700 data.account = account;
702 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
703 account_chooser_find_account_foreach,
710 account_chooser_account_remove_foreach (TpAccount *account,
711 EmpathyAccountChooser *chooser)
714 GtkComboBox *combobox;
717 combobox = GTK_COMBO_BOX (chooser);
718 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
720 if (account_chooser_find_account (chooser, account, &iter)) {
721 gtk_list_store_remove (store, &iter);
726 account_chooser_update_iter (EmpathyAccountChooser *chooser,
729 EmpathyAccountChooserPriv *priv;
731 GtkComboBox *combobox;
733 const gchar *icon_name;
734 gboolean is_enabled = TRUE;
736 priv = GET_PRIV (chooser);
738 combobox = GTK_COMBO_BOX (chooser);
739 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
741 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
742 COL_ACCOUNT_POINTER, &account,
745 icon_name = tp_account_get_icon_name (account);
747 is_enabled = priv->filter (account, priv->filter_data);
750 gtk_list_store_set (store, iter,
751 COL_ACCOUNT_IMAGE, icon_name,
752 COL_ACCOUNT_TEXT, tp_account_get_display_name (account),
753 COL_ACCOUNT_ENABLED, is_enabled,
756 /* set first connected account as active account */
757 if (priv->account_manually_set == FALSE &&
758 priv->set_active_item == FALSE && is_enabled) {
759 priv->set_active_item = TRUE;
760 gtk_combo_box_set_active_iter (combobox, iter);
763 g_object_unref (account);
767 account_chooser_status_changed_cb (TpAccount *account,
771 gchar *dbus_error_name,
775 EmpathyAccountChooser *chooser = user_data;
778 if (account_chooser_find_account (chooser, account, &iter)) {
779 account_chooser_update_iter (chooser, &iter);
784 account_chooser_separator_func (GtkTreeModel *model,
786 EmpathyAccountChooser *chooser)
790 gtk_tree_model_get (model, iter, COL_ACCOUNT_ROW_TYPE, &row_type, -1);
791 return (row_type == ROW_SEPARATOR);
795 account_chooser_set_account_foreach (GtkTreeModel *model,
798 SetAccountData *data)
803 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
805 equal = (data->account == account);
808 g_object_unref (account);
812 GtkComboBox *combobox;
814 combobox = GTK_COMBO_BOX (data->chooser);
815 gtk_combo_box_set_active_iter (combobox, iter);
824 account_chooser_filter_foreach (GtkTreeModel *model,
829 account_chooser_update_iter (chooser, iter);
834 * empathy_account_chooser_set_filter:
835 * @chooser: an #EmpathyAccountChooser
837 * @user_data: data to pass to @filter, or %NULL
839 * Sets a filter on the @chooser so only accounts that are %TRUE in the eyes
840 * of the filter are visible in the @chooser.
843 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
844 EmpathyAccountChooserFilterFunc filter,
847 EmpathyAccountChooserPriv *priv;
850 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
852 priv = GET_PRIV (chooser);
854 priv->filter = filter;
855 priv->filter_data = user_data;
857 /* Refilter existing data */
858 priv->set_active_item = FALSE;
859 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
860 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
864 * EmpathyAccountChooserFilterFunc:
865 * @account: a #TpAccount
866 * @user_data: user data, or %NULL
868 * A function which decides whether the account indicated by @account
871 * Return value: whether the account indicated by @account is visible.
875 * empathy_account_chooser_filter_is_connected:
876 * @account: a #TpAccount
877 * @user_data: user data or %NULL
879 * A useful #EmpathyAccountChooserFilterFunc that one could pass into
880 * empathy_account_chooser_set_filter() and only show connected accounts.
882 * Return value: Whether @account is connected
885 empathy_account_chooser_filter_is_connected (TpAccount *account,
888 return (tp_account_get_connection_status (account, NULL)
889 == TP_CONNECTION_STATUS_CONNECTED);
893 empathy_account_chooser_is_ready (EmpathyAccountChooser *self)
895 EmpathyAccountChooserPriv *priv = GET_PRIV (self);