]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-chooser.c
6e7ec29402aa7517623a4a8d03ed33ef718f4c16
[empathy.git] / libempathy-gtk / empathy-account-chooser.c
1 /*
2  * Copyright (C) 2005-2007 Imendio AB
3  * Copyright (C) 2007-2011 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors: Martyn Russell <martyn@imendio.com>
21  *          Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27
28 #include <glib/gi18n-lib.h>
29 #include <gtk/gtk.h>
30
31 #include <telepathy-glib/account-manager.h>
32 #include <telepathy-glib/util.h>
33
34 #include <libempathy/empathy-utils.h>
35
36 #include "empathy-ui-utils.h"
37 #include "empathy-account-chooser.h"
38
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include <libempathy/empathy-debug.h>
41
42 /**
43  * SECTION:empathy-account-chooser
44  * @title:EmpathyAccountChooser
45  * @short_description: A widget used to choose from a list of accounts
46  * @include: libempathy-gtk/empathy-account-chooser.h
47  *
48  * #EmpathyAccountChooser is a widget which extends #GtkComboBox to provide
49  * a chooser of available accounts.
50  */
51
52 /**
53  * EmpathyAccountChooser:
54  * @parent: parent object
55  *
56  * Widget which extends #GtkComboBox to provide a chooser of available accounts.
57  */
58
59 struct _EmpathyAccountChooserPriv
60 {
61   TpAccountManager *manager;
62   gboolean set_active_item;
63   gboolean account_manually_set;
64   gboolean has_all_option;
65   EmpathyAccountChooserFilterFunc filter;
66   gpointer filter_data;
67   gboolean ready;
68
69   TpAccount *select_when_ready;
70 };
71
72 typedef struct
73 {
74   EmpathyAccountChooser *self;
75   TpAccount *account;
76   gboolean set;
77 } SetAccountData;
78
79 typedef struct
80 {
81   EmpathyAccountChooser *self;
82   TpAccount *account;
83   GtkTreeIter *iter;
84 } FilterResultCallbackData;
85
86 static FilterResultCallbackData *
87 filter_result_callback_data_new (EmpathyAccountChooser *self,
88     TpAccount *account,
89     GtkTreeIter *iter)
90 {
91   FilterResultCallbackData *data;
92
93   g_return_val_if_fail (self != NULL, NULL);
94   g_return_val_if_fail (account != NULL, NULL);
95   g_return_val_if_fail (iter != NULL, NULL);
96
97   data = g_slice_new0 (FilterResultCallbackData);
98   data->self = g_object_ref (self);
99   data->account = g_object_ref (account);
100   data->iter = gtk_tree_iter_copy (iter);
101
102   return data;
103 }
104
105 static void
106 filter_result_callback_data_free (FilterResultCallbackData *data)
107 {
108   g_object_unref (data->self);
109   g_object_unref (data->account);
110   gtk_tree_iter_free (data->iter);
111   g_slice_free (FilterResultCallbackData, data);
112 }
113
114 /* Distinguishes between store entries which are actually accounts, and special
115  * items like the "All" entry and the separator below it, so they can be sorted
116  * correctly. Higher-numbered entries will sort earlier.
117  */
118 typedef enum {
119   ROW_ACCOUNT = 0,
120   ROW_SEPARATOR,
121   ROW_ALL
122 } RowType;
123
124 enum {
125   COL_ACCOUNT_IMAGE,
126   COL_ACCOUNT_TEXT,
127   COL_ACCOUNT_ENABLED, /* Usually tied to connected state */
128   COL_ACCOUNT_ROW_TYPE,
129   COL_ACCOUNT_POINTER,
130   COL_ACCOUNT_COUNT
131 };
132
133 static void account_chooser_account_validity_changed_cb (
134     TpAccountManager *manager,
135     TpAccount *account,
136     gboolean valid,
137     EmpathyAccountChooser *self);
138 static void account_chooser_account_add_foreach (TpAccount *account,
139     EmpathyAccountChooser *self);
140 static void account_chooser_account_removed_cb (TpAccountManager *manager,
141     TpAccount *account,
142     EmpathyAccountChooser *self);
143 static void account_chooser_account_remove_foreach (TpAccount *account,
144     EmpathyAccountChooser *self);
145 static void account_chooser_update_iter (EmpathyAccountChooser *self,
146     GtkTreeIter *iter);
147 static void account_chooser_status_changed_cb (TpAccount *account,
148     guint old_status,
149     guint new_status,
150     guint reason,
151     gchar *dbus_error_name,
152     GHashTable *details,
153     gpointer user_data);
154 static gboolean account_chooser_separator_func (GtkTreeModel *model,
155     GtkTreeIter *iter,
156     EmpathyAccountChooser *self);
157 static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
158     GtkTreePath *path,
159     GtkTreeIter *iter,
160     SetAccountData *data);
161 static void update_account (EmpathyAccountChooser *self,
162     TpAccount *account);
163 static gboolean select_account (EmpathyAccountChooser *self,
164     TpAccount *account);
165
166 enum {
167   PROP_0,
168   PROP_HAS_ALL_OPTION,
169 };
170
171 enum {
172   READY,
173   LAST_SIGNAL
174 };
175
176 static guint signals[LAST_SIGNAL] = { 0 };
177
178 G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser,
179     GTK_TYPE_COMBO_BOX)
180
181 static void
182 empathy_account_chooser_init (EmpathyAccountChooser *self)
183 {
184   TpSimpleClientFactory *factory;
185
186   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
187     EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
188
189   self->priv->set_active_item = FALSE;
190   self->priv->account_manually_set = FALSE;
191   self->priv->filter = NULL;
192   self->priv->filter_data = NULL;
193
194   self->priv->manager = tp_account_manager_dup ();
195
196   tp_g_signal_connect_object (self->priv->manager, "account-validity-changed",
197       G_CALLBACK (account_chooser_account_validity_changed_cb), self, 0);
198
199   tp_g_signal_connect_object (self->priv->manager, "account-removed",
200       G_CALLBACK (account_chooser_account_removed_cb), self, 0);
201
202   /* Make sure we'll have the capabilities feature on TpAccount's connection */
203   factory = tp_proxy_get_factory (self->priv->manager);
204
205   tp_simple_client_factory_add_account_features_varargs (factory,
206       TP_ACCOUNT_FEATURE_CONNECTION, NULL);
207   tp_simple_client_factory_add_connection_features_varargs (factory,
208       TP_CONNECTION_FEATURE_CAPABILITIES, NULL);
209 }
210
211 static gint
212 account_cmp (GtkTreeModel *model,
213     GtkTreeIter *a,
214     GtkTreeIter *b,
215     gpointer user_data)
216 {
217   RowType a_type, b_type;
218   gboolean a_enabled, b_enabled;
219   gchar *a_text, *b_text;
220   gint result;
221
222   gtk_tree_model_get (model, a,
223       COL_ACCOUNT_ENABLED, &a_enabled,
224       COL_ACCOUNT_ROW_TYPE, &a_type,
225       -1);
226   gtk_tree_model_get (model, b,
227       COL_ACCOUNT_ENABLED, &b_enabled,
228       COL_ACCOUNT_ROW_TYPE, &b_type,
229       -1);
230
231   /* This assumes that we have at most one of each special row type. */
232   if (a_type != b_type)
233     /* Display higher-numbered special row types first. */
234     return (b_type - a_type);
235
236   /* Enabled accounts are displayed first */
237   if (a_enabled != b_enabled)
238     return a_enabled ? -1: 1;
239
240   gtk_tree_model_get (model, a, COL_ACCOUNT_TEXT, &a_text, -1);
241   gtk_tree_model_get (model, b, COL_ACCOUNT_TEXT, &b_text, -1);
242
243   if (a_text == b_text)
244     result = 0;
245   else if (a_text == NULL)
246     result = 1;
247   else if (b_text == NULL)
248     result = -1;
249   else
250     result = g_ascii_strcasecmp (a_text, b_text);
251
252   g_free (a_text);
253   g_free (b_text);
254
255   return result;
256 }
257
258 static void
259 account_connection_notify_cb (TpAccount *account,
260     GParamSpec *spec,
261     EmpathyAccountChooser *self)
262 {
263   update_account (self, account);
264 }
265
266 static void
267 account_manager_prepared_cb (GObject *source_object,
268     GAsyncResult *result,
269     gpointer user_data)
270 {
271   GList *accounts, *l;
272   TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
273   EmpathyAccountChooser *self = user_data;
274   GError *error = NULL;
275
276   if (!tp_proxy_prepare_finish (manager, result, &error))
277     {
278       DEBUG ("Failed to prepare account manager: %s", error->message);
279       g_error_free (error);
280       return;
281     }
282
283   accounts = tp_account_manager_dup_valid_accounts (manager);
284
285   for (l = accounts; l != NULL; l = l->next)
286     {
287       TpAccount *account = l->data;
288
289       account_chooser_account_add_foreach (account, self);
290
291       tp_g_signal_connect_object (account, "status-changed",
292           G_CALLBACK (account_chooser_status_changed_cb),
293           self, 0);
294
295       /* We generally use the TpConnection from the account to filter it so,
296        * just relying on the account status is not enough. In some case we the
297        * status change can be notified while the TpConnection is still
298        * preparing. */
299       tp_g_signal_connect_object (account, "notify::connection",
300           G_CALLBACK (account_connection_notify_cb),
301           self, 0);
302     }
303
304   g_list_free_full (accounts, g_object_unref);
305
306   if (self->priv->select_when_ready != NULL)
307     {
308       select_account (self, self->priv->select_when_ready);
309
310       g_clear_object (&self->priv->select_when_ready);
311     }
312
313   self->priv->ready = TRUE;
314   g_signal_emit (self, signals[READY], 0);
315 }
316
317 static void
318 account_chooser_constructed (GObject *object)
319 {
320   EmpathyAccountChooser *self = (EmpathyAccountChooser *) object;
321   GtkListStore *store;
322   GtkCellRenderer *renderer;
323   GtkComboBox *combobox;
324
325   /* Set up combo box with new store */
326   combobox = GTK_COMBO_BOX (self);
327
328   gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
329
330   store = gtk_list_store_new (COL_ACCOUNT_COUNT,
331       GDK_TYPE_PIXBUF,  /* Image */
332       G_TYPE_STRING,    /* Name */
333       G_TYPE_BOOLEAN,   /* Enabled */
334       G_TYPE_UINT,      /* Row type */
335       TP_TYPE_ACCOUNT);
336
337   gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
338     account_cmp, self, NULL);
339   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
340     GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
341
342   gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
343
344   renderer = gtk_cell_renderer_pixbuf_new ();
345   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
346   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
347       "pixbuf", COL_ACCOUNT_IMAGE,
348       "sensitive", COL_ACCOUNT_ENABLED,
349       NULL);
350
351   renderer = gtk_cell_renderer_text_new ();
352   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
353   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
354       "text", COL_ACCOUNT_TEXT,
355       "sensitive", COL_ACCOUNT_ENABLED,
356       NULL);
357
358   /* Populate accounts */
359   tp_proxy_prepare_async (self->priv->manager, NULL,
360       account_manager_prepared_cb, self);
361
362   g_object_unref (store);
363
364 }
365
366 static void
367 account_chooser_dispose (GObject *object)
368 {
369   EmpathyAccountChooser *self = EMPATHY_ACCOUNT_CHOOSER (object);
370
371   g_clear_object (&self->priv->manager);
372   g_clear_object (&self->priv->select_when_ready);
373
374   G_OBJECT_CLASS (empathy_account_chooser_parent_class)->dispose (object);
375 }
376
377 static void
378 account_chooser_get_property (GObject *object,
379     guint param_id,
380     GValue *value,
381     GParamSpec *pspec)
382 {
383   EmpathyAccountChooser *self = (EmpathyAccountChooser *) object;
384
385   switch (param_id)
386     {
387       case PROP_HAS_ALL_OPTION:
388         g_value_set_boolean (value, self->priv->has_all_option);
389         break;
390       default:
391         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
392         break;
393     };
394 }
395
396 static void
397 account_chooser_set_property (GObject *object,
398     guint param_id,
399     const GValue *value,
400     GParamSpec *pspec)
401 {
402   switch (param_id)
403     {
404       case PROP_HAS_ALL_OPTION:
405         empathy_account_chooser_set_has_all_option (
406             EMPATHY_ACCOUNT_CHOOSER (object), g_value_get_boolean (value));
407         break;
408       default:
409         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
410         break;
411     };
412 }
413
414 static void
415 empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
416 {
417   GObjectClass *object_class = G_OBJECT_CLASS (klass);
418
419   object_class->constructed = account_chooser_constructed;
420   object_class->dispose = account_chooser_dispose;
421   object_class->get_property = account_chooser_get_property;
422   object_class->set_property = account_chooser_set_property;
423
424   /**
425    * EmpathyAccountChooser:has-all-option:
426    *
427    * Have an additional option in the list to mean all accounts.
428    */
429   g_object_class_install_property (object_class,
430       PROP_HAS_ALL_OPTION,
431       g_param_spec_boolean ("has-all-option",
432         "Has All Option",
433         "Have a separate option in the list to mean ALL accounts",
434         FALSE,
435         G_PARAM_READWRITE));
436
437   signals[READY] =
438     g_signal_new ("ready",
439         G_OBJECT_CLASS_TYPE (object_class),
440         G_SIGNAL_RUN_LAST,
441         0,
442         NULL, NULL,
443         g_cclosure_marshal_generic,
444         G_TYPE_NONE,
445         0);
446
447   g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
448 }
449
450 /**
451  * empathy_account_chooser_new:
452  *
453  * Creates a new #EmpathyAccountChooser.
454  *
455  * Return value: A new #EmpathyAccountChooser
456  */
457 GtkWidget *
458 empathy_account_chooser_new (void)
459 {
460   GtkWidget *self;
461
462   self = g_object_new (EMPATHY_TYPE_ACCOUNT_CHOOSER, NULL);
463
464   return self;
465 }
466
467 gboolean
468 empathy_account_chooser_has_all_selected (EmpathyAccountChooser *self)
469 {
470   GtkTreeModel *model;
471   GtkTreeIter iter;
472   RowType type;
473
474   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
475
476   g_return_val_if_fail (self->priv->has_all_option == TRUE, FALSE);
477
478   model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
479   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
480     return FALSE;
481
482   gtk_tree_model_get (model, &iter, COL_ACCOUNT_ROW_TYPE, &type, -1);
483
484   return type == ROW_ALL;
485 }
486
487 /**
488  * empathy_account_chooser_dup_account:
489  * @self: an #EmpathyAccountChooser
490  *
491  * Returns the account which is currently selected in the chooser or %NULL
492  * if there is no account selected. The #TpAccount returned should be
493  * unrefed with g_object_unref() when finished with.
494  *
495  * Return value: a new ref to the #TpAccount currently selected, or %NULL.
496  */
497 TpAccount *
498 empathy_account_chooser_dup_account (EmpathyAccountChooser *self)
499 {
500   TpAccount *account;
501   GtkTreeModel *model;
502   GtkTreeIter iter;
503
504   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), NULL);
505
506   model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
507   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
508     return NULL;
509
510   gtk_tree_model_get (model, &iter, COL_ACCOUNT_POINTER, &account, -1);
511
512   return account;
513 }
514
515 /**
516  * empathy_account_chooser_get_connection:
517  * @self: an #EmpathyAccountChooser
518  *
519  * Returns a borrowed reference to the #TpConnection associated with the
520  * account currently selected. The caller must reference the returned object
521  * with g_object_ref() if it will be kept
522  *
523  * Return value: a borrowed reference to the #TpConnection associated with the
524  * account curently selected.
525  */
526 TpConnection *
527 empathy_account_chooser_get_connection (EmpathyAccountChooser *self)
528 {
529   TpAccount *account;
530   TpConnection *connection;
531
532   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), NULL);
533
534   account = empathy_account_chooser_dup_account (self);
535
536   /* if the returned account is NULL, then the account manager probably
537    * hasn't been prepared yet. It should be safe to return NULL here
538    * though. */
539   if (account == NULL)
540     return NULL;
541
542   connection = tp_account_get_connection (account);
543   g_object_unref (account);
544
545   return connection;
546 }
547
548 static gboolean
549 select_account (EmpathyAccountChooser *self,
550     TpAccount *account)
551 {
552   GtkComboBox *combobox;
553   GtkTreeModel *model;
554   GtkTreeIter iter;
555   SetAccountData data;
556
557   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
558
559   combobox = GTK_COMBO_BOX (self);
560   model = gtk_combo_box_get_model (combobox);
561   gtk_combo_box_get_active_iter (combobox, &iter);
562
563   data.self = self;
564   data.account = account;
565   data.set = FALSE;
566
567   gtk_tree_model_foreach (model,
568       (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
569       &data);
570
571   self->priv->account_manually_set = data.set;
572
573   return data.set;
574 }
575
576 /**
577  * empathy_account_chooser_set_account:
578  * @self: an #EmpathyAccountChooser
579  * @account: a #TpAccount
580  *
581  * Sets the currently selected account to @account, if it exists in the list.
582  *
583  * Return value: whether the chooser was set to @account.
584  */
585 gboolean
586 empathy_account_chooser_set_account (EmpathyAccountChooser *self,
587     TpAccount *account)
588 {
589   if (self->priv->ready)
590     return select_account (self, account);
591
592   /* Account chooser is not ready yet, we'll try selecting the account once it
593    * is */
594   g_clear_object (&self->priv->select_when_ready);
595
596   if (account != NULL)
597     self->priv->select_when_ready = g_object_ref (account);
598
599   return FALSE;
600 }
601
602 void
603 empathy_account_chooser_set_all (EmpathyAccountChooser *self)
604 {
605   GtkComboBox *combobox;
606   GtkTreeModel *model;
607   GtkTreeIter iter;
608
609   g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
610
611   g_return_if_fail (self->priv->has_all_option);
612
613   combobox = GTK_COMBO_BOX (self);
614   model = gtk_combo_box_get_model (combobox);
615
616   if (gtk_tree_model_get_iter_first (model, &iter))
617     {
618       /* 'All accounts' is the first row */
619       gtk_combo_box_set_active_iter (combobox, &iter);
620       self->priv->account_manually_set = TRUE;
621     }
622 }
623
624 /**
625  * empathy_account_chooser_get_has_all_option:
626  * @self: an #EmpathyAccountChooser
627  *
628  * Returns whether @self has the #EmpathyAccountChooser:has-all-option
629  * property set to true.
630  *
631  * Return value: whether @self has the #EmpathyAccountChooser:has-all-option
632  * property enabled.
633  */
634 gboolean
635 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *self)
636 {
637   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
638
639   return self->priv->has_all_option;
640 }
641
642 /**
643  * empathy_account_chooser_set_has_all_option:
644  * @self: an #EmpathyAccountChooser
645  * @has_all_option: a new value for the #EmpathyAccountChooser:has-all-option
646  * property
647  *
648  * Sets the #EmpathyAccountChooser:has-all-option property.
649  */
650 void
651 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *self,
652     gboolean has_all_option)
653 {
654   GtkComboBox *combobox;
655   GtkListStore *store;
656   GtkTreeModel *model;
657   GtkTreeIter iter;
658
659   g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
660
661   if (self->priv->has_all_option == has_all_option)
662     return;
663
664   combobox = GTK_COMBO_BOX (self);
665   model = gtk_combo_box_get_model (combobox);
666   store = GTK_LIST_STORE (model);
667
668   self->priv->has_all_option = has_all_option;
669
670   /*
671    * The first 2 options are the ALL and separator
672    */
673
674   if (has_all_option)
675     {
676       gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
677           (GtkTreeViewRowSeparatorFunc)
678           account_chooser_separator_func,
679           self,
680           NULL);
681
682       gtk_list_store_prepend (store, &iter);
683       gtk_list_store_set (store, &iter,
684           COL_ACCOUNT_TEXT, NULL,
685           COL_ACCOUNT_ENABLED, TRUE,
686           COL_ACCOUNT_POINTER, NULL,
687           COL_ACCOUNT_ROW_TYPE, ROW_SEPARATOR,
688           -1);
689
690       gtk_list_store_prepend (store, &iter);
691       gtk_list_store_set (store, &iter,
692           COL_ACCOUNT_TEXT, _("All accounts"),
693           COL_ACCOUNT_ENABLED, TRUE,
694           COL_ACCOUNT_POINTER, NULL,
695           COL_ACCOUNT_ROW_TYPE, ROW_ALL,
696           -1);
697     }
698   else
699     {
700       if (gtk_tree_model_get_iter_first (model, &iter))
701         {
702           if (gtk_list_store_remove (GTK_LIST_STORE (model), &iter))
703             gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
704         }
705
706     gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
707         (GtkTreeViewRowSeparatorFunc)
708         NULL,
709         NULL,
710         NULL);
711   }
712
713   g_object_notify (G_OBJECT (self), "has-all-option");
714 }
715
716 static void
717 account_chooser_account_validity_changed_cb (TpAccountManager *manager,
718     TpAccount *account,
719     gboolean valid,
720     EmpathyAccountChooser *self)
721 {
722   if (valid)
723     account_chooser_account_add_foreach (account, self);
724   else
725     account_chooser_account_remove_foreach (account, self);
726 }
727
728 static void
729 account_chooser_account_add_foreach (TpAccount *account,
730     EmpathyAccountChooser *self)
731 {
732   GtkListStore *store;
733   GtkComboBox *combobox;
734   GtkTreeIter iter;
735   gint position;
736
737   combobox = GTK_COMBO_BOX (self);
738   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
739
740   position = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
741   gtk_list_store_insert_with_values (store, &iter, position,
742       COL_ACCOUNT_POINTER, account,
743       -1);
744
745   account_chooser_update_iter (self, &iter);
746 }
747
748 static void
749 account_chooser_account_removed_cb (TpAccountManager *manager,
750     TpAccount *account,
751     EmpathyAccountChooser *self)
752 {
753   account_chooser_account_remove_foreach (account, self);
754 }
755
756 typedef struct
757 {
758   TpAccount *account;
759   GtkTreeIter *iter;
760   gboolean found;
761 } FindAccountData;
762
763 static gboolean
764 account_chooser_find_account_foreach (GtkTreeModel *model,
765     GtkTreePath *path,
766     GtkTreeIter *iter,
767     gpointer user_data)
768 {
769   FindAccountData *data = user_data;
770   TpAccount *account;
771   RowType type;
772
773   gtk_tree_model_get (model, iter,
774     COL_ACCOUNT_POINTER, &account,
775     COL_ACCOUNT_ROW_TYPE, &type,
776      -1);
777
778   if (type != ROW_ACCOUNT)
779     return FALSE;
780
781   if (account == data->account)
782     {
783       data->found = TRUE;
784       *(data->iter) = *iter;
785       g_object_unref (account);
786
787       return TRUE;
788     }
789
790   g_object_unref (account);
791
792   return FALSE;
793 }
794
795 static gboolean
796 account_chooser_find_account (EmpathyAccountChooser *self,
797     TpAccount *account,
798     GtkTreeIter *iter)
799 {
800   GtkListStore *store;
801   GtkComboBox *combobox;
802   FindAccountData data;
803
804   combobox = GTK_COMBO_BOX (self);
805   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
806
807   data.account = account;
808   data.iter = iter;
809   gtk_tree_model_foreach (GTK_TREE_MODEL (store),
810         account_chooser_find_account_foreach,
811         &data);
812
813   return data.found;
814 }
815
816 static void
817 account_chooser_account_remove_foreach (TpAccount *account,
818     EmpathyAccountChooser *self)
819 {
820   GtkListStore *store;
821   GtkComboBox *combobox;
822   GtkTreeIter iter;
823
824   combobox = GTK_COMBO_BOX (self);
825   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
826
827   if (account_chooser_find_account (self, account, &iter))
828     gtk_list_store_remove (store, &iter);
829 }
830
831 static void
832 account_chooser_filter_ready_cb (gboolean is_enabled,
833     gpointer data)
834 {
835   FilterResultCallbackData *fr_data = data;
836   EmpathyAccountChooser *self;
837   TpAccount *account;
838   GtkTreeIter *iter;
839   GtkListStore *store;
840   GtkComboBox *combobox;
841   const gchar *icon_name;
842   GdkPixbuf *pixbuf;
843
844   self = fr_data->self;
845   account = fr_data->account;
846   iter = fr_data->iter;
847   combobox = GTK_COMBO_BOX (self);
848   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
849
850   icon_name = tp_account_get_icon_name (account);
851   pixbuf = empathy_pixbuf_from_icon_name (icon_name,
852     GTK_ICON_SIZE_BUTTON);
853
854   gtk_list_store_set (store, iter,
855           COL_ACCOUNT_IMAGE, pixbuf,
856           COL_ACCOUNT_TEXT, tp_account_get_display_name (account),
857           COL_ACCOUNT_ENABLED, is_enabled,
858           -1);
859
860   if (pixbuf != NULL)
861     g_object_unref (pixbuf);
862
863   /* set first connected account as active account */
864   if (self->priv->account_manually_set == FALSE &&
865       self->priv->set_active_item == FALSE && is_enabled)
866     {
867       self->priv->set_active_item = TRUE;
868       gtk_combo_box_set_active_iter (combobox, iter);
869     }
870
871   filter_result_callback_data_free (fr_data);
872 }
873
874 static void
875 account_chooser_update_iter (EmpathyAccountChooser *self,
876     GtkTreeIter *iter)
877 {
878   GtkListStore *store;
879   GtkComboBox *combobox;
880   TpAccount *account;
881   FilterResultCallbackData *data;
882
883   combobox = GTK_COMBO_BOX (self);
884   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
885
886   gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
887           COL_ACCOUNT_POINTER, &account,
888           -1);
889
890   /* Skip rows without account associated */
891   if (account == NULL)
892     return;
893
894   data = filter_result_callback_data_new (self, account, iter);
895
896   if (self->priv->filter)
897     self->priv->filter (account, account_chooser_filter_ready_cb,
898             (gpointer) data, self->priv->filter_data);
899   else
900     account_chooser_filter_ready_cb (TRUE, (gpointer) data);
901
902   g_object_unref (account);
903 }
904
905 static void
906 update_account (EmpathyAccountChooser *self,
907     TpAccount *account)
908 {
909   GtkTreeIter iter;
910
911   if (account_chooser_find_account (self, account, &iter))
912     account_chooser_update_iter (self, &iter);
913 }
914
915 static void
916 account_chooser_status_changed_cb (TpAccount *account,
917     guint old_status,
918     guint new_status,
919     guint reason,
920     gchar *dbus_error_name,
921     GHashTable *details,
922     gpointer user_data)
923 {
924   EmpathyAccountChooser *self = user_data;
925
926   update_account (self, account);
927 }
928
929 static gboolean
930 account_chooser_separator_func (GtkTreeModel *model,
931     GtkTreeIter *iter,
932     EmpathyAccountChooser *self)
933 {
934   RowType row_type;
935
936   gtk_tree_model_get (model, iter, COL_ACCOUNT_ROW_TYPE, &row_type, -1);
937   return (row_type == ROW_SEPARATOR);
938 }
939
940 static gboolean
941 account_chooser_set_account_foreach (GtkTreeModel *model,
942     GtkTreePath *path,
943     GtkTreeIter *iter,
944     SetAccountData *data)
945 {
946   TpAccount *account;
947   gboolean equal;
948
949   gtk_tree_model_get (model, iter, COL_ACCOUNT_POINTER, &account, -1);
950
951   equal = (data->account == account);
952
953   if (account)
954     g_object_unref (account);
955
956   if (equal)
957     {
958       GtkComboBox *combobox;
959
960       combobox = GTK_COMBO_BOX (data->self);
961       gtk_combo_box_set_active_iter (combobox, iter);
962
963       data->set = TRUE;
964     }
965
966   return equal;
967 }
968
969 static gboolean
970 account_chooser_filter_foreach (GtkTreeModel *model,
971     GtkTreePath *path,
972     GtkTreeIter *iter,
973     gpointer self)
974 {
975   account_chooser_update_iter (self, iter);
976   return FALSE;
977 }
978
979 void
980 empathy_account_chooser_refilter (EmpathyAccountChooser *self)
981 {
982   GtkTreeModel *model;
983
984   self->priv->set_active_item = FALSE;
985   model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
986   gtk_tree_model_foreach (model, account_chooser_filter_foreach, self);
987 }
988
989 /**
990  * empathy_account_chooser_set_filter:
991  * @self: an #EmpathyAccountChooser
992  * @filter: a filter
993  * @user_data: data to pass to @filter, or %NULL
994  *
995  * Sets a filter on the @self so only accounts that are %TRUE in the eyes
996  * of the filter are visible in the @self.
997  */
998 void
999 empathy_account_chooser_set_filter (EmpathyAccountChooser *self,
1000     EmpathyAccountChooserFilterFunc filter,
1001     gpointer user_data)
1002 {
1003   g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
1004
1005   self->priv->filter = filter;
1006   self->priv->filter_data = user_data;
1007
1008   /* Refilter existing data */
1009   empathy_account_chooser_refilter (self);
1010 }
1011
1012 /**
1013  * EmpathyAccountChooserFilterFunc:
1014  * @account: a #TpAccount
1015  * @user_data: user data, or %NULL
1016  *
1017  * A function which decides whether the account indicated by @account
1018  * is visible.
1019  *
1020  * Return value: whether the account indicated by @account is visible.
1021  */
1022
1023 gboolean
1024 empathy_account_chooser_is_ready (EmpathyAccountChooser *self)
1025 {
1026   return self->priv->ready;
1027 }
1028
1029 TpAccount *
1030 empathy_account_chooser_get_account (EmpathyAccountChooser *self)
1031 {
1032   TpAccount *account;
1033
1034   account = empathy_account_chooser_dup_account (self);
1035   if (account == NULL)
1036     return NULL;
1037
1038   g_object_unref (account);
1039
1040   return account;
1041 }
1042
1043 TpAccountManager *
1044 empathy_account_chooser_get_account_manager (EmpathyAccountChooser *self)
1045 {
1046   return self->priv->manager;
1047 }
1048
1049 /* Pre-defined filters */
1050
1051 /**
1052  * empathy_account_chooser_filter_is_connected:
1053  * @account: a #TpAccount
1054  * @callback: an #EmpathyAccountChooserFilterResultCallback accepting the result
1055  * @callback_data: data passed to the @callback
1056  * @user_data: user data or %NULL
1057  *
1058  * A useful #EmpathyAccountChooserFilterFunc that one could pass into
1059  * empathy_account_chooser_set_filter() and only show connected accounts.
1060  *
1061  * Returns (via the callback) TRUE is @account is connected
1062  */
1063 void
1064 empathy_account_chooser_filter_is_connected (TpAccount *account,
1065   EmpathyAccountChooserFilterResultCallback callback,
1066   gpointer callback_data,
1067   gpointer user_data)
1068 {
1069   gboolean is_connected =
1070     tp_account_get_connection_status (account, NULL)
1071     == TP_CONNECTION_STATUS_CONNECTED;
1072
1073   callback (is_connected, callback_data);
1074 }
1075
1076 /**
1077  * empathy_account_chooser_filter_supports_multichat:
1078  * @account: a #TpAccount
1079  * @callback: an #EmpathyAccountChooserFilterResultCallback accepting the result
1080  * @callback_data: data passed to the @callback
1081  * @user_data: user data or %NULL
1082  *
1083  * An #EmpathyAccountChooserFilterFunc that returns accounts that both
1084  * support multiuser text chat and are connected.
1085  *
1086  * Returns (via the callback) TRUE if @account both supports muc and
1087  * is connected
1088  */
1089 void
1090 empathy_account_chooser_filter_supports_chatrooms (TpAccount *account,
1091   EmpathyAccountChooserFilterResultCallback callback,
1092   gpointer callback_data,
1093   gpointer user_data)
1094 {
1095   TpConnection *connection;
1096   gboolean supported = FALSE;
1097   TpCapabilities *caps;
1098
1099   /* check if CM supports multiuser text chat */
1100   connection = tp_account_get_connection (account);
1101   if (connection == NULL)
1102     goto out;
1103
1104   caps = tp_connection_get_capabilities (connection);
1105   if (caps == NULL)
1106     goto out;
1107
1108   supported = tp_capabilities_supports_text_chatrooms (caps);
1109
1110 out:
1111   callback (supported, callback_data);
1112 }