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