Updated German help translation
[empathy/ppotvin.git] / libempathy-gtk / empathy-account-chooser.c
blobbd46efc16a0a326902caf910443658733f7d5e3d
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
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>
25 #include "config.h"
27 #include <string.h>
29 #include <glib/gi18n-lib.h>
30 #include <gtk/gtk.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>
42 /**
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.
52 /**
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)
60 typedef struct {
61 TpAccountManager *manager;
62 gboolean set_active_item;
63 gboolean account_manually_set;
64 gboolean has_all_option;
65 EmpathyAccountChooserFilterFunc filter;
66 gpointer filter_data;
67 gboolean ready;
68 } EmpathyAccountChooserPriv;
70 typedef struct {
71 EmpathyAccountChooser *chooser;
72 TpAccount *account;
73 gboolean set;
74 } SetAccountData;
76 enum {
77 COL_ACCOUNT_IMAGE,
78 COL_ACCOUNT_TEXT,
79 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
80 COL_ACCOUNT_POINTER,
81 COL_ACCOUNT_COUNT
84 static void account_chooser_finalize (GObject *object);
85 static void account_chooser_get_property (GObject *object,
86 guint param_id,
87 GValue *value,
88 GParamSpec *pspec);
89 static void account_chooser_set_property (GObject *object,
90 guint param_id,
91 const GValue *value,
92 GParamSpec *pspec);
93 static void account_chooser_setup (EmpathyAccountChooser *chooser);
94 static void account_chooser_account_validity_changed_cb (TpAccountManager *manager,
95 TpAccount *account,
96 gboolean valid,
97 EmpathyAccountChooser *chooser);
98 static void account_chooser_account_add_foreach (TpAccount *account,
99 EmpathyAccountChooser *chooser);
100 static void account_chooser_account_removed_cb (TpAccountManager *manager,
101 TpAccount *account,
102 EmpathyAccountChooser *chooser);
103 static void account_chooser_account_remove_foreach (TpAccount *account,
104 EmpathyAccountChooser *chooser);
105 static void account_chooser_update_iter (EmpathyAccountChooser *chooser,
106 GtkTreeIter *iter);
107 static void account_chooser_status_changed_cb (TpAccount *account,
108 guint old_status,
109 guint new_status,
110 guint reason,
111 gchar *dbus_error_name,
112 GHashTable *details,
113 gpointer user_data);
114 static gboolean account_chooser_separator_func (GtkTreeModel *model,
115 GtkTreeIter *iter,
116 EmpathyAccountChooser *chooser);
117 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
118 GtkTreePath *path,
119 GtkTreeIter *iter,
120 SetAccountData *data);
122 enum {
123 PROP_0,
124 PROP_HAS_ALL_OPTION,
127 enum {
128 READY,
129 LAST_SIGNAL
132 static guint signals[LAST_SIGNAL] = { 0 };
134 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
136 static void
137 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
139 GObjectClass *object_class = G_OBJECT_CLASS (klass);
141 object_class->finalize = account_chooser_finalize;
142 object_class->get_property = account_chooser_get_property;
143 object_class->set_property = account_chooser_set_property;
146 * EmpathyAccountChooser:has-all-option:
148 * Have an additional option in the list to mean all accounts.
150 g_object_class_install_property (object_class,
151 PROP_HAS_ALL_OPTION,
152 g_param_spec_boolean ("has-all-option",
153 "Has All Option",
154 "Have a separate option in the list to mean ALL accounts",
155 FALSE,
156 G_PARAM_READWRITE));
158 signals[READY] =
159 g_signal_new ("ready",
160 G_OBJECT_CLASS_TYPE (object_class),
161 G_SIGNAL_RUN_LAST,
163 NULL, NULL,
164 g_cclosure_marshal_VOID__VOID,
165 G_TYPE_NONE,
168 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
171 static void
172 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
174 EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
175 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
177 chooser->priv = priv;
178 priv->set_active_item = FALSE;
179 priv->account_manually_set = FALSE;
180 priv->filter = NULL;
181 priv->filter_data = NULL;
183 priv->manager = tp_account_manager_dup ();
185 g_signal_connect (priv->manager, "account-validity-changed",
186 G_CALLBACK (account_chooser_account_validity_changed_cb),
187 chooser);
188 g_signal_connect (priv->manager, "account-removed",
189 G_CALLBACK (account_chooser_account_removed_cb),
190 chooser);
192 account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
195 static void
196 account_chooser_finalize (GObject *object)
198 EmpathyAccountChooserPriv *priv = GET_PRIV (object);
200 g_signal_handlers_disconnect_by_func (priv->manager,
201 account_chooser_account_validity_changed_cb,
202 object);
203 g_signal_handlers_disconnect_by_func (priv->manager,
204 account_chooser_account_removed_cb,
205 object);
206 g_object_unref (priv->manager);
208 G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
211 static void
212 account_chooser_get_property (GObject *object,
213 guint param_id,
214 GValue *value,
215 GParamSpec *pspec)
217 EmpathyAccountChooserPriv *priv;
219 priv = GET_PRIV (object);
221 switch (param_id) {
222 case PROP_HAS_ALL_OPTION:
223 g_value_set_boolean (value, priv->has_all_option);
224 break;
225 default:
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
227 break;
231 static void
232 account_chooser_set_property (GObject *object,
233 guint param_id,
234 const GValue *value,
235 GParamSpec *pspec)
237 EmpathyAccountChooserPriv *priv;
239 priv = GET_PRIV (object);
241 switch (param_id) {
242 case PROP_HAS_ALL_OPTION:
243 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
244 g_value_get_boolean (value));
245 break;
246 default:
247 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
248 break;
253 * empathy_account_chooser_new:
255 * Creates a new #EmpathyAccountChooser.
257 * Return value: A new #EmpathyAccountChooser
259 GtkWidget *
260 empathy_account_chooser_new (void)
262 GtkWidget *chooser;
264 chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
266 return chooser;
270 * empathy_account_chooser_dup_account:
271 * @chooser: an #EmpathyAccountChooser
273 * Returns the account which is currently selected in the chooser or %NULL
274 * if there is no account selected. The #TpAccount returned should be
275 * unrefed with g_object_unref() when finished with.
277 * Return value: a new ref to the #TpAccount currently selected, or %NULL.
279 TpAccount *
280 empathy_account_chooser_dup_account (EmpathyAccountChooser *chooser)
282 EmpathyAccountChooserPriv *priv;
283 TpAccount *account;
284 GtkTreeModel *model;
285 GtkTreeIter iter;
287 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
289 priv = GET_PRIV (chooser);
291 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
292 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
293 return NULL;
296 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
298 return account;
302 * empathy_account_chooser_get_connection:
303 * @chooser: an #EmpathyAccountChooser
305 * Returns a borrowed reference to the #TpConnection associated with the
306 * account currently selected. The caller must reference the returned object with
307 * g_object_ref() if it will be kept
309 * Return value: a borrowed reference to the #TpConnection associated with the
310 * account curently selected.
312 TpConnection *
313 empathy_account_chooser_get_connection (EmpathyAccountChooser *chooser)
315 EmpathyAccountChooserPriv *priv;
316 TpAccount *account;
317 TpConnection *connection;
319 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
321 priv = GET_PRIV (chooser);
323 account = empathy_account_chooser_dup_account (chooser);
325 /* if the returned account is NULL, then the account manager probably
326 * hasn't been prepared yet. It should be safe to return NULL here
327 * though. */
328 if (account == NULL) {
329 return NULL;
332 connection = tp_account_get_connection (account);
333 g_object_unref (account);
335 return connection;
339 * empathy_account_chooser_set_account:
340 * @chooser: an #EmpathyAccountChooser
341 * @account: a #TpAccount
343 * Sets the currently selected account to @account, if it exists in the list.
345 * Return value: whether the chooser was set to @account.
347 gboolean
348 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
349 TpAccount *account)
351 EmpathyAccountChooserPriv *priv;
352 GtkComboBox *combobox;
353 GtkTreeModel *model;
354 GtkTreeIter iter;
355 SetAccountData data;
357 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
359 priv = GET_PRIV (chooser);
361 combobox = GTK_COMBO_BOX (chooser);
362 model = gtk_combo_box_get_model (combobox);
363 gtk_combo_box_get_active_iter (combobox, &iter);
365 data.chooser = chooser;
366 data.account = account;
368 gtk_tree_model_foreach (model,
369 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
370 &data);
372 priv->account_manually_set = data.set;
374 return data.set;
378 * empathy_account_chooser_get_has_all_option:
379 * @chooser: an #EmpathyAccountChooser
381 * Returns whether @chooser has the #EmpathyAccountChooser:has-all-option property
382 * set to true.
384 * Return value: whether @chooser has the #EmpathyAccountChooser:has-all-option property
385 * enabled.
387 gboolean
388 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
390 EmpathyAccountChooserPriv *priv;
392 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
394 priv = GET_PRIV (chooser);
396 return priv->has_all_option;
400 * empathy_account_chooser_set_has_all_option:
401 * @chooser: an #EmpathyAccountChooser
402 * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option property
404 * Sets the #EmpathyAccountChooser:has-all-option property.
406 void
407 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
408 gboolean has_all_option)
410 EmpathyAccountChooserPriv *priv;
411 GtkComboBox *combobox;
412 GtkListStore *store;
413 GtkTreeModel *model;
414 GtkTreeIter iter;
416 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
418 priv = GET_PRIV (chooser);
420 if (priv->has_all_option == has_all_option) {
421 return;
424 combobox = GTK_COMBO_BOX (chooser);
425 model = gtk_combo_box_get_model (combobox);
426 store = GTK_LIST_STORE (model);
428 priv->has_all_option = has_all_option;
431 * The first 2 options are the ALL and separator
434 if (has_all_option) {
435 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
436 (GtkTreeViewRowSeparatorFunc)
437 account_chooser_separator_func,
438 chooser,
439 NULL);
441 gtk_list_store_prepend (store, &iter);
442 gtk_list_store_set (store, &iter,
443 COL_ACCOUNT_TEXT, NULL,
444 COL_ACCOUNT_ENABLED, TRUE,
445 COL_ACCOUNT_POINTER, NULL,
446 -1);
448 gtk_list_store_prepend (store, &iter);
449 gtk_list_store_set (store, &iter,
450 COL_ACCOUNT_TEXT, _("All"),
451 COL_ACCOUNT_ENABLED, TRUE,
452 COL_ACCOUNT_POINTER, NULL,
453 -1);
454 } else {
455 if (gtk_tree_model_get_iter_first (model, &iter)) {
456 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
457 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
461 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
462 (GtkTreeViewRowSeparatorFunc)
463 NULL,
464 NULL,
465 NULL);
468 g_object_notify (G_OBJECT (chooser), "has-all-option");
471 static void
472 account_manager_prepared_cb (GObject *source_object,
473 GAsyncResult *result,
474 gpointer user_data)
476 GList *accounts, *l;
477 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
478 EmpathyAccountChooser *chooser = user_data;
479 EmpathyAccountChooserPriv *priv = GET_PRIV (chooser);
480 GError *error = NULL;
482 if (!tp_account_manager_prepare_finish (manager, result, &error)) {
483 DEBUG ("Failed to prepare account manager: %s", error->message);
484 g_error_free (error);
485 return;
488 accounts = tp_account_manager_get_valid_accounts (manager);
490 for (l = accounts; l != NULL; l = l->next) {
491 TpAccount *account = l->data;
493 account_chooser_account_add_foreach (account, chooser);
495 empathy_signal_connect_weak (account, "status-changed",
496 G_CALLBACK (account_chooser_status_changed_cb),
497 G_OBJECT (chooser));
500 g_list_free (accounts);
502 priv->ready = TRUE;
503 g_signal_emit (chooser, signals[READY], 0);
506 static gint
507 account_cmp (GtkTreeModel *model,
508 GtkTreeIter *a,
509 GtkTreeIter *b,
510 gpointer user_data)
512 gboolean a_enabled, b_enabled;
513 gchar *a_text, *b_text;
514 gint result;
516 gtk_tree_model_get (model, a, COL_ACCOUNT_ENABLED, &a_enabled, -1);
517 gtk_tree_model_get (model, b, COL_ACCOUNT_ENABLED, &b_enabled, -1);
519 /* Enabled accounts are displayed first */
520 if (a_enabled != b_enabled)
521 return a_enabled ? -1: 1;
523 gtk_tree_model_get (model, a, COL_ACCOUNT_TEXT, &a_text, -1);
524 gtk_tree_model_get (model, b, COL_ACCOUNT_TEXT, &b_text, -1);
526 if (a_text == b_text)
527 result = 0;
528 else if (a_text == NULL)
529 result = 1;
530 else if (b_text == NULL)
531 result = -1;
532 else
533 result = g_ascii_strcasecmp (a_text, b_text);
535 g_free (a_text);
536 g_free (b_text);
538 return result;
541 static void
542 account_chooser_setup (EmpathyAccountChooser *chooser)
544 EmpathyAccountChooserPriv *priv;
545 GtkListStore *store;
546 GtkCellRenderer *renderer;
547 GtkComboBox *combobox;
549 priv = GET_PRIV (chooser);
551 /* Set up combo box with new store */
552 combobox = GTK_COMBO_BOX (chooser);
554 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
556 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
557 G_TYPE_STRING, /* Image */
558 G_TYPE_STRING, /* Name */
559 G_TYPE_BOOLEAN, /* Enabled */
560 TP_TYPE_ACCOUNT);
562 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
563 account_cmp, chooser, NULL);
564 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
565 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
567 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
569 renderer = gtk_cell_renderer_pixbuf_new ();
570 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
571 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
572 "icon-name", COL_ACCOUNT_IMAGE,
573 "sensitive", COL_ACCOUNT_ENABLED,
574 NULL);
575 g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
577 renderer = gtk_cell_renderer_text_new ();
578 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
579 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
580 "text", COL_ACCOUNT_TEXT,
581 "sensitive", COL_ACCOUNT_ENABLED,
582 NULL);
584 /* Populate accounts */
585 tp_account_manager_prepare_async (priv->manager, NULL,
586 account_manager_prepared_cb, chooser);
588 g_object_unref (store);
591 static void
592 account_chooser_account_validity_changed_cb (TpAccountManager *manager,
593 TpAccount *account,
594 gboolean valid,
595 EmpathyAccountChooser *chooser)
597 if (valid) {
598 account_chooser_account_add_foreach (account, chooser);
599 } else {
600 account_chooser_account_remove_foreach (account, chooser);
604 static void
605 account_chooser_account_add_foreach (TpAccount *account,
606 EmpathyAccountChooser *chooser)
608 GtkListStore *store;
609 GtkComboBox *combobox;
610 GtkTreeIter iter;
611 gint position;
613 combobox = GTK_COMBO_BOX (chooser);
614 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
616 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
617 gtk_list_store_insert_with_values (store, &iter, position,
618 COL_ACCOUNT_POINTER, account,
619 -1);
620 account_chooser_update_iter (chooser, &iter);
623 static void
624 account_chooser_account_removed_cb (TpAccountManager *manager,
625 TpAccount *account,
626 EmpathyAccountChooser *chooser)
628 account_chooser_account_remove_foreach (account, chooser);
631 typedef struct {
632 TpAccount *account;
633 GtkTreeIter *iter;
634 gboolean found;
635 } FindAccountData;
637 static gboolean
638 account_chooser_find_account_foreach (GtkTreeModel *model,
639 GtkTreePath *path,
640 GtkTreeIter *iter,
641 gpointer user_data)
643 FindAccountData *data = user_data;
644 TpAccount *account;
646 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
648 if (account == data->account) {
649 data->found = TRUE;
650 *(data->iter) = *iter;
651 g_object_unref (account);
653 return TRUE;
656 g_object_unref (account);
658 return FALSE;
661 static gboolean
662 account_chooser_find_account (EmpathyAccountChooser *chooser,
663 TpAccount *account,
664 GtkTreeIter *iter)
666 GtkListStore *store;
667 GtkComboBox *combobox;
668 FindAccountData data;
670 combobox = GTK_COMBO_BOX (chooser);
671 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
673 data.account = account;
674 data.iter = iter;
675 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
676 account_chooser_find_account_foreach,
677 &data);
679 return data.found;
682 static void
683 account_chooser_account_remove_foreach (TpAccount *account,
684 EmpathyAccountChooser *chooser)
686 GtkListStore *store;
687 GtkComboBox *combobox;
688 GtkTreeIter iter;
690 combobox = GTK_COMBO_BOX (chooser);
691 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
693 if (account_chooser_find_account (chooser, account, &iter)) {
694 gtk_list_store_remove (store, &iter);
698 static void
699 account_chooser_update_iter (EmpathyAccountChooser *chooser,
700 GtkTreeIter *iter)
702 EmpathyAccountChooserPriv *priv;
703 GtkListStore *store;
704 GtkComboBox *combobox;
705 TpAccount *account;
706 const gchar *icon_name;
707 gboolean is_enabled = TRUE;
709 priv = GET_PRIV (chooser);
711 combobox = GTK_COMBO_BOX (chooser);
712 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
714 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
715 COL_ACCOUNT_POINTER, &account,
716 -1);
718 icon_name = tp_account_get_icon_name (account);
719 if (priv->filter) {
720 is_enabled = priv->filter (account, priv->filter_data);
723 gtk_list_store_set (store, iter,
724 COL_ACCOUNT_IMAGE, icon_name,
725 COL_ACCOUNT_TEXT, tp_account_get_display_name (account),
726 COL_ACCOUNT_ENABLED, is_enabled,
727 -1);
729 /* set first connected account as active account */
730 if (priv->account_manually_set == FALSE &&
731 priv->set_active_item == FALSE && is_enabled) {
732 priv->set_active_item = TRUE;
733 gtk_combo_box_set_active_iter (combobox, iter);
736 g_object_unref (account);
739 static void
740 account_chooser_status_changed_cb (TpAccount *account,
741 guint old_status,
742 guint new_status,
743 guint reason,
744 gchar *dbus_error_name,
745 GHashTable *details,
746 gpointer user_data)
748 EmpathyAccountChooser *chooser = user_data;
749 GtkTreeIter iter;
751 if (account_chooser_find_account (chooser, account, &iter)) {
752 account_chooser_update_iter (chooser, &iter);
756 static gboolean
757 account_chooser_separator_func (GtkTreeModel *model,
758 GtkTreeIter *iter,
759 EmpathyAccountChooser *chooser)
761 EmpathyAccountChooserPriv *priv;
762 gchar *text;
763 gboolean is_separator;
765 priv = GET_PRIV (chooser);
767 if (!priv->has_all_option) {
768 return FALSE;
771 gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
772 is_separator = text == NULL;
773 g_free (text);
775 return is_separator;
778 static gboolean
779 account_chooser_set_account_foreach (GtkTreeModel *model,
780 GtkTreePath *path,
781 GtkTreeIter *iter,
782 SetAccountData *data)
784 TpAccount *account;
785 gboolean equal;
787 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
789 /* Special case so we can make it possible to select the All option */
790 if ((data->account == NULL) != (account == NULL)) {
791 equal = FALSE;
793 else {
794 equal = (data->account == account);
797 if (account) {
798 g_object_unref (account);
801 if (equal) {
802 GtkComboBox *combobox;
804 combobox = GTK_COMBO_BOX (data->chooser);
805 gtk_combo_box_set_active_iter (combobox, iter);
807 data->set = TRUE;
810 return equal;
813 static gboolean
814 account_chooser_filter_foreach (GtkTreeModel *model,
815 GtkTreePath *path,
816 GtkTreeIter *iter,
817 gpointer chooser)
819 account_chooser_update_iter (chooser, iter);
820 return FALSE;
824 * empathy_account_chooser_set_filter:
825 * @chooser: an #EmpathyAccountChooser
826 * @filter: a filter
827 * @user_data: data to pass to @filter, or %NULL
829 * Sets a filter on the @chooser so only accounts that are %TRUE in the eyes
830 * of the filter are visible in the @chooser.
832 void
833 empathy_account_chooser_set_filter (EmpathyAccountChooser *chooser,
834 EmpathyAccountChooserFilterFunc filter,
835 gpointer user_data)
837 EmpathyAccountChooserPriv *priv;
838 GtkTreeModel *model;
840 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
842 priv = GET_PRIV (chooser);
844 priv->filter = filter;
845 priv->filter_data = user_data;
847 /* Refilter existing data */
848 priv->set_active_item = FALSE;
849 model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
850 gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
854 * EmpathyAccountChooserFilterFunc:
855 * @account: a #TpAccount
856 * @user_data: user data, or %NULL
858 * A function which decides whether the account indicated by @account
859 * is visible.
861 * Return value: whether the account indicated by @account is visible.
865 * empathy_account_chooser_filter_is_connected:
866 * @account: a #TpAccount
867 * @user_data: user data or %NULL
869 * A useful #EmpathyAccountChooserFilterFunc that one could pass into
870 * empathy_account_chooser_set_filter() and only show connected accounts.
872 * Return value: Whether @account is connected
874 gboolean
875 empathy_account_chooser_filter_is_connected (TpAccount *account,
876 gpointer user_data)
878 return (tp_account_get_connection_status (account, NULL)
879 == TP_CONNECTION_STATUS_CONNECTED);
882 gboolean
883 empathy_account_chooser_is_ready (EmpathyAccountChooser *self)
885 EmpathyAccountChooserPriv *priv = GET_PRIV (self);
887 return priv->ready;