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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, 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 has_all_option;
62 EmpathyAccountChooserFilterFunc filter;
64 } EmpathyAccountChooserPriv;
67 EmpathyAccountChooser *chooser;
75 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
80 static void account_chooser_finalize (GObject *object);
81 static void account_chooser_get_property (GObject *object,
85 static void account_chooser_set_property (GObject *object,
89 static void account_chooser_setup (EmpathyAccountChooser *chooser);
90 static void account_chooser_account_created_cb (EmpathyAccountManager *manager,
92 EmpathyAccountChooser *chooser);
93 static void account_chooser_account_add_foreach (McAccount *account,
94 EmpathyAccountChooser *chooser);
95 static void account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
97 EmpathyAccountChooser *chooser);
98 static void account_chooser_account_remove_foreach (McAccount *account,
99 EmpathyAccountChooser *chooser);
100 static void account_chooser_update_iter (EmpathyAccountChooser *chooser,
102 static void account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
104 TpConnectionStatusReason reason,
105 TpConnectionStatus new_status,
106 TpConnectionStatus old_status,
107 EmpathyAccountChooser *chooser);
108 static gboolean account_chooser_separator_func (GtkTreeModel *model,
110 EmpathyAccountChooser *chooser);
111 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
114 SetAccountData *data);
121 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
124 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
126 GObjectClass *object_class = G_OBJECT_CLASS (klass);
128 object_class->finalize = account_chooser_finalize;
129 object_class->get_property = account_chooser_get_property;
130 object_class->set_property = account_chooser_set_property;
133 * EmpathyAccountChooser:has-all-option:
135 * Have an additional option in the list to mean all accounts.
137 g_object_class_install_property (object_class,
139 g_param_spec_boolean ("has-all-option",
141 "Have a separate option in the list to mean ALL accounts",
145 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
149 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
151 EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
152 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
154 chooser->priv = priv;
155 priv->set_active_item = 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 #McAccount returned should be
257 * unrefed when finished with.
259 * Return value: a new ref to the #McAccount 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 the #TpConnection associated with the account currently selected.
289 * Return value: the #TpConnection associated with the account curently selected.
292 empathy_account_chooser_get_connection (EmpathyAccountChooser *chooser)
294 EmpathyAccountChooserPriv *priv;
296 TpConnection *connection;
298 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
300 priv = GET_PRIV (chooser);
302 account = empathy_account_chooser_dup_account (chooser);
303 connection = empathy_account_manager_get_connection (priv->manager, account);
304 g_object_unref (account);
310 * empathy_account_chooser_set_account:
311 * @chooser: an #EmpathyAccountChooser
312 * @account: an #McAccount
314 * Sets the currently selected account to @account, if it exists in the list.
316 * Return value: whether the chooser was set to @account.
319 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
322 GtkComboBox *combobox;
327 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
329 combobox = GTK_COMBO_BOX (chooser);
330 model = gtk_combo_box_get_model (combobox);
331 gtk_combo_box_get_active_iter (combobox, &iter);
333 data.chooser = chooser;
334 data.account = account;
336 gtk_tree_model_foreach (model,
337 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
344 * empathy_account_chooser_get_has_all_option:
345 * @chooser: an #EmpathyAccountChooser
347 * Returns whether @chooser has the #EmpathyAccountChooser:has-all-option property
350 * Return value: whether @chooser has the #EmpathyAccountChooser:has-all-option property
354 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
356 EmpathyAccountChooserPriv *priv;
358 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
360 priv = GET_PRIV (chooser);
362 return priv->has_all_option;
366 * empathy_account_chooser_set_has_all_option:
367 * @chooser: an #EmpathyAccountChooser
368 * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option property
370 * Sets the #EmpathyAccountChooser:has-all-option property.
373 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
374 gboolean has_all_option)
376 EmpathyAccountChooserPriv *priv;
377 GtkComboBox *combobox;
382 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
384 priv = GET_PRIV (chooser);
386 if (priv->has_all_option == has_all_option) {
390 combobox = GTK_COMBO_BOX (chooser);
391 model = gtk_combo_box_get_model (combobox);
392 store = GTK_LIST_STORE (model);
394 priv->has_all_option = has_all_option;
397 * The first 2 options are the ALL and separator
400 if (has_all_option) {
401 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
402 (GtkTreeViewRowSeparatorFunc)
403 account_chooser_separator_func,
407 gtk_list_store_prepend (store, &iter);
408 gtk_list_store_set (store, &iter,
409 COL_ACCOUNT_TEXT, NULL,
410 COL_ACCOUNT_ENABLED, TRUE,
411 COL_ACCOUNT_POINTER, NULL,
414 gtk_list_store_prepend (store, &iter);
415 gtk_list_store_set (store, &iter,
416 COL_ACCOUNT_TEXT, _("All"),
417 COL_ACCOUNT_ENABLED, TRUE,
418 COL_ACCOUNT_POINTER, NULL,
421 if (gtk_tree_model_get_iter_first (model, &iter)) {
422 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
423 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
427 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
428 (GtkTreeViewRowSeparatorFunc)
434 g_object_notify (G_OBJECT (chooser), "has-all-option");
438 account_chooser_setup (EmpathyAccountChooser *chooser)
440 EmpathyAccountChooserPriv *priv;
443 GtkCellRenderer *renderer;
444 GtkComboBox *combobox;
446 priv = GET_PRIV (chooser);
448 /* Set up combo box with new store */
449 combobox = GTK_COMBO_BOX (chooser);
451 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
453 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
454 G_TYPE_STRING, /* Image */
455 G_TYPE_STRING, /* Name */
456 G_TYPE_BOOLEAN, /* Enabled */
459 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
461 renderer = gtk_cell_renderer_pixbuf_new ();
462 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
463 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
464 "icon-name", COL_ACCOUNT_IMAGE,
465 "sensitive", COL_ACCOUNT_ENABLED,
467 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
469 renderer = gtk_cell_renderer_text_new ();
470 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
471 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
472 "text", COL_ACCOUNT_TEXT,
473 "sensitive", COL_ACCOUNT_ENABLED,
476 /* Populate accounts */
477 accounts = mc_accounts_list ();
478 g_list_foreach (accounts,
479 (GFunc) account_chooser_account_add_foreach,
482 mc_accounts_list_free (accounts);
483 g_object_unref (store);
487 account_chooser_account_created_cb (EmpathyAccountManager *manager,
489 EmpathyAccountChooser *chooser)
491 account_chooser_account_add_foreach (account, chooser);
495 account_chooser_account_add_foreach (McAccount *account,
496 EmpathyAccountChooser *chooser)
499 GtkComboBox *combobox;
503 combobox = GTK_COMBO_BOX (chooser);
504 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
506 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
507 gtk_list_store_insert_with_values (store, &iter, position,
508 COL_ACCOUNT_POINTER, account,
510 account_chooser_update_iter (chooser, &iter);
514 account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
516 EmpathyAccountChooser *chooser)
518 account_chooser_account_remove_foreach (account, chooser);
528 account_chooser_find_account_foreach (GtkTreeModel *model,
533 FindAccountData *data = user_data;
536 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
538 if (empathy_account_equal (account, data->account)) {
540 *(data->iter) = *iter;
541 g_object_unref (account);
546 g_object_unref (account);
552 account_chooser_find_account (EmpathyAccountChooser *chooser,
557 GtkComboBox *combobox;
558 FindAccountData data;
560 combobox = GTK_COMBO_BOX (chooser);
561 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
563 data.account = account;
565 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
566 account_chooser_find_account_foreach,
573 account_chooser_account_remove_foreach (McAccount *account,
574 EmpathyAccountChooser *chooser)
577 GtkComboBox *combobox;
580 combobox = GTK_COMBO_BOX (chooser);
581 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
583 if (account_chooser_find_account (chooser, account, &iter)) {
584 gtk_list_store_remove (store, &iter);
589 account_chooser_update_iter (EmpathyAccountChooser *chooser,
592 EmpathyAccountChooserPriv *priv;
594 GtkComboBox *combobox;
596 const gchar *icon_name;
597 gboolean is_enabled = TRUE;
599 priv = GET_PRIV (chooser);
601 combobox = GTK_COMBO_BOX (chooser);
602 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
604 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
605 COL_ACCOUNT_POINTER, &account,
608 icon_name = empathy_icon_name_from_account (account);
610 is_enabled = priv->filter (account, priv->filter_data);
613 gtk_list_store_set (store, iter,
614 COL_ACCOUNT_IMAGE, icon_name,
615 COL_ACCOUNT_TEXT, mc_account_get_display_name (account),
616 COL_ACCOUNT_ENABLED, is_enabled,
619 /* set first connected account as active account */
620 if (priv->set_active_item == FALSE && is_enabled) {
621 priv->set_active_item = TRUE;
622 gtk_combo_box_set_active_iter (combobox, iter);
625 g_object_unref (account);
629 account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
631 TpConnectionStatusReason reason,
632 TpConnectionStatus new_status,
633 TpConnectionStatus old_status,
634 EmpathyAccountChooser *chooser)
638 if (account_chooser_find_account (chooser, account, &iter)) {
639 account_chooser_update_iter (chooser, &iter);
644 account_chooser_separator_func (GtkTreeModel *model,
646 EmpathyAccountChooser *chooser)
648 EmpathyAccountChooserPriv *priv;
650 gboolean is_separator;
652 priv = GET_PRIV (chooser);
654 if (!priv->has_all_option) {
658 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
659 is_separator = text == NULL;
666 account_chooser_set_account_foreach (GtkTreeModel *model,
669 SetAccountData *data)
674 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
676 /* Special case so we can make it possible to select the All option */
677 if ((data->account == NULL) != (account == NULL)) {
680 else if (data->account == account) {
683 equal = empathy_account_equal (data->account, account);
687 g_object_unref (account);
691 GtkComboBox *combobox;
693 combobox = GTK_COMBO_BOX (data->chooser);
694 gtk_combo_box_set_active_iter (combobox, iter);
703 account_chooser_filter_foreach (GtkTreeModel *model,
708 account_chooser_update_iter (chooser, iter);
713 * empathy_account_chooser_set_filter:
714 * @chooser: an #EmpathyAccountChooser
716 * @user_data: data to pass to @filter, or %NULL
718 * Sets a filter on the @chooser so only accounts that are %TRUE in the eyes
719 * of the filter are visible in the @chooser.
722 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
723 EmpathyAccountChooserFilterFunc filter,
726 EmpathyAccountChooserPriv *priv;
729 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
731 priv = GET_PRIV (chooser);
733 priv->filter = filter;
734 priv->filter_data = user_data;
736 /* Refilter existing data */
737 priv->set_active_item = FALSE;
738 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
739 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
743 * EmpathyAccountChooserFilterFunc:
744 * @account: an #McAccount
745 * @user_data: user data, or %NULL
747 * A function which decides whether the account indicated by @account
750 * Return value: whether the account indicated by @account is visible.
754 * empathy_account_chooser_filter_is_connected:
755 * @account: an #McAccount
756 * @user_data: user data or %NULL
758 * A useful #EmpathyAccountChooserFilterFunc that one could pass into
759 * empathy_account_chooser_set_filter() and only show connected accounts.
761 * Return value: Whether @account is connected
764 empathy_account_chooser_filter_is_connected (McAccount *account,
768 TpConnectionStatus status;
770 g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
772 mc = empathy_mission_control_dup_singleton ();
773 status = mission_control_get_connection_status (mc, account, NULL);
776 return status == TP_CONNECTION_STATUS_CONNECTED;