]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-chooser.c
1bf8f3703af1b6377df41a0cfd77437ae944c17a
[empathy.git] / libempathy-gtk / empathy-account-chooser.c
1 /*
2  * Copyright (C) 2005-2007 Imendio AB
3  * Copyright (C) 2007-2011 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
19  *
20  * Authors: Martyn Russell <martyn@imendio.com>
21  *          Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27
28 #include <glib/gi18n-lib.h>
29 #include <gtk/gtk.h>
30
31 #include <telepathy-glib/account-manager.h>
32 #include <telepathy-glib/util.h>
33
34 #include <libempathy/empathy-utils.h>
35
36 #include "empathy-ui-utils.h"
37 #include "empathy-account-chooser.h"
38
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include <libempathy/empathy-debug.h>
41
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
47  *
48  * #EmpathyAccountChooser is a widget which extends #GtkComboBox to provide
49  * a chooser of available accounts.
50  */
51
52 /**
53  * EmpathyAccountChooser:
54  * @parent: parent object
55  *
56  * Widget which extends #GtkComboBox to provide a chooser of available accounts.
57  */
58
59 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountChooser)
60
61 typedef struct
62 {
63   TpAccountManager *manager;
64   gboolean set_active_item;
65   gboolean account_manually_set;
66   gboolean has_all_option;
67   EmpathyAccountChooserFilterFunc filter;
68   gpointer filter_data;
69   gboolean ready;
70 } EmpathyAccountChooserPriv;
71
72 typedef struct
73 {
74   EmpathyAccountChooser *self;
75   TpAccount *account;
76   gboolean set;
77 } SetAccountData;
78
79 typedef struct
80 {
81   EmpathyAccountChooser *self;
82   TpAccount *account;
83   GtkTreeIter *iter;
84 } FilterResultCallbackData;
85
86 static FilterResultCallbackData *
87 filter_result_callback_data_new (EmpathyAccountChooser *self,
88     TpAccount *account,
89     GtkTreeIter *iter)
90 {
91   FilterResultCallbackData *data;
92
93   data = g_slice_new0 (FilterResultCallbackData);
94   data->self = g_object_ref (self);
95   data->account = g_object_ref (account);
96   data->iter = gtk_tree_iter_copy (iter);
97
98   return data;
99 }
100
101 static void
102 filter_result_callback_data_free (FilterResultCallbackData *data)
103 {
104   g_object_unref (data->self);
105   g_object_unref (data->account);
106   gtk_tree_iter_free (data->iter);
107   g_slice_free (FilterResultCallbackData, data);
108 }
109
110 /* Distinguishes between store entries which are actually accounts, and special
111  * items like the "All" entry and the separator below it, so they can be sorted
112  * correctly. Higher-numbered entries will sort earlier.
113  */
114 typedef enum {
115   ROW_ACCOUNT = 0,
116   ROW_SEPARATOR,
117   ROW_ALL
118 } RowType;
119
120 enum {
121   COL_ACCOUNT_IMAGE,
122   COL_ACCOUNT_TEXT,
123   COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
124   COL_ACCOUNT_ROW_TYPE,
125   COL_ACCOUNT_POINTER,
126   COL_ACCOUNT_COUNT
127 };
128
129 static void account_chooser_setup (EmpathyAccountChooser *self);
130 static void account_chooser_account_validity_changed_cb (
131     TpAccountManager *manager,
132     TpAccount *account,
133     gboolean valid,
134     EmpathyAccountChooser *self);
135 static void account_chooser_account_add_foreach (TpAccount *account,
136     EmpathyAccountChooser *self);
137 static void account_chooser_account_removed_cb (TpAccountManager *manager,
138     TpAccount *account,
139     EmpathyAccountChooser *self);
140 static void account_chooser_account_remove_foreach (TpAccount *account,
141     EmpathyAccountChooser*self);
142 static void account_chooser_update_iter (EmpathyAccountChooser *self,
143     GtkTreeIter *iter);
144 static void account_chooser_status_changed_cb (TpAccount *account,
145     guint old_status,
146     guint new_status,
147     guint reason,
148     gchar *dbus_error_name,
149     GHashTable *details,
150     gpointer user_data);
151 static gboolean account_chooser_separator_func (GtkTreeModel *model,
152     GtkTreeIter *iter,
153     EmpathyAccountChooser *self);
154 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
155     GtkTreePath *path,
156     GtkTreeIter *iter,
157     SetAccountData *data);
158
159 enum {
160   PROP_0,
161   PROP_HAS_ALL_OPTION,
162 };
163
164 enum {
165   READY,
166   LAST_SIGNAL
167 };
168
169 static guint signals[LAST_SIGNAL] = { 0 };
170
171 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser,
172     GTK_TYPE_COMBO_BOX)
173
174 static void
175 empathy_account_chooser_init (EmpathyAccountChooser *self)
176 {
177   EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
178     EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
179
180   self->priv = priv;
181   priv->set_active_item = FALSE;
182   priv->account_manually_set = FALSE;
183   priv->filter = NULL;
184   priv->filter_data = NULL;
185
186   priv->manager = tp_account_manager_dup ();
187
188   g_signal_connect (priv->manager, "account-validity-changed",
189       G_CALLBACK (account_chooser_account_validity_changed_cb),
190       self);
191   g_signal_connect (priv->manager, "account-removed",
192       G_CALLBACK (account_chooser_account_removed_cb),
193       self);
194 }
195
196 static void
197 account_chooser_constructed (GObject *object)
198 {
199   EmpathyAccountChooser *self = (EmpathyAccountChooser *) object;
200
201   account_chooser_setup (self);
202 }
203
204 static void
205 account_chooser_finalize (GObject *object)
206 {
207   EmpathyAccountChooserPriv *priv = GET_PRIV (object);
208
209   g_signal_handlers_disconnect_by_func (priv->manager,
210       account_chooser_account_validity_changed_cb,
211       object);
212   g_signal_handlers_disconnect_by_func (priv->manager,
213       account_chooser_account_removed_cb,
214       object);
215   g_object_unref (priv->manager);
216
217   G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
218 }
219
220 static void
221 account_chooser_get_property (GObject *object,
222     guint param_id,
223     GValue *value,
224     GParamSpec *pspec)
225 {
226   EmpathyAccountChooserPriv *priv;
227
228   priv = GET_PRIV (object);
229
230   switch (param_id)
231     {
232       case PROP_HAS_ALL_OPTION:
233         g_value_set_boolean (value, priv->has_all_option);
234         break;
235       default:
236         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
237         break;
238     };
239 }
240
241 static void
242 account_chooser_set_property (GObject *object,
243     guint param_id,
244     const GValue *value,
245     GParamSpec *pspec)
246 {
247   switch (param_id)
248     {
249       case PROP_HAS_ALL_OPTION:
250         empathy_account_chooser_set_has_all_option (
251             EMPATHY_ACCOUNT_CHOOSER (object), g_value_get_boolean (value));
252         break;
253       default:
254         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
255         break;
256     };
257 }
258
259 static void
260 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
261 {
262   GObjectClass *object_class = G_OBJECT_CLASS (klass);
263
264   object_class->constructed = account_chooser_constructed;
265   object_class->finalize = account_chooser_finalize;
266   object_class->get_property = account_chooser_get_property;
267   object_class->set_property = account_chooser_set_property;
268
269   /**
270    * EmpathyAccountChooser:has-all-option:
271    *
272    * Have an additional option in the list to mean all accounts.
273    */
274   g_object_class_install_property (object_class,
275       PROP_HAS_ALL_OPTION,
276       g_param_spec_boolean ("has-all-option",
277         "Has All Option",
278         "Have a separate option in the list to mean ALL accounts",
279         FALSE,
280         G_PARAM_READWRITE));
281
282   signals[READY] =
283     g_signal_new ("ready",
284         G_OBJECT_CLASS_TYPE (object_class),
285         G_SIGNAL_RUN_LAST,
286         0,
287         NULL, NULL,
288         g_cclosure_marshal_generic,
289         G_TYPE_NONE,
290         0);
291
292   g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
293 }
294
295 /**
296  * empathy_account_chooser_new:
297  *
298  * Creates a new #EmpathyAccountChooser.
299  *
300  * Return value: A new #EmpathyAccountChooser
301  */
302 GtkWidget *
303 empathy_account_chooser_new (void)
304 {
305   GtkWidget *self;
306
307   self = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
308
309   return self;
310 }
311
312 gboolean
313 empathy_account_chooser_has_all_selected (EmpathyAccountChooser *self)
314 {
315   EmpathyAccountChooserPriv *priv;
316   GtkTreeModel *model;
317   GtkTreeIter iter;
318   RowType type;
319
320   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
321
322   priv = GET_PRIV (self);
323
324   g_return_val_if_fail (priv->has_all_option == TRUE, FALSE);
325
326   model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
327   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
328     return FALSE;
329
330   gtk_tree_model_get (model, &iter, COL_ACCOUNT_ROW_TYPE, &type, -1);
331
332   return type == ROW_ALL;
333 }
334
335 /**
336  * empathy_account_chooser_dup_account:
337  * @self: an #EmpathyAccountChooser
338  *
339  * Returns the account which is currently selected in the chooser or %NULL
340  * if there is no account selected. The #TpAccount returned should be
341  * unrefed with g_object_unref() when finished with.
342  *
343  * Return value: a new ref to the #TpAccount currently selected, or %NULL.
344  */
345 TpAccount *
346 empathy_account_chooser_dup_account (EmpathyAccountChooser *self)
347 {
348   TpAccount *account;
349   GtkTreeModel *model;
350   GtkTreeIter iter;
351
352   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), NULL);
353
354   model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
355   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
356     return NULL;
357
358   gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
359
360   return account;
361 }
362
363 /**
364  * empathy_account_chooser_get_connection:
365  * @self: an #EmpathyAccountChooser
366  *
367  * Returns a borrowed reference to the #TpConnection associated with the
368  * account currently selected. The caller must reference the returned object
369  * with g_object_ref() if it will be kept
370  *
371  * Return value: a borrowed reference to the #TpConnection associated with the
372  * account curently selected.
373  */
374 TpConnection *
375 empathy_account_chooser_get_connection (EmpathyAccountChooser *self)
376 {
377   TpAccount *account;
378   TpConnection *connection;
379
380   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), NULL);
381
382   account = empathy_account_chooser_dup_account (self);
383
384   /* if the returned account is NULL, then the account manager probably
385    * hasn't been prepared yet. It should be safe to return NULL here
386    * though. */
387   if (account == NULL)
388     return NULL;
389
390   connection = tp_account_get_connection (account);
391   g_object_unref (account);
392
393   return connection;
394 }
395
396 /**
397  * empathy_account_chooser_set_account:
398  * @self: an #EmpathyAccountChooser
399  * @account: a #TpAccount
400  *
401  * Sets the currently selected account to @account, if it exists in the list.
402  *
403  * Return value: whether the chooser was set to @account.
404  */
405 gboolean
406 empathy_account_chooser_set_account (EmpathyAccountChooser *self,
407     TpAccount *account)
408 {
409   EmpathyAccountChooserPriv *priv;
410   GtkComboBox *combobox;
411   GtkTreeModel *model;
412   GtkTreeIter iter;
413   SetAccountData data;
414
415   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
416
417   priv = GET_PRIV (self);
418
419   combobox = GTK_COMBO_BOX (self);
420   model = gtk_combo_box_get_model (combobox);
421   gtk_combo_box_get_active_iter (combobox, &iter);
422
423   data.self = self;
424   data.account = account;
425   data.set = FALSE;
426
427   gtk_tree_model_foreach (model,
428       (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
429       &data);
430
431   priv->account_manually_set = data.set;
432
433   return data.set;
434 }
435
436 void
437 empathy_account_chooser_set_all (EmpathyAccountChooser *self)
438 {
439   EmpathyAccountChooserPriv *priv;
440   GtkComboBox *combobox;
441   GtkTreeModel *model;
442   GtkTreeIter iter;
443
444   g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
445
446   priv = GET_PRIV (self);
447
448   g_return_if_fail (priv->has_all_option);
449
450   combobox = GTK_COMBO_BOX (self);
451   model = gtk_combo_box_get_model (combobox);
452
453   if (gtk_tree_model_get_iter_first (model, &iter))
454     {
455       /* 'All accounts' is the first row */
456       gtk_combo_box_set_active_iter (combobox, &iter);
457       priv->account_manually_set = TRUE;
458     }
459 }
460
461 /**
462  * empathy_account_chooser_get_has_all_option:
463  * @self: an #EmpathyAccountChooser
464  *
465  * Returns whether @self has the #EmpathyAccountChooser:has-all-option
466  * property set to true.
467  *
468  * Return value: whether @self has the #EmpathyAccountChooser:has-all-option
469  * property enabled.
470  */
471 gboolean
472 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *self)
473 {
474   EmpathyAccountChooserPriv *priv;
475
476   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
477
478   priv = GET_PRIV (self);
479
480   return priv->has_all_option;
481 }
482
483 /**
484  * empathy_account_chooser_set_has_all_option:
485  * @self: an #EmpathyAccountChooser
486  * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option
487  * property
488  *
489  * Sets the #EmpathyAccountChooser:has-all-option property.
490  */
491 void
492 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *self,
493     gboolean has_all_option)
494 {
495   EmpathyAccountChooserPriv *priv;
496   GtkComboBox *combobox;
497   GtkListStore *store;
498   GtkTreeModel *model;
499   GtkTreeIter iter;
500
501   g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
502
503   priv = GET_PRIV (self);
504
505   if (priv->has_all_option == has_all_option)
506     return;
507
508   combobox = GTK_COMBO_BOX (self);
509   model = gtk_combo_box_get_model (combobox);
510   store = GTK_LIST_STORE (model);
511
512   priv->has_all_option = has_all_option;
513
514   /*
515    * The first 2 options are the ALL and separator
516    */
517
518   if (has_all_option)
519     {
520       gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
521           (GtkTreeViewRowSeparatorFunc)
522           account_chooser_separator_func,
523           self,
524           NULL);
525
526       gtk_list_store_prepend (store, &iter);
527       gtk_list_store_set (store, &iter,
528           COL_ACCOUNT_TEXT, NULL,
529           COL_ACCOUNT_ENABLED, TRUE,
530           COL_ACCOUNT_POINTER, NULL,
531           COL_ACCOUNT_ROW_TYPE, ROW_SEPARATOR,
532           -1);
533
534       gtk_list_store_prepend (store, &iter);
535       gtk_list_store_set (store, &iter,
536           COL_ACCOUNT_TEXT, _("All accounts"),
537           COL_ACCOUNT_ENABLED, TRUE,
538           COL_ACCOUNT_POINTER, NULL,
539           COL_ACCOUNT_ROW_TYPE, ROW_ALL,
540           -1);
541     }
542   else
543     {
544       if (gtk_tree_model_get_iter_first (model, &iter))
545         {
546           if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter))
547             gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
548         }
549
550     gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
551         (GtkTreeViewRowSeparatorFunc)
552         NULL,
553         NULL,
554         NULL);
555   }
556
557   g_object_notify (G_OBJECT (self), "has-all-option");
558 }
559
560 static void
561 account_manager_prepared_cb (GObject *source_object,
562     GAsyncResult *result,
563     gpointer user_data)
564 {
565   GList *accounts, *l;
566   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
567   EmpathyAccountChooser *self = user_data;
568   EmpathyAccountChooserPriv *priv = GET_PRIV (self);
569   GError *error = NULL;
570
571   if (!tp_proxy_prepare_finish (manager, result, &error))
572     {
573       DEBUG ("Failed to prepare account manager: %s", error->message);
574       g_error_free (error);
575       return;
576     }
577
578   accounts = tp_account_manager_get_valid_accounts (manager);
579
580   for (l = accounts; l != NULL; l = l->next)
581     {
582       TpAccount *account = l->data;
583
584       account_chooser_account_add_foreach (account, self);
585
586       tp_g_signal_connect_object (account, "status-changed",
587           G_CALLBACK (account_chooser_status_changed_cb),
588           self, 0);
589     }
590
591   g_list_free (accounts);
592
593   priv->ready = TRUE;
594   g_signal_emit (self, signals[READY], 0);
595 }
596
597 static gint
598 account_cmp (GtkTreeModel *model,
599     GtkTreeIter *a,
600     GtkTreeIter *b,
601     gpointer user_data)
602 {
603   RowType a_type, b_type;
604   gboolean a_enabled, b_enabled;
605   gchar *a_text, *b_text;
606   gint result;
607
608   gtk_tree_model_get (model, a,
609       COL_ACCOUNT_ENABLED, &a_enabled,
610       COL_ACCOUNT_ROW_TYPE, &a_type,
611       -1);
612   gtk_tree_model_get (model, b,
613       COL_ACCOUNT_ENABLED, &b_enabled,
614       COL_ACCOUNT_ROW_TYPE, &b_type,
615       -1);
616
617   /* This assumes that we have at most one of each special row type. */
618   if (a_type != b_type)
619     /* Display higher-numbered special row types first. */
620     return (b_type - a_type);
621
622   /* Enabled accounts are displayed first */
623   if (a_enabled != b_enabled)
624     return a_enabled ? -1: 1;
625
626   gtk_tree_model_get (model, a, COL_ACCOUNT_TEXT, &a_text, -1);
627   gtk_tree_model_get (model, b, COL_ACCOUNT_TEXT, &b_text, -1);
628
629   if (a_text == b_text)
630     result = 0;
631   else if (a_text == NULL)
632     result = 1;
633   else if (b_text == NULL)
634     result = -1;
635   else
636     result = g_ascii_strcasecmp (a_text, b_text);
637
638   g_free (a_text);
639   g_free (b_text);
640
641   return result;
642 }
643
644 static void
645 account_chooser_setup (EmpathyAccountChooser *self)
646 {
647   EmpathyAccountChooserPriv *priv;
648   GtkListStore *store;
649   GtkCellRenderer *renderer;
650   GtkComboBox *combobox;
651
652   priv = GET_PRIV (self);
653
654   /* Set up combo box with new store */
655   combobox = GTK_COMBO_BOX (self);
656
657   gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
658
659   store = gtk_list_store_new (COL_ACCOUNT_COUNT,
660       GDK_TYPE_PIXBUF,  /* Image */
661       G_TYPE_STRING,    /* Name */
662       G_TYPE_BOOLEAN,   /* Enabled */
663       G_TYPE_UINT,      /* Row type */
664       TP_TYPE_ACCOUNT);
665
666   gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
667     account_cmp, self, NULL);
668   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
669     GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
670
671   gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
672
673   renderer = gtk_cell_renderer_pixbuf_new ();
674   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
675   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
676       "pixbuf", COL_ACCOUNT_IMAGE,
677       "sensitive", COL_ACCOUNT_ENABLED,
678       NULL);
679
680   renderer = gtk_cell_renderer_text_new ();
681   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
682   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
683       "text", COL_ACCOUNT_TEXT,
684       "sensitive", COL_ACCOUNT_ENABLED,
685       NULL);
686
687   /* Populate accounts */
688   tp_proxy_prepare_async (priv->manager, NULL, account_manager_prepared_cb,
689       self);
690
691   g_object_unref (store);
692 }
693
694 static void
695 account_chooser_account_validity_changed_cb (TpAccountManager *manager,
696     TpAccount *account,
697     gboolean valid,
698     EmpathyAccountChooser *self)
699 {
700   if (valid)
701     account_chooser_account_add_foreach (account, self);
702   else
703     account_chooser_account_remove_foreach (account, self);
704 }
705
706 static void
707 account_chooser_account_add_foreach (TpAccount *account,
708     EmpathyAccountChooser *self)
709 {
710   GtkListStore *store;
711   GtkComboBox *combobox;
712   GtkTreeIter iter;
713   gint position;
714
715   combobox = GTK_COMBO_BOX (self);
716   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
717
718   position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
719   gtk_list_store_insert_with_values (store, &iter, position,
720       COL_ACCOUNT_POINTER, account,
721       -1);
722
723   account_chooser_update_iter (self, &iter);
724 }
725
726 static void
727 account_chooser_account_removed_cb (TpAccountManager *manager,
728     TpAccount *account,
729     EmpathyAccountChooser *self)
730 {
731   account_chooser_account_remove_foreach (account, self);
732 }
733
734 typedef struct
735 {
736   TpAccount *account;
737   GtkTreeIter *iter;
738   gboolean found;
739 } FindAccountData;
740
741 static gboolean
742 account_chooser_find_account_foreach (GtkTreeModel *model,
743     GtkTreePath *path,
744     GtkTreeIter *iter,
745     gpointer user_data)
746 {
747   FindAccountData *data = user_data;
748   TpAccount *account;
749   RowType type;
750
751   gtk_tree_model_get (model, iter,
752     COL_ACCOUNT_POINTER, &account,
753     COL_ACCOUNT_ROW_TYPE, &type,
754      -1);
755
756   if (type != ROW_ACCOUNT)
757     return FALSE;
758
759   if (account == data->account)
760     {
761       data->found = TRUE;
762       *(data->iter) = *iter;
763       g_object_unref (account);
764
765       return TRUE;
766     }
767
768   g_object_unref (account);
769
770   return FALSE;
771 }
772
773 static gboolean
774 account_chooser_find_account (EmpathyAccountChooser *self,
775     TpAccount *account,
776     GtkTreeIter*iter)
777 {
778   GtkListStore *store;
779   GtkComboBox *combobox;
780   FindAccountData data;
781
782   combobox = GTK_COMBO_BOX (self);
783   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
784
785   data.account = account;
786   data.iter = iter;
787   gtk_tree_model_foreach (GTK_TREE_MODEL (store),
788         account_chooser_find_account_foreach,
789         &data);
790
791   return data.found;
792 }
793
794 static void
795 account_chooser_account_remove_foreach (TpAccount *account,
796     EmpathyAccountChooser *self)
797 {
798   GtkListStore *store;
799   GtkComboBox *combobox;
800   GtkTreeIter iter;
801
802   combobox = GTK_COMBO_BOX (self);
803   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
804
805   if (account_chooser_find_account (self, account, &iter))
806     gtk_list_store_remove (store, &iter);
807 }
808
809 static void
810 account_chooser_filter_ready_cb (gboolean is_enabled,
811     gpointer data)
812 {
813   FilterResultCallbackData *fr_data = data;
814   EmpathyAccountChooser *self;
815   EmpathyAccountChooserPriv *priv;
816   TpAccount *account;
817   GtkTreeIter *iter;
818   GtkListStore *store;
819   GtkComboBox *combobox;
820   const gchar *icon_name;
821   GdkPixbuf *pixbuf;
822
823   self = fr_data->self;
824   priv = GET_PRIV (self);
825   account = fr_data->account;
826   iter = fr_data->iter;
827   combobox = GTK_COMBO_BOX (self);
828   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
829
830   icon_name = tp_account_get_icon_name (account);
831   pixbuf = empathy_pixbuf_from_icon_name (icon_name,
832     GTK_ICON_SIZE_BUTTON);
833
834   gtk_list_store_set (store, iter,
835           COL_ACCOUNT_IMAGE, pixbuf,
836           COL_ACCOUNT_TEXT, tp_account_get_display_name (account),
837           COL_ACCOUNT_ENABLED, is_enabled,
838           -1);
839
840   if (pixbuf != NULL)
841     g_object_unref (pixbuf);
842
843   /* set first connected account as active account */
844   if (priv->account_manually_set == FALSE &&
845       priv->set_active_item == FALSE && is_enabled)
846     {
847       priv->set_active_item = TRUE;
848       gtk_combo_box_set_active_iter (combobox, iter);
849     }
850
851   filter_result_callback_data_free (fr_data);
852 }
853
854 static void
855 account_chooser_update_iter (EmpathyAccountChooser *self,
856     GtkTreeIter *iter)
857 {
858   EmpathyAccountChooserPriv *priv;
859   GtkListStore *store;
860   GtkComboBox *combobox;
861   TpAccount *account;
862   FilterResultCallbackData *data;
863
864   priv = GET_PRIV (self);
865
866   combobox = GTK_COMBO_BOX (self);
867   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
868
869   gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
870           COL_ACCOUNT_POINTER, &account,
871           -1);
872
873   /* Skip rows without account associated */
874   if (account == NULL)
875     return;
876
877   data = filter_result_callback_data_new (self, account, iter);
878
879   if (priv->filter)
880     priv->filter (account, account_chooser_filter_ready_cb,
881             (gpointer) data, priv->filter_data);
882   else
883     account_chooser_filter_ready_cb (TRUE, (gpointer) data);
884
885   g_object_unref (account);
886 }
887
888 static void
889 account_chooser_status_changed_cb (TpAccount *account,
890     guint old_status,
891     guint new_status,
892     guint reason,
893     gchar *dbus_error_name,
894     GHashTable *details,
895     gpointer user_data)
896 {
897   EmpathyAccountChooser *self = user_data;
898   GtkTreeIter iter;
899
900   if (account_chooser_find_account (self, account, &iter))
901     account_chooser_update_iter (self, &iter);
902 }
903
904 static gboolean
905 account_chooser_separator_func (GtkTreeModel *model,
906     GtkTreeIter *iter,
907     EmpathyAccountChooser *self)
908 {
909   RowType row_type;
910
911   gtk_tree_model_get (model, iter, COL_ACCOUNT_ROW_TYPE, &row_type, -1);
912   return (row_type == ROW_SEPARATOR);
913 }
914
915 static gboolean
916 account_chooser_set_account_foreach (GtkTreeModel *model,
917     GtkTreePath *path,
918     GtkTreeIter *iter,
919     SetAccountData *data)
920 {
921   TpAccount *account;
922   gboolean equal;
923
924   gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
925
926   equal = (data->account == account);
927
928   if (account)
929     g_object_unref (account);
930
931   if (equal)
932     {
933       GtkComboBox *combobox;
934
935       combobox = GTK_COMBO_BOX (data->self);
936       gtk_combo_box_set_active_iter (combobox, iter);
937
938       data->set = TRUE;
939     }
940
941   return equal;
942 }
943
944 static gboolean
945 account_chooser_filter_foreach (GtkTreeModel *model,
946     GtkTreePath *path,
947     GtkTreeIter *iter,
948     gpointer self)
949 {
950   account_chooser_update_iter (self, iter);
951   return FALSE;
952 }
953
954 /**
955  * empathy_account_chooser_set_filter:
956  * @self: an #EmpathyAccountChooser
957  * @filter: a filter
958  * @user_data: data to pass to @filter, or %NULL
959  *
960  * Sets a filter on the @self so only accounts that are %TRUE in the eyes
961  * of the filter are visible in the @self.
962  */
963 void
964 empathy_account_chooser_set_filter (EmpathyAccountChooser *self,
965     EmpathyAccountChooserFilterFunc filter,
966     gpointer user_data)
967 {
968   EmpathyAccountChooserPriv *priv;
969   GtkTreeModel *model;
970
971   g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
972
973   priv = GET_PRIV (self);
974
975   priv->filter = filter;
976   priv->filter_data = user_data;
977
978   /* Refilter existing data */
979   priv->set_active_item = FALSE;
980   model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
981   gtk_tree_model_foreach (model, account_chooser_filter_foreach, self);
982 }
983
984 /**
985  * EmpathyAccountChooserFilterFunc:
986  * @account: a #TpAccount
987  * @user_data: user data, or %NULL
988  *
989  * A function which decides whether the account indicated by @account
990  * is visible.
991  *
992  * Return value: whether the account indicated by @account is visible.
993  */
994
995 /**
996  * empathy_account_chooser_filter_is_connected:
997  * @account: a #TpAccount
998  * @callback: an #EmpathyAccountChooserFilterResultCallback accepting the result
999  * @callback_data: data passed to the @callback
1000  * @user_data: user data or %NULL
1001  *
1002  * A useful #EmpathyAccountChooserFilterFunc that one could pass into
1003  * empathy_account_chooser_set_filter() and only show connected accounts.
1004  *
1005  * Returns (via the callback) TRUE is @account is connected
1006  */
1007 void
1008 empathy_account_chooser_filter_is_connected (TpAccount *account,
1009   EmpathyAccountChooserFilterResultCallback callback,
1010   gpointer callback_data,
1011   gpointer user_data)
1012 {
1013   gboolean is_connected =
1014     tp_account_get_connection_status (account, NULL)
1015     == TP_CONNECTION_STATUS_CONNECTED;
1016
1017   callback (is_connected, callback_data);
1018 }
1019
1020 /**
1021  * empathy_account_chooser_filter_supports_multichat:
1022  * @account: a #TpAccount
1023  * @callback: an #EmpathyAccountChooserFilterResultCallback accepting the result
1024  * @callback_data: data passed to the @callback
1025  * @user_data: user data or %NULL
1026  *
1027  * An #EmpathyAccountChooserFilterFunc that returns accounts that both
1028  * support multiuser text chat and are connected.
1029  *
1030  * Returns (via the callback) TRUE if @account both supports muc and
1031  * is connected
1032  */
1033 void
1034 empathy_account_chooser_filter_supports_chatrooms (TpAccount *account,
1035   EmpathyAccountChooserFilterResultCallback callback,
1036   gpointer callback_data,
1037   gpointer user_data)
1038 {
1039   TpConnection *connection;
1040   gboolean supported = FALSE;
1041   TpCapabilities *caps;
1042
1043   /* check if CM supports multiuser text chat */
1044   connection = tp_account_get_connection (account);
1045   if (connection == NULL)
1046     goto out;
1047
1048   caps = tp_connection_get_capabilities (connection);
1049   if (caps == NULL)
1050     goto out;
1051
1052   supported = tp_capabilities_supports_text_chatrooms (caps);
1053
1054 out:
1055   callback (supported, callback_data);
1056 }
1057
1058 gboolean
1059 empathy_account_chooser_is_ready (EmpathyAccountChooser *self)
1060 {
1061   EmpathyAccountChooserPriv *priv = GET_PRIV (self);
1062
1063   return priv->ready;
1064 }
1065
1066 TpAccount *
1067 empathy_account_chooser_get_account (EmpathyAccountChooser *self)
1068 {
1069   TpAccount *account;
1070
1071   account = empathy_account_chooser_dup_account (self);
1072   if (account == NULL)
1073     return NULL;
1074
1075   g_object_unref (account);
1076
1077   return account;
1078 }
1079
1080 TpAccountManager *
1081 empathy_account_chooser_get_account_manager (EmpathyAccountChooser *self)
1082 {
1083   EmpathyAccountChooserPriv *priv = GET_PRIV (self);
1084
1085   return priv->manager;
1086 }