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