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;
67 } EmpathyAccountChooserPriv;
70 EmpathyAccountChooser *chooser;
78 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
83 static void account_chooser_finalize (GObject *object);
84 static void account_chooser_get_property (GObject *object,
88 static void account_chooser_set_property (GObject *object,
92 static void account_chooser_setup (EmpathyAccountChooser *chooser);
93 static void account_chooser_account_validity_changed_cb (TpAccountManager *manager,
96 EmpathyAccountChooser *chooser);
97 static void account_chooser_account_add_foreach (TpAccount *account,
98 EmpathyAccountChooser *chooser);
99 static void account_chooser_account_removed_cb (TpAccountManager *manager,
101 EmpathyAccountChooser *chooser);
102 static void account_chooser_account_remove_foreach (TpAccount *account,
103 EmpathyAccountChooser *chooser);
104 static void account_chooser_update_iter (EmpathyAccountChooser *chooser,
106 static void account_chooser_status_changed_cb (TpAccount *account,
110 gchar *dbus_error_name,
113 static gboolean account_chooser_separator_func (GtkTreeModel *model,
115 EmpathyAccountChooser *chooser);
116 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
119 SetAccountData *data);
126 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
129 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
131 GObjectClass *object_class = G_OBJECT_CLASS (klass);
133 object_class->finalize = account_chooser_finalize;
134 object_class->get_property = account_chooser_get_property;
135 object_class->set_property = account_chooser_set_property;
138 * EmpathyAccountChooser:has-all-option:
140 * Have an additional option in the list to mean all accounts.
142 g_object_class_install_property (object_class,
144 g_param_spec_boolean ("has-all-option",
146 "Have a separate option in the list to mean ALL accounts",
150 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
154 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
156 EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
157 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
159 chooser->priv = priv;
160 priv->set_active_item = FALSE;
161 priv->account_manually_set = FALSE;
163 priv->filter_data = NULL;
165 priv->manager = tp_account_manager_dup ();
167 g_signal_connect (priv->manager, "account-validity-changed",
168 G_CALLBACK (account_chooser_account_validity_changed_cb),
170 g_signal_connect (priv->manager, "account-removed",
171 G_CALLBACK (account_chooser_account_removed_cb),
174 account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
178 account_chooser_finalize (GObject *object)
180 EmpathyAccountChooserPriv *priv = GET_PRIV (object);
182 g_signal_handlers_disconnect_by_func (priv->manager,
183 account_chooser_account_validity_changed_cb,
185 g_signal_handlers_disconnect_by_func (priv->manager,
186 account_chooser_account_removed_cb,
188 g_object_unref (priv->manager);
190 G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
194 account_chooser_get_property (GObject *object,
199 EmpathyAccountChooserPriv *priv;
201 priv = GET_PRIV (object);
204 case PROP_HAS_ALL_OPTION:
205 g_value_set_boolean (value, priv->has_all_option);
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
214 account_chooser_set_property (GObject *object,
219 EmpathyAccountChooserPriv *priv;
221 priv = GET_PRIV (object);
224 case PROP_HAS_ALL_OPTION:
225 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
226 g_value_get_boolean (value));
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
235 * empathy_account_chooser_new:
237 * Creates a new #EmpathyAccountChooser.
239 * Return value: A new #EmpathyAccountChooser
242 empathy_account_chooser_new (void)
246 chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
252 * empathy_account_chooser_dup_account:
253 * @chooser: an #EmpathyAccountChooser
255 * Returns the account which is currently selected in the chooser or %NULL
256 * if there is no account selected. The #TpAccount returned should be
257 * unrefed with g_object_unref() when finished with.
259 * Return value: a new ref to the #TpAccount currently selected, or %NULL.
262 empathy_account_chooser_dup_account (EmpathyAccountChooser *chooser)
264 EmpathyAccountChooserPriv *priv;
269 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
271 priv = GET_PRIV (chooser);
273 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
274 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
278 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
284 * empathy_account_chooser_get_connection:
285 * @chooser: an #EmpathyAccountChooser
287 * Returns a borrowed reference to the #TpConnection associated with the
288 * account currently selected. The caller must reference the returned object with
289 * g_object_ref() if it will be kept
291 * Return value: a borrowed reference to the #TpConnection associated with the
292 * account curently selected.
295 empathy_account_chooser_get_connection (EmpathyAccountChooser *chooser)
297 EmpathyAccountChooserPriv *priv;
299 TpConnection *connection;
301 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
303 priv = GET_PRIV (chooser);
305 account = empathy_account_chooser_dup_account (chooser);
306 connection = tp_account_get_connection (account);
307 g_object_unref (account);
313 * empathy_account_chooser_set_account:
314 * @chooser: an #EmpathyAccountChooser
315 * @account: a #TpAccount
317 * Sets the currently selected account to @account, if it exists in the list.
319 * Return value: whether the chooser was set to @account.
322 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
325 EmpathyAccountChooserPriv *priv;
326 GtkComboBox *combobox;
331 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
333 priv = GET_PRIV (chooser);
335 combobox = GTK_COMBO_BOX (chooser);
336 model = gtk_combo_box_get_model (combobox);
337 gtk_combo_box_get_active_iter (combobox, &iter);
339 data.chooser = chooser;
340 data.account = account;
342 gtk_tree_model_foreach (model,
343 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
346 priv->account_manually_set = data.set;
352 * empathy_account_chooser_get_has_all_option:
353 * @chooser: an #EmpathyAccountChooser
355 * Returns whether @chooser has the #EmpathyAccountChooser:has-all-option property
358 * Return value: whether @chooser has the #EmpathyAccountChooser:has-all-option property
362 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
364 EmpathyAccountChooserPriv *priv;
366 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
368 priv = GET_PRIV (chooser);
370 return priv->has_all_option;
374 * empathy_account_chooser_set_has_all_option:
375 * @chooser: an #EmpathyAccountChooser
376 * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option property
378 * Sets the #EmpathyAccountChooser:has-all-option property.
381 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
382 gboolean has_all_option)
384 EmpathyAccountChooserPriv *priv;
385 GtkComboBox *combobox;
390 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
392 priv = GET_PRIV (chooser);
394 if (priv->has_all_option == has_all_option) {
398 combobox = GTK_COMBO_BOX (chooser);
399 model = gtk_combo_box_get_model (combobox);
400 store = GTK_LIST_STORE (model);
402 priv->has_all_option = has_all_option;
405 * The first 2 options are the ALL and separator
408 if (has_all_option) {
409 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
410 (GtkTreeViewRowSeparatorFunc)
411 account_chooser_separator_func,
415 gtk_list_store_prepend (store, &iter);
416 gtk_list_store_set (store, &iter,
417 COL_ACCOUNT_TEXT, NULL,
418 COL_ACCOUNT_ENABLED, TRUE,
419 COL_ACCOUNT_POINTER, NULL,
422 gtk_list_store_prepend (store, &iter);
423 gtk_list_store_set (store, &iter,
424 COL_ACCOUNT_TEXT, _("All"),
425 COL_ACCOUNT_ENABLED, TRUE,
426 COL_ACCOUNT_POINTER, NULL,
429 if (gtk_tree_model_get_iter_first (model, &iter)) {
430 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
431 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
435 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
436 (GtkTreeViewRowSeparatorFunc)
442 g_object_notify (G_OBJECT (chooser), "has-all-option");
446 account_manager_prepared_cb (GObject *source_object,
447 GAsyncResult *result,
451 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
452 EmpathyAccountChooser *chooser = user_data;
453 GError *error = NULL;
455 if (!tp_account_manager_prepare_finish (manager, result, &error)) {
456 DEBUG ("Failed to prepare account manager: %s", error->message);
457 g_error_free (error);
461 accounts = tp_account_manager_get_valid_accounts (manager);
463 for (l = accounts; l != NULL; l = l->next) {
464 TpAccount *account = l->data;
466 account_chooser_account_add_foreach (account, chooser);
468 empathy_signal_connect_weak (account, "status-changed",
469 G_CALLBACK (account_chooser_status_changed_cb),
473 g_list_free (accounts);
477 account_chooser_setup (EmpathyAccountChooser *chooser)
479 EmpathyAccountChooserPriv *priv;
481 GtkCellRenderer *renderer;
482 GtkComboBox *combobox;
484 priv = GET_PRIV (chooser);
486 /* Set up combo box with new store */
487 combobox = GTK_COMBO_BOX (chooser);
489 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
491 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
492 G_TYPE_STRING, /* Image */
493 G_TYPE_STRING, /* Name */
494 G_TYPE_BOOLEAN, /* Enabled */
497 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
499 renderer = gtk_cell_renderer_pixbuf_new ();
500 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
501 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
502 "icon-name", COL_ACCOUNT_IMAGE,
503 "sensitive", COL_ACCOUNT_ENABLED,
505 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
507 renderer = gtk_cell_renderer_text_new ();
508 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
509 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
510 "text", COL_ACCOUNT_TEXT,
511 "sensitive", COL_ACCOUNT_ENABLED,
514 /* Populate accounts */
515 tp_account_manager_prepare_async (priv->manager, NULL,
516 account_manager_prepared_cb, chooser);
518 g_object_unref (store);
522 account_chooser_account_validity_changed_cb (TpAccountManager *manager,
525 EmpathyAccountChooser *chooser)
528 account_chooser_account_add_foreach (account, chooser);
530 account_chooser_account_remove_foreach (account, chooser);
535 account_chooser_account_add_foreach (TpAccount *account,
536 EmpathyAccountChooser *chooser)
539 GtkComboBox *combobox;
543 combobox = GTK_COMBO_BOX (chooser);
544 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
546 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
547 gtk_list_store_insert_with_values (store, &iter, position,
548 COL_ACCOUNT_POINTER, account,
550 account_chooser_update_iter (chooser, &iter);
554 account_chooser_account_removed_cb (TpAccountManager *manager,
556 EmpathyAccountChooser *chooser)
558 account_chooser_account_remove_foreach (account, chooser);
568 account_chooser_find_account_foreach (GtkTreeModel *model,
573 FindAccountData *data = user_data;
576 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
578 if (account == data->account) {
580 *(data->iter) = *iter;
581 g_object_unref (account);
586 g_object_unref (account);
592 account_chooser_find_account (EmpathyAccountChooser *chooser,
597 GtkComboBox *combobox;
598 FindAccountData data;
600 combobox = GTK_COMBO_BOX (chooser);
601 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
603 data.account = account;
605 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
606 account_chooser_find_account_foreach,
613 account_chooser_account_remove_foreach (TpAccount *account,
614 EmpathyAccountChooser *chooser)
617 GtkComboBox *combobox;
620 combobox = GTK_COMBO_BOX (chooser);
621 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
623 if (account_chooser_find_account (chooser, account, &iter)) {
624 gtk_list_store_remove (store, &iter);
629 account_chooser_update_iter (EmpathyAccountChooser *chooser,
632 EmpathyAccountChooserPriv *priv;
634 GtkComboBox *combobox;
636 const gchar *icon_name;
637 gboolean is_enabled = TRUE;
639 priv = GET_PRIV (chooser);
641 combobox = GTK_COMBO_BOX (chooser);
642 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
644 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
645 COL_ACCOUNT_POINTER, &account,
648 icon_name = tp_account_get_icon_name (account);
650 is_enabled = priv->filter (account, priv->filter_data);
653 gtk_list_store_set (store, iter,
654 COL_ACCOUNT_IMAGE, icon_name,
655 COL_ACCOUNT_TEXT, tp_account_get_display_name (account),
656 COL_ACCOUNT_ENABLED, is_enabled,
659 /* set first connected account as active account */
660 if (priv->account_manually_set == FALSE &&
661 priv->set_active_item == FALSE && is_enabled) {
662 priv->set_active_item = TRUE;
663 gtk_combo_box_set_active_iter (combobox, iter);
666 g_object_unref (account);
670 account_chooser_status_changed_cb (TpAccount *account,
674 gchar *dbus_error_name,
678 EmpathyAccountChooser *chooser = user_data;
681 if (account_chooser_find_account (chooser, account, &iter)) {
682 account_chooser_update_iter (chooser, &iter);
687 account_chooser_separator_func (GtkTreeModel *model,
689 EmpathyAccountChooser *chooser)
691 EmpathyAccountChooserPriv *priv;
693 gboolean is_separator;
695 priv = GET_PRIV (chooser);
697 if (!priv->has_all_option) {
701 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
702 is_separator = text == NULL;
709 account_chooser_set_account_foreach (GtkTreeModel *model,
712 SetAccountData *data)
717 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
719 /* Special case so we can make it possible to select the All option */
720 if ((data->account == NULL) != (account == NULL)) {
724 equal = (data->account == account);
728 g_object_unref (account);
732 GtkComboBox *combobox;
734 combobox = GTK_COMBO_BOX (data->chooser);
735 gtk_combo_box_set_active_iter (combobox, iter);
744 account_chooser_filter_foreach (GtkTreeModel *model,
749 account_chooser_update_iter (chooser, iter);
754 * empathy_account_chooser_set_filter:
755 * @chooser: an #EmpathyAccountChooser
757 * @user_data: data to pass to @filter, or %NULL
759 * Sets a filter on the @chooser so only accounts that are %TRUE in the eyes
760 * of the filter are visible in the @chooser.
763 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
764 EmpathyAccountChooserFilterFunc filter,
767 EmpathyAccountChooserPriv *priv;
770 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
772 priv = GET_PRIV (chooser);
774 priv->filter = filter;
775 priv->filter_data = user_data;
777 /* Refilter existing data */
778 priv->set_active_item = FALSE;
779 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
780 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
784 * EmpathyAccountChooserFilterFunc:
785 * @account: a #TpAccount
786 * @user_data: user data, or %NULL
788 * A function which decides whether the account indicated by @account
791 * Return value: whether the account indicated by @account is visible.
795 * empathy_account_chooser_filter_is_connected:
796 * @account: a #TpAccount
797 * @user_data: user data or %NULL
799 * A useful #EmpathyAccountChooserFilterFunc that one could pass into
800 * empathy_account_chooser_set_filter() and only show connected accounts.
802 * Return value: Whether @account is connected
805 empathy_account_chooser_filter_is_connected (TpAccount *account,
808 return (tp_account_get_connection_status (account, NULL)
809 == TP_CONNECTION_STATUS_CONNECTED);