]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-chooser.c
Get the connection status, not the presence status string
[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                        has_all_option;
60         EmpathyAccountChooserFilterFunc filter;
61         gpointer                        filter_data;
62 } EmpathyAccountChooserPriv;
63
64 typedef struct {
65         EmpathyAccountChooser *chooser;
66         EmpathyAccount        *account;
67         gboolean               set;
68 } SetAccountData;
69
70 enum {
71         COL_ACCOUNT_IMAGE,
72         COL_ACCOUNT_TEXT,
73         COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
74         COL_ACCOUNT_POINTER,
75         COL_ACCOUNT_COUNT
76 };
77
78 static void     account_chooser_finalize               (GObject                  *object);
79 static void     account_chooser_get_property           (GObject                  *object,
80                                                         guint                     param_id,
81                                                         GValue                   *value,
82                                                         GParamSpec               *pspec);
83 static void     account_chooser_set_property           (GObject                  *object,
84                                                         guint                     param_id,
85                                                         const GValue             *value,
86                                                         GParamSpec               *pspec);
87 static void     account_chooser_setup                  (EmpathyAccountChooser    *chooser);
88 static void     account_chooser_account_created_cb     (EmpathyAccountManager    *manager,
89                                                         EmpathyAccount           *account,
90                                                         EmpathyAccountChooser    *chooser);
91 static void     account_chooser_account_add_foreach    (EmpathyAccount                *account,
92                                                         EmpathyAccountChooser    *chooser);
93 static void     account_chooser_account_deleted_cb     (EmpathyAccountManager    *manager,
94                                                         EmpathyAccount           *account,
95                                                         EmpathyAccountChooser    *chooser);
96 static void     account_chooser_account_remove_foreach (EmpathyAccount                *account,
97                                                         EmpathyAccountChooser    *chooser);
98 static void     account_chooser_update_iter            (EmpathyAccountChooser    *chooser,
99                                                         GtkTreeIter              *iter);
100 static void     account_chooser_connection_changed_cb  (EmpathyAccountManager    *manager,
101                                                         EmpathyAccount                *account,
102                                                         TpConnectionStatusReason  reason,
103                                                         TpConnectionStatus        new_status,
104                                                         TpConnectionStatus        old_status,
105                                                         EmpathyAccountChooser    *chooser);
106 static gboolean account_chooser_separator_func         (GtkTreeModel             *model,
107                                                         GtkTreeIter              *iter,
108                                                         EmpathyAccountChooser    *chooser);
109 static gboolean account_chooser_set_account_foreach    (GtkTreeModel             *model,
110                                                         GtkTreePath              *path,
111                                                         GtkTreeIter              *iter,
112                                                         SetAccountData           *data);
113
114 enum {
115         PROP_0,
116         PROP_HAS_ALL_OPTION,
117 };
118
119 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser, GTK_TYPE_COMBO_BOX);
120
121 static void
122 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
123 {
124         GObjectClass *object_class = G_OBJECT_CLASS (klass);
125
126         object_class->finalize = account_chooser_finalize;
127         object_class->get_property = account_chooser_get_property;
128         object_class->set_property = account_chooser_set_property;
129
130         /**
131          * EmpathyAccountChooser:has-all-option:
132          *
133          * Have an additional option in the list to mean all accounts.
134          */
135         g_object_class_install_property (object_class,
136                                          PROP_HAS_ALL_OPTION,
137                                          g_param_spec_boolean ("has-all-option",
138                                                                "Has All Option",
139                                                                "Have a separate option in the list to mean ALL accounts",
140                                                                FALSE,
141                                                                G_PARAM_READWRITE));
142
143         g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
144 }
145
146 static void
147 empathy_account_chooser_init (EmpathyAccountChooser *chooser)
148 {
149         EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser,
150                 EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
151
152         chooser->priv = priv;
153         priv->set_active_item = FALSE;
154         priv->filter = NULL;
155         priv->filter_data = NULL;
156
157         priv->manager = empathy_account_manager_dup_singleton ();
158
159         g_signal_connect (priv->manager, "account-created",
160                           G_CALLBACK (account_chooser_account_created_cb),
161                           chooser);
162         g_signal_connect (priv->manager, "account-deleted",
163                           G_CALLBACK (account_chooser_account_deleted_cb),
164                           chooser);
165         g_signal_connect (priv->manager, "account-connection-changed",
166                           G_CALLBACK (account_chooser_connection_changed_cb),
167                           chooser);
168
169         account_chooser_setup (EMPATHY_ACCOUNT_CHOOSER (chooser));
170 }
171
172 static void
173 account_chooser_finalize (GObject *object)
174 {
175         EmpathyAccountChooserPriv *priv = GET_PRIV (object);
176
177         g_signal_handlers_disconnect_by_func (priv->manager,
178                                               account_chooser_connection_changed_cb,
179                                               object);
180         g_signal_handlers_disconnect_by_func (priv->manager,
181                                               account_chooser_account_created_cb,
182                                               object);
183         g_signal_handlers_disconnect_by_func (priv->manager,
184                                               account_chooser_account_deleted_cb,
185                                               object);
186         g_object_unref (priv->manager);
187
188         G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
189 }
190
191 static void
192 account_chooser_get_property (GObject    *object,
193                               guint       param_id,
194                               GValue     *value,
195                               GParamSpec *pspec)
196 {
197         EmpathyAccountChooserPriv *priv;
198
199         priv = GET_PRIV (object);
200
201         switch (param_id) {
202         case PROP_HAS_ALL_OPTION:
203                 g_value_set_boolean (value, priv->has_all_option);
204                 break;
205         default:
206                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
207                 break;
208         };
209 }
210
211 static void
212 account_chooser_set_property (GObject      *object,
213                               guint         param_id,
214                               const GValue *value,
215                               GParamSpec   *pspec)
216 {
217         EmpathyAccountChooserPriv *priv;
218
219         priv = GET_PRIV (object);
220
221         switch (param_id) {
222         case PROP_HAS_ALL_OPTION:
223                 empathy_account_chooser_set_has_all_option (EMPATHY_ACCOUNT_CHOOSER (object),
224                                                            g_value_get_boolean (value));
225                 break;
226         default:
227                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
228                 break;
229         };
230 }
231
232 /**
233  * empathy_account_chooser_new:
234  *
235  * Creates a new #EmpathyAccountChooser.
236  *
237  * Return value: A new #EmpathyAccountChooser
238  */
239 GtkWidget *
240 empathy_account_chooser_new (void)
241 {
242         GtkWidget                *chooser;
243
244         chooser = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
245
246         return chooser;
247 }
248
249 /**
250  * empathy_account_chooser_dup_account:
251  * @chooser: an #EmpathyAccountChooser
252  *
253  * Returns the account which is currently selected in the chooser or %NULL
254  * if there is no account selected. The #EmpathyAccount returned should be
255  * unrefed with g_object_unref() when finished with.
256  *
257  * Return value: a new ref to the #EmpathyAccount currently selected, or %NULL.
258  */
259 EmpathyAccount *
260 empathy_account_chooser_dup_account (EmpathyAccountChooser *chooser)
261 {
262         EmpathyAccountChooserPriv *priv;
263         EmpathyAccount           *account;
264         GtkTreeModel             *model;
265         GtkTreeIter               iter;
266
267         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
268
269         priv = GET_PRIV (chooser);
270
271         model = gtk_combo_box_get_model (GTK_COMBO_BOX (chooser));
272         if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (chooser), &iter)) {
273                 return NULL;
274         }
275
276         gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
277
278         return account;
279 }
280
281 /**
282  * empathy_account_chooser_get_connection:
283  * @chooser: an #EmpathyAccountChooser
284  *
285  * Returns a borrowed reference to the #TpConnection associated with the
286  * account currently selected. The caller must reference the returned object with
287  * g_object_ref() if it will be kept
288  *
289  * Return value: a borrowed reference to the #TpConnection associated with the
290  * account curently selected.
291  */
292 TpConnection *
293 empathy_account_chooser_get_connection (EmpathyAccountChooser *chooser)
294 {
295         EmpathyAccountChooserPriv *priv;
296         EmpathyAccount            *account;
297         TpConnection              *connection;
298
299         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), NULL);
300
301         priv = GET_PRIV (chooser);
302
303         account = empathy_account_chooser_dup_account (chooser);
304         connection = empathy_account_get_connection (account);
305         g_object_unref (account);
306
307         return connection;
308 }
309
310 /**
311  * empathy_account_chooser_set_account:
312  * @chooser: an #EmpathyAccountChooser
313  * @account: an #EmpathyAccount
314  *
315  * Sets the currently selected account to @account, if it exists in the list.
316  *
317  * Return value: whether the chooser was set to @account.
318  */
319 gboolean
320 empathy_account_chooser_set_account (EmpathyAccountChooser *chooser,
321                                      EmpathyAccount *account)
322 {
323         GtkComboBox    *combobox;
324         GtkTreeModel   *model;
325         GtkTreeIter     iter;
326         SetAccountData  data;
327
328         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
329
330         combobox = GTK_COMBO_BOX (chooser);
331         model = gtk_combo_box_get_model (combobox);
332         gtk_combo_box_get_active_iter (combobox, &iter);
333
334         data.chooser = chooser;
335         data.account = account;
336
337         gtk_tree_model_foreach (model,
338                                 (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
339                                 &data);
340
341         return data.set;
342 }
343
344 /**
345  * empathy_account_chooser_get_has_all_option:
346  * @chooser: an #EmpathyAccountChooser
347  *
348  * Returns whether @chooser has the #EmpathyAccountChooser:has-all-option property
349  * set to true.
350  *
351  * Return value: whether @chooser has the #EmpathyAccountChooser:has-all-option property
352  * enabled.
353  */
354 gboolean
355 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *chooser)
356 {
357         EmpathyAccountChooserPriv *priv;
358
359         g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser), FALSE);
360
361         priv = GET_PRIV (chooser);
362
363         return priv->has_all_option;
364 }
365
366 /**
367  * empathy_account_chooser_set_has_all_option:
368  * @chooser: an #EmpathyAccountChooser
369  * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option property
370  *
371  * Sets the #EmpathyAccountChooser:has-all-option property.
372  */
373 void
374 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *chooser,
375                                            gboolean              has_all_option)
376 {
377         EmpathyAccountChooserPriv *priv;
378         GtkComboBox              *combobox;
379         GtkListStore             *store;
380         GtkTreeModel             *model;
381         GtkTreeIter               iter;
382
383         g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (chooser));
384
385         priv = GET_PRIV (chooser);
386
387         if (priv->has_all_option == has_all_option) {
388                 return;
389         }
390
391         combobox = GTK_COMBO_BOX (chooser);
392         model = gtk_combo_box_get_model (combobox);
393         store = GTK_LIST_STORE (model);
394
395         priv->has_all_option = has_all_option;
396
397         /*
398          * The first 2 options are the ALL and separator
399          */
400
401         if (has_all_option) {
402                 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
403                                                       (GtkTreeViewRowSeparatorFunc)
404                                                       account_chooser_separator_func,
405                                                       chooser,
406                                                       NULL);
407
408                 gtk_list_store_prepend (store, &iter);
409                 gtk_list_store_set (store, &iter,
410                                     COL_ACCOUNT_TEXT, NULL,
411                                     COL_ACCOUNT_ENABLED, TRUE,
412                                     COL_ACCOUNT_POINTER, NULL,
413                                     -1);
414
415                 gtk_list_store_prepend (store, &iter);
416                 gtk_list_store_set (store, &iter,
417                                     COL_ACCOUNT_TEXT, _("All"),
418                                     COL_ACCOUNT_ENABLED, TRUE,
419                                     COL_ACCOUNT_POINTER, NULL,
420                                     -1);
421         } else {
422                 if (gtk_tree_model_get_iter_first (model, &iter)) {
423                         if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter)) {
424                                 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
425                         }
426                 }
427
428                 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (chooser),
429                                                       (GtkTreeViewRowSeparatorFunc)
430                                                       NULL,
431                                                       NULL,
432                                                       NULL);
433         }
434
435         g_object_notify (G_OBJECT (chooser), "has-all-option");
436 }
437
438 static void
439 account_chooser_setup (EmpathyAccountChooser *chooser)
440 {
441         EmpathyAccountChooserPriv *priv;
442         GList                    *accounts;
443         GtkListStore             *store;
444         GtkCellRenderer          *renderer;
445         GtkComboBox              *combobox;
446
447         priv = GET_PRIV (chooser);
448
449         /* Set up combo box with new store */
450         combobox = GTK_COMBO_BOX (chooser);
451
452         gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
453
454         store = gtk_list_store_new (COL_ACCOUNT_COUNT,
455                                     G_TYPE_STRING,    /* Image */
456                                     G_TYPE_STRING,    /* Name */
457                                     G_TYPE_BOOLEAN,   /* Enabled */
458                                     EMPATHY_TYPE_ACCOUNT);
459
460         gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
461
462         renderer = gtk_cell_renderer_pixbuf_new ();
463         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
464         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
465                                         "icon-name", COL_ACCOUNT_IMAGE,
466                                         "sensitive", COL_ACCOUNT_ENABLED,
467                                         NULL);
468         g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
469
470         renderer = gtk_cell_renderer_text_new ();
471         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
472         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
473                                         "text", COL_ACCOUNT_TEXT,
474                                         "sensitive", COL_ACCOUNT_ENABLED,
475                                         NULL);
476
477         /* Populate accounts */
478         accounts = empathy_account_manager_dup_accounts (priv->manager);
479         g_list_foreach (accounts,
480                         (GFunc) account_chooser_account_add_foreach,
481                         chooser);
482
483         g_list_free (accounts);
484         g_object_unref (store);
485 }
486
487 static void
488 account_chooser_account_created_cb (EmpathyAccountManager *manager,
489                                     EmpathyAccount        *account,
490                                     EmpathyAccountChooser *chooser)
491 {
492         account_chooser_account_add_foreach (account, chooser);
493 }
494
495 static void
496 account_chooser_account_add_foreach (EmpathyAccount        *account,
497                                      EmpathyAccountChooser *chooser)
498 {
499         GtkListStore *store;
500         GtkComboBox  *combobox;
501         GtkTreeIter   iter;
502         gint          position;
503
504         combobox = GTK_COMBO_BOX (chooser);
505         store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
506
507         position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
508         gtk_list_store_insert_with_values (store, &iter, position,
509                                            COL_ACCOUNT_POINTER, account,
510                                            -1);
511         account_chooser_update_iter (chooser, &iter);
512         /* We got a reffed account and it was reffed by the liststore as well */
513         g_object_unref (account);
514 }
515
516 static void
517 account_chooser_account_deleted_cb (EmpathyAccountManager *manager,
518                                     EmpathyAccount        *account,
519                                     EmpathyAccountChooser *chooser)
520 {
521         account_chooser_account_remove_foreach (account, chooser);
522 }
523
524 typedef struct {
525         EmpathyAccount   *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         EmpathyAccount  *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                               EmpathyAccount        *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 (EmpathyAccount        *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         EmpathyAccount            *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_account_get_icon_name (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, empathy_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                                        EmpathyAccount          *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         EmpathyAccount *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 #EmpathyAccount
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 #EmpathyAccount
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 (EmpathyAccount *account,
768                                              gpointer   user_data)
769 {
770         TpConnectionStatus  status;
771
772         g_object_get (account, "connection-status", &status, NULL);
773
774         return status == TP_CONNECTION_STATUS_CONNECTED;
775 }
776