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