]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-chooser.c
Updated Polish translation
[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         data.set = FALSE;
432
433         gtk_tree_model_foreach (model,
434                                 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
435                                 &data);
436
437         priv->account_manually_set = data.set;
438
439         return data.set;
440 }
441
442 void
443 empathy_account_chooser_set_all (EmpathyAccountChooser *chooser)
444 {
445         EmpathyAccountChooserPriv *priv;
446         GtkComboBox    *combobox;
447         GtkTreeModel   *model;
448         GtkTreeIter     iter;
449
450         g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
451
452         priv = GET_PRIV (chooser);
453
454         g_return_if_fail (priv->has_all_option);
455
456         combobox = GTK_COMBO_BOX (chooser);
457         model = gtk_combo_box_get_model (combobox);
458
459         if (gtk_tree_model_get_iter_first (model, &iter)) {
460                 /* 'All accounts' is the first row */
461                 gtk_combo_box_set_active_iter (combobox, &iter);
462                 priv->account_manually_set = TRUE;
463         }
464 }
465
466 /**
467  * empathy_account_chooser_get_has_all_option:
468  * @chooser: an #EmpathyAccountChooser
469  *
470  * Returns whether @chooser has the #EmpathyAccountChooser:has-all-option property
471  * set to true.
472  *
473  * Return value: whether @chooser has the #EmpathyAccountChooser:has-all-option property
474  * enabled.
475  */
476 gboolean
477 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
478 {
479         EmpathyAccountChooserPriv *priv;
480
481         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
482
483         priv = GET_PRIV (chooser);
484
485         return priv->has_all_option;
486 }
487
488 /**
489  * empathy_account_chooser_set_has_all_option:
490  * @chooser: an #EmpathyAccountChooser
491  * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option property
492  *
493  * Sets the #EmpathyAccountChooser:has-all-option property.
494  */
495 void
496 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
497                                            gboolean              has_all_option)
498 {
499         EmpathyAccountChooserPriv *priv;
500         GtkComboBox              *combobox;
501         GtkListStore             *store;
502         GtkTreeModel             *model;
503         GtkTreeIter               iter;
504
505         g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
506
507         priv = GET_PRIV (chooser);
508
509         if (priv->has_all_option == has_all_option) {
510                 return;
511         }
512
513         combobox = GTK_COMBO_BOX (chooser);
514         model = gtk_combo_box_get_model (combobox);
515         store = GTK_LIST_STORE (model);
516
517         priv->has_all_option = has_all_option;
518
519         /*
520          * The first 2 options are the ALL and separator
521          */
522
523         if (has_all_option) {
524                 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
525                                                       (GtkTreeViewRowSeparatorFunc)
526                                                       account_chooser_separator_func,
527                                                       chooser,
528                                                       NULL);
529
530                 gtk_list_store_prepend (store, &iter);
531                 gtk_list_store_set (store, &iter,
532                                     COL_ACCOUNT_TEXT, NULL,
533                                     COL_ACCOUNT_ENABLED, TRUE,
534                                     COL_ACCOUNT_POINTER, NULL,
535                                     COL_ACCOUNT_ROW_TYPE, ROW_SEPARATOR,
536                                     -1);
537
538                 gtk_list_store_prepend (store, &iter);
539                 gtk_list_store_set (store, &iter,
540                                     COL_ACCOUNT_TEXT, _("All accounts"),
541                                     COL_ACCOUNT_ENABLED, TRUE,
542                                     COL_ACCOUNT_POINTER, NULL,
543                                     COL_ACCOUNT_ROW_TYPE, ROW_ALL,
544                                     -1);
545         } else {
546                 if (gtk_tree_model_get_iter_first (model, &iter)) {
547                         if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
548                                 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
549                         }
550                 }
551
552                 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
553                                                       (GtkTreeViewRowSeparatorFunc)
554                                                       NULL,
555                                                       NULL,
556                                                       NULL);
557         }
558
559         g_object_notify (G_OBJECT (chooser), "has-all-option");
560 }
561
562 static void
563 account_manager_prepared_cb (GObject *source_object,
564                              GAsyncResult *result,
565                              gpointer user_data)
566 {
567         GList *accounts, *l;
568         TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
569         EmpathyAccountChooser *chooser = user_data;
570         EmpathyAccountChooserPriv *priv = GET_PRIV (chooser);
571         GError *error = NULL;
572
573         if (!tp_account_manager_prepare_finish (manager, result, &error)) {
574                 DEBUG ("Failed to prepare account manager: %s", error->message);
575                 g_error_free (error);
576                 return;
577         }
578
579         accounts = tp_account_manager_get_valid_accounts (manager);
580
581         for (l = accounts; l != NULL; l = l->next) {
582                 TpAccount *account = l->data;
583
584                 account_chooser_account_add_foreach (account, chooser);
585
586                 tp_g_signal_connect_object (account, "status-changed",
587                                              G_CALLBACK (account_chooser_status_changed_cb),
588                                              chooser, 0);
589         }
590
591         g_list_free (accounts);
592
593         priv->ready = TRUE;
594         g_signal_emit (chooser, signals[READY], 0);
595 }
596
597 static gint
598 account_cmp (GtkTreeModel *model,
599              GtkTreeIter *a,
600              GtkTreeIter *b,
601              gpointer user_data)
602 {
603         RowType a_type, b_type;
604         gboolean a_enabled, b_enabled;
605         gchar *a_text, *b_text;
606         gint result;
607
608         gtk_tree_model_get (model, a,
609                 COL_ACCOUNT_ENABLED, &a_enabled,
610                 COL_ACCOUNT_ROW_TYPE, &a_type,
611                 -1);
612         gtk_tree_model_get (model, b,
613                 COL_ACCOUNT_ENABLED, &b_enabled,
614                 COL_ACCOUNT_ROW_TYPE, &b_type,
615                 -1);
616
617         /* This assumes that we have at most one of each special row type. */
618         if (a_type != b_type) {
619                 /* Display higher-numbered special row types first. */
620                 return (b_type - a_type);
621         }
622
623         /* Enabled accounts are displayed first */
624         if (a_enabled != b_enabled)
625                 return a_enabled ? -1: 1;
626
627         gtk_tree_model_get (model, a, COL_ACCOUNT_TEXT, &a_text, -1);
628         gtk_tree_model_get (model, b, COL_ACCOUNT_TEXT, &b_text, -1);
629
630         if (a_text == b_text)
631                 result = 0;
632         else if (a_text == NULL)
633                 result = 1;
634         else if (b_text == NULL)
635                 result = -1;
636         else
637                 result = g_ascii_strcasecmp (a_text, b_text);
638
639         g_free (a_text);
640         g_free (b_text);
641
642         return result;
643 }
644
645 static void
646 account_chooser_setup (EmpathyAccountChooser *chooser)
647 {
648         EmpathyAccountChooserPriv *priv;
649         GtkListStore             *store;
650         GtkCellRenderer          *renderer;
651         GtkComboBox              *combobox;
652
653         priv = GET_PRIV (chooser);
654
655         /* Set up combo box with new store */
656         combobox = GTK_COMBO_BOX (chooser);
657
658         gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
659
660         store = gtk_list_store_new (COL_ACCOUNT_COUNT,
661                                     GDK_TYPE_PIXBUF,    /* Image */
662                                     G_TYPE_STRING,    /* Name */
663                                     G_TYPE_BOOLEAN,   /* Enabled */
664                                     G_TYPE_UINT,      /* Row type */
665                                     TP_TYPE_ACCOUNT);
666
667         gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
668                 account_cmp, chooser, NULL);
669         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
670                 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
671
672         gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
673
674         renderer = gtk_cell_renderer_pixbuf_new ();
675         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
676         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
677                                         "pixbuf", COL_ACCOUNT_IMAGE,
678                                         "sensitive", COL_ACCOUNT_ENABLED,
679                                         NULL);
680
681         renderer = gtk_cell_renderer_text_new ();
682         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
683         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
684                                         "text", COL_ACCOUNT_TEXT,
685                                         "sensitive", COL_ACCOUNT_ENABLED,
686                                         NULL);
687
688         /* Populate accounts */
689         tp_account_manager_prepare_async (priv->manager, NULL,
690                                           account_manager_prepared_cb, chooser);
691
692         g_object_unref (store);
693 }
694
695 static void
696 account_chooser_account_validity_changed_cb (TpAccountManager      *manager,
697                                              TpAccount             *account,
698                                              gboolean               valid,
699                                              EmpathyAccountChooser *chooser)
700 {
701         if (valid) {
702                 account_chooser_account_add_foreach (account, chooser);
703         } else {
704                 account_chooser_account_remove_foreach (account, chooser);
705         }
706 }
707
708 static void
709 account_chooser_account_add_foreach (TpAccount             *account,
710                                      EmpathyAccountChooser *chooser)
711 {
712         GtkListStore *store;
713         GtkComboBox  *combobox;
714         GtkTreeIter   iter;
715         gint          position;
716
717         combobox = GTK_COMBO_BOX (chooser);
718         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
719
720         position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
721         gtk_list_store_insert_with_values (store, &iter, position,
722                                            COL_ACCOUNT_POINTER, account,
723                                            -1);
724         account_chooser_update_iter (chooser, &iter);
725 }
726
727 static void
728 account_chooser_account_removed_cb (TpAccountManager      *manager,
729                                     TpAccount             *account,
730                                     EmpathyAccountChooser *chooser)
731 {
732         account_chooser_account_remove_foreach (account, chooser);
733 }
734
735 typedef struct {
736         TpAccount   *account;
737         GtkTreeIter *iter;
738         gboolean     found;
739 } FindAccountData;
740
741 static gboolean
742 account_chooser_find_account_foreach (GtkTreeModel *model,
743                                       GtkTreePath  *path,
744                                       GtkTreeIter  *iter,
745                                       gpointer      user_data)
746 {
747         FindAccountData *data = user_data;
748         TpAccount  *account;
749         RowType type;
750
751         gtk_tree_model_get (model, iter,
752                 COL_ACCOUNT_POINTER, &account,
753                 COL_ACCOUNT_ROW_TYPE, &type,
754                  -1);
755
756         if (type != ROW_ACCOUNT)
757                 return FALSE;
758
759         if (account == data->account) {
760                 data->found = TRUE;
761                 *(data->iter) = *iter;
762                 g_object_unref (account);
763
764                 return TRUE;
765         }
766
767         g_object_unref (account);
768
769         return FALSE;
770 }
771
772 static gboolean
773 account_chooser_find_account (EmpathyAccountChooser *chooser,
774                               TpAccount             *account,
775                               GtkTreeIter           *iter)
776 {
777         GtkListStore    *store;
778         GtkComboBox     *combobox;
779         FindAccountData  data;
780
781         combobox = GTK_COMBO_BOX (chooser);
782         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
783
784         data.account = account;
785         data.iter = iter;
786         gtk_tree_model_foreach (GTK_TREE_MODEL (store),
787                                 account_chooser_find_account_foreach,
788                                 &data);
789
790         return data.found;
791 }
792
793 static void
794 account_chooser_account_remove_foreach (TpAccount             *account,
795                                         EmpathyAccountChooser *chooser)
796 {
797         GtkListStore *store;
798         GtkComboBox  *combobox;
799         GtkTreeIter   iter;
800
801         combobox = GTK_COMBO_BOX (chooser);
802         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
803
804         if (account_chooser_find_account (chooser, account, &iter)) {
805                 gtk_list_store_remove (store, &iter);
806         }
807 }
808
809 static void
810 account_chooser_filter_ready_cb (gboolean is_enabled,
811                                  gpointer data)
812 {
813         FilterResultCallbackData  *fr_data = data;
814         EmpathyAccountChooser     *chooser;
815         EmpathyAccountChooserPriv *priv;
816         TpAccount                 *account;
817         GtkTreeIter               *iter;
818         GtkListStore              *store;
819         GtkComboBox               *combobox;
820         const gchar               *icon_name;
821         GdkPixbuf                 *pixbuf;
822
823         chooser = fr_data->chooser;
824         priv = GET_PRIV (chooser);
825         account = fr_data->account;
826         iter = fr_data->iter;
827         combobox = GTK_COMBO_BOX (chooser);
828         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
829
830         icon_name = tp_account_get_icon_name (account);
831         pixbuf = empathy_pixbuf_from_icon_name (icon_name,
832                 GTK_ICON_SIZE_BUTTON);
833
834         gtk_list_store_set (store, iter,
835                             COL_ACCOUNT_IMAGE, pixbuf,
836                             COL_ACCOUNT_TEXT, tp_account_get_display_name (account),
837                             COL_ACCOUNT_ENABLED, is_enabled,
838                             -1);
839
840         if (pixbuf != NULL)
841                 g_object_unref (pixbuf);
842
843         /* set first connected account as active account */
844         if (priv->account_manually_set == FALSE &&
845             priv->set_active_item == FALSE && is_enabled) {
846                 priv->set_active_item = TRUE;
847                 gtk_combo_box_set_active_iter (combobox, iter);
848         }
849
850         filter_result_callback_data_free (fr_data);
851 }
852
853 static void
854 account_chooser_update_iter (EmpathyAccountChooser *chooser,
855                              GtkTreeIter           *iter)
856 {
857         EmpathyAccountChooserPriv *priv;
858         GtkListStore              *store;
859         GtkComboBox               *combobox;
860         TpAccount                 *account;
861         FilterResultCallbackData  *data;
862
863         priv = GET_PRIV (chooser);
864
865         combobox = GTK_COMBO_BOX (chooser);
866         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
867
868         gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
869                             COL_ACCOUNT_POINTER, &account,
870                             -1);
871
872         /* Skip rows without account associated */
873         if (account == NULL)
874                 return;
875
876         data = filter_result_callback_data_new (chooser, account, iter);
877
878         if (priv->filter)
879                 priv->filter (account, account_chooser_filter_ready_cb,
880                               (gpointer) data, priv->filter_data);
881         else
882                 account_chooser_filter_ready_cb (TRUE, (gpointer) data);
883
884         g_object_unref (account);
885 }
886
887 static void
888 account_chooser_status_changed_cb (TpAccount  *account,
889                                    guint       old_status,
890                                    guint       new_status,
891                                    guint       reason,
892                                    gchar      *dbus_error_name,
893                                    GHashTable *details,
894                                    gpointer    user_data)
895 {
896         EmpathyAccountChooser *chooser = user_data;
897         GtkTreeIter iter;
898
899         if (account_chooser_find_account (chooser, account, &iter)) {
900                 account_chooser_update_iter (chooser, &iter);
901         }
902 }
903
904 static gboolean
905 account_chooser_separator_func (GtkTreeModel         *model,
906                                 GtkTreeIter          *iter,
907                                 EmpathyAccountChooser *chooser)
908 {
909         RowType row_type;
910
911         gtk_tree_model_get (model, iter, COL_ACCOUNT_ROW_TYPE, &row_type, -1);
912         return (row_type == ROW_SEPARATOR);
913 }
914
915 static gboolean
916 account_chooser_set_account_foreach (GtkTreeModel   *model,
917                                      GtkTreePath    *path,
918                                      GtkTreeIter    *iter,
919                                      SetAccountData *data)
920 {
921         TpAccount *account;
922         gboolean   equal;
923
924         gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
925
926         equal = (data->account == account);
927
928         if (account) {
929                 g_object_unref (account);
930         }
931
932         if (equal) {
933                 GtkComboBox *combobox;
934
935                 combobox = GTK_COMBO_BOX (data->chooser);
936                 gtk_combo_box_set_active_iter (combobox, iter);
937
938                 data->set = TRUE;
939         }
940
941         return equal;
942 }
943
944 static gboolean
945 account_chooser_filter_foreach (GtkTreeModel *model,
946                                 GtkTreePath  *path,
947                                 GtkTreeIter  *iter,
948                                 gpointer      chooser)
949 {
950         account_chooser_update_iter (chooser, iter);
951         return FALSE;
952 }
953
954 /**
955  * empathy_account_chooser_set_filter:
956  * @chooser: an #EmpathyAccountChooser
957  * @filter: a filter
958  * @user_data: data to pass to @filter, or %NULL
959  *
960  * Sets a filter on the @chooser so only accounts that are %TRUE in the eyes
961  * of the filter are visible in the @chooser.
962  */
963 void
964 empathy_account_chooser_set_filter (EmpathyAccountChooser           *chooser,
965                                     EmpathyAccountChooserFilterFunc  filter,
966                                     gpointer                         user_data)
967 {
968         EmpathyAccountChooserPriv *priv;
969         GtkTreeModel *model;
970
971         g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
972
973         priv = GET_PRIV (chooser);
974
975         priv->filter = filter;
976         priv->filter_data = user_data;
977
978         /* Refilter existing data */
979         priv->set_active_item = FALSE;
980         model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
981         gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
982 }
983
984 /**
985  * EmpathyAccountChooserFilterFunc:
986  * @account: a #TpAccount
987  * @user_data: user data, or %NULL
988  *
989  * A function which decides whether the account indicated by @account
990  * is visible.
991  *
992  * Return value: whether the account indicated by @account is visible.
993  */
994
995 /**
996  * empathy_account_chooser_filter_is_connected:
997  * @account: a #TpAccount
998  * @callback: an #EmpathyAccountChooserFilterResultCallback accepting the result
999  * @callback_data: data passed to the @callback
1000  * @user_data: user data or %NULL
1001  *
1002  * A useful #EmpathyAccountChooserFilterFunc that one could pass into
1003  * empathy_account_chooser_set_filter() and only show connected accounts.
1004  *
1005  * Returns (via the callback) TRUE is @account is connected
1006  */
1007 void
1008 empathy_account_chooser_filter_is_connected (
1009         TpAccount                                 *account,
1010         EmpathyAccountChooserFilterResultCallback  callback,
1011         gpointer                                   callback_data,
1012         gpointer                                   user_data)
1013 {
1014         gboolean is_connected =
1015                 tp_account_get_connection_status (account, NULL)
1016                 == TP_CONNECTION_STATUS_CONNECTED;
1017         callback (is_connected, callback_data);
1018 }
1019
1020 /**
1021  * empathy_account_chooser_filter_supports_multichat:
1022  * @account: a #TpAccount
1023  * @callback: an #EmpathyAccountChooserFilterResultCallback accepting the result
1024  * @callback_data: data passed to the @callback
1025  * @user_data: user data or %NULL
1026  *
1027  * An #EmpathyAccountChooserFilterFunc that returns accounts that both
1028  * support multiuser text chat and are connected.
1029  *
1030  * Returns (via the callback) TRUE if @account both supports muc and is connected
1031  */
1032 void
1033 empathy_account_chooser_filter_supports_chatrooms (
1034         TpAccount                                 *account,
1035         EmpathyAccountChooserFilterResultCallback  callback,
1036         gpointer                                   callback_data,
1037         gpointer                                   user_data)
1038 {
1039         TpConnection       *connection;
1040         gboolean           supported = FALSE;
1041         TpCapabilities     *caps;
1042
1043         /* check if CM supports multiuser text chat */
1044         connection = tp_account_get_connection (account);
1045         if (connection == NULL)
1046                 goto out;
1047
1048         caps = tp_connection_get_capabilities (connection);
1049         if (caps == NULL)
1050                 goto out;
1051
1052         supported = tp_capabilities_supports_text_chatrooms (caps);
1053
1054 out:
1055         callback (supported, callback_data);
1056 }
1057
1058 gboolean
1059 empathy_account_chooser_is_ready (EmpathyAccountChooser *self)
1060 {
1061         EmpathyAccountChooserPriv *priv = GET_PRIV (self);
1062
1063         return priv->ready;
1064 }
1065
1066 TpAccount *
1067 empathy_account_chooser_get_account (EmpathyAccountChooser *chooser)
1068 {
1069         TpAccount *account;
1070
1071         account = empathy_account_chooser_dup_account (chooser);
1072         if (account == NULL)
1073                 return NULL;
1074
1075         g_object_unref (account);
1076
1077         return account;
1078 }
1079
1080 TpAccountManager *
1081 empathy_account_chooser_get_account_manager (EmpathyAccountChooser *self)
1082 {
1083         EmpathyAccountChooserPriv *priv = GET_PRIV (self);
1084
1085         return priv->manager;
1086 }