Create one single GAction to join rooms
[empathy-mirror.git] / libempathy-gtk / empathy-account-chooser.c
blob69fc54fe22f95bcbb9ba68c07f6f9e0d608d26ea
1 /*
2 * Copyright (C) 2005-2007 Imendio AB
3 * Copyright (C) 2007-2011 Collabora Ltd.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
20 * Authors: Martyn Russell <martyn@imendio.com>
21 * Xavier Claessens <xclaesse@gmail.com>
24 #include "config.h"
25 #include "empathy-account-chooser.h"
27 #include <glib/gi18n-lib.h>
28 #include <tp-account-widgets/tpaw-pixbuf-utils.h>
30 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
31 #include "empathy-debug.h"
33 /**
34 * SECTION:empathy-account-chooser
35 * @title:EmpathyAccountChooser
36 * @short_description: A widget used to choose from a list of accounts
37 * @include: libempathy-gtk/empathy-account-chooser.h
39 * #EmpathyAccountChooser is a widget which extends #GtkComboBox to provide
40 * a chooser of available accounts.
43 /**
44 * EmpathyAccountChooser:
45 * @parent: parent object
47 * Widget which extends #GtkComboBox to provide a chooser of available accounts.
50 struct _EmpathyAccountChooserPriv
52 TpAccountManager *manager;
53 gboolean set_active_item;
54 gboolean account_manually_set;
55 gboolean has_all_option;
56 EmpathyAccountChooserFilterFunc filter;
57 gpointer filter_data;
58 gboolean ready;
60 TpAccount *select_when_ready;
63 typedef struct
65 EmpathyAccountChooser *self;
66 TpAccount *account;
67 gboolean set;
68 } SetAccountData;
70 typedef struct
72 EmpathyAccountChooser *self;
73 TpAccount *account;
74 GtkTreeIter *iter;
75 } FilterResultCallbackData;
77 static FilterResultCallbackData *
78 filter_result_callback_data_new (EmpathyAccountChooser *self,
79 TpAccount *account,
80 GtkTreeIter *iter)
82 FilterResultCallbackData *data;
84 g_return_val_if_fail (self != NULL, NULL);
85 g_return_val_if_fail (account != NULL, NULL);
86 g_return_val_if_fail (iter != NULL, NULL);
88 data = g_slice_new0 (FilterResultCallbackData);
89 data->self = g_object_ref (self);
90 data->account = g_object_ref (account);
91 data->iter = gtk_tree_iter_copy (iter);
93 return data;
96 static void
97 filter_result_callback_data_free (FilterResultCallbackData *data)
99 g_object_unref (data->self);
100 g_object_unref (data->account);
101 gtk_tree_iter_free (data->iter);
102 g_slice_free (FilterResultCallbackData, data);
105 /* Distinguishes between store entries which are actually accounts, and special
106 * items like the "All" entry and the separator below it, so they can be sorted
107 * correctly. Higher-numbered entries will sort earlier.
109 typedef enum {
110 ROW_ACCOUNT = 0,
111 ROW_SEPARATOR,
112 ROW_ALL
113 } RowType;
115 enum {
116 COL_ACCOUNT_IMAGE,
117 COL_ACCOUNT_TEXT,
118 COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
119 COL_ACCOUNT_ROW_TYPE,
120 COL_ACCOUNT_POINTER,
121 COL_ACCOUNT_COUNT
124 static void account_chooser_account_validity_changed_cb (
125 TpAccountManager *manager,
126 TpAccount *account,
127 gboolean valid,
128 EmpathyAccountChooser *self);
129 static void account_chooser_account_add_foreach (TpAccount *account,
130 EmpathyAccountChooser *self);
131 static void account_chooser_account_removed_cb (TpAccountManager *manager,
132 TpAccount *account,
133 EmpathyAccountChooser *self);
134 static void account_chooser_account_remove_foreach (TpAccount *account,
135 EmpathyAccountChooser *self);
136 static void account_chooser_update_iter (EmpathyAccountChooser *self,
137 GtkTreeIter *iter);
138 static void account_chooser_status_changed_cb (TpAccount *account,
139 guint old_status,
140 guint new_status,
141 guint reason,
142 gchar *dbus_error_name,
143 GHashTable *details,
144 gpointer user_data);
145 static gboolean account_chooser_separator_func (GtkTreeModel *model,
146 GtkTreeIter *iter,
147 EmpathyAccountChooser *self);
148 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
149 GtkTreePath *path,
150 GtkTreeIter *iter,
151 SetAccountData *data);
152 static void update_account (EmpathyAccountChooser *self,
153 TpAccount *account);
154 static gboolean select_account (EmpathyAccountChooser *self,
155 TpAccount *account);
157 enum {
158 PROP_0,
159 PROP_HAS_ALL_OPTION,
162 enum {
163 READY,
164 LAST_SIGNAL
167 static guint signals[LAST_SIGNAL] = { 0 };
169 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser,
170 GTK_TYPE_COMBO_BOX)
172 static void
173 empathy_account_chooser_init (EmpathyAccountChooser *self)
175 TpSimpleClientFactory *factory;
177 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
178 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
180 self->priv->set_active_item = FALSE;
181 self->priv->account_manually_set = FALSE;
182 self->priv->filter = NULL;
183 self->priv->filter_data = NULL;
185 self->priv->manager = tp_account_manager_dup ();
187 tp_g_signal_connect_object (self->priv->manager, "account-validity-changed",
188 G_CALLBACK (account_chooser_account_validity_changed_cb), self, 0);
190 tp_g_signal_connect_object (self->priv->manager, "account-removed",
191 G_CALLBACK (account_chooser_account_removed_cb), self, 0);
193 /* Make sure we'll have the capabilities feature on TpAccount's connection */
194 factory = tp_proxy_get_factory (self->priv->manager);
196 tp_simple_client_factory_add_account_features_varargs (factory,
197 TP_ACCOUNT_FEATURE_CONNECTION, NULL);
198 tp_simple_client_factory_add_connection_features_varargs (factory,
199 TP_CONNECTION_FEATURE_CAPABILITIES, NULL);
202 static gint
203 account_cmp (GtkTreeModel *model,
204 GtkTreeIter *a,
205 GtkTreeIter *b,
206 gpointer user_data)
208 RowType a_type, b_type;
209 gboolean a_enabled, b_enabled;
210 gchar *a_text, *b_text;
211 gint result;
213 gtk_tree_model_get (model, a,
214 COL_ACCOUNT_ENABLED, &a_enabled,
215 COL_ACCOUNT_ROW_TYPE, &a_type,
216 -1);
217 gtk_tree_model_get (model, b,
218 COL_ACCOUNT_ENABLED, &b_enabled,
219 COL_ACCOUNT_ROW_TYPE, &b_type,
220 -1);
222 /* This assumes that we have at most one of each special row type. */
223 if (a_type != b_type)
224 /* Display higher-numbered special row types first. */
225 return (b_type - a_type);
227 /* Enabled accounts are displayed first */
228 if (a_enabled != b_enabled)
229 return a_enabled ? -1: 1;
231 gtk_tree_model_get (model, a, COL_ACCOUNT_TEXT, &a_text, -1);
232 gtk_tree_model_get (model, b, COL_ACCOUNT_TEXT, &b_text, -1);
234 if (a_text == b_text)
235 result = 0;
236 else if (a_text == NULL)
237 result = 1;
238 else if (b_text == NULL)
239 result = -1;
240 else
241 result = g_ascii_strcasecmp (a_text, b_text);
243 g_free (a_text);
244 g_free (b_text);
246 return result;
249 static void
250 account_connection_notify_cb (TpAccount *account,
251 GParamSpec *spec,
252 EmpathyAccountChooser *self)
254 update_account (self, account);
257 static void
258 account_manager_prepared_cb (GObject *source_object,
259 GAsyncResult *result,
260 gpointer user_data)
262 GList *accounts, *l;
263 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
264 EmpathyAccountChooser *self = user_data;
265 GError *error = NULL;
267 if (!tp_proxy_prepare_finish (manager, result, &error))
269 DEBUG ("Failed to prepare account manager: %s", error->message);
270 g_error_free (error);
271 return;
274 accounts = tp_account_manager_dup_valid_accounts (manager);
276 for (l = accounts; l != NULL; l = l->next)
278 TpAccount *account = l->data;
280 account_chooser_account_add_foreach (account, self);
282 tp_g_signal_connect_object (account, "status-changed",
283 G_CALLBACK (account_chooser_status_changed_cb),
284 self, 0);
286 /* We generally use the TpConnection from the account to filter it so,
287 * just relying on the account status is not enough. In some case we the
288 * status change can be notified while the TpConnection is still
289 * preparing. */
290 tp_g_signal_connect_object (account, "notify::connection",
291 G_CALLBACK (account_connection_notify_cb),
292 self, 0);
295 g_list_free_full (accounts, g_object_unref);
297 if (self->priv->select_when_ready != NULL)
299 select_account (self, self->priv->select_when_ready);
301 g_clear_object (&self->priv->select_when_ready);
304 self->priv->ready = TRUE;
305 g_signal_emit (self, signals[READY], 0);
308 static void
309 account_chooser_constructed (GObject *object)
311 EmpathyAccountChooser *self = (EmpathyAccountChooser *) object;
312 GtkListStore *store;
313 GtkCellRenderer *renderer;
314 GtkComboBox *combobox;
316 /* Set up combo box with new store */
317 combobox = GTK_COMBO_BOX (self);
319 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
321 store = gtk_list_store_new (COL_ACCOUNT_COUNT,
322 GDK_TYPE_PIXBUF, /* Image */
323 G_TYPE_STRING, /* Name */
324 G_TYPE_BOOLEAN, /* Enabled */
325 G_TYPE_UINT, /* Row type */
326 TP_TYPE_ACCOUNT);
328 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
329 account_cmp, self, NULL);
330 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
331 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
333 gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
335 renderer = gtk_cell_renderer_pixbuf_new ();
336 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
337 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
338 "pixbuf", COL_ACCOUNT_IMAGE,
339 "sensitive", COL_ACCOUNT_ENABLED,
340 NULL);
342 renderer = gtk_cell_renderer_text_new ();
343 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
344 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
345 "text", COL_ACCOUNT_TEXT,
346 "sensitive", COL_ACCOUNT_ENABLED,
347 NULL);
349 /* Populate accounts */
350 tp_proxy_prepare_async (self->priv->manager, NULL,
351 account_manager_prepared_cb, self);
353 g_object_unref (store);
357 static void
358 account_chooser_dispose (GObject *object)
360 EmpathyAccountChooser *self = EMPATHY_ACCOUNT_CHOOSER (object);
362 g_clear_object (&self->priv->manager);
363 g_clear_object (&self->priv->select_when_ready);
365 G_OBJECT_CLASS (empathy_account_chooser_parent_class)->dispose (object);
368 static void
369 account_chooser_get_property (GObject *object,
370 guint param_id,
371 GValue *value,
372 GParamSpec *pspec)
374 EmpathyAccountChooser *self = (EmpathyAccountChooser *) object;
376 switch (param_id)
378 case PROP_HAS_ALL_OPTION:
379 g_value_set_boolean (value, self->priv->has_all_option);
380 break;
381 default:
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
383 break;
387 static void
388 account_chooser_set_property (GObject *object,
389 guint param_id,
390 const GValue *value,
391 GParamSpec *pspec)
393 switch (param_id)
395 case PROP_HAS_ALL_OPTION:
396 empathy_account_chooser_set_has_all_option (
397 EMPATHY_ACCOUNT_CHOOSER (object), g_value_get_boolean (value));
398 break;
399 default:
400 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
401 break;
405 static void
406 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
408 GObjectClass *object_class = G_OBJECT_CLASS (klass);
410 object_class->constructed = account_chooser_constructed;
411 object_class->dispose = account_chooser_dispose;
412 object_class->get_property = account_chooser_get_property;
413 object_class->set_property = account_chooser_set_property;
416 * EmpathyAccountChooser:has-all-option:
418 * Have an additional option in the list to mean all accounts.
420 g_object_class_install_property (object_class,
421 PROP_HAS_ALL_OPTION,
422 g_param_spec_boolean ("has-all-option",
423 "Has All Option",
424 "Have a separate option in the list to mean ALL accounts",
425 FALSE,
426 G_PARAM_READWRITE));
428 signals[READY] =
429 g_signal_new ("ready",
430 G_OBJECT_CLASS_TYPE (object_class),
431 G_SIGNAL_RUN_LAST,
433 NULL, NULL,
434 g_cclosure_marshal_generic,
435 G_TYPE_NONE,
438 g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
442 * empathy_account_chooser_new:
444 * Creates a new #EmpathyAccountChooser.
446 * Return value: A new #EmpathyAccountChooser
448 GtkWidget *
449 empathy_account_chooser_new (void)
451 GtkWidget *self;
453 self = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
455 return self;
458 gboolean
459 empathy_account_chooser_has_all_selected (EmpathyAccountChooser *self)
461 GtkTreeModel *model;
462 GtkTreeIter iter;
463 RowType type;
465 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
467 g_return_val_if_fail (self->priv->has_all_option == TRUE, FALSE);
469 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
470 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
471 return FALSE;
473 gtk_tree_model_get (model, &iter, COL_ACCOUNT_ROW_TYPE, &type, -1);
475 return type == ROW_ALL;
479 * empathy_account_chooser_dup_account:
480 * @self: an #EmpathyAccountChooser
482 * Returns the account which is currently selected in the chooser or %NULL
483 * if there is no account selected. The #TpAccount returned should be
484 * unrefed with g_object_unref() when finished with.
486 * Return value: a new ref to the #TpAccount currently selected, or %NULL.
488 TpAccount *
489 empathy_account_chooser_dup_account (EmpathyAccountChooser *self)
491 TpAccount *account;
492 GtkTreeModel *model;
493 GtkTreeIter iter;
495 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), NULL);
497 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
498 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
499 return NULL;
501 gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
503 return account;
507 * empathy_account_chooser_get_connection:
508 * @self: an #EmpathyAccountChooser
510 * Returns a borrowed reference to the #TpConnection associated with the
511 * account currently selected. The caller must reference the returned object
512 * with g_object_ref() if it will be kept
514 * Return value: a borrowed reference to the #TpConnection associated with the
515 * account curently selected.
517 TpConnection *
518 empathy_account_chooser_get_connection (EmpathyAccountChooser *self)
520 TpAccount *account;
521 TpConnection *connection;
523 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), NULL);
525 account = empathy_account_chooser_dup_account (self);
527 /* if the returned account is NULL, then the account manager probably
528 * hasn't been prepared yet. It should be safe to return NULL here
529 * though. */
530 if (account == NULL)
531 return NULL;
533 connection = tp_account_get_connection (account);
534 g_object_unref (account);
536 return connection;
539 static gboolean
540 select_account (EmpathyAccountChooser *self,
541 TpAccount *account)
543 GtkComboBox *combobox;
544 GtkTreeModel *model;
545 GtkTreeIter iter;
546 SetAccountData data;
548 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
550 combobox = GTK_COMBO_BOX (self);
551 model = gtk_combo_box_get_model (combobox);
552 gtk_combo_box_get_active_iter (combobox, &iter);
554 data.self = self;
555 data.account = account;
556 data.set = FALSE;
558 gtk_tree_model_foreach (model,
559 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
560 &data);
562 self->priv->account_manually_set = data.set;
564 return data.set;
568 * empathy_account_chooser_set_account:
569 * @self: an #EmpathyAccountChooser
570 * @account: a #TpAccount
572 * Sets the currently selected account to @account, if it exists in the list.
574 * Return value: whether the chooser was set to @account.
576 gboolean
577 empathy_account_chooser_set_account (EmpathyAccountChooser *self,
578 TpAccount *account)
580 if (self->priv->ready)
581 return select_account (self, account);
583 /* Account chooser is not ready yet, we'll try selecting the account once it
584 * is */
585 g_clear_object (&self->priv->select_when_ready);
587 if (account != NULL)
588 self->priv->select_when_ready = g_object_ref (account);
590 return FALSE;
593 void
594 empathy_account_chooser_set_all (EmpathyAccountChooser *self)
596 GtkComboBox *combobox;
597 GtkTreeModel *model;
598 GtkTreeIter iter;
600 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
602 g_return_if_fail (self->priv->has_all_option);
604 combobox = GTK_COMBO_BOX (self);
605 model = gtk_combo_box_get_model (combobox);
607 if (gtk_tree_model_get_iter_first (model, &iter))
609 /* 'All accounts' is the first row */
610 gtk_combo_box_set_active_iter (combobox, &iter);
611 self->priv->account_manually_set = TRUE;
616 * empathy_account_chooser_get_has_all_option:
617 * @self: an #EmpathyAccountChooser
619 * Returns whether @self has the #EmpathyAccountChooser:has-all-option
620 * property set to true.
622 * Return value: whether @self has the #EmpathyAccountChooser:has-all-option
623 * property enabled.
625 gboolean
626 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *self)
628 g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
630 return self->priv->has_all_option;
634 * empathy_account_chooser_set_has_all_option:
635 * @self: an #EmpathyAccountChooser
636 * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option
637 * property
639 * Sets the #EmpathyAccountChooser:has-all-option property.
641 void
642 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *self,
643 gboolean has_all_option)
645 GtkComboBox *combobox;
646 GtkListStore *store;
647 GtkTreeModel *model;
648 GtkTreeIter iter;
650 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
652 if (self->priv->has_all_option == has_all_option)
653 return;
655 combobox = GTK_COMBO_BOX (self);
656 model = gtk_combo_box_get_model (combobox);
657 store = GTK_LIST_STORE (model);
659 self->priv->has_all_option = has_all_option;
662 * The first 2 options are the ALL and separator
665 if (has_all_option)
667 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
668 (GtkTreeViewRowSeparatorFunc)
669 account_chooser_separator_func,
670 self,
671 NULL);
673 gtk_list_store_prepend (store, &iter);
674 gtk_list_store_set (store, &iter,
675 COL_ACCOUNT_TEXT, NULL,
676 COL_ACCOUNT_ENABLED, TRUE,
677 COL_ACCOUNT_POINTER, NULL,
678 COL_ACCOUNT_ROW_TYPE, ROW_SEPARATOR,
679 -1);
681 gtk_list_store_prepend (store, &iter);
682 gtk_list_store_set (store, &iter,
683 COL_ACCOUNT_TEXT, _("All accounts"),
684 COL_ACCOUNT_ENABLED, TRUE,
685 COL_ACCOUNT_POINTER, NULL,
686 COL_ACCOUNT_ROW_TYPE, ROW_ALL,
687 -1);
689 else
691 if (gtk_tree_model_get_iter_first (model, &iter))
693 if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter))
694 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
697 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
698 (GtkTreeViewRowSeparatorFunc)
699 NULL,
700 NULL,
701 NULL);
704 g_object_notify (G_OBJECT (self), "has-all-option");
707 static void
708 account_chooser_account_validity_changed_cb (TpAccountManager *manager,
709 TpAccount *account,
710 gboolean valid,
711 EmpathyAccountChooser *self)
713 if (valid)
714 account_chooser_account_add_foreach (account, self);
715 else
716 account_chooser_account_remove_foreach (account, self);
719 static void
720 account_chooser_account_add_foreach (TpAccount *account,
721 EmpathyAccountChooser *self)
723 GtkListStore *store;
724 GtkComboBox *combobox;
725 GtkTreeIter iter;
726 gint position;
728 combobox = GTK_COMBO_BOX (self);
729 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
731 position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
732 gtk_list_store_insert_with_values (store, &iter, position,
733 COL_ACCOUNT_POINTER, account,
734 -1);
736 account_chooser_update_iter (self, &iter);
739 static void
740 account_chooser_account_removed_cb (TpAccountManager *manager,
741 TpAccount *account,
742 EmpathyAccountChooser *self)
744 account_chooser_account_remove_foreach (account, self);
747 typedef struct
749 TpAccount *account;
750 GtkTreeIter *iter;
751 gboolean found;
752 } FindAccountData;
754 static gboolean
755 account_chooser_find_account_foreach (GtkTreeModel *model,
756 GtkTreePath *path,
757 GtkTreeIter *iter,
758 gpointer user_data)
760 FindAccountData *data = user_data;
761 TpAccount *account;
762 RowType type;
764 gtk_tree_model_get (model, iter,
765 COL_ACCOUNT_POINTER, &account,
766 COL_ACCOUNT_ROW_TYPE, &type,
767 -1);
769 if (type != ROW_ACCOUNT)
770 return FALSE;
772 if (account == data->account)
774 data->found = TRUE;
775 *(data->iter) = *iter;
776 g_object_unref (account);
778 return TRUE;
781 g_object_unref (account);
783 return FALSE;
786 static gboolean
787 account_chooser_find_account (EmpathyAccountChooser *self,
788 TpAccount *account,
789 GtkTreeIter *iter)
791 GtkListStore *store;
792 GtkComboBox *combobox;
793 FindAccountData data;
795 combobox = GTK_COMBO_BOX (self);
796 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
798 data.account = account;
799 data.iter = iter;
800 gtk_tree_model_foreach (GTK_TREE_MODEL (store),
801 account_chooser_find_account_foreach,
802 &data);
804 return data.found;
807 static void
808 account_chooser_account_remove_foreach (TpAccount *account,
809 EmpathyAccountChooser *self)
811 GtkListStore *store;
812 GtkComboBox *combobox;
813 GtkTreeIter iter;
815 combobox = GTK_COMBO_BOX (self);
816 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
818 if (account_chooser_find_account (self, account, &iter))
819 gtk_list_store_remove (store, &iter);
822 static void
823 account_chooser_filter_ready_cb (gboolean is_enabled,
824 gpointer data)
826 FilterResultCallbackData *fr_data = data;
827 EmpathyAccountChooser *self;
828 TpAccount *account;
829 GtkTreeIter *iter;
830 GtkListStore *store;
831 GtkComboBox *combobox;
832 const gchar *icon_name;
833 GdkPixbuf *pixbuf;
835 self = fr_data->self;
836 account = fr_data->account;
837 iter = fr_data->iter;
838 combobox = GTK_COMBO_BOX (self);
839 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
841 icon_name = tp_account_get_icon_name (account);
842 pixbuf = tpaw_pixbuf_from_icon_name (icon_name,
843 GTK_ICON_SIZE_BUTTON);
845 gtk_list_store_set (store, iter,
846 COL_ACCOUNT_IMAGE, pixbuf,
847 COL_ACCOUNT_TEXT, tp_account_get_display_name (account),
848 COL_ACCOUNT_ENABLED, is_enabled,
849 -1);
851 if (pixbuf != NULL)
852 g_object_unref (pixbuf);
854 /* set first connected account as active account */
855 if (self->priv->account_manually_set == FALSE &&
856 self->priv->set_active_item == FALSE && is_enabled)
858 self->priv->set_active_item = TRUE;
859 gtk_combo_box_set_active_iter (combobox, iter);
862 filter_result_callback_data_free (fr_data);
865 static void
866 account_chooser_update_iter (EmpathyAccountChooser *self,
867 GtkTreeIter *iter)
869 GtkListStore *store;
870 GtkComboBox *combobox;
871 TpAccount *account;
872 FilterResultCallbackData *data;
874 combobox = GTK_COMBO_BOX (self);
875 store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
877 gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
878 COL_ACCOUNT_POINTER, &account,
879 -1);
881 /* Skip rows without account associated */
882 if (account == NULL)
883 return;
885 data = filter_result_callback_data_new (self, account, iter);
887 if (self->priv->filter)
888 self->priv->filter (account, account_chooser_filter_ready_cb,
889 (gpointer) data, self->priv->filter_data);
890 else
891 account_chooser_filter_ready_cb (TRUE, (gpointer) data);
893 g_object_unref (account);
896 static void
897 update_account (EmpathyAccountChooser *self,
898 TpAccount *account)
900 GtkTreeIter iter;
902 if (account_chooser_find_account (self, account, &iter))
903 account_chooser_update_iter (self, &iter);
906 static void
907 account_chooser_status_changed_cb (TpAccount *account,
908 guint old_status,
909 guint new_status,
910 guint reason,
911 gchar *dbus_error_name,
912 GHashTable *details,
913 gpointer user_data)
915 EmpathyAccountChooser *self = user_data;
917 update_account (self, account);
920 static gboolean
921 account_chooser_separator_func (GtkTreeModel *model,
922 GtkTreeIter *iter,
923 EmpathyAccountChooser *self)
925 RowType row_type;
927 gtk_tree_model_get (model, iter, COL_ACCOUNT_ROW_TYPE, &row_type, -1);
928 return (row_type == ROW_SEPARATOR);
931 static gboolean
932 account_chooser_set_account_foreach (GtkTreeModel *model,
933 GtkTreePath *path,
934 GtkTreeIter *iter,
935 SetAccountData *data)
937 TpAccount *account;
938 gboolean equal;
940 gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
942 equal = (data->account == account);
944 if (account)
945 g_object_unref (account);
947 if (equal)
949 GtkComboBox *combobox;
951 combobox = GTK_COMBO_BOX (data->self);
952 gtk_combo_box_set_active_iter (combobox, iter);
954 data->set = TRUE;
957 return equal;
960 static gboolean
961 account_chooser_filter_foreach (GtkTreeModel *model,
962 GtkTreePath *path,
963 GtkTreeIter *iter,
964 gpointer self)
966 account_chooser_update_iter (self, iter);
967 return FALSE;
970 void
971 empathy_account_chooser_refilter (EmpathyAccountChooser *self)
973 GtkTreeModel *model;
975 self->priv->set_active_item = FALSE;
976 model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
977 gtk_tree_model_foreach (model, account_chooser_filter_foreach, self);
981 * empathy_account_chooser_set_filter:
982 * @self: an #EmpathyAccountChooser
983 * @filter: a filter
984 * @user_data: data to pass to @filter, or %NULL
986 * Sets a filter on the @self so only accounts that are %TRUE in the eyes
987 * of the filter are visible in the @self.
989 void
990 empathy_account_chooser_set_filter (EmpathyAccountChooser *self,
991 EmpathyAccountChooserFilterFunc filter,
992 gpointer user_data)
994 g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
996 self->priv->filter = filter;
997 self->priv->filter_data = user_data;
999 /* Refilter existing data */
1000 empathy_account_chooser_refilter (self);
1004 * EmpathyAccountChooserFilterFunc:
1005 * @account: a #TpAccount
1006 * @user_data: user data, or %NULL
1008 * A function which decides whether the account indicated by @account
1009 * is visible.
1011 * Return value: whether the account indicated by @account is visible.
1014 gboolean
1015 empathy_account_chooser_is_ready (EmpathyAccountChooser *self)
1017 return self->priv->ready;
1020 TpAccount *
1021 empathy_account_chooser_get_account (EmpathyAccountChooser *self)
1023 TpAccount *account;
1025 account = empathy_account_chooser_dup_account (self);
1026 if (account == NULL)
1027 return NULL;
1029 g_object_unref (account);
1031 return account;
1034 TpAccountManager *
1035 empathy_account_chooser_get_account_manager (EmpathyAccountChooser *self)
1037 return self->priv->manager;
1040 /* Pre-defined filters */
1043 * empathy_account_chooser_filter_is_connected:
1044 * @account: a #TpAccount
1045 * @callback: an #EmpathyAccountChooserFilterResultCallback accepting the result
1046 * @callback_data: data passed to the @callback
1047 * @user_data: user data or %NULL
1049 * A useful #EmpathyAccountChooserFilterFunc that one could pass into
1050 * empathy_account_chooser_set_filter() and only show connected accounts.
1052 * Returns (via the callback) TRUE is @account is connected
1054 void
1055 empathy_account_chooser_filter_is_connected (TpAccount *account,
1056 EmpathyAccountChooserFilterResultCallback callback,
1057 gpointer callback_data,
1058 gpointer user_data)
1060 gboolean is_connected =
1061 tp_account_get_connection_status (account, NULL)
1062 == TP_CONNECTION_STATUS_CONNECTED;
1064 callback (is_connected, callback_data);
1068 * empathy_account_chooser_filter_supports_multichat:
1069 * @account: a #TpAccount
1070 * @callback: an #EmpathyAccountChooserFilterResultCallback accepting the result
1071 * @callback_data: data passed to the @callback
1072 * @user_data: user data or %NULL
1074 * An #EmpathyAccountChooserFilterFunc that returns accounts that both
1075 * support multiuser text chat and are connected.
1077 * Returns (via the callback) TRUE if @account both supports muc and
1078 * is connected
1080 void
1081 empathy_account_chooser_filter_supports_chatrooms (TpAccount *account,
1082 EmpathyAccountChooserFilterResultCallback callback,
1083 gpointer callback_data,
1084 gpointer user_data)
1086 TpConnection *connection;
1087 gboolean supported = FALSE;
1088 TpCapabilities *caps;
1090 /* check if CM supports multiuser text chat */
1091 connection = tp_account_get_connection (account);
1092 if (connection == NULL)
1093 goto out;
1095 caps = tp_connection_get_capabilities (connection);
1096 if (caps == NULL)
1097 goto out;
1099 supported = tp_capabilities_supports_text_chatrooms (caps);
1101 out:
1102 callback (supported, callback_data);