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 has_all_option;
62 EmpathyAccountChooserFilterFunc filter;
64 } EmpathyAccountChooserPriv;
67 EmpathyAccountChooser *chooser;
68 EmpathyAccount *account;
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,
91 EmpathyAccount *account,
92 EmpathyAccountChooser *chooser);
93 static void account_chooser_account_add_foreach (EmpathyAccount *account,
94 EmpathyAccountChooser *chooser);
95 static void account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
96 EmpathyAccount *account,
97 EmpathyAccountChooser *chooser);
98 static void account_chooser_account_remove_foreach (EmpathyAccount *account,
99 EmpathyAccountChooser *chooser);
100 static void account_chooser_update_iter (EmpathyAccountChooser *chooser,
102 static void account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
103 EmpathyAccount *account,
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 #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 GtkComboBox *combobox;
330 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
332 combobox = GTK_COMBO_BOX (chooser);
333 model = gtk_combo_box_get_model (combobox);
334 gtk_combo_box_get_active_iter (combobox, &iter);
336 data.chooser = chooser;
337 data.account = account;
339 gtk_tree_model_foreach (model,
340 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
347 * empathy_account_chooser_get_has_all_option:
348 * @chooser: an #EmpathyAccountChooser
350 * Returns whether @chooser has the #EmpathyAccountChooser:has-all-option property
353 * Return value: whether @chooser has the #EmpathyAccountChooser:has-all-option property
357 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
359 EmpathyAccountChooserPriv *priv;
361 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
363 priv = GET_PRIV (chooser);
365 return priv->has_all_option;
369 * empathy_account_chooser_set_has_all_option:
370 * @chooser: an #EmpathyAccountChooser
371 * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option property
373 * Sets the #EmpathyAccountChooser:has-all-option property.
376 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
377 gboolean has_all_option)
379 EmpathyAccountChooserPriv *priv;
380 GtkComboBox *combobox;
385 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
387 priv = GET_PRIV (chooser);
389 if (priv->has_all_option == has_all_option) {
393 combobox = GTK_COMBO_BOX (chooser);
394 model = gtk_combo_box_get_model (combobox);
395 store = GTK_LIST_STORE (model);
397 priv->has_all_option = has_all_option;
400 * The first 2 options are the ALL and separator
403 if (has_all_option) {
404 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
405 (GtkTreeViewRowSeparatorFunc)
406 account_chooser_separator_func,
410 gtk_list_store_prepend (store, &iter);
411 gtk_list_store_set (store, &iter,
412 COL_ACCOUNT_TEXT, NULL,
413 COL_ACCOUNT_ENABLED, TRUE,
414 COL_ACCOUNT_POINTER, NULL,
417 gtk_list_store_prepend (store, &iter);
418 gtk_list_store_set (store, &iter,
419 COL_ACCOUNT_TEXT, _("All"),
420 COL_ACCOUNT_ENABLED, TRUE,
421 COL_ACCOUNT_POINTER, NULL,
424 if (gtk_tree_model_get_iter_first (model, &iter)) {
425 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
426 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
430 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
431 (GtkTreeViewRowSeparatorFunc)
437 g_object_notify (G_OBJECT (chooser), "has-all-option");
441 account_chooser_setup (EmpathyAccountChooser *chooser)
443 EmpathyAccountChooserPriv *priv;
446 GtkCellRenderer *renderer;
447 GtkComboBox *combobox;
449 priv = GET_PRIV (chooser);
451 /* Set up combo box with new store */
452 combobox = GTK_COMBO_BOX (chooser);
454 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
456 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
457 G_TYPE_STRING, /* Image */
458 G_TYPE_STRING, /* Name */
459 G_TYPE_BOOLEAN, /* Enabled */
460 EMPATHY_TYPE_ACCOUNT);
462 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
464 renderer = gtk_cell_renderer_pixbuf_new ();
465 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
466 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
467 "icon-name", COL_ACCOUNT_IMAGE,
468 "sensitive", COL_ACCOUNT_ENABLED,
470 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
472 renderer = gtk_cell_renderer_text_new ();
473 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
474 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
475 "text", COL_ACCOUNT_TEXT,
476 "sensitive", COL_ACCOUNT_ENABLED,
479 /* Populate accounts */
480 accounts = empathy_account_manager_dup_accounts (priv->manager);
481 g_list_foreach (accounts,
482 (GFunc) account_chooser_account_add_foreach,
485 g_list_free (accounts);
486 g_object_unref (store);
490 account_chooser_account_created_cb (EmpathyAccountManager *manager,
491 EmpathyAccount *account,
492 EmpathyAccountChooser *chooser)
494 account_chooser_account_add_foreach (account, chooser);
498 account_chooser_account_add_foreach (EmpathyAccount *account,
499 EmpathyAccountChooser *chooser)
502 GtkComboBox *combobox;
506 combobox = GTK_COMBO_BOX (chooser);
507 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
509 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
510 gtk_list_store_insert_with_values (store, &iter, position,
511 COL_ACCOUNT_POINTER, account,
513 account_chooser_update_iter (chooser, &iter);
514 /* We got a reffed account and it was reffed by the liststore as well */
515 g_object_unref (account);
519 account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
520 EmpathyAccount *account,
521 EmpathyAccountChooser *chooser)
523 account_chooser_account_remove_foreach (account, chooser);
527 EmpathyAccount *account;
533 account_chooser_find_account_foreach (GtkTreeModel *model,
538 FindAccountData *data = user_data;
539 EmpathyAccount *account;
541 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
543 if (empathy_account_equal (account, data->account)) {
545 *(data->iter) = *iter;
546 g_object_unref (account);
551 g_object_unref (account);
557 account_chooser_find_account (EmpathyAccountChooser *chooser,
558 EmpathyAccount *account,
562 GtkComboBox *combobox;
563 FindAccountData data;
565 combobox = GTK_COMBO_BOX (chooser);
566 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
568 data.account = account;
570 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
571 account_chooser_find_account_foreach,
578 account_chooser_account_remove_foreach (EmpathyAccount *account,
579 EmpathyAccountChooser *chooser)
582 GtkComboBox *combobox;
585 combobox = GTK_COMBO_BOX (chooser);
586 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
588 if (account_chooser_find_account (chooser, account, &iter)) {
589 gtk_list_store_remove (store, &iter);
594 account_chooser_update_iter (EmpathyAccountChooser *chooser,
597 EmpathyAccountChooserPriv *priv;
599 GtkComboBox *combobox;
600 EmpathyAccount *account;
601 const gchar *icon_name;
602 gboolean is_enabled = TRUE;
604 priv = GET_PRIV (chooser);
606 combobox = GTK_COMBO_BOX (chooser);
607 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
609 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
610 COL_ACCOUNT_POINTER, &account,
613 icon_name = empathy_icon_name_from_account (account);
615 is_enabled = priv->filter (account, priv->filter_data);
618 gtk_list_store_set (store, iter,
619 COL_ACCOUNT_IMAGE, icon_name,
620 COL_ACCOUNT_TEXT, empathy_account_get_display_name (account),
621 COL_ACCOUNT_ENABLED, is_enabled,
624 /* set first connected account as active account */
625 if (priv->set_active_item == FALSE && is_enabled) {
626 priv->set_active_item = TRUE;
627 gtk_combo_box_set_active_iter (combobox, iter);
630 g_object_unref (account);
634 account_chooser_connection_changed_cb (EmpathyAccountManager *manager,
635 EmpathyAccount *account,
636 TpConnectionStatusReason reason,
637 TpConnectionStatus new_status,
638 TpConnectionStatus old_status,
639 EmpathyAccountChooser *chooser)
643 if (account_chooser_find_account (chooser, account, &iter)) {
644 account_chooser_update_iter (chooser, &iter);
649 account_chooser_separator_func (GtkTreeModel *model,
651 EmpathyAccountChooser *chooser)
653 EmpathyAccountChooserPriv *priv;
655 gboolean is_separator;
657 priv = GET_PRIV (chooser);
659 if (!priv->has_all_option) {
663 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
664 is_separator = text == NULL;
671 account_chooser_set_account_foreach (GtkTreeModel *model,
674 SetAccountData *data)
676 EmpathyAccount *account;
679 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
681 /* Special case so we can make it possible to select the All option */
682 if ((data->account == NULL) != (account == NULL)) {
685 else if (data->account == account) {
688 equal = empathy_account_equal (data->account, account);
692 g_object_unref (account);
696 GtkComboBox *combobox;
698 combobox = GTK_COMBO_BOX (data->chooser);
699 gtk_combo_box_set_active_iter (combobox, iter);
708 account_chooser_filter_foreach (GtkTreeModel *model,
713 account_chooser_update_iter (chooser, iter);
718 * empathy_account_chooser_set_filter:
719 * @chooser: an #EmpathyAccountChooser
721 * @user_data: data to pass to @filter, or %NULL
723 * Sets a filter on the @chooser so only accounts that are %TRUE in the eyes
724 * of the filter are visible in the @chooser.
727 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
728 EmpathyAccountChooserFilterFunc filter,
731 EmpathyAccountChooserPriv *priv;
734 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
736 priv = GET_PRIV (chooser);
738 priv->filter = filter;
739 priv->filter_data = user_data;
741 /* Refilter existing data */
742 priv->set_active_item = FALSE;
743 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
744 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
748 * EmpathyAccountChooserFilterFunc:
749 * @account: an #EmpathyAccount
750 * @user_data: user data, or %NULL
752 * A function which decides whether the account indicated by @account
755 * Return value: whether the account indicated by @account is visible.
759 * empathy_account_chooser_filter_is_connected:
760 * @account: an #EmpathyAccount
761 * @user_data: user data or %NULL
763 * A useful #EmpathyAccountChooserFilterFunc that one could pass into
764 * empathy_account_chooser_set_filter() and only show connected accounts.
766 * Return value: Whether @account is connected
769 empathy_account_chooser_filter_is_connected (EmpathyAccount *account,
772 TpConnectionStatus status;
774 g_object_get (account, "status", &status, NULL);
776 return status == TP_CONNECTION_STATUS_CONNECTED;