]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-chooser.c
Merge commit 'upstream/master' into mc5
[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 <libempathy/empathy-account-manager.h>
33 #include <libempathy/empathy-utils.h>
34
35 #include "empathy-ui-utils.h"
36 #include "empathy-account-chooser.h"
37
38 /**
39  * SECTION:empathy-account-chooser
40  * @title:EmpathyAccountChooser
41  * @short_description: A widget used to choose from a list of accounts
42  * @include: libempathy-gtk/empathy-account-chooser.h
43  *
44  * #EmpathyAccountChooser is a widget which extends #GtkComboBox to provide
45  * a chooser of available accounts.
46  */
47
48 /**
49  * EmpathyAccountChooser:
50  * @parent: parent object
51  *
52  * Widget which extends #GtkComboBox to provide a chooser of available accounts.
53  */
54
55 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountChooser)
56 typedef struct {
57         EmpathyAccountManager          *manager;
58         gboolean                        set_active_item;
59         gboolean                        account_manually_set;
60         gboolean                        has_all_option;
61         EmpathyAccountChooserFilterFunc filter;
62         gpointer                        filter_data;
63 } EmpathyAccountChooserPriv;
64
65 typedef struct {
66         EmpathyAccountChooser *chooser;
67         EmpathyAccount        *account;
68         gboolean               set;
69 } SetAccountData;
70
71 enum {
72         COL_ACCOUNT_IMAGE,
73         COL_ACCOUNT_TEXT,
74         COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
75         COL_ACCOUNT_POINTER,
76         COL_ACCOUNT_COUNT
77 };
78
79 static void     account_chooser_finalize               (GObject                  *object);
80 static void     account_chooser_get_property           (GObject                  *object,
81                                                         guint                     param_id,
82                                                         GValue                   *value,
83                                                         GParamSpec               *pspec);
84 static void     account_chooser_set_property           (GObject                  *object,
85                                                         guint                     param_id,
86                                                         const GValue             *value,
87                                                         GParamSpec               *pspec);
88 static void     account_chooser_setup                  (EmpathyAccountChooser    *chooser);
89 static void     account_chooser_account_created_cb     (EmpathyAccountManager    *manager,
90                                                         EmpathyAccount           *account,
91                                                         EmpathyAccountChooser    *chooser);
92 static void     account_chooser_account_add_foreach    (EmpathyAccount                *account,
93                                                         EmpathyAccountChooser    *chooser);
94 static void     account_chooser_account_deleted_cb     (EmpathyAccountManager    *manager,
95                                                         EmpathyAccount           *account,
96                                                         EmpathyAccountChooser    *chooser);
97 static void     account_chooser_account_remove_foreach (EmpathyAccount                *account,
98                                                         EmpathyAccountChooser    *chooser);
99 static void     account_chooser_update_iter            (EmpathyAccountChooser    *chooser,
100                                                         GtkTreeIter              *iter);
101 static void     account_chooser_connection_changed_cb  (EmpathyAccountManager    *manager,
102                                                         EmpathyAccount                *account,
103                                                         TpConnectionStatusReason  reason,
104                                                         TpConnectionStatus        new_status,
105                                                         TpConnectionStatus        old_status,
106                                                         EmpathyAccountChooser    *chooser);
107 static gboolean account_chooser_separator_func         (GtkTreeModel             *model,
108                                                         GtkTreeIter              *iter,
109                                                         EmpathyAccountChooser    *chooser);
110 static gboolean account_chooser_set_account_foreach    (GtkTreeModel             *model,
111                                                         GtkTreePath              *path,
112                                                         GtkTreeIter              *iter,
113                                                         SetAccountData           *data);
114
115 enum {
116         PROP_0,
117         PROP_HAS_ALL_OPTION,
118 };
119
120 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
121
122 static void
123 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
124 {
125         GObjectClass *object_class = G_OBJECT_CLASS (klass);
126
127         object_class->finalize = account_chooser_finalize;
128         object_class->get_property = account_chooser_get_property;
129         object_class->set_property = account_chooser_set_property;
130
131         /**
132          * EmpathyAccountChooser:has-all-option:
133          *
134          * Have an additional option in the list to mean all accounts.
135          */
136         g_object_class_install_property (object_class,
137                                          PROP_HAS_ALL_OPTION,
138                                          g_param_spec_boolean ("has-all-option",
139                                                                "Has All Option",
140                                                                "Have a separate option in the list to mean ALL accounts",
141                                                                FALSE,
142                                                                G_PARAM_READWRITE));
143
144         g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
145 }
146
147 static void
148 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
149 {
150         EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
151                 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
152
153         chooser->priv = priv;
154         priv->set_active_item = FALSE;
155         priv->account_manually_set = FALSE;
156         priv->filter = NULL;
157         priv->filter_data = NULL;
158
159         priv->manager = empathy_account_manager_dup_singleton ();
160
161         g_signal_connect (priv->manager, "account-created",
162                           G_CALLBACK (account_chooser_account_created_cb),
163                           chooser);
164         g_signal_connect (priv->manager, "account-deleted",
165                           G_CALLBACK (account_chooser_account_deleted_cb),
166                           chooser);
167         g_signal_connect (priv->manager, "account-connection-changed",
168                           G_CALLBACK (account_chooser_connection_changed_cb),
169                           chooser);
170
171         account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
172 }
173
174 static void
175 account_chooser_finalize (GObject *object)
176 {
177         EmpathyAccountChooserPriv *priv = GET_PRIV (object);
178
179         g_signal_handlers_disconnect_by_func (priv->manager,
180                                               account_chooser_connection_changed_cb,
181                                               object);
182         g_signal_handlers_disconnect_by_func (priv->manager,
183                                               account_chooser_account_created_cb,
184                                               object);
185         g_signal_handlers_disconnect_by_func (priv->manager,
186                                               account_chooser_account_deleted_cb,
187                                               object);
188         g_object_unref (priv->manager);
189
190         G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
191 }
192
193 static void
194 account_chooser_get_property (GObject    *object,
195                               guint       param_id,
196                               GValue     *value,
197                               GParamSpec *pspec)
198 {
199         EmpathyAccountChooserPriv *priv;
200
201         priv = GET_PRIV (object);
202
203         switch (param_id) {
204         case PROP_HAS_ALL_OPTION:
205                 g_value_set_boolean (value, priv->has_all_option);
206                 break;
207         default:
208                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
209                 break;
210         };
211 }
212
213 static void
214 account_chooser_set_property (GObject      *object,
215                               guint         param_id,
216                               const GValue *value,
217                               GParamSpec   *pspec)
218 {
219         EmpathyAccountChooserPriv *priv;
220
221         priv = GET_PRIV (object);
222
223         switch (param_id) {
224         case PROP_HAS_ALL_OPTION:
225                 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
226                                                            g_value_get_boolean (value));
227                 break;
228         default:
229                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
230                 break;
231         };
232 }
233
234 /**
235  * empathy_account_chooser_new:
236  *
237  * Creates a new #EmpathyAccountChooser.
238  *
239  * Return value: A new #EmpathyAccountChooser
240  */
241 GtkWidget *
242 empathy_account_chooser_new (void)
243 {
244         GtkWidget                *chooser;
245
246         chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
247
248         return chooser;
249 }
250
251 /**
252  * empathy_account_chooser_dup_account:
253  * @chooser: an #EmpathyAccountChooser
254  *
255  * Returns the account which is currently selected in the chooser or %NULL
256  * if there is no account selected. The #EmpathyAccount returned should be
257  * unrefed with g_object_unref() when finished with.
258  *
259  * Return value: a new ref to the #EmpathyAccount currently selected, or %NULL.
260  */
261 EmpathyAccount *
262 empathy_account_chooser_dup_account (EmpathyAccountChooser *chooser)
263 {
264         EmpathyAccountChooserPriv *priv;
265         EmpathyAccount           *account;
266         GtkTreeModel             *model;
267         GtkTreeIter               iter;
268
269         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
270
271         priv = GET_PRIV (chooser);
272
273         model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
274         if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
275                 return NULL;
276         }
277
278         gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
279
280         return account;
281 }
282
283 /**
284  * empathy_account_chooser_get_connection:
285  * @chooser: an #EmpathyAccountChooser
286  *
287  * Returns a borrowed reference to the #TpConnection associated with the
288  * account currently selected. The caller must reference the returned object with
289  * g_object_ref() if it will be kept
290  *
291  * Return value: a borrowed reference to the #TpConnection associated with the
292  * account curently selected.
293  */
294 TpConnection *
295 empathy_account_chooser_get_connection (EmpathyAccountChooser *chooser)
296 {
297         EmpathyAccountChooserPriv *priv;
298         EmpathyAccount            *account;
299         TpConnection              *connection;
300
301         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
302
303         priv = GET_PRIV (chooser);
304
305         account = empathy_account_chooser_dup_account (chooser);
306         connection = empathy_account_get_connection (account);
307         g_object_unref (account);
308
309         return connection;
310 }
311
312 /**
313  * empathy_account_chooser_set_account:
314  * @chooser: an #EmpathyAccountChooser
315  * @account: an #EmpathyAccount
316  *
317  * Sets the currently selected account to @account, if it exists in the list.
318  *
319  * Return value: whether the chooser was set to @account.
320  */
321 gboolean
322 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
323                                      EmpathyAccount *account)
324 {
325         EmpathyAccountChooserPriv *priv;
326         GtkComboBox    *combobox;
327         GtkTreeModel   *model;
328         GtkTreeIter     iter;
329         SetAccountData  data;
330
331         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
332
333         priv = GET_PRIV (chooser);
334
335         combobox = GTK_COMBO_BOX (chooser);
336         model = gtk_combo_box_get_model (combobox);
337         gtk_combo_box_get_active_iter (combobox, &iter);
338
339         data.chooser = chooser;
340         data.account = account;
341
342         gtk_tree_model_foreach (model,
343                                 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
344                                 &data);
345
346         priv->account_manually_set = data.set;
347
348         return data.set;
349 }
350
351 /**
352  * empathy_account_chooser_get_has_all_option:
353  * @chooser: an #EmpathyAccountChooser
354  *
355  * Returns whether @chooser has the #EmpathyAccountChooser:has-all-option property
356  * set to true.
357  *
358  * Return value: whether @chooser has the #EmpathyAccountChooser:has-all-option property
359  * enabled.
360  */
361 gboolean
362 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
363 {
364         EmpathyAccountChooserPriv *priv;
365
366         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
367
368         priv = GET_PRIV (chooser);
369
370         return priv->has_all_option;
371 }
372
373 /**
374  * empathy_account_chooser_set_has_all_option:
375  * @chooser: an #EmpathyAccountChooser
376  * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option property
377  *
378  * Sets the #EmpathyAccountChooser:has-all-option property.
379  */
380 void
381 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
382                                            gboolean              has_all_option)
383 {
384         EmpathyAccountChooserPriv *priv;
385         GtkComboBox              *combobox;
386         GtkListStore             *store;
387         GtkTreeModel             *model;
388         GtkTreeIter               iter;
389
390         g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
391
392         priv = GET_PRIV (chooser);
393
394         if (priv->has_all_option == has_all_option) {
395                 return;
396         }
397
398         combobox = GTK_COMBO_BOX (chooser);
399         model = gtk_combo_box_get_model (combobox);
400         store = GTK_LIST_STORE (model);
401
402         priv->has_all_option = has_all_option;
403
404         /*
405          * The first 2 options are the ALL and separator
406          */
407
408         if (has_all_option) {
409                 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
410                                                       (GtkTreeViewRowSeparatorFunc)
411                                                       account_chooser_separator_func,
412                                                       chooser,
413                                                       NULL);
414
415                 gtk_list_store_prepend (store, &iter);
416                 gtk_list_store_set (store, &iter,
417                                     COL_ACCOUNT_TEXT, NULL,
418                                     COL_ACCOUNT_ENABLED, TRUE,
419                                     COL_ACCOUNT_POINTER, NULL,
420                                     -1);
421
422                 gtk_list_store_prepend (store, &iter);
423                 gtk_list_store_set (store, &iter,
424                                     COL_ACCOUNT_TEXT, _("All"),
425                                     COL_ACCOUNT_ENABLED, TRUE,
426                                     COL_ACCOUNT_POINTER, NULL,
427                                     -1);
428         } else {
429                 if (gtk_tree_model_get_iter_first (model, &iter)) {
430                         if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
431                                 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
432                         }
433                 }
434
435                 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
436                                                       (GtkTreeViewRowSeparatorFunc)
437                                                       NULL,
438                                                       NULL,
439                                                       NULL);
440         }
441
442         g_object_notify (G_OBJECT (chooser), "has-all-option");
443 }
444
445 static void
446 account_chooser_setup (EmpathyAccountChooser *chooser)
447 {
448         EmpathyAccountChooserPriv *priv;
449         GList                    *accounts;
450         GtkListStore             *store;
451         GtkCellRenderer          *renderer;
452         GtkComboBox              *combobox;
453
454         priv = GET_PRIV (chooser);
455
456         /* Set up combo box with new store */
457         combobox = GTK_COMBO_BOX (chooser);
458
459         gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
460
461         store = gtk_list_store_new (COL_ACCOUNT_COUNT,
462                                     G_TYPE_STRING,    /* Image */
463                                     G_TYPE_STRING,    /* Name */
464                                     G_TYPE_BOOLEAN,   /* Enabled */
465                                     EMPATHY_TYPE_ACCOUNT);
466
467         gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
468
469         renderer = gtk_cell_renderer_pixbuf_new ();
470         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
471         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
472                                         "icon-name", COL_ACCOUNT_IMAGE,
473                                         "sensitive", COL_ACCOUNT_ENABLED,
474                                         NULL);
475         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
476
477         renderer = gtk_cell_renderer_text_new ();
478         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
479         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
480                                         "text", COL_ACCOUNT_TEXT,
481                                         "sensitive", COL_ACCOUNT_ENABLED,
482                                         NULL);
483
484         /* Populate accounts */
485         accounts = empathy_account_manager_dup_accounts (priv->manager);
486         g_list_foreach (accounts,
487                         (GFunc) account_chooser_account_add_foreach,
488                         chooser);
489
490         g_list_free (accounts);
491         g_object_unref (store);
492 }
493
494 static void
495 account_chooser_account_created_cb (EmpathyAccountManager *manager,
496                                     EmpathyAccount        *account,
497                                     EmpathyAccountChooser *chooser)
498 {
499         account_chooser_account_add_foreach (account, chooser);
500 }
501
502 static void
503 account_chooser_account_add_foreach (EmpathyAccount        *account,
504                                      EmpathyAccountChooser *chooser)
505 {
506         GtkListStore *store;
507         GtkComboBox  *combobox;
508         GtkTreeIter   iter;
509         gint          position;
510
511         combobox = GTK_COMBO_BOX (chooser);
512         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
513
514         position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
515         gtk_list_store_insert_with_values (store, &iter, position,
516                                            COL_ACCOUNT_POINTER, account,
517                                            -1);
518         account_chooser_update_iter (chooser, &iter);
519         /* We got a reffed account and it was reffed by the liststore as well */
520         g_object_unref (account);
521 }
522
523 static void
524 account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
525                                     EmpathyAccount        *account,
526                                     EmpathyAccountChooser *chooser)
527 {
528         account_chooser_account_remove_foreach (account, chooser);
529 }
530
531 typedef struct {
532         EmpathyAccount   *account;
533         GtkTreeIter *iter;
534         gboolean     found;
535 } FindAccountData;
536
537 static gboolean
538 account_chooser_find_account_foreach (GtkTreeModel *model,
539                                       GtkTreePath  *path,
540                                       GtkTreeIter  *iter,
541                                       gpointer      user_data)
542 {
543         FindAccountData *data = user_data;
544         EmpathyAccount  *account;
545
546         gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
547
548         if (empathy_account_equal (account, data->account)) {
549                 data->found = TRUE;
550                 *(data->iter) = *iter;
551                 g_object_unref (account);
552
553                 return TRUE;
554         }
555
556         g_object_unref (account);
557
558         return FALSE;
559 }
560
561 static gboolean
562 account_chooser_find_account (EmpathyAccountChooser *chooser,
563                               EmpathyAccount        *account,
564                               GtkTreeIter           *iter)
565 {
566         GtkListStore    *store;
567         GtkComboBox     *combobox;
568         FindAccountData  data;
569
570         combobox = GTK_COMBO_BOX (chooser);
571         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
572
573         data.account = account;
574         data.iter = iter;
575         gtk_tree_model_foreach (GTK_TREE_MODEL (store),
576                                 account_chooser_find_account_foreach,
577                                 &data);
578
579         return data.found;
580 }
581
582 static void
583 account_chooser_account_remove_foreach (EmpathyAccount        *account,
584                                         EmpathyAccountChooser *chooser)
585 {
586         GtkListStore *store;
587         GtkComboBox  *combobox;
588         GtkTreeIter   iter;
589
590         combobox = GTK_COMBO_BOX (chooser);
591         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
592
593         if (account_chooser_find_account (chooser, account, &iter)) {
594                 gtk_list_store_remove (store, &iter);
595         }
596 }
597
598 static void
599 account_chooser_update_iter (EmpathyAccountChooser *chooser,
600                              GtkTreeIter           *iter)
601 {
602         EmpathyAccountChooserPriv *priv;
603         GtkListStore              *store;
604         GtkComboBox               *combobox;
605         EmpathyAccount            *account;
606         const gchar               *icon_name;
607         gboolean                   is_enabled = TRUE;
608
609         priv = GET_PRIV (chooser);
610
611         combobox = GTK_COMBO_BOX (chooser);
612         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
613
614         gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
615                             COL_ACCOUNT_POINTER, &account,
616                             -1);
617
618         icon_name = empathy_account_get_icon_name (account);
619         if (priv->filter) {
620                 is_enabled = priv->filter (account, priv->filter_data);
621         }
622
623         gtk_list_store_set (store, iter,
624                             COL_ACCOUNT_IMAGE, icon_name,
625                             COL_ACCOUNT_TEXT, empathy_account_get_display_name (account),
626                             COL_ACCOUNT_ENABLED, is_enabled,
627                             -1);
628
629         /* set first connected account as active account */
630         if (priv->account_manually_set == FALSE &&
631             priv->set_active_item == FALSE && is_enabled) {
632                 priv->set_active_item = TRUE;
633                 gtk_combo_box_set_active_iter (combobox, iter);
634         }
635
636         g_object_unref (account);
637 }
638
639 static void
640 account_chooser_connection_changed_cb (EmpathyAccountManager   *manager,
641                                        EmpathyAccount          *account,
642                                        TpConnectionStatusReason reason,
643                                        TpConnectionStatus       new_status,
644                                        TpConnectionStatus       old_status,
645                                        EmpathyAccountChooser   *chooser)
646 {
647         GtkTreeIter iter;
648
649         if (account_chooser_find_account (chooser, account, &iter)) {
650                 account_chooser_update_iter (chooser, &iter);
651         }
652 }
653
654 static gboolean
655 account_chooser_separator_func (GtkTreeModel         *model,
656                                 GtkTreeIter          *iter,
657                                 EmpathyAccountChooser *chooser)
658 {
659         EmpathyAccountChooserPriv *priv;
660         gchar                    *text;
661         gboolean                  is_separator;
662
663         priv = GET_PRIV (chooser);
664
665         if (!priv->has_all_option) {
666                 return FALSE;
667         }
668
669         gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
670         is_separator = text == NULL;
671         g_free (text);
672
673         return is_separator;
674 }
675
676 static gboolean
677 account_chooser_set_account_foreach (GtkTreeModel   *model,
678                                      GtkTreePath    *path,
679                                      GtkTreeIter    *iter,
680                                      SetAccountData *data)
681 {
682         EmpathyAccount *account;
683         gboolean   equal;
684
685         gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
686
687         /* Special case so we can make it possible to select the All option */
688         if ((data->account == NULL) != (account == NULL)) {
689                 equal = FALSE;
690         }
691         else if (data->account == account) {
692                 equal = TRUE;
693         } else {
694                 equal = empathy_account_equal (data->account, account);
695         }
696
697         if (account) {
698                 g_object_unref (account);
699         }
700
701         if (equal) {
702                 GtkComboBox *combobox;
703
704                 combobox = GTK_COMBO_BOX (data->chooser);
705                 gtk_combo_box_set_active_iter (combobox, iter);
706
707                 data->set = TRUE;
708         }
709
710         return equal;
711 }
712
713 static gboolean
714 account_chooser_filter_foreach (GtkTreeModel *model,
715                                 GtkTreePath  *path,
716                                 GtkTreeIter  *iter,
717                                 gpointer      chooser)
718 {
719         account_chooser_update_iter (chooser, iter);
720         return FALSE;
721 }
722
723 /**
724  * empathy_account_chooser_set_filter:
725  * @chooser: an #EmpathyAccountChooser
726  * @filter: a filter
727  * @user_data: data to pass to @filter, or %NULL
728  *
729  * Sets a filter on the @chooser so only accounts that are %TRUE in the eyes
730  * of the filter are visible in the @chooser.
731  */
732 void
733 empathy_account_chooser_set_filter (EmpathyAccountChooser           *chooser,
734                                     EmpathyAccountChooserFilterFunc  filter,
735                                     gpointer                         user_data)
736 {
737         EmpathyAccountChooserPriv *priv;
738         GtkTreeModel *model;
739
740         g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
741
742         priv = GET_PRIV (chooser);
743
744         priv->filter = filter;
745         priv->filter_data = user_data;
746
747         /* Refilter existing data */
748         priv->set_active_item = FALSE;
749         model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
750         gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
751 }
752
753 /**
754  * EmpathyAccountChooserFilterFunc:
755  * @account: an #EmpathyAccount
756  * @user_data: user data, or %NULL
757  *
758  * A function which decides whether the account indicated by @account
759  * is visible.
760  *
761  * Return value: whether the account indicated by @account is visible.
762  */
763
764 /**
765  * empathy_account_chooser_filter_is_connected:
766  * @account: an #EmpathyAccount
767  * @user_data: user data or %NULL
768  *
769  * A useful #EmpathyAccountChooserFilterFunc that one could pass into
770  * empathy_account_chooser_set_filter() and only show connected accounts.
771  *
772  * Return value: Whether @account is connected
773  */
774 gboolean
775 empathy_account_chooser_filter_is_connected (EmpathyAccount *account,
776                                              gpointer   user_data)
777 {
778         TpConnectionStatus  status;
779
780         g_object_get (account, "connection-status", &status, NULL);
781
782         return status == TP_CONNECTION_STATUS_CONNECTED;
783 }
784