]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-chooser.c
Merge branch 'sasl'
[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_finalize               (GObject                  *object);
127 static void     account_chooser_get_property           (GObject                  *object,
128                                                         guint                     param_id,
129                                                         GValue                   *value,
130                                                         GParamSpec               *pspec);
131 static void     account_chooser_set_property           (GObject                  *object,
132                                                         guint                     param_id,
133                                                         const GValue             *value,
134                                                         GParamSpec               *pspec);
135 static void     account_chooser_setup                  (EmpathyAccountChooser    *chooser);
136 static void     account_chooser_account_validity_changed_cb (TpAccountManager    *manager,
137                                                         TpAccount                *account,
138                                                         gboolean                  valid,
139                                                         EmpathyAccountChooser    *chooser);
140 static void     account_chooser_account_add_foreach    (TpAccount                *account,
141                                                         EmpathyAccountChooser    *chooser);
142 static void     account_chooser_account_removed_cb     (TpAccountManager         *manager,
143                                                         TpAccount                *account,
144                                                         EmpathyAccountChooser    *chooser);
145 static void     account_chooser_account_remove_foreach (TpAccount                *account,
146                                                         EmpathyAccountChooser    *chooser);
147 static void     account_chooser_update_iter            (EmpathyAccountChooser    *chooser,
148                                                         GtkTreeIter              *iter);
149 static void     account_chooser_status_changed_cb      (TpAccount  *account,
150                                                         guint       old_status,
151                                                         guint       new_status,
152                                                         guint       reason,
153                                                         gchar      *dbus_error_name,
154                                                         GHashTable *details,
155                                                         gpointer    user_data);
156 static gboolean account_chooser_separator_func         (GtkTreeModel             *model,
157                                                         GtkTreeIter              *iter,
158                                                         EmpathyAccountChooser    *chooser);
159 static gboolean account_chooser_set_account_foreach    (GtkTreeModel             *model,
160                                                         GtkTreePath              *path,
161                                                         GtkTreeIter              *iter,
162                                                         SetAccountData           *data);
163
164 enum {
165         PROP_0,
166         PROP_HAS_ALL_OPTION,
167 };
168
169 enum {
170         READY,
171         LAST_SIGNAL
172 };
173
174 static guint signals[LAST_SIGNAL] = { 0 };
175
176 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
177
178 static void
179 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
180 {
181         GObjectClass *object_class = G_OBJECT_CLASS (klass);
182
183         object_class->finalize = account_chooser_finalize;
184         object_class->get_property = account_chooser_get_property;
185         object_class->set_property = account_chooser_set_property;
186
187         /**
188          * EmpathyAccountChooser:has-all-option:
189          *
190          * Have an additional option in the list to mean all accounts.
191          */
192         g_object_class_install_property (object_class,
193                                          PROP_HAS_ALL_OPTION,
194                                          g_param_spec_boolean ("has-all-option",
195                                                                "Has All Option",
196                                                                "Have a separate option in the list to mean ALL accounts",
197                                                                FALSE,
198                                                                G_PARAM_READWRITE));
199
200         signals[READY] =
201                 g_signal_new ("ready",
202                               G_OBJECT_CLASS_TYPE (object_class),
203                               G_SIGNAL_RUN_LAST,
204                               0,
205                               NULL, NULL,
206                               g_cclosure_marshal_VOID__VOID,
207                               G_TYPE_NONE,
208                               0);
209
210         g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
211 }
212
213 static void
214 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
215 {
216         EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
217                 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
218
219         chooser->priv = priv;
220         priv->set_active_item = FALSE;
221         priv->account_manually_set = FALSE;
222         priv->filter = NULL;
223         priv->filter_data = NULL;
224
225         priv->manager = tp_account_manager_dup ();
226
227         g_signal_connect (priv->manager, "account-validity-changed",
228                           G_CALLBACK (account_chooser_account_validity_changed_cb),
229                           chooser);
230         g_signal_connect (priv->manager, "account-removed",
231                           G_CALLBACK (account_chooser_account_removed_cb),
232                           chooser);
233
234         account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
235 }
236
237 static void
238 account_chooser_finalize (GObject *object)
239 {
240         EmpathyAccountChooserPriv *priv = GET_PRIV (object);
241
242         g_signal_handlers_disconnect_by_func (priv->manager,
243                                               account_chooser_account_validity_changed_cb,
244                                               object);
245         g_signal_handlers_disconnect_by_func (priv->manager,
246                                               account_chooser_account_removed_cb,
247                                               object);
248         g_object_unref (priv->manager);
249
250         G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
251 }
252
253 static void
254 account_chooser_get_property (GObject    *object,
255                               guint       param_id,
256                               GValue     *value,
257                               GParamSpec *pspec)
258 {
259         EmpathyAccountChooserPriv *priv;
260
261         priv = GET_PRIV (object);
262
263         switch (param_id) {
264         case PROP_HAS_ALL_OPTION:
265                 g_value_set_boolean (value, priv->has_all_option);
266                 break;
267         default:
268                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
269                 break;
270         };
271 }
272
273 static void
274 account_chooser_set_property (GObject      *object,
275                               guint         param_id,
276                               const GValue *value,
277                               GParamSpec   *pspec)
278 {
279         EmpathyAccountChooserPriv *priv;
280
281         priv = GET_PRIV (object);
282
283         switch (param_id) {
284         case PROP_HAS_ALL_OPTION:
285                 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
286                                                            g_value_get_boolean (value));
287                 break;
288         default:
289                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
290                 break;
291         };
292 }
293
294 /**
295  * empathy_account_chooser_new:
296  *
297  * Creates a new #EmpathyAccountChooser.
298  *
299  * Return value: A new #EmpathyAccountChooser
300  */
301 GtkWidget *
302 empathy_account_chooser_new (void)
303 {
304         GtkWidget                *chooser;
305
306         chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
307
308         return chooser;
309 }
310
311 /**
312  * empathy_account_chooser_dup_account:
313  * @chooser: an #EmpathyAccountChooser
314  *
315  * Returns the account which is currently selected in the chooser or %NULL
316  * if there is no account selected. The #TpAccount returned should be
317  * unrefed with g_object_unref() when finished with.
318  *
319  * Return value: a new ref to the #TpAccount currently selected, or %NULL.
320  */
321 TpAccount *
322 empathy_account_chooser_dup_account (EmpathyAccountChooser *chooser)
323 {
324         EmpathyAccountChooserPriv *priv;
325         TpAccount                 *account;
326         GtkTreeModel             *model;
327         GtkTreeIter               iter;
328
329         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
330
331         priv = GET_PRIV (chooser);
332
333         model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
334         if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
335                 return NULL;
336         }
337
338         gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
339
340         return account;
341 }
342
343 /**
344  * empathy_account_chooser_get_connection:
345  * @chooser: an #EmpathyAccountChooser
346  *
347  * Returns a borrowed reference to the #TpConnection associated with the
348  * account currently selected. The caller must reference the returned object with
349  * g_object_ref() if it will be kept
350  *
351  * Return value: a borrowed reference to the #TpConnection associated with the
352  * account curently selected.
353  */
354 TpConnection *
355 empathy_account_chooser_get_connection (EmpathyAccountChooser *chooser)
356 {
357         EmpathyAccountChooserPriv *priv;
358         TpAccount                 *account;
359         TpConnection              *connection;
360
361         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
362
363         priv = GET_PRIV (chooser);
364
365         account = empathy_account_chooser_dup_account (chooser);
366
367         /* if the returned account is NULL, then the account manager probably
368          * hasn't been prepared yet. It should be safe to return NULL here
369          * though. */
370         if (account == NULL) {
371                 return NULL;
372         }
373
374         connection = tp_account_get_connection (account);
375         g_object_unref (account);
376
377         return connection;
378 }
379
380 /**
381  * empathy_account_chooser_set_account:
382  * @chooser: an #EmpathyAccountChooser
383  * @account: a #TpAccount
384  *
385  * Sets the currently selected account to @account, if it exists in the list.
386  *
387  * Return value: whether the chooser was set to @account.
388  */
389 gboolean
390 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
391                                      TpAccount             *account)
392 {
393         EmpathyAccountChooserPriv *priv;
394         GtkComboBox    *combobox;
395         GtkTreeModel   *model;
396         GtkTreeIter     iter;
397         SetAccountData  data;
398
399         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
400
401         priv = GET_PRIV (chooser);
402
403         combobox = GTK_COMBO_BOX (chooser);
404         model = gtk_combo_box_get_model (combobox);
405         gtk_combo_box_get_active_iter (combobox, &iter);
406
407         data.chooser = chooser;
408         data.account = account;
409
410         gtk_tree_model_foreach (model,
411                                 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
412                                 &data);
413
414         priv->account_manually_set = data.set;
415
416         return data.set;
417 }
418
419 /**
420  * empathy_account_chooser_get_has_all_option:
421  * @chooser: an #EmpathyAccountChooser
422  *
423  * Returns whether @chooser has the #EmpathyAccountChooser:has-all-option property
424  * set to true.
425  *
426  * Return value: whether @chooser has the #EmpathyAccountChooser:has-all-option property
427  * enabled.
428  */
429 gboolean
430 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
431 {
432         EmpathyAccountChooserPriv *priv;
433
434         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
435
436         priv = GET_PRIV (chooser);
437
438         return priv->has_all_option;
439 }
440
441 /**
442  * empathy_account_chooser_set_has_all_option:
443  * @chooser: an #EmpathyAccountChooser
444  * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option property
445  *
446  * Sets the #EmpathyAccountChooser:has-all-option property.
447  */
448 void
449 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
450                                            gboolean              has_all_option)
451 {
452         EmpathyAccountChooserPriv *priv;
453         GtkComboBox              *combobox;
454         GtkListStore             *store;
455         GtkTreeModel             *model;
456         GtkTreeIter               iter;
457
458         g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
459
460         priv = GET_PRIV (chooser);
461
462         if (priv->has_all_option == has_all_option) {
463                 return;
464         }
465
466         combobox = GTK_COMBO_BOX (chooser);
467         model = gtk_combo_box_get_model (combobox);
468         store = GTK_LIST_STORE (model);
469
470         priv->has_all_option = has_all_option;
471
472         /*
473          * The first 2 options are the ALL and separator
474          */
475
476         if (has_all_option) {
477                 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
478                                                       (GtkTreeViewRowSeparatorFunc)
479                                                       account_chooser_separator_func,
480                                                       chooser,
481                                                       NULL);
482
483                 gtk_list_store_prepend (store, &iter);
484                 gtk_list_store_set (store, &iter,
485                                     COL_ACCOUNT_TEXT, NULL,
486                                     COL_ACCOUNT_ENABLED, TRUE,
487                                     COL_ACCOUNT_POINTER, NULL,
488                                     COL_ACCOUNT_ROW_TYPE, ROW_SEPARATOR,
489                                     -1);
490
491                 gtk_list_store_prepend (store, &iter);
492                 gtk_list_store_set (store, &iter,
493                                     COL_ACCOUNT_TEXT, _("All"),
494                                     COL_ACCOUNT_ENABLED, TRUE,
495                                     COL_ACCOUNT_POINTER, NULL,
496                                     COL_ACCOUNT_ROW_TYPE, ROW_ALL,
497                                     -1);
498         } else {
499                 if (gtk_tree_model_get_iter_first (model, &iter)) {
500                         if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
501                                 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
502                         }
503                 }
504
505                 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
506                                                       (GtkTreeViewRowSeparatorFunc)
507                                                       NULL,
508                                                       NULL,
509                                                       NULL);
510         }
511
512         g_object_notify (G_OBJECT (chooser), "has-all-option");
513 }
514
515 static void
516 account_manager_prepared_cb (GObject *source_object,
517                              GAsyncResult *result,
518                              gpointer user_data)
519 {
520         GList *accounts, *l;
521         TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
522         EmpathyAccountChooser *chooser = user_data;
523         EmpathyAccountChooserPriv *priv = GET_PRIV (chooser);
524         GError *error = NULL;
525
526         if (!tp_account_manager_prepare_finish (manager, result, &error)) {
527                 DEBUG ("Failed to prepare account manager: %s", error->message);
528                 g_error_free (error);
529                 return;
530         }
531
532         accounts = tp_account_manager_get_valid_accounts (manager);
533
534         for (l = accounts; l != NULL; l = l->next) {
535                 TpAccount *account = l->data;
536
537                 account_chooser_account_add_foreach (account, chooser);
538
539                 tp_g_signal_connect_object (account, "status-changed",
540                                              G_CALLBACK (account_chooser_status_changed_cb),
541                                              chooser, 0);
542         }
543
544         g_list_free (accounts);
545
546         priv->ready = TRUE;
547         g_signal_emit (chooser, signals[READY], 0);
548 }
549
550 static gint
551 account_cmp (GtkTreeModel *model,
552              GtkTreeIter *a,
553              GtkTreeIter *b,
554              gpointer user_data)
555 {
556         RowType a_type, b_type;
557         gboolean a_enabled, b_enabled;
558         gchar *a_text, *b_text;
559         gint result;
560
561         gtk_tree_model_get (model, a,
562                 COL_ACCOUNT_ENABLED, &a_enabled,
563                 COL_ACCOUNT_ROW_TYPE, &a_type,
564                 -1);
565         gtk_tree_model_get (model, b,
566                 COL_ACCOUNT_ENABLED, &b_enabled,
567                 COL_ACCOUNT_ROW_TYPE, &b_type,
568                 -1);
569
570         /* This assumes that we have at most one of each special row type. */
571         if (a_type != b_type) {
572                 /* Display higher-numbered special row types first. */
573                 return (b_type - a_type);
574         }
575
576         /* Enabled accounts are displayed first */
577         if (a_enabled != b_enabled)
578                 return a_enabled ? -1: 1;
579
580         gtk_tree_model_get (model, a, COL_ACCOUNT_TEXT, &a_text, -1);
581         gtk_tree_model_get (model, b, COL_ACCOUNT_TEXT, &b_text, -1);
582
583         if (a_text == b_text)
584                 result = 0;
585         else if (a_text == NULL)
586                 result = 1;
587         else if (b_text == NULL)
588                 result = -1;
589         else
590                 result = g_ascii_strcasecmp (a_text, b_text);
591
592         g_free (a_text);
593         g_free (b_text);
594
595         return result;
596 }
597
598 static void
599 account_chooser_setup (EmpathyAccountChooser *chooser)
600 {
601         EmpathyAccountChooserPriv *priv;
602         GtkListStore             *store;
603         GtkCellRenderer          *renderer;
604         GtkComboBox              *combobox;
605
606         priv = GET_PRIV (chooser);
607
608         /* Set up combo box with new store */
609         combobox = GTK_COMBO_BOX (chooser);
610
611         gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
612
613         store = gtk_list_store_new (COL_ACCOUNT_COUNT,
614                                     G_TYPE_STRING,    /* Image */
615                                     G_TYPE_STRING,    /* Name */
616                                     G_TYPE_BOOLEAN,   /* Enabled */
617                                     G_TYPE_UINT,      /* Row type */
618                                     TP_TYPE_ACCOUNT);
619
620         gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
621                 account_cmp, chooser, NULL);
622         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
623                 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
624
625         gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
626
627         renderer = gtk_cell_renderer_pixbuf_new ();
628         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
629         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
630                                         "icon-name", COL_ACCOUNT_IMAGE,
631                                         "sensitive", COL_ACCOUNT_ENABLED,
632                                         NULL);
633         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
634
635         renderer = gtk_cell_renderer_text_new ();
636         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
637         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
638                                         "text", COL_ACCOUNT_TEXT,
639                                         "sensitive", COL_ACCOUNT_ENABLED,
640                                         NULL);
641
642         /* Populate accounts */
643         tp_account_manager_prepare_async (priv->manager, NULL,
644                                           account_manager_prepared_cb, chooser);
645
646         g_object_unref (store);
647 }
648
649 static void
650 account_chooser_account_validity_changed_cb (TpAccountManager      *manager,
651                                              TpAccount             *account,
652                                              gboolean               valid,
653                                              EmpathyAccountChooser *chooser)
654 {
655         if (valid) {
656                 account_chooser_account_add_foreach (account, chooser);
657         } else {
658                 account_chooser_account_remove_foreach (account, chooser);
659         }
660 }
661
662 static void
663 account_chooser_account_add_foreach (TpAccount             *account,
664                                      EmpathyAccountChooser *chooser)
665 {
666         GtkListStore *store;
667         GtkComboBox  *combobox;
668         GtkTreeIter   iter;
669         gint          position;
670
671         combobox = GTK_COMBO_BOX (chooser);
672         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
673
674         position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
675         gtk_list_store_insert_with_values (store, &iter, position,
676                                            COL_ACCOUNT_POINTER, account,
677                                            -1);
678         account_chooser_update_iter (chooser, &iter);
679 }
680
681 static void
682 account_chooser_account_removed_cb (TpAccountManager      *manager,
683                                     TpAccount             *account,
684                                     EmpathyAccountChooser *chooser)
685 {
686         account_chooser_account_remove_foreach (account, chooser);
687 }
688
689 typedef struct {
690         TpAccount   *account;
691         GtkTreeIter *iter;
692         gboolean     found;
693 } FindAccountData;
694
695 static gboolean
696 account_chooser_find_account_foreach (GtkTreeModel *model,
697                                       GtkTreePath  *path,
698                                       GtkTreeIter  *iter,
699                                       gpointer      user_data)
700 {
701         FindAccountData *data = user_data;
702         TpAccount  *account;
703
704         gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
705
706         if (account == data->account) {
707                 data->found = TRUE;
708                 *(data->iter) = *iter;
709                 g_object_unref (account);
710
711                 return TRUE;
712         }
713
714         g_object_unref (account);
715
716         return FALSE;
717 }
718
719 static gboolean
720 account_chooser_find_account (EmpathyAccountChooser *chooser,
721                               TpAccount             *account,
722                               GtkTreeIter           *iter)
723 {
724         GtkListStore    *store;
725         GtkComboBox     *combobox;
726         FindAccountData  data;
727
728         combobox = GTK_COMBO_BOX (chooser);
729         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
730
731         data.account = account;
732         data.iter = iter;
733         gtk_tree_model_foreach (GTK_TREE_MODEL (store),
734                                 account_chooser_find_account_foreach,
735                                 &data);
736
737         return data.found;
738 }
739
740 static void
741 account_chooser_account_remove_foreach (TpAccount             *account,
742                                         EmpathyAccountChooser *chooser)
743 {
744         GtkListStore *store;
745         GtkComboBox  *combobox;
746         GtkTreeIter   iter;
747
748         combobox = GTK_COMBO_BOX (chooser);
749         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
750
751         if (account_chooser_find_account (chooser, account, &iter)) {
752                 gtk_list_store_remove (store, &iter);
753         }
754 }
755
756 static void
757 account_chooser_filter_ready_cb (gboolean is_enabled,
758                                  gpointer data)
759 {
760         FilterResultCallbackData  *fr_data = data;
761         EmpathyAccountChooser     *chooser;
762         EmpathyAccountChooserPriv *priv;
763         TpAccount                 *account;
764         GtkTreeIter               *iter;
765         GtkListStore              *store;
766         GtkComboBox               *combobox;
767         const gchar               *icon_name;
768
769         chooser = fr_data->chooser;
770         priv = GET_PRIV (chooser);
771         account = fr_data->account;
772         iter = fr_data->iter;
773         combobox = GTK_COMBO_BOX (chooser);
774         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
775
776         icon_name = tp_account_get_icon_name (account);
777
778         gtk_list_store_set (store, iter,
779                             COL_ACCOUNT_IMAGE, icon_name,
780                             COL_ACCOUNT_TEXT, tp_account_get_display_name (account),
781                             COL_ACCOUNT_ENABLED, is_enabled,
782                             -1);
783
784         /* set first connected account as active account */
785         if (priv->account_manually_set == FALSE &&
786             priv->set_active_item == FALSE && is_enabled) {
787                 priv->set_active_item = TRUE;
788                 gtk_combo_box_set_active_iter (combobox, iter);
789         }
790
791         g_object_unref (account);
792         filter_result_callback_data_free (fr_data);
793 }
794
795 static void
796 account_chooser_update_iter (EmpathyAccountChooser *chooser,
797                              GtkTreeIter           *iter)
798 {
799         EmpathyAccountChooserPriv *priv;
800         GtkListStore              *store;
801         GtkComboBox               *combobox;
802         TpAccount                 *account;
803         FilterResultCallbackData  *data;
804
805         priv = GET_PRIV (chooser);
806
807         combobox = GTK_COMBO_BOX (chooser);
808         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
809
810         gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
811                             COL_ACCOUNT_POINTER, &account,
812                             -1);
813
814         /* Skip rows without account associated */
815         if (account == NULL)
816                 return;
817
818         data = filter_result_callback_data_new (chooser, account, iter);
819
820         if (priv->filter)
821                 priv->filter (account, account_chooser_filter_ready_cb,
822                               (gpointer) data, priv->filter_data);
823         else
824                 account_chooser_filter_ready_cb (TRUE, (gpointer) data);
825 }
826
827 static void
828 account_chooser_status_changed_cb (TpAccount  *account,
829                                    guint       old_status,
830                                    guint       new_status,
831                                    guint       reason,
832                                    gchar      *dbus_error_name,
833                                    GHashTable *details,
834                                    gpointer    user_data)
835 {
836         EmpathyAccountChooser *chooser = user_data;
837         GtkTreeIter iter;
838
839         if (account_chooser_find_account (chooser, account, &iter)) {
840                 account_chooser_update_iter (chooser, &iter);
841         }
842 }
843
844 static gboolean
845 account_chooser_separator_func (GtkTreeModel         *model,
846                                 GtkTreeIter          *iter,
847                                 EmpathyAccountChooser *chooser)
848 {
849         RowType row_type;
850
851         gtk_tree_model_get (model, iter, COL_ACCOUNT_ROW_TYPE, &row_type, -1);
852         return (row_type == ROW_SEPARATOR);
853 }
854
855 static gboolean
856 account_chooser_set_account_foreach (GtkTreeModel   *model,
857                                      GtkTreePath    *path,
858                                      GtkTreeIter    *iter,
859                                      SetAccountData *data)
860 {
861         TpAccount *account;
862         gboolean   equal;
863
864         gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
865
866         equal = (data->account == account);
867
868         if (account) {
869                 g_object_unref (account);
870         }
871
872         if (equal) {
873                 GtkComboBox *combobox;
874
875                 combobox = GTK_COMBO_BOX (data->chooser);
876                 gtk_combo_box_set_active_iter (combobox, iter);
877
878                 data->set = TRUE;
879         }
880
881         return equal;
882 }
883
884 static gboolean
885 account_chooser_filter_foreach (GtkTreeModel *model,
886                                 GtkTreePath  *path,
887                                 GtkTreeIter  *iter,
888                                 gpointer      chooser)
889 {
890         account_chooser_update_iter (chooser, iter);
891         return FALSE;
892 }
893
894 /**
895  * empathy_account_chooser_set_filter:
896  * @chooser: an #EmpathyAccountChooser
897  * @filter: a filter
898  * @user_data: data to pass to @filter, or %NULL
899  *
900  * Sets a filter on the @chooser so only accounts that are %TRUE in the eyes
901  * of the filter are visible in the @chooser.
902  */
903 void
904 empathy_account_chooser_set_filter (EmpathyAccountChooser           *chooser,
905                                     EmpathyAccountChooserFilterFunc  filter,
906                                     gpointer                         user_data)
907 {
908         EmpathyAccountChooserPriv *priv;
909         GtkTreeModel *model;
910
911         g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
912
913         priv = GET_PRIV (chooser);
914
915         priv->filter = filter;
916         priv->filter_data = user_data;
917
918         /* Refilter existing data */
919         priv->set_active_item = FALSE;
920         model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
921         gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
922 }
923
924 /**
925  * EmpathyAccountChooserFilterFunc:
926  * @account: a #TpAccount
927  * @user_data: user data, or %NULL
928  *
929  * A function which decides whether the account indicated by @account
930  * is visible.
931  *
932  * Return value: whether the account indicated by @account is visible.
933  */
934
935 /**
936  * empathy_account_chooser_filter_is_connected:
937  * @account: a #TpAccount
938  * @callback: an #EmpathyAccountChooserFilterResultCallback accepting the result
939  * @callback_data: data passed to the @callback
940  * @user_data: user data or %NULL
941  *
942  * A useful #EmpathyAccountChooserFilterFunc that one could pass into
943  * empathy_account_chooser_set_filter() and only show connected accounts.
944  *
945  * Returns (via the callback) TRUE is @account is connected
946  */
947 void
948 empathy_account_chooser_filter_is_connected (
949         TpAccount                                 *account,
950         EmpathyAccountChooserFilterResultCallback  callback,
951         gpointer                                   callback_data,
952         gpointer                                   user_data)
953 {
954         gboolean is_connected =
955                 tp_account_get_connection_status (account, NULL)
956                 == TP_CONNECTION_STATUS_CONNECTED;
957         callback (is_connected, callback_data);
958 }
959
960 gboolean
961 empathy_account_chooser_is_ready (EmpathyAccountChooser *self)
962 {
963         EmpathyAccountChooserPriv *priv = GET_PRIV (self);
964
965         return priv->ready;
966 }
967
968 TpAccount *
969 empathy_account_chooser_get_account (EmpathyAccountChooser *chooser)
970 {
971         TpAccount *account;
972
973         account = empathy_account_chooser_dup_account (chooser);
974         if (account == NULL)
975                 return NULL;
976
977         g_object_unref (account);
978
979         return account;
980 }