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 <libempathy/empathy-account-manager.h>
33 #include <libempathy/empathy-utils.h>
35 #include "empathy-ui-utils.h"
36 #include "empathy-account-chooser.h"
39 * SECTION:empathy-account-chooser
40 * @title:EmpathyAccountChooser
41 * @short_description: A widget used to choose from a list of accounts
42 * @include: libempathy-gtk/empathy-account-chooser.h
44 * #EmpathyAccountChooser is a widget which extends #GtkComboBox to provide
45 * a chooser of available accounts.
49 * EmpathyAccountChooser:
50 * @parent: parent object
52 * Widget which extends #GtkComboBox to provide a chooser of available accounts.
55 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountChooser)
57 EmpathyAccountManager *manager;
58 gboolean set_active_item;
59 gboolean account_manually_set;
60 gboolean has_all_option;
61 EmpathyAccountChooserFilterFunc filter;
63 } EmpathyAccountChooserPriv;
66 EmpathyAccountChooser *chooser;
67 EmpathyAccount *account;
74 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
79 static void account_chooser_finalize (GObject *object);
80 static void account_chooser_get_property (GObject *object,
84 static void account_chooser_set_property (GObject *object,
88 static void account_chooser_setup (EmpathyAccountChooser *chooser);
89 static void account_chooser_account_created_cb (EmpathyAccountManager *manager,
90 EmpathyAccount *account,
91 EmpathyAccountChooser *chooser);
92 static void account_chooser_account_add_foreach (EmpathyAccount *account,
93 EmpathyAccountChooser *chooser);
94 static void account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
95 EmpathyAccount *account,
96 EmpathyAccountChooser *chooser);
97 static void account_chooser_account_remove_foreach (EmpathyAccount *account,
98 EmpathyAccountChooser *chooser);
99 static void account_chooser_update_iter (EmpathyAccountChooser *chooser,
101 static void account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
102 EmpathyAccount *account,
103 TpConnectionStatusReason reason,
104 TpConnectionStatus new_status,
105 TpConnectionStatus old_status,
106 EmpathyAccountChooser *chooser);
107 static gboolean account_chooser_separator_func (GtkTreeModel *model,
109 EmpathyAccountChooser *chooser);
110 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
113 SetAccountData *data);
120 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
123 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
125 GObjectClass *object_class = G_OBJECT_CLASS (klass);
127 object_class->finalize = account_chooser_finalize;
128 object_class->get_property = account_chooser_get_property;
129 object_class->set_property = account_chooser_set_property;
132 * EmpathyAccountChooser:has-all-option:
134 * Have an additional option in the list to mean all accounts.
136 g_object_class_install_property (object_class,
138 g_param_spec_boolean ("has-all-option",
140 "Have a separate option in the list to mean ALL accounts",
144 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
148 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
150 EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
151 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
153 chooser->priv = priv;
154 priv->set_active_item = FALSE;
155 priv->account_manually_set = FALSE;
157 priv->filter_data = NULL;
159 priv->manager = empathy_account_manager_dup_singleton ();
161 g_signal_connect (priv->manager, "account-created",
162 G_CALLBACK (account_chooser_account_created_cb),
164 g_signal_connect (priv->manager, "account-deleted",
165 G_CALLBACK (account_chooser_account_deleted_cb),
167 g_signal_connect (priv->manager, "account-connection-changed",
168 G_CALLBACK (account_chooser_connection_changed_cb),
171 account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
175 account_chooser_finalize (GObject *object)
177 EmpathyAccountChooserPriv *priv = GET_PRIV (object);
179 g_signal_handlers_disconnect_by_func (priv->manager,
180 account_chooser_connection_changed_cb,
182 g_signal_handlers_disconnect_by_func (priv->manager,
183 account_chooser_account_created_cb,
185 g_signal_handlers_disconnect_by_func (priv->manager,
186 account_chooser_account_deleted_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 #EmpathyAccount returned should be
257 * unrefed with g_object_unref() when finished with.
259 * Return value: a new ref to the #EmpathyAccount currently selected, or %NULL.
262 empathy_account_chooser_dup_account (EmpathyAccountChooser *chooser)
264 EmpathyAccountChooserPriv *priv;
265 EmpathyAccount *account;
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;
298 EmpathyAccount *account;
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 = empathy_account_get_connection (account);
307 g_object_unref (account);
313 * empathy_account_chooser_set_account:
314 * @chooser: an #EmpathyAccountChooser
315 * @account: an #EmpathyAccount
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,
323 EmpathyAccount *account)
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_chooser_setup (EmpathyAccountChooser *chooser)
448 EmpathyAccountChooserPriv *priv;
451 GtkCellRenderer *renderer;
452 GtkComboBox *combobox;
454 priv = GET_PRIV (chooser);
456 /* Set up combo box with new store */
457 combobox = GTK_COMBO_BOX (chooser);
459 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
461 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
462 G_TYPE_STRING, /* Image */
463 G_TYPE_STRING, /* Name */
464 G_TYPE_BOOLEAN, /* Enabled */
465 EMPATHY_TYPE_ACCOUNT);
467 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
469 renderer = gtk_cell_renderer_pixbuf_new ();
470 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
471 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
472 "icon-name", COL_ACCOUNT_IMAGE,
473 "sensitive", COL_ACCOUNT_ENABLED,
475 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
477 renderer = gtk_cell_renderer_text_new ();
478 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
479 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
480 "text", COL_ACCOUNT_TEXT,
481 "sensitive", COL_ACCOUNT_ENABLED,
484 /* Populate accounts */
485 accounts = empathy_account_manager_dup_accounts (priv->manager);
486 g_list_foreach (accounts,
487 (GFunc) account_chooser_account_add_foreach,
490 g_list_free (accounts);
491 g_object_unref (store);
495 account_chooser_account_created_cb (EmpathyAccountManager *manager,
496 EmpathyAccount *account,
497 EmpathyAccountChooser *chooser)
499 account_chooser_account_add_foreach (account, chooser);
503 account_chooser_account_add_foreach (EmpathyAccount *account,
504 EmpathyAccountChooser *chooser)
507 GtkComboBox *combobox;
511 combobox = GTK_COMBO_BOX (chooser);
512 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
514 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
515 gtk_list_store_insert_with_values (store, &iter, position,
516 COL_ACCOUNT_POINTER, account,
518 account_chooser_update_iter (chooser, &iter);
519 /* We got a reffed account and it was reffed by the liststore as well */
520 g_object_unref (account);
524 account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
525 EmpathyAccount *account,
526 EmpathyAccountChooser *chooser)
528 account_chooser_account_remove_foreach (account, chooser);
532 EmpathyAccount *account;
538 account_chooser_find_account_foreach (GtkTreeModel *model,
543 FindAccountData *data = user_data;
544 EmpathyAccount *account;
546 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
548 if (empathy_account_equal (account, data->account)) {
550 *(data->iter) = *iter;
551 g_object_unref (account);
556 g_object_unref (account);
562 account_chooser_find_account (EmpathyAccountChooser *chooser,
563 EmpathyAccount *account,
567 GtkComboBox *combobox;
568 FindAccountData data;
570 combobox = GTK_COMBO_BOX (chooser);
571 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
573 data.account = account;
575 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
576 account_chooser_find_account_foreach,
583 account_chooser_account_remove_foreach (EmpathyAccount *account,
584 EmpathyAccountChooser *chooser)
587 GtkComboBox *combobox;
590 combobox = GTK_COMBO_BOX (chooser);
591 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
593 if (account_chooser_find_account (chooser, account, &iter)) {
594 gtk_list_store_remove (store, &iter);
599 account_chooser_update_iter (EmpathyAccountChooser *chooser,
602 EmpathyAccountChooserPriv *priv;
604 GtkComboBox *combobox;
605 EmpathyAccount *account;
606 const gchar *icon_name;
607 gboolean is_enabled = TRUE;
609 priv = GET_PRIV (chooser);
611 combobox = GTK_COMBO_BOX (chooser);
612 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
614 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
615 COL_ACCOUNT_POINTER, &account,
618 icon_name = empathy_account_get_icon_name (account);
620 is_enabled = priv->filter (account, priv->filter_data);
623 gtk_list_store_set (store, iter,
624 COL_ACCOUNT_IMAGE, icon_name,
625 COL_ACCOUNT_TEXT, empathy_account_get_display_name (account),
626 COL_ACCOUNT_ENABLED, is_enabled,
629 /* set first connected account as active account */
630 if (priv->account_manually_set == FALSE &&
631 priv->set_active_item == FALSE && is_enabled) {
632 priv->set_active_item = TRUE;
633 gtk_combo_box_set_active_iter (combobox, iter);
636 g_object_unref (account);
640 account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
641 EmpathyAccount *account,
642 TpConnectionStatusReason reason,
643 TpConnectionStatus new_status,
644 TpConnectionStatus old_status,
645 EmpathyAccountChooser *chooser)
649 if (account_chooser_find_account (chooser, account, &iter)) {
650 account_chooser_update_iter (chooser, &iter);
655 account_chooser_separator_func (GtkTreeModel *model,
657 EmpathyAccountChooser *chooser)
659 EmpathyAccountChooserPriv *priv;
661 gboolean is_separator;
663 priv = GET_PRIV (chooser);
665 if (!priv->has_all_option) {
669 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
670 is_separator = text == NULL;
677 account_chooser_set_account_foreach (GtkTreeModel *model,
680 SetAccountData *data)
682 EmpathyAccount *account;
685 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
687 /* Special case so we can make it possible to select the All option */
688 if ((data->account == NULL) != (account == NULL)) {
691 else if (data->account == account) {
694 equal = empathy_account_equal (data->account, account);
698 g_object_unref (account);
702 GtkComboBox *combobox;
704 combobox = GTK_COMBO_BOX (data->chooser);
705 gtk_combo_box_set_active_iter (combobox, iter);
714 account_chooser_filter_foreach (GtkTreeModel *model,
719 account_chooser_update_iter (chooser, iter);
724 * empathy_account_chooser_set_filter:
725 * @chooser: an #EmpathyAccountChooser
727 * @user_data: data to pass to @filter, or %NULL
729 * Sets a filter on the @chooser so only accounts that are %TRUE in the eyes
730 * of the filter are visible in the @chooser.
733 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
734 EmpathyAccountChooserFilterFunc filter,
737 EmpathyAccountChooserPriv *priv;
740 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
742 priv = GET_PRIV (chooser);
744 priv->filter = filter;
745 priv->filter_data = user_data;
747 /* Refilter existing data */
748 priv->set_active_item = FALSE;
749 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
750 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
754 * EmpathyAccountChooserFilterFunc:
755 * @account: an #EmpathyAccount
756 * @user_data: user data, or %NULL
758 * A function which decides whether the account indicated by @account
761 * Return value: whether the account indicated by @account is visible.
765 * empathy_account_chooser_filter_is_connected:
766 * @account: an #EmpathyAccount
767 * @user_data: user data or %NULL
769 * A useful #EmpathyAccountChooserFilterFunc that one could pass into
770 * empathy_account_chooser_set_filter() and only show connected accounts.
772 * Return value: Whether @account is connected
775 empathy_account_chooser_filter_is_connected (EmpathyAccount *account,
778 TpConnectionStatus status;
780 g_object_get (account, "connection-status", &status, NULL);
782 return status == TP_CONNECTION_STATUS_CONNECTED;