]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-chooser.c
Updated Spanish Translation
[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 #include "empathy-account-chooser.h"
26
27 #include <glib/gi18n-lib.h>
28 #include <tp-account-widgets/tpaw-pixbuf-utils.h>
29
30 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
31 #include "empathy-debug.h"
32
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
38  *
39  * #EmpathyAccountChooser is a widget which extends #GtkComboBox to provide
40  * a chooser of available accounts.
41  */
42
43 /**
44  * EmpathyAccountChooser:
45  * @parent: parent object
46  *
47  * Widget which extends #GtkComboBox to provide a chooser of available accounts.
48  */
49
50 struct _EmpathyAccountChooserPriv
51 {
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;
59
60   TpAccount *select_when_ready;
61 };
62
63 typedef struct
64 {
65   EmpathyAccountChooser *self;
66   TpAccount *account;
67   gboolean set;
68 } SetAccountData;
69
70 typedef struct
71 {
72   EmpathyAccountChooser *self;
73   TpAccount *account;
74   GtkTreeIter *iter;
75 } FilterResultCallbackData;
76
77 static FilterResultCallbackData *
78 filter_result_callback_data_new (EmpathyAccountChooser *self,
79     TpAccount *account,
80     GtkTreeIter *iter)
81 {
82   FilterResultCallbackData *data;
83
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);
87
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);
92
93   return data;
94 }
95
96 static void
97 filter_result_callback_data_free (FilterResultCallbackData *data)
98 {
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);
103 }
104
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.
108  */
109 typedef enum {
110   ROW_ACCOUNT = 0,
111   ROW_SEPARATOR,
112   ROW_ALL
113 } RowType;
114
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
122 };
123
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);
156
157 enum {
158   PROP_0,
159   PROP_HAS_ALL_OPTION,
160 };
161
162 enum {
163   READY,
164   LAST_SIGNAL
165 };
166
167 static guint signals[LAST_SIGNAL] = { 0 };
168
169 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser,
170     GTK_TYPE_COMBO_BOX)
171
172 static void
173 empathy_account_chooser_init (EmpathyAccountChooser *self)
174 {
175   TpSimpleClientFactory *factory;
176
177   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
178     EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
179
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;
184
185   self->priv->manager = tp_account_manager_dup ();
186
187   tp_g_signal_connect_object (self->priv->manager, "account-validity-changed",
188       G_CALLBACK (account_chooser_account_validity_changed_cb), self, 0);
189
190   tp_g_signal_connect_object (self->priv->manager, "account-removed",
191       G_CALLBACK (account_chooser_account_removed_cb), self, 0);
192
193   /* Make sure we'll have the capabilities feature on TpAccount's connection */
194   factory = tp_proxy_get_factory (self->priv->manager);
195
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);
200 }
201
202 static gint
203 account_cmp (GtkTreeModel *model,
204     GtkTreeIter *a,
205     GtkTreeIter *b,
206     gpointer user_data)
207 {
208   RowType a_type, b_type;
209   gboolean a_enabled, b_enabled;
210   gchar *a_text, *b_text;
211   gint result;
212
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);
221
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);
226
227   /* Enabled accounts are displayed first */
228   if (a_enabled != b_enabled)
229     return a_enabled ? -1: 1;
230
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);
233
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);
242
243   g_free (a_text);
244   g_free (b_text);
245
246   return result;
247 }
248
249 static void
250 account_connection_notify_cb (TpAccount *account,
251     GParamSpec *spec,
252     EmpathyAccountChooser *self)
253 {
254   update_account (self, account);
255 }
256
257 static void
258 account_manager_prepared_cb (GObject *source_object,
259     GAsyncResult *result,
260     gpointer user_data)
261 {
262   GList *accounts, *l;
263   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
264   EmpathyAccountChooser *self = user_data;
265   GError *error = NULL;
266
267   if (!tp_proxy_prepare_finish (manager, result, &error))
268     {
269       DEBUG ("Failed to prepare account manager: %s", error->message);
270       g_error_free (error);
271       return;
272     }
273
274   accounts = tp_account_manager_dup_valid_accounts (manager);
275
276   for (l = accounts; l != NULL; l = l->next)
277     {
278       TpAccount *account = l->data;
279
280       account_chooser_account_add_foreach (account, self);
281
282       tp_g_signal_connect_object (account, "status-changed",
283           G_CALLBACK (account_chooser_status_changed_cb),
284           self, 0);
285
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);
293     }
294
295   g_list_free_full (accounts, g_object_unref);
296
297   if (self->priv->select_when_ready != NULL)
298     {
299       select_account (self, self->priv->select_when_ready);
300
301       g_clear_object (&self->priv->select_when_ready);
302     }
303
304   self->priv->ready = TRUE;
305   g_signal_emit (self, signals[READY], 0);
306 }
307
308 static void
309 account_chooser_constructed (GObject *object)
310 {
311   EmpathyAccountChooser *self = (EmpathyAccountChooser *) object;
312   GtkListStore *store;
313   GtkCellRenderer *renderer;
314   GtkComboBox *combobox;
315
316   /* Set up combo box with new store */
317   combobox = GTK_COMBO_BOX (self);
318
319   gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
320
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);
327
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);
332
333   gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
334
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);
341
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);
348
349   /* Populate accounts */
350   tp_proxy_prepare_async (self->priv->manager, NULL,
351       account_manager_prepared_cb, self);
352
353   g_object_unref (store);
354
355 }
356
357 static void
358 account_chooser_dispose (GObject *object)
359 {
360   EmpathyAccountChooser *self = EMPATHY_ACCOUNT_CHOOSER (object);
361
362   g_clear_object (&self->priv->manager);
363   g_clear_object (&self->priv->select_when_ready);
364
365   G_OBJECT_CLASS (empathy_account_chooser_parent_class)->dispose (object);
366 }
367
368 static void
369 account_chooser_get_property (GObject *object,
370     guint param_id,
371     GValue *value,
372     GParamSpec *pspec)
373 {
374   EmpathyAccountChooser *self = (EmpathyAccountChooser *) object;
375
376   switch (param_id)
377     {
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;
384     };
385 }
386
387 static void
388 account_chooser_set_property (GObject *object,
389     guint param_id,
390     const GValue *value,
391     GParamSpec *pspec)
392 {
393   switch (param_id)
394     {
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;
402     };
403 }
404
405 static void
406 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
407 {
408   GObjectClass *object_class = G_OBJECT_CLASS (klass);
409
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;
414
415   /**
416    * EmpathyAccountChooser:has-all-option:
417    *
418    * Have an additional option in the list to mean all accounts.
419    */
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));
427
428   signals[READY] =
429     g_signal_new ("ready",
430         G_OBJECT_CLASS_TYPE (object_class),
431         G_SIGNAL_RUN_LAST,
432         0,
433         NULL, NULL,
434         g_cclosure_marshal_generic,
435         G_TYPE_NONE,
436         0);
437
438   g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
439 }
440
441 /**
442  * empathy_account_chooser_new:
443  *
444  * Creates a new #EmpathyAccountChooser.
445  *
446  * Return value: A new #EmpathyAccountChooser
447  */
448 GtkWidget *
449 empathy_account_chooser_new (void)
450 {
451   GtkWidget *self;
452
453   self = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
454
455   return self;
456 }
457
458 gboolean
459 empathy_account_chooser_has_all_selected (EmpathyAccountChooser *self)
460 {
461   GtkTreeModel *model;
462   GtkTreeIter iter;
463   RowType type;
464
465   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
466
467   g_return_val_if_fail (self->priv->has_all_option == TRUE, FALSE);
468
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;
472
473   gtk_tree_model_get (model, &iter, COL_ACCOUNT_ROW_TYPE, &type, -1);
474
475   return type == ROW_ALL;
476 }
477
478 /**
479  * empathy_account_chooser_dup_account:
480  * @self: an #EmpathyAccountChooser
481  *
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.
485  *
486  * Return value: a new ref to the #TpAccount currently selected, or %NULL.
487  */
488 TpAccount *
489 empathy_account_chooser_dup_account (EmpathyAccountChooser *self)
490 {
491   TpAccount *account;
492   GtkTreeModel *model;
493   GtkTreeIter iter;
494
495   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), NULL);
496
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;
500
501   gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
502
503   return account;
504 }
505
506 /**
507  * empathy_account_chooser_get_connection:
508  * @self: an #EmpathyAccountChooser
509  *
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
513  *
514  * Return value: a borrowed reference to the #TpConnection associated with the
515  * account curently selected.
516  */
517 TpConnection *
518 empathy_account_chooser_get_connection (EmpathyAccountChooser *self)
519 {
520   TpAccount *account;
521   TpConnection *connection;
522
523   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), NULL);
524
525   account = empathy_account_chooser_dup_account (self);
526
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;
532
533   connection = tp_account_get_connection (account);
534   g_object_unref (account);
535
536   return connection;
537 }
538
539 static gboolean
540 select_account (EmpathyAccountChooser *self,
541     TpAccount *account)
542 {
543   GtkComboBox *combobox;
544   GtkTreeModel *model;
545   GtkTreeIter iter;
546   SetAccountData data;
547
548   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
549
550   combobox = GTK_COMBO_BOX (self);
551   model = gtk_combo_box_get_model (combobox);
552   gtk_combo_box_get_active_iter (combobox, &iter);
553
554   data.self = self;
555   data.account = account;
556   data.set = FALSE;
557
558   gtk_tree_model_foreach (model,
559       (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
560       &data);
561
562   self->priv->account_manually_set = data.set;
563
564   return data.set;
565 }
566
567 /**
568  * empathy_account_chooser_set_account:
569  * @self: an #EmpathyAccountChooser
570  * @account: a #TpAccount
571  *
572  * Sets the currently selected account to @account, if it exists in the list.
573  *
574  * Return value: whether the chooser was set to @account.
575  */
576 gboolean
577 empathy_account_chooser_set_account (EmpathyAccountChooser *self,
578     TpAccount *account)
579 {
580   if (self->priv->ready)
581     return select_account (self, account);
582
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);
586
587   if (account != NULL)
588     self->priv->select_when_ready = g_object_ref (account);
589
590   return FALSE;
591 }
592
593 void
594 empathy_account_chooser_set_all (EmpathyAccountChooser *self)
595 {
596   GtkComboBox *combobox;
597   GtkTreeModel *model;
598   GtkTreeIter iter;
599
600   g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
601
602   g_return_if_fail (self->priv->has_all_option);
603
604   combobox = GTK_COMBO_BOX (self);
605   model = gtk_combo_box_get_model (combobox);
606
607   if (gtk_tree_model_get_iter_first (model, &iter))
608     {
609       /* 'All accounts' is the first row */
610       gtk_combo_box_set_active_iter (combobox, &iter);
611       self->priv->account_manually_set = TRUE;
612     }
613 }
614
615 /**
616  * empathy_account_chooser_get_has_all_option:
617  * @self: an #EmpathyAccountChooser
618  *
619  * Returns whether @self has the #EmpathyAccountChooser:has-all-option
620  * property set to true.
621  *
622  * Return value: whether @self has the #EmpathyAccountChooser:has-all-option
623  * property enabled.
624  */
625 gboolean
626 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *self)
627 {
628   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
629
630   return self->priv->has_all_option;
631 }
632
633 /**
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
638  *
639  * Sets the #EmpathyAccountChooser:has-all-option property.
640  */
641 void
642 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *self,
643     gboolean has_all_option)
644 {
645   GtkComboBox *combobox;
646   GtkListStore *store;
647   GtkTreeModel *model;
648   GtkTreeIter iter;
649
650   g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
651
652   if (self->priv->has_all_option == has_all_option)
653     return;
654
655   combobox = GTK_COMBO_BOX (self);
656   model = gtk_combo_box_get_model (combobox);
657   store = GTK_LIST_STORE (model);
658
659   self->priv->has_all_option = has_all_option;
660
661   /*
662    * The first 2 options are the ALL and separator
663    */
664
665   if (has_all_option)
666     {
667       gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
668           (GtkTreeViewRowSeparatorFunc)
669           account_chooser_separator_func,
670           self,
671           NULL);
672
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);
680
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);
688     }
689   else
690     {
691       if (gtk_tree_model_get_iter_first (model, &iter))
692         {
693           if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter))
694             gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
695         }
696
697     gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
698         (GtkTreeViewRowSeparatorFunc)
699         NULL,
700         NULL,
701         NULL);
702   }
703
704   g_object_notify (G_OBJECT (self), "has-all-option");
705 }
706
707 static void
708 account_chooser_account_validity_changed_cb (TpAccountManager *manager,
709     TpAccount *account,
710     gboolean valid,
711     EmpathyAccountChooser *self)
712 {
713   if (valid)
714     account_chooser_account_add_foreach (account, self);
715   else
716     account_chooser_account_remove_foreach (account, self);
717 }
718
719 static void
720 account_chooser_account_add_foreach (TpAccount *account,
721     EmpathyAccountChooser *self)
722 {
723   GtkListStore *store;
724   GtkComboBox *combobox;
725   GtkTreeIter iter;
726   gint position;
727
728   combobox = GTK_COMBO_BOX (self);
729   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
730
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);
735
736   account_chooser_update_iter (self, &iter);
737 }
738
739 static void
740 account_chooser_account_removed_cb (TpAccountManager *manager,
741     TpAccount *account,
742     EmpathyAccountChooser *self)
743 {
744   account_chooser_account_remove_foreach (account, self);
745 }
746
747 typedef struct
748 {
749   TpAccount *account;
750   GtkTreeIter *iter;
751   gboolean found;
752 } FindAccountData;
753
754 static gboolean
755 account_chooser_find_account_foreach (GtkTreeModel *model,
756     GtkTreePath *path,
757     GtkTreeIter *iter,
758     gpointer user_data)
759 {
760   FindAccountData *data = user_data;
761   TpAccount *account;
762   RowType type;
763
764   gtk_tree_model_get (model, iter,
765     COL_ACCOUNT_POINTER, &account,
766     COL_ACCOUNT_ROW_TYPE, &type,
767      -1);
768
769   if (type != ROW_ACCOUNT)
770     return FALSE;
771
772   if (account == data->account)
773     {
774       data->found = TRUE;
775       *(data->iter) = *iter;
776       g_object_unref (account);
777
778       return TRUE;
779     }
780
781   g_object_unref (account);
782
783   return FALSE;
784 }
785
786 static gboolean
787 account_chooser_find_account (EmpathyAccountChooser *self,
788     TpAccount *account,
789     GtkTreeIter *iter)
790 {
791   GtkListStore *store;
792   GtkComboBox *combobox;
793   FindAccountData data;
794
795   combobox = GTK_COMBO_BOX (self);
796   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
797
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);
803
804   return data.found;
805 }
806
807 static void
808 account_chooser_account_remove_foreach (TpAccount *account,
809     EmpathyAccountChooser *self)
810 {
811   GtkListStore *store;
812   GtkComboBox *combobox;
813   GtkTreeIter iter;
814
815   combobox = GTK_COMBO_BOX (self);
816   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
817
818   if (account_chooser_find_account (self, account, &iter))
819     gtk_list_store_remove (store, &iter);
820 }
821
822 static void
823 account_chooser_filter_ready_cb (gboolean is_enabled,
824     gpointer data)
825 {
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;
834
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));
840
841   icon_name = tp_account_get_icon_name (account);
842   pixbuf = tpaw_pixbuf_from_icon_name (icon_name,
843     GTK_ICON_SIZE_BUTTON);
844
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);
850
851   if (pixbuf != NULL)
852     g_object_unref (pixbuf);
853
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)
857     {
858       self->priv->set_active_item = TRUE;
859       gtk_combo_box_set_active_iter (combobox, iter);
860     }
861
862   filter_result_callback_data_free (fr_data);
863 }
864
865 static void
866 account_chooser_update_iter (EmpathyAccountChooser *self,
867     GtkTreeIter *iter)
868 {
869   GtkListStore *store;
870   GtkComboBox *combobox;
871   TpAccount *account;
872   FilterResultCallbackData *data;
873
874   combobox = GTK_COMBO_BOX (self);
875   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
876
877   gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
878           COL_ACCOUNT_POINTER, &account,
879           -1);
880
881   /* Skip rows without account associated */
882   if (account == NULL)
883     return;
884
885   data = filter_result_callback_data_new (self, account, iter);
886
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);
892
893   g_object_unref (account);
894 }
895
896 static void
897 update_account (EmpathyAccountChooser *self,
898     TpAccount *account)
899 {
900   GtkTreeIter iter;
901
902   if (account_chooser_find_account (self, account, &iter))
903     account_chooser_update_iter (self, &iter);
904 }
905
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)
914 {
915   EmpathyAccountChooser *self = user_data;
916
917   update_account (self, account);
918 }
919
920 static gboolean
921 account_chooser_separator_func (GtkTreeModel *model,
922     GtkTreeIter *iter,
923     EmpathyAccountChooser *self)
924 {
925   RowType row_type;
926
927   gtk_tree_model_get (model, iter, COL_ACCOUNT_ROW_TYPE, &row_type, -1);
928   return (row_type == ROW_SEPARATOR);
929 }
930
931 static gboolean
932 account_chooser_set_account_foreach (GtkTreeModel *model,
933     GtkTreePath *path,
934     GtkTreeIter *iter,
935     SetAccountData *data)
936 {
937   TpAccount *account;
938   gboolean equal;
939
940   gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
941
942   equal = (data->account == account);
943
944   if (account)
945     g_object_unref (account);
946
947   if (equal)
948     {
949       GtkComboBox *combobox;
950
951       combobox = GTK_COMBO_BOX (data->self);
952       gtk_combo_box_set_active_iter (combobox, iter);
953
954       data->set = TRUE;
955     }
956
957   return equal;
958 }
959
960 static gboolean
961 account_chooser_filter_foreach (GtkTreeModel *model,
962     GtkTreePath *path,
963     GtkTreeIter *iter,
964     gpointer self)
965 {
966   account_chooser_update_iter (self, iter);
967   return FALSE;
968 }
969
970 void
971 empathy_account_chooser_refilter (EmpathyAccountChooser *self)
972 {
973   GtkTreeModel *model;
974
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);
978 }
979
980 /**
981  * empathy_account_chooser_set_filter:
982  * @self: an #EmpathyAccountChooser
983  * @filter: a filter
984  * @user_data: data to pass to @filter, or %NULL
985  *
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.
988  */
989 void
990 empathy_account_chooser_set_filter (EmpathyAccountChooser *self,
991     EmpathyAccountChooserFilterFunc filter,
992     gpointer user_data)
993 {
994   g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
995
996   self->priv->filter = filter;
997   self->priv->filter_data = user_data;
998
999   /* Refilter existing data */
1000   empathy_account_chooser_refilter (self);
1001 }
1002
1003 /**
1004  * EmpathyAccountChooserFilterFunc:
1005  * @account: a #TpAccount
1006  * @user_data: user data, or %NULL
1007  *
1008  * A function which decides whether the account indicated by @account
1009  * is visible.
1010  *
1011  * Return value: whether the account indicated by @account is visible.
1012  */
1013
1014 gboolean
1015 empathy_account_chooser_is_ready (EmpathyAccountChooser *self)
1016 {
1017   return self->priv->ready;
1018 }
1019
1020 TpAccount *
1021 empathy_account_chooser_get_account (EmpathyAccountChooser *self)
1022 {
1023   TpAccount *account;
1024
1025   account = empathy_account_chooser_dup_account (self);
1026   if (account == NULL)
1027     return NULL;
1028
1029   g_object_unref (account);
1030
1031   return account;
1032 }
1033
1034 TpAccountManager *
1035 empathy_account_chooser_get_account_manager (EmpathyAccountChooser *self)
1036 {
1037   return self->priv->manager;
1038 }
1039
1040 /* Pre-defined filters */
1041
1042 /**
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
1048  *
1049  * A useful #EmpathyAccountChooserFilterFunc that one could pass into
1050  * empathy_account_chooser_set_filter() and only show connected accounts.
1051  *
1052  * Returns (via the callback) TRUE is @account is connected
1053  */
1054 void
1055 empathy_account_chooser_filter_is_connected (TpAccount *account,
1056   EmpathyAccountChooserFilterResultCallback callback,
1057   gpointer callback_data,
1058   gpointer user_data)
1059 {
1060   gboolean is_connected =
1061     tp_account_get_connection_status (account, NULL)
1062     == TP_CONNECTION_STATUS_CONNECTED;
1063
1064   callback (is_connected, callback_data);
1065 }
1066
1067 /**
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
1073  *
1074  * An #EmpathyAccountChooserFilterFunc that returns accounts that both
1075  * support multiuser text chat and are connected.
1076  *
1077  * Returns (via the callback) TRUE if @account both supports muc and
1078  * is connected
1079  */
1080 void
1081 empathy_account_chooser_filter_supports_chatrooms (TpAccount *account,
1082   EmpathyAccountChooserFilterResultCallback callback,
1083   gpointer callback_data,
1084   gpointer user_data)
1085 {
1086   TpConnection *connection;
1087   gboolean supported = FALSE;
1088   TpCapabilities *caps;
1089
1090   /* check if CM supports multiuser text chat */
1091   connection = tp_account_get_connection (account);
1092   if (connection == NULL)
1093     goto out;
1094
1095   caps = tp_connection_get_capabilities (connection);
1096   if (caps == NULL)
1097     goto out;
1098
1099   supported = tp_capabilities_supports_text_chatrooms (caps);
1100
1101 out:
1102   callback (supported, callback_data);
1103 }