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