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 <libmissioncontrol/mission-control.h>
34 #include <libempathy/empathy-account-manager.h>
35 #include <libempathy/empathy-utils.h>
37 #include "empathy-ui-utils.h"
38 #include "empathy-account-chooser.h"
41 * SECTION:empathy-account-chooser
42 * @title:EmpathyAccountChooser
43 * @short_description: A widget used to choose from a list of accounts
44 * @include: libempathy-gtk/empathy-account-chooser.h
46 * #EmpathyAccountChooser is a widget which extends #GtkComboBox to provide
47 * a chooser of available accounts.
51 * EmpathyAccountChooser:
52 * @parent: parent object
54 * Widget which extends #GtkComboBox to provide a chooser of available accounts.
57 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountChooser)
59 EmpathyAccountManager *manager;
60 gboolean set_active_item;
61 gboolean account_manually_set;
62 gboolean has_all_option;
63 EmpathyAccountChooserFilterFunc filter;
65 } EmpathyAccountChooserPriv;
68 EmpathyAccountChooser *chooser;
69 EmpathyAccount *account;
76 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
81 static void account_chooser_finalize (GObject *object);
82 static void account_chooser_get_property (GObject *object,
86 static void account_chooser_set_property (GObject *object,
90 static void account_chooser_setup (EmpathyAccountChooser *chooser);
91 static void account_chooser_account_created_cb (EmpathyAccountManager *manager,
92 EmpathyAccount *account,
93 EmpathyAccountChooser *chooser);
94 static void account_chooser_account_add_foreach (EmpathyAccount *account,
95 EmpathyAccountChooser *chooser);
96 static void account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
97 EmpathyAccount *account,
98 EmpathyAccountChooser *chooser);
99 static void account_chooser_account_remove_foreach (EmpathyAccount *account,
100 EmpathyAccountChooser *chooser);
101 static void account_chooser_update_iter (EmpathyAccountChooser *chooser,
103 static void account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
104 EmpathyAccount *account,
105 TpConnectionStatusReason reason,
106 TpConnectionStatus new_status,
107 TpConnectionStatus old_status,
108 EmpathyAccountChooser *chooser);
109 static gboolean account_chooser_separator_func (GtkTreeModel *model,
111 EmpathyAccountChooser *chooser);
112 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
115 SetAccountData *data);
122 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
125 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
127 GObjectClass *object_class = G_OBJECT_CLASS (klass);
129 object_class->finalize = account_chooser_finalize;
130 object_class->get_property = account_chooser_get_property;
131 object_class->set_property = account_chooser_set_property;
134 * EmpathyAccountChooser:has-all-option:
136 * Have an additional option in the list to mean all accounts.
138 g_object_class_install_property (object_class,
140 g_param_spec_boolean ("has-all-option",
142 "Have a separate option in the list to mean ALL accounts",
146 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
150 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
152 EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
153 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
155 chooser->priv = priv;
156 priv->set_active_item = FALSE;
157 priv->account_manually_set = FALSE;
159 priv->filter_data = NULL;
161 priv->manager = empathy_account_manager_dup_singleton ();
163 g_signal_connect (priv->manager, "account-created",
164 G_CALLBACK (account_chooser_account_created_cb),
166 g_signal_connect (priv->manager, "account-deleted",
167 G_CALLBACK (account_chooser_account_deleted_cb),
169 g_signal_connect (priv->manager, "account-connection-changed",
170 G_CALLBACK (account_chooser_connection_changed_cb),
173 account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
177 account_chooser_finalize (GObject *object)
179 EmpathyAccountChooserPriv *priv = GET_PRIV (object);
181 g_signal_handlers_disconnect_by_func (priv->manager,
182 account_chooser_connection_changed_cb,
184 g_signal_handlers_disconnect_by_func (priv->manager,
185 account_chooser_account_created_cb,
187 g_signal_handlers_disconnect_by_func (priv->manager,
188 account_chooser_account_deleted_cb,
190 g_object_unref (priv->manager);
192 G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
196 account_chooser_get_property (GObject *object,
201 EmpathyAccountChooserPriv *priv;
203 priv = GET_PRIV (object);
206 case PROP_HAS_ALL_OPTION:
207 g_value_set_boolean (value, priv->has_all_option);
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
216 account_chooser_set_property (GObject *object,
221 EmpathyAccountChooserPriv *priv;
223 priv = GET_PRIV (object);
226 case PROP_HAS_ALL_OPTION:
227 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
228 g_value_get_boolean (value));
231 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
237 * empathy_account_chooser_new:
239 * Creates a new #EmpathyAccountChooser.
241 * Return value: A new #EmpathyAccountChooser
244 empathy_account_chooser_new (void)
248 chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
254 * empathy_account_chooser_dup_account:
255 * @chooser: an #EmpathyAccountChooser
257 * Returns the account which is currently selected in the chooser or %NULL
258 * if there is no account selected. The #EmpathyAccount returned should be
259 * unrefed with g_object_unref() when finished with.
261 * Return value: a new ref to the #EmpathyAccount currently selected, or %NULL.
264 empathy_account_chooser_dup_account (EmpathyAccountChooser *chooser)
266 EmpathyAccountChooserPriv *priv;
267 EmpathyAccount *account;
271 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
273 priv = GET_PRIV (chooser);
275 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
276 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
280 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
286 * empathy_account_chooser_get_connection:
287 * @chooser: an #EmpathyAccountChooser
289 * Returns a borrowed reference to the #TpConnection associated with the
290 * account currently selected. The caller must reference the returned object with
291 * g_object_ref() if it will be kept
293 * Return value: a borrowed reference to the #TpConnection associated with the
294 * account curently selected.
297 empathy_account_chooser_get_connection (EmpathyAccountChooser *chooser)
299 EmpathyAccountChooserPriv *priv;
300 EmpathyAccount *account;
301 TpConnection *connection;
303 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
305 priv = GET_PRIV (chooser);
307 account = empathy_account_chooser_dup_account (chooser);
308 connection = empathy_account_get_connection (account);
309 g_object_unref (account);
315 * empathy_account_chooser_set_account:
316 * @chooser: an #EmpathyAccountChooser
317 * @account: an #EmpathyAccount
319 * Sets the currently selected account to @account, if it exists in the list.
321 * Return value: whether the chooser was set to @account.
324 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
325 EmpathyAccount *account)
327 EmpathyAccountChooserPriv *priv;
328 GtkComboBox *combobox;
333 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
335 priv = GET_PRIV (chooser);
337 combobox = GTK_COMBO_BOX (chooser);
338 model = gtk_combo_box_get_model (combobox);
339 gtk_combo_box_get_active_iter (combobox, &iter);
341 data.chooser = chooser;
342 data.account = account;
344 gtk_tree_model_foreach (model,
345 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
348 priv->account_manually_set = data.set;
354 * empathy_account_chooser_get_has_all_option:
355 * @chooser: an #EmpathyAccountChooser
357 * Returns whether @chooser has the #EmpathyAccountChooser:has-all-option property
360 * Return value: whether @chooser has the #EmpathyAccountChooser:has-all-option property
364 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
366 EmpathyAccountChooserPriv *priv;
368 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
370 priv = GET_PRIV (chooser);
372 return priv->has_all_option;
376 * empathy_account_chooser_set_has_all_option:
377 * @chooser: an #EmpathyAccountChooser
378 * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option property
380 * Sets the #EmpathyAccountChooser:has-all-option property.
383 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
384 gboolean has_all_option)
386 EmpathyAccountChooserPriv *priv;
387 GtkComboBox *combobox;
392 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
394 priv = GET_PRIV (chooser);
396 if (priv->has_all_option == has_all_option) {
400 combobox = GTK_COMBO_BOX (chooser);
401 model = gtk_combo_box_get_model (combobox);
402 store = GTK_LIST_STORE (model);
404 priv->has_all_option = has_all_option;
407 * The first 2 options are the ALL and separator
410 if (has_all_option) {
411 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
412 (GtkTreeViewRowSeparatorFunc)
413 account_chooser_separator_func,
417 gtk_list_store_prepend (store, &iter);
418 gtk_list_store_set (store, &iter,
419 COL_ACCOUNT_TEXT, NULL,
420 COL_ACCOUNT_ENABLED, TRUE,
421 COL_ACCOUNT_POINTER, NULL,
424 gtk_list_store_prepend (store, &iter);
425 gtk_list_store_set (store, &iter,
426 COL_ACCOUNT_TEXT, _("All"),
427 COL_ACCOUNT_ENABLED, TRUE,
428 COL_ACCOUNT_POINTER, NULL,
431 if (gtk_tree_model_get_iter_first (model, &iter)) {
432 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
433 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
437 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
438 (GtkTreeViewRowSeparatorFunc)
444 g_object_notify (G_OBJECT (chooser), "has-all-option");
448 account_chooser_setup (EmpathyAccountChooser *chooser)
450 EmpathyAccountChooserPriv *priv;
453 GtkCellRenderer *renderer;
454 GtkComboBox *combobox;
456 priv = GET_PRIV (chooser);
458 /* Set up combo box with new store */
459 combobox = GTK_COMBO_BOX (chooser);
461 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
463 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
464 G_TYPE_STRING, /* Image */
465 G_TYPE_STRING, /* Name */
466 G_TYPE_BOOLEAN, /* Enabled */
467 EMPATHY_TYPE_ACCOUNT);
469 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
471 renderer = gtk_cell_renderer_pixbuf_new ();
472 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
473 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
474 "icon-name", COL_ACCOUNT_IMAGE,
475 "sensitive", COL_ACCOUNT_ENABLED,
477 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
479 renderer = gtk_cell_renderer_text_new ();
480 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
481 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
482 "text", COL_ACCOUNT_TEXT,
483 "sensitive", COL_ACCOUNT_ENABLED,
486 /* Populate accounts */
487 accounts = empathy_account_manager_dup_accounts (priv->manager);
488 g_list_foreach (accounts,
489 (GFunc) account_chooser_account_add_foreach,
492 g_list_free (accounts);
493 g_object_unref (store);
497 account_chooser_account_created_cb (EmpathyAccountManager *manager,
498 EmpathyAccount *account,
499 EmpathyAccountChooser *chooser)
501 account_chooser_account_add_foreach (account, chooser);
505 account_chooser_account_add_foreach (EmpathyAccount *account,
506 EmpathyAccountChooser *chooser)
509 GtkComboBox *combobox;
513 combobox = GTK_COMBO_BOX (chooser);
514 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
516 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
517 gtk_list_store_insert_with_values (store, &iter, position,
518 COL_ACCOUNT_POINTER, account,
520 account_chooser_update_iter (chooser, &iter);
521 /* We got a reffed account and it was reffed by the liststore as well */
522 g_object_unref (account);
526 account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
527 EmpathyAccount *account,
528 EmpathyAccountChooser *chooser)
530 account_chooser_account_remove_foreach (account, chooser);
534 EmpathyAccount *account;
540 account_chooser_find_account_foreach (GtkTreeModel *model,
545 FindAccountData *data = user_data;
546 EmpathyAccount *account;
548 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
550 if (empathy_account_equal (account, data->account)) {
552 *(data->iter) = *iter;
553 g_object_unref (account);
558 g_object_unref (account);
564 account_chooser_find_account (EmpathyAccountChooser *chooser,
565 EmpathyAccount *account,
569 GtkComboBox *combobox;
570 FindAccountData data;
572 combobox = GTK_COMBO_BOX (chooser);
573 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
575 data.account = account;
577 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
578 account_chooser_find_account_foreach,
585 account_chooser_account_remove_foreach (EmpathyAccount *account,
586 EmpathyAccountChooser *chooser)
589 GtkComboBox *combobox;
592 combobox = GTK_COMBO_BOX (chooser);
593 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
595 if (account_chooser_find_account (chooser, account, &iter)) {
596 gtk_list_store_remove (store, &iter);
601 account_chooser_update_iter (EmpathyAccountChooser *chooser,
604 EmpathyAccountChooserPriv *priv;
606 GtkComboBox *combobox;
607 EmpathyAccount *account;
608 const gchar *icon_name;
609 gboolean is_enabled = TRUE;
611 priv = GET_PRIV (chooser);
613 combobox = GTK_COMBO_BOX (chooser);
614 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
616 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
617 COL_ACCOUNT_POINTER, &account,
620 icon_name = empathy_icon_name_from_account (account);
622 is_enabled = priv->filter (account, priv->filter_data);
625 gtk_list_store_set (store, iter,
626 COL_ACCOUNT_IMAGE, icon_name,
627 COL_ACCOUNT_TEXT, empathy_account_get_display_name (account),
628 COL_ACCOUNT_ENABLED, is_enabled,
631 /* set first connected account as active account */
632 if (priv->account_manually_set == FALSE &&
633 priv->set_active_item == FALSE && is_enabled) {
634 priv->set_active_item = TRUE;
635 gtk_combo_box_set_active_iter (combobox, iter);
638 g_object_unref (account);
642 account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
643 EmpathyAccount *account,
644 TpConnectionStatusReason reason,
645 TpConnectionStatus new_status,
646 TpConnectionStatus old_status,
647 EmpathyAccountChooser *chooser)
651 if (account_chooser_find_account (chooser, account, &iter)) {
652 account_chooser_update_iter (chooser, &iter);
657 account_chooser_separator_func (GtkTreeModel *model,
659 EmpathyAccountChooser *chooser)
661 EmpathyAccountChooserPriv *priv;
663 gboolean is_separator;
665 priv = GET_PRIV (chooser);
667 if (!priv->has_all_option) {
671 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
672 is_separator = text == NULL;
679 account_chooser_set_account_foreach (GtkTreeModel *model,
682 SetAccountData *data)
684 EmpathyAccount *account;
687 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
689 /* Special case so we can make it possible to select the All option */
690 if ((data->account == NULL) != (account == NULL)) {
693 else if (data->account == account) {
696 equal = empathy_account_equal (data->account, account);
700 g_object_unref (account);
704 GtkComboBox *combobox;
706 combobox = GTK_COMBO_BOX (data->chooser);
707 gtk_combo_box_set_active_iter (combobox, iter);
716 account_chooser_filter_foreach (GtkTreeModel *model,
721 account_chooser_update_iter (chooser, iter);
726 * empathy_account_chooser_set_filter:
727 * @chooser: an #EmpathyAccountChooser
729 * @user_data: data to pass to @filter, or %NULL
731 * Sets a filter on the @chooser so only accounts that are %TRUE in the eyes
732 * of the filter are visible in the @chooser.
735 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
736 EmpathyAccountChooserFilterFunc filter,
739 EmpathyAccountChooserPriv *priv;
742 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
744 priv = GET_PRIV (chooser);
746 priv->filter = filter;
747 priv->filter_data = user_data;
749 /* Refilter existing data */
750 priv->set_active_item = FALSE;
751 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
752 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
756 * EmpathyAccountChooserFilterFunc:
757 * @account: an #EmpathyAccount
758 * @user_data: user data, or %NULL
760 * A function which decides whether the account indicated by @account
763 * Return value: whether the account indicated by @account is visible.
767 * empathy_account_chooser_filter_is_connected:
768 * @account: an #EmpathyAccount
769 * @user_data: user data or %NULL
771 * A useful #EmpathyAccountChooserFilterFunc that one could pass into
772 * empathy_account_chooser_set_filter() and only show connected accounts.
774 * Return value: Whether @account is connected
777 empathy_account_chooser_filter_is_connected (EmpathyAccount *account,
780 TpConnectionStatus status;
782 g_object_get (account, "status", &status, NULL);
784 return status == TP_CONNECTION_STATUS_CONNECTED;