]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-chooser.c
Merge branch 'master' into tp-tube
[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 with g_object_unref() 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 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         McAccount                 *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_manager_get_connection (priv->manager, 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 #McAccount
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                                      McAccount             *account)
324 {
325         GtkComboBox    *combobox;
326         GtkTreeModel   *model;
327         GtkTreeIter     iter;
328         SetAccountData  data;
329
330         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
331
332         combobox = GTK_COMBO_BOX (chooser);
333         model = gtk_combo_box_get_model (combobox);
334         gtk_combo_box_get_active_iter (combobox, &iter);
335
336         data.chooser = chooser;
337         data.account = account;
338
339         gtk_tree_model_foreach (model,
340                                 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
341                                 &data);
342
343         return data.set;
344 }
345
346 /**
347  * empathy_account_chooser_get_has_all_option:
348  * @chooser: an #EmpathyAccountChooser
349  *
350  * Returns whether @chooser has the #EmpathyAccountChooser:has-all-option property
351  * set to true.
352  *
353  * Return value: whether @chooser has the #EmpathyAccountChooser:has-all-option property
354  * enabled.
355  */
356 gboolean
357 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
358 {
359         EmpathyAccountChooserPriv *priv;
360
361         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
362
363         priv = GET_PRIV (chooser);
364         
365         return priv->has_all_option;
366 }
367
368 /**
369  * empathy_account_chooser_set_has_all_option:
370  * @chooser: an #EmpathyAccountChooser
371  * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option property
372  *
373  * Sets the #EmpathyAccountChooser:has-all-option property.
374  */
375 void
376 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
377                                            gboolean              has_all_option)
378 {
379         EmpathyAccountChooserPriv *priv;
380         GtkComboBox              *combobox;
381         GtkListStore             *store;
382         GtkTreeModel             *model;
383         GtkTreeIter               iter;
384
385         g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
386
387         priv = GET_PRIV (chooser);
388
389         if (priv->has_all_option == has_all_option) {
390                 return;
391         }
392
393         combobox = GTK_COMBO_BOX (chooser);
394         model = gtk_combo_box_get_model (combobox);
395         store = GTK_LIST_STORE (model);
396
397         priv->has_all_option = has_all_option;
398
399         /*
400          * The first 2 options are the ALL and separator
401          */
402
403         if (has_all_option) {
404                 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser), 
405                                                       (GtkTreeViewRowSeparatorFunc)
406                                                       account_chooser_separator_func,
407                                                       chooser, 
408                                                       NULL);
409
410                 gtk_list_store_prepend (store, &iter);
411                 gtk_list_store_set (store, &iter, 
412                                     COL_ACCOUNT_TEXT, NULL,
413                                     COL_ACCOUNT_ENABLED, TRUE,
414                                     COL_ACCOUNT_POINTER, NULL,
415                                     -1);
416
417                 gtk_list_store_prepend (store, &iter);
418                 gtk_list_store_set (store, &iter, 
419                                     COL_ACCOUNT_TEXT, _("All"), 
420                                     COL_ACCOUNT_ENABLED, TRUE,
421                                     COL_ACCOUNT_POINTER, NULL,
422                                     -1);
423         } else {
424                 if (gtk_tree_model_get_iter_first (model, &iter)) {
425                         if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
426                                 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
427                         }
428                 }
429
430                 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser), 
431                                                       (GtkTreeViewRowSeparatorFunc)
432                                                       NULL,
433                                                       NULL, 
434                                                       NULL);
435         }
436
437         g_object_notify (G_OBJECT (chooser), "has-all-option");
438 }
439
440 static void
441 account_chooser_setup (EmpathyAccountChooser *chooser)
442 {
443         EmpathyAccountChooserPriv *priv;
444         GList                    *accounts;
445         GtkListStore             *store;
446         GtkCellRenderer          *renderer;
447         GtkComboBox              *combobox;
448
449         priv = GET_PRIV (chooser);
450
451         /* Set up combo box with new store */
452         combobox = GTK_COMBO_BOX (chooser);
453
454         gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
455
456         store = gtk_list_store_new (COL_ACCOUNT_COUNT,
457                                     G_TYPE_STRING,    /* Image */
458                                     G_TYPE_STRING,    /* Name */
459                                     G_TYPE_BOOLEAN,   /* Enabled */
460                                     MC_TYPE_ACCOUNT);
461
462         gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
463
464         renderer = gtk_cell_renderer_pixbuf_new ();
465         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
466         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
467                                         "icon-name", COL_ACCOUNT_IMAGE,
468                                         "sensitive", COL_ACCOUNT_ENABLED,
469                                         NULL);
470         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
471
472         renderer = gtk_cell_renderer_text_new ();
473         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
474         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
475                                         "text", COL_ACCOUNT_TEXT,
476                                         "sensitive", COL_ACCOUNT_ENABLED,
477                                         NULL);
478
479         /* Populate accounts */
480         accounts = mc_accounts_list ();
481         g_list_foreach (accounts,
482                         (GFunc) account_chooser_account_add_foreach,
483                         chooser);
484
485         mc_accounts_list_free (accounts);
486         g_object_unref (store);
487 }
488
489 static void
490 account_chooser_account_created_cb (EmpathyAccountManager *manager,
491                                     McAccount             *account,
492                                     EmpathyAccountChooser *chooser)
493 {
494         account_chooser_account_add_foreach (account, chooser);
495 }
496
497 static void
498 account_chooser_account_add_foreach (McAccount             *account,
499                                      EmpathyAccountChooser *chooser)
500 {
501         GtkListStore *store;
502         GtkComboBox  *combobox;
503         GtkTreeIter   iter;
504         gint          position;
505
506         combobox = GTK_COMBO_BOX (chooser);
507         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
508
509         position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
510         gtk_list_store_insert_with_values (store, &iter, position,
511                                            COL_ACCOUNT_POINTER, account,
512                                            -1);
513         account_chooser_update_iter (chooser, &iter);
514 }
515
516 static void
517 account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
518                                     McAccount             *account,
519                                     EmpathyAccountChooser *chooser)
520 {
521         account_chooser_account_remove_foreach (account, chooser);
522 }
523
524 typedef struct {
525         McAccount   *account;
526         GtkTreeIter *iter;
527         gboolean     found;
528 } FindAccountData;
529
530 static gboolean
531 account_chooser_find_account_foreach (GtkTreeModel *model,
532                                       GtkTreePath  *path,
533                                       GtkTreeIter  *iter,
534                                       gpointer      user_data)
535 {
536         FindAccountData *data = user_data;
537         McAccount       *account;
538
539         gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
540
541         if (empathy_account_equal (account, data->account)) {
542                 data->found = TRUE;
543                 *(data->iter) = *iter;
544                 g_object_unref (account);
545
546                 return TRUE;
547         }
548
549         g_object_unref (account);
550
551         return FALSE;
552 }
553
554 static gboolean
555 account_chooser_find_account (EmpathyAccountChooser *chooser,
556                               McAccount             *account,
557                               GtkTreeIter           *iter)
558 {
559         GtkListStore    *store;
560         GtkComboBox     *combobox;
561         FindAccountData  data;
562
563         combobox = GTK_COMBO_BOX (chooser);
564         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
565
566         data.account = account;
567         data.iter = iter;
568         gtk_tree_model_foreach (GTK_TREE_MODEL (store),
569                                 account_chooser_find_account_foreach,
570                                 &data);
571
572         return data.found;
573 }
574
575 static void
576 account_chooser_account_remove_foreach (McAccount            *account,
577                                         EmpathyAccountChooser *chooser)
578 {
579         GtkListStore *store;
580         GtkComboBox  *combobox;
581         GtkTreeIter   iter;
582
583         combobox = GTK_COMBO_BOX (chooser);
584         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
585
586         if (account_chooser_find_account (chooser, account, &iter)) {
587                 gtk_list_store_remove (store, &iter);
588         }
589 }
590
591 static void
592 account_chooser_update_iter (EmpathyAccountChooser *chooser,
593                              GtkTreeIter           *iter)
594 {
595         EmpathyAccountChooserPriv *priv;
596         GtkListStore              *store;
597         GtkComboBox               *combobox;
598         McAccount                 *account;
599         const gchar               *icon_name;
600         gboolean                   is_enabled = TRUE;
601
602         priv = GET_PRIV (chooser);
603
604         combobox = GTK_COMBO_BOX (chooser);
605         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
606
607         gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
608                             COL_ACCOUNT_POINTER, &account,
609                             -1);
610
611         icon_name = empathy_icon_name_from_account (account);
612         if (priv->filter) {
613                 is_enabled = priv->filter (account, priv->filter_data);
614         }
615
616         gtk_list_store_set (store, iter,
617                             COL_ACCOUNT_IMAGE, icon_name,
618                             COL_ACCOUNT_TEXT, mc_account_get_display_name (account),
619                             COL_ACCOUNT_ENABLED, is_enabled,
620                             -1);
621
622         /* set first connected account as active account */
623         if (priv->set_active_item == FALSE && is_enabled) {
624                 priv->set_active_item = TRUE;
625                 gtk_combo_box_set_active_iter (combobox, iter);
626         }
627
628         g_object_unref (account);
629 }
630
631 static void
632 account_chooser_connection_changed_cb (EmpathyAccountManager   *manager,
633                                        McAccount               *account,
634                                        TpConnectionStatusReason reason,
635                                        TpConnectionStatus       new_status,
636                                        TpConnectionStatus       old_status,
637                                        EmpathyAccountChooser   *chooser)
638 {
639         GtkTreeIter iter;
640
641         if (account_chooser_find_account (chooser, account, &iter)) {
642                 account_chooser_update_iter (chooser, &iter);
643         }
644 }
645
646 static gboolean
647 account_chooser_separator_func (GtkTreeModel         *model,
648                                 GtkTreeIter          *iter,
649                                 EmpathyAccountChooser *chooser)
650 {
651         EmpathyAccountChooserPriv *priv;
652         gchar                    *text;
653         gboolean                  is_separator;
654
655         priv = GET_PRIV (chooser);
656         
657         if (!priv->has_all_option) {
658                 return FALSE;
659         }
660         
661         gtk_tree_model_get (model, iter, COL_ACCOUNT_TEXT, &text, -1);
662         is_separator = text == NULL;
663         g_free (text);
664
665         return is_separator;
666 }
667
668 static gboolean
669 account_chooser_set_account_foreach (GtkTreeModel   *model,
670                                      GtkTreePath    *path,
671                                      GtkTreeIter    *iter,
672                                      SetAccountData *data)
673 {
674         McAccount *account;
675         gboolean   equal;
676
677         gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
678
679         /* Special case so we can make it possible to select the All option */
680         if ((data->account == NULL) != (account == NULL)) {
681                 equal = FALSE;
682         }
683         else if (data->account == account) {
684                 equal = TRUE;
685         } else {
686                 equal = empathy_account_equal (data->account, account);
687         }
688
689         if (account) {
690                 g_object_unref (account);
691         }
692
693         if (equal) {
694                 GtkComboBox *combobox;
695
696                 combobox = GTK_COMBO_BOX (data->chooser);
697                 gtk_combo_box_set_active_iter (combobox, iter);
698
699                 data->set = TRUE;
700         }
701
702         return equal;
703 }
704
705 static gboolean
706 account_chooser_filter_foreach (GtkTreeModel *model,
707                                 GtkTreePath  *path,
708                                 GtkTreeIter  *iter,
709                                 gpointer      chooser)
710 {
711         account_chooser_update_iter (chooser, iter);
712         return FALSE;
713 }
714
715 /**
716  * empathy_account_chooser_set_filter:
717  * @chooser: an #EmpathyAccountChooser
718  * @filter: a filter
719  * @user_data: data to pass to @filter, or %NULL
720  *
721  * Sets a filter on the @chooser so only accounts that are %TRUE in the eyes
722  * of the filter are visible in the @chooser.
723  */
724 void
725 empathy_account_chooser_set_filter (EmpathyAccountChooser           *chooser,
726                                     EmpathyAccountChooserFilterFunc  filter,
727                                     gpointer                         user_data)
728 {
729         EmpathyAccountChooserPriv *priv;
730         GtkTreeModel *model;
731
732         g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
733
734         priv = GET_PRIV (chooser);
735
736         priv->filter = filter;
737         priv->filter_data = user_data;
738
739         /* Refilter existing data */
740         priv->set_active_item = FALSE;
741         model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
742         gtk_tree_model_foreach (model, account_chooser_filter_foreach, chooser);
743 }
744
745 /**
746  * EmpathyAccountChooserFilterFunc:
747  * @account: an #McAccount
748  * @user_data: user data, or %NULL
749  *
750  * A function which decides whether the account indicated by @account
751  * is visible.
752  *
753  * Return value: whether the account indicated by @account is visible.
754  */
755
756 /**
757  * empathy_account_chooser_filter_is_connected:
758  * @account: an #McAccount
759  * @user_data: user data or %NULL
760  *
761  * A useful #EmpathyAccountChooserFilterFunc that one could pass into
762  * empathy_account_chooser_set_filter() and only show connected accounts.
763  *
764  * Return value: Whether @account is connected
765  */
766 gboolean
767 empathy_account_chooser_filter_is_connected (McAccount *account,
768                                              gpointer   user_data)
769 {
770         MissionControl     *mc;
771         TpConnectionStatus  status;
772
773         g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
774
775         mc = empathy_mission_control_dup_singleton ();
776         status = mission_control_get_connection_status (mc, account, NULL);
777         g_object_unref (mc);
778
779         return status == TP_CONNECTION_STATUS_CONNECTED;
780 }
781