]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-account-chooser.c
Merge branch 'gnome-3-8'
[empathy.git] / libempathy-gtk / empathy-account-chooser.c
index e9e8b569ee0a2e49d0d532a489586d5f8176ad57..e6f4367c9eda828ccc8a37eecfd1fb50de3ef6ae 100644 (file)
  */
 
 #include "config.h"
-
-#include <string.h>
+#include "empathy-account-chooser.h"
 
 #include <glib/gi18n-lib.h>
-#include <gtk/gtk.h>
-
-#include <telepathy-glib/account-manager.h>
-#include <telepathy-glib/util.h>
-
-#include <libempathy/empathy-utils.h>
 
 #include "empathy-ui-utils.h"
-#include "empathy-account-chooser.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
-#include <libempathy/empathy-debug.h>
+#include "empathy-debug.h"
 
 /**
  * SECTION:empathy-account-chooser
@@ -56,9 +48,7 @@
  * Widget which extends #GtkComboBox to provide a chooser of available accounts.
  */
 
-#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyAccountChooser)
-
-typedef struct
+struct _EmpathyAccountChooserPriv
 {
   TpAccountManager *manager;
   gboolean set_active_item;
@@ -67,7 +57,9 @@ typedef struct
   EmpathyAccountChooserFilterFunc filter;
   gpointer filter_data;
   gboolean ready;
-} EmpathyAccountChooserPriv;
+
+  TpAccount *select_when_ready;
+};
 
 typedef struct
 {
@@ -90,6 +82,10 @@ filter_result_callback_data_new (EmpathyAccountChooser *self,
 {
   FilterResultCallbackData *data;
 
+  g_return_val_if_fail (self != NULL, NULL);
+  g_return_val_if_fail (account != NULL, NULL);
+  g_return_val_if_fail (iter != NULL, NULL);
+
   data = g_slice_new0 (FilterResultCallbackData);
   data->self = g_object_ref (self);
   data->account = g_object_ref (account);
@@ -126,17 +122,6 @@ enum {
   COL_ACCOUNT_COUNT
 };
 
-static void account_chooser_constructed (GObject *object);
-static void account_chooser_finalize (GObject *object);
-static void account_chooser_get_property (GObject *object,
-    guint param_id,
-    GValue *value,
-    GParamSpec *pspec);
-static void account_chooser_set_property (GObject *object,
-    guint param_id,
-    const GValue *value,
-    GParamSpec *pspec);
-static void account_chooser_setup (EmpathyAccountChooser *self);
 static void account_chooser_account_validity_changed_cb (
     TpAccountManager *manager,
     TpAccount *account,
@@ -148,7 +133,7 @@ static void account_chooser_account_removed_cb (TpAccountManager *manager,
     TpAccount *account,
     EmpathyAccountChooser *self);
 static void account_chooser_account_remove_foreach (TpAccount *account,
-    EmpathyAccountChooser*self);
+    EmpathyAccountChooser *self);
 static void account_chooser_update_iter (EmpathyAccountChooser *self,
     GtkTreeIter *iter);
 static void account_chooser_status_changed_cb (TpAccount *account,
@@ -165,6 +150,10 @@ static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
     GtkTreePath *path,
     GtkTreeIter *iter,
     SetAccountData *data);
+static void update_account (EmpathyAccountChooser *self,
+    TpAccount *account);
+static gboolean select_account (EmpathyAccountChooser *self,
+    TpAccount *account);
 
 enum {
   PROP_0,
@@ -182,85 +171,199 @@ G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser,
     GTK_TYPE_COMBO_BOX)
 
 static void
-empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
+empathy_account_chooser_init (EmpathyAccountChooser *self)
 {
-  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  TpSimpleClientFactory *factory;
 
-  object_class->constructed = account_chooser_constructed;
-  object_class->finalize = account_chooser_finalize;
-  object_class->get_property = account_chooser_get_property;
-  object_class->set_property = account_chooser_set_property;
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+    EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
 
-  /**
-   * EmpathyAccountChooser:has-all-option:
-   *
-   * Have an additional option in the list to mean all accounts.
-   */
-  g_object_class_install_property (object_class,
-      PROP_HAS_ALL_OPTION,
-      g_param_spec_boolean ("has-all-option",
-        "Has All Option",
-        "Have a separate option in the list to mean ALL accounts",
-        FALSE,
-        G_PARAM_READWRITE));
+  self->priv->set_active_item = FALSE;
+  self->priv->account_manually_set = FALSE;
+  self->priv->filter = NULL;
+  self->priv->filter_data = NULL;
 
-  signals[READY] =
-    g_signal_new ("ready",
-        G_OBJECT_CLASS_TYPE (object_class),
-        G_SIGNAL_RUN_LAST,
-        0,
-        NULL, NULL,
-        g_cclosure_marshal_generic,
-        G_TYPE_NONE,
-        0);
+  self->priv->manager = tp_account_manager_dup ();
 
-  g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
+  tp_g_signal_connect_object (self->priv->manager, "account-validity-changed",
+      G_CALLBACK (account_chooser_account_validity_changed_cb), self, 0);
+
+  tp_g_signal_connect_object (self->priv->manager, "account-removed",
+      G_CALLBACK (account_chooser_account_removed_cb), self, 0);
+
+  /* Make sure we'll have the capabilities feature on TpAccount's connection */
+  factory = tp_proxy_get_factory (self->priv->manager);
+
+  tp_simple_client_factory_add_account_features_varargs (factory,
+      TP_ACCOUNT_FEATURE_CONNECTION, NULL);
+  tp_simple_client_factory_add_connection_features_varargs (factory,
+      TP_CONNECTION_FEATURE_CAPABILITIES, NULL);
+}
+
+static gint
+account_cmp (GtkTreeModel *model,
+    GtkTreeIter *a,
+    GtkTreeIter *b,
+    gpointer user_data)
+{
+  RowType a_type, b_type;
+  gboolean a_enabled, b_enabled;
+  gchar *a_text, *b_text;
+  gint result;
+
+  gtk_tree_model_get (model, a,
+      COL_ACCOUNT_ENABLED, &a_enabled,
+      COL_ACCOUNT_ROW_TYPE, &a_type,
+      -1);
+  gtk_tree_model_get (model, b,
+      COL_ACCOUNT_ENABLED, &b_enabled,
+      COL_ACCOUNT_ROW_TYPE, &b_type,
+      -1);
+
+  /* This assumes that we have at most one of each special row type. */
+  if (a_type != b_type)
+    /* Display higher-numbered special row types first. */
+    return (b_type - a_type);
+
+  /* Enabled accounts are displayed first */
+  if (a_enabled != b_enabled)
+    return a_enabled ? -1: 1;
+
+  gtk_tree_model_get (model, a, COL_ACCOUNT_TEXT, &a_text, -1);
+  gtk_tree_model_get (model, b, COL_ACCOUNT_TEXT, &b_text, -1);
+
+  if (a_text == b_text)
+    result = 0;
+  else if (a_text == NULL)
+    result = 1;
+  else if (b_text == NULL)
+    result = -1;
+  else
+    result = g_ascii_strcasecmp (a_text, b_text);
+
+  g_free (a_text);
+  g_free (b_text);
+
+  return result;
 }
 
 static void
-empathy_account_chooser_init (EmpathyAccountChooser *self)
+account_connection_notify_cb (TpAccount *account,
+    GParamSpec *spec,
+    EmpathyAccountChooser *self)
 {
-  EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
-    EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
+  update_account (self, account);
+}
 
-  self->priv = priv;
-  priv->set_active_item = FALSE;
-  priv->account_manually_set = FALSE;
-  priv->filter = NULL;
-  priv->filter_data = NULL;
+static void
+account_manager_prepared_cb (GObject *source_object,
+    GAsyncResult *result,
+    gpointer user_data)
+{
+  GList *accounts, *l;
+  TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
+  EmpathyAccountChooser *self = user_data;
+  GError *error = NULL;
 
-  priv->manager = tp_account_manager_dup ();
+  if (!tp_proxy_prepare_finish (manager, result, &error))
+    {
+      DEBUG ("Failed to prepare account manager: %s", error->message);
+      g_error_free (error);
+      return;
+    }
 
-  g_signal_connect (priv->manager, "account-validity-changed",
-      G_CALLBACK (account_chooser_account_validity_changed_cb),
-      self);
-  g_signal_connect (priv->manager, "account-removed",
-      G_CALLBACK (account_chooser_account_removed_cb),
-      self);
+  accounts = tp_account_manager_dup_valid_accounts (manager);
+
+  for (l = accounts; l != NULL; l = l->next)
+    {
+      TpAccount *account = l->data;
+
+      account_chooser_account_add_foreach (account, self);
+
+      tp_g_signal_connect_object (account, "status-changed",
+          G_CALLBACK (account_chooser_status_changed_cb),
+          self, 0);
+
+      /* We generally use the TpConnection from the account to filter it so,
+       * just relying on the account status is not enough. In some case we the
+       * status change can be notified while the TpConnection is still
+       * preparing. */
+      tp_g_signal_connect_object (account, "notify::connection",
+          G_CALLBACK (account_connection_notify_cb),
+          self, 0);
+    }
+
+  g_list_free_full (accounts, g_object_unref);
+
+  if (self->priv->select_when_ready != NULL)
+    {
+      select_account (self, self->priv->select_when_ready);
+
+      g_clear_object (&self->priv->select_when_ready);
+    }
+
+  self->priv->ready = TRUE;
+  g_signal_emit (self, signals[READY], 0);
 }
 
 static void
 account_chooser_constructed (GObject *object)
 {
   EmpathyAccountChooser *self = (EmpathyAccountChooser *) object;
+  GtkListStore *store;
+  GtkCellRenderer *renderer;
+  GtkComboBox *combobox;
+
+  /* Set up combo box with new store */
+  combobox = GTK_COMBO_BOX (self);
+
+  gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
+
+  store = gtk_list_store_new (COL_ACCOUNT_COUNT,
+      GDK_TYPE_PIXBUF,  /* Image */
+      G_TYPE_STRING,    /* Name */
+      G_TYPE_BOOLEAN,   /* Enabled */
+      G_TYPE_UINT,      /* Row type */
+      TP_TYPE_ACCOUNT);
+
+  gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
+    account_cmp, self, NULL);
+  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
+    GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
+
+  gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
+
+  renderer = gtk_cell_renderer_pixbuf_new ();
+  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
+  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
+      "pixbuf", COL_ACCOUNT_IMAGE,
+      "sensitive", COL_ACCOUNT_ENABLED,
+      NULL);
+
+  renderer = gtk_cell_renderer_text_new ();
+  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
+  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
+      "text", COL_ACCOUNT_TEXT,
+      "sensitive", COL_ACCOUNT_ENABLED,
+      NULL);
+
+  /* Populate accounts */
+  tp_proxy_prepare_async (self->priv->manager, NULL,
+      account_manager_prepared_cb, self);
+
+  g_object_unref (store);
 
-  account_chooser_setup (self);
 }
 
 static void
-account_chooser_finalize (GObject *object)
+account_chooser_dispose (GObject *object)
 {
-  EmpathyAccountChooserPriv *priv = GET_PRIV (object);
+  EmpathyAccountChooser *self = EMPATHY_ACCOUNT_CHOOSER (object);
 
-  g_signal_handlers_disconnect_by_func (priv->manager,
-      account_chooser_account_validity_changed_cb,
-      object);
-  g_signal_handlers_disconnect_by_func (priv->manager,
-      account_chooser_account_removed_cb,
-      object);
-  g_object_unref (priv->manager);
+  g_clear_object (&self->priv->manager);
+  g_clear_object (&self->priv->select_when_ready);
 
-  G_OBJECT_CLASS (empathy_account_chooser_parent_class)->finalize (object);
+  G_OBJECT_CLASS (empathy_account_chooser_parent_class)->dispose (object);
 }
 
 static void
@@ -269,14 +372,12 @@ account_chooser_get_property (GObject *object,
     GValue *value,
     GParamSpec *pspec)
 {
-  EmpathyAccountChooserPriv *priv;
-
-  priv = GET_PRIV (object);
+  EmpathyAccountChooser *self = (EmpathyAccountChooser *) object;
 
   switch (param_id)
     {
       case PROP_HAS_ALL_OPTION:
-        g_value_set_boolean (value, priv->has_all_option);
+        g_value_set_boolean (value, self->priv->has_all_option);
         break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -302,6 +403,42 @@ account_chooser_set_property (GObject *object,
     };
 }
 
+static void
+empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->constructed = account_chooser_constructed;
+  object_class->dispose = account_chooser_dispose;
+  object_class->get_property = account_chooser_get_property;
+  object_class->set_property = account_chooser_set_property;
+
+  /**
+   * EmpathyAccountChooser:has-all-option:
+   *
+   * Have an additional option in the list to mean all accounts.
+   */
+  g_object_class_install_property (object_class,
+      PROP_HAS_ALL_OPTION,
+      g_param_spec_boolean ("has-all-option",
+        "Has All Option",
+        "Have a separate option in the list to mean ALL accounts",
+        FALSE,
+        G_PARAM_READWRITE));
+
+  signals[READY] =
+    g_signal_new ("ready",
+        G_OBJECT_CLASS_TYPE (object_class),
+        G_SIGNAL_RUN_LAST,
+        0,
+        NULL, NULL,
+        g_cclosure_marshal_generic,
+        G_TYPE_NONE,
+        0);
+
+  g_type_class_add_private (object_class, sizeof (EmpathyAccountChooserPriv));
+}
+
 /**
  * empathy_account_chooser_new:
  *
@@ -322,16 +459,13 @@ empathy_account_chooser_new (void)
 gboolean
 empathy_account_chooser_has_all_selected (EmpathyAccountChooser *self)
 {
-  EmpathyAccountChooserPriv *priv;
   GtkTreeModel *model;
   GtkTreeIter iter;
   RowType type;
 
   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
 
-  priv = GET_PRIV (self);
-
-  g_return_val_if_fail (priv->has_all_option == TRUE, FALSE);
+  g_return_val_if_fail (self->priv->has_all_option == TRUE, FALSE);
 
   model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &iter))
@@ -403,20 +537,10 @@ empathy_account_chooser_get_connection (EmpathyAccountChooser *self)
   return connection;
 }
 
-/**
- * empathy_account_chooser_set_account:
- * @self: an #EmpathyAccountChooser
- * @account: a #TpAccount
- *
- * Sets the currently selected account to @account, if it exists in the list.
- *
- * Return value: whether the chooser was set to @account.
- */
-gboolean
-empathy_account_chooser_set_account (EmpathyAccountChooser *self,
+static gboolean
+select_account (EmpathyAccountChooser *self,
     TpAccount *account)
 {
-  EmpathyAccountChooserPriv *priv;
   GtkComboBox *combobox;
   GtkTreeModel *model;
   GtkTreeIter iter;
@@ -424,8 +548,6 @@ empathy_account_chooser_set_account (EmpathyAccountChooser *self,
 
   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
 
-  priv = GET_PRIV (self);
-
   combobox = GTK_COMBO_BOX (self);
   model = gtk_combo_box_get_model (combobox);
   gtk_combo_box_get_active_iter (combobox, &iter);
@@ -438,24 +560,47 @@ empathy_account_chooser_set_account (EmpathyAccountChooser *self,
       (GtkTreeModelForeachFunc) account_chooser_set_account_foreach,
       &data);
 
-  priv->account_manually_set = data.set;
+  self->priv->account_manually_set = data.set;
 
   return data.set;
 }
 
+/**
+ * empathy_account_chooser_set_account:
+ * @self: an #EmpathyAccountChooser
+ * @account: a #TpAccount
+ *
+ * Sets the currently selected account to @account, if it exists in the list.
+ *
+ * Return value: whether the chooser was set to @account.
+ */
+gboolean
+empathy_account_chooser_set_account (EmpathyAccountChooser *self,
+    TpAccount *account)
+{
+  if (self->priv->ready)
+    return select_account (self, account);
+
+  /* Account chooser is not ready yet, we'll try selecting the account once it
+   * is */
+  g_clear_object (&self->priv->select_when_ready);
+
+  if (account != NULL)
+    self->priv->select_when_ready = g_object_ref (account);
+
+  return FALSE;
+}
+
 void
 empathy_account_chooser_set_all (EmpathyAccountChooser *self)
 {
-  EmpathyAccountChooserPriv *priv;
   GtkComboBox *combobox;
   GtkTreeModel *model;
   GtkTreeIter iter;
 
   g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
 
-  priv = GET_PRIV (self);
-
-  g_return_if_fail (priv->has_all_option);
+  g_return_if_fail (self->priv->has_all_option);
 
   combobox = GTK_COMBO_BOX (self);
   model = gtk_combo_box_get_model (combobox);
@@ -464,7 +609,7 @@ empathy_account_chooser_set_all (EmpathyAccountChooser *self)
     {
       /* 'All accounts' is the first row */
       gtk_combo_box_set_active_iter (combobox, &iter);
-      priv->account_manually_set = TRUE;
+      self->priv->account_manually_set = TRUE;
     }
 }
 
@@ -481,13 +626,9 @@ empathy_account_chooser_set_all (EmpathyAccountChooser *self)
 gboolean
 empathy_account_chooser_get_has_all_option (EmpathyAccountChooser *self)
 {
-  EmpathyAccountChooserPriv *priv;
-
   g_return_val_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self), FALSE);
 
-  priv = GET_PRIV (self);
-
-  return priv->has_all_option;
+  return self->priv->has_all_option;
 }
 
 /**
@@ -502,7 +643,6 @@ void
 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *self,
     gboolean has_all_option)
 {
-  EmpathyAccountChooserPriv *priv;
   GtkComboBox *combobox;
   GtkListStore *store;
   GtkTreeModel *model;
@@ -510,16 +650,14 @@ empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *self,
 
   g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
 
-  priv = GET_PRIV (self);
-
-  if (priv->has_all_option == has_all_option)
+  if (self->priv->has_all_option == has_all_option)
     return;
 
   combobox = GTK_COMBO_BOX (self);
   model = gtk_combo_box_get_model (combobox);
   store = GTK_LIST_STORE (model);
 
-  priv->has_all_option = has_all_option;
+  self->priv->has_all_option = has_all_option;
 
   /*
    * The first 2 options are the ALL and separator
@@ -567,140 +705,6 @@ empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *self,
   g_object_notify (G_OBJECT (self), "has-all-option");
 }
 
-static void
-account_manager_prepared_cb (GObject *source_object,
-    GAsyncResult *result,
-    gpointer user_data)
-{
-  GList *accounts, *l;
-  TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
-  EmpathyAccountChooser *self = user_data;
-  EmpathyAccountChooserPriv *priv = GET_PRIV (self);
-  GError *error = NULL;
-
-  if (!tp_proxy_prepare_finish (manager, result, &error))
-    {
-      DEBUG ("Failed to prepare account manager: %s", error->message);
-      g_error_free (error);
-      return;
-    }
-
-  accounts = tp_account_manager_get_valid_accounts (manager);
-
-  for (l = accounts; l != NULL; l = l->next)
-    {
-      TpAccount *account = l->data;
-
-      account_chooser_account_add_foreach (account, self);
-
-      tp_g_signal_connect_object (account, "status-changed",
-          G_CALLBACK (account_chooser_status_changed_cb),
-          self, 0);
-    }
-
-  g_list_free (accounts);
-
-  priv->ready = TRUE;
-  g_signal_emit (self, signals[READY], 0);
-}
-
-static gint
-account_cmp (GtkTreeModel *model,
-    GtkTreeIter *a,
-    GtkTreeIter *b,
-    gpointer user_data)
-{
-  RowType a_type, b_type;
-  gboolean a_enabled, b_enabled;
-  gchar *a_text, *b_text;
-  gint result;
-
-  gtk_tree_model_get (model, a,
-      COL_ACCOUNT_ENABLED, &a_enabled,
-      COL_ACCOUNT_ROW_TYPE, &a_type,
-      -1);
-  gtk_tree_model_get (model, b,
-      COL_ACCOUNT_ENABLED, &b_enabled,
-      COL_ACCOUNT_ROW_TYPE, &b_type,
-      -1);
-
-  /* This assumes that we have at most one of each special row type. */
-  if (a_type != b_type)
-    /* Display higher-numbered special row types first. */
-    return (b_type - a_type);
-
-  /* Enabled accounts are displayed first */
-  if (a_enabled != b_enabled)
-    return a_enabled ? -1: 1;
-
-  gtk_tree_model_get (model, a, COL_ACCOUNT_TEXT, &a_text, -1);
-  gtk_tree_model_get (model, b, COL_ACCOUNT_TEXT, &b_text, -1);
-
-  if (a_text == b_text)
-    result = 0;
-  else if (a_text == NULL)
-    result = 1;
-  else if (b_text == NULL)
-    result = -1;
-  else
-    result = g_ascii_strcasecmp (a_text, b_text);
-
-  g_free (a_text);
-  g_free (b_text);
-
-  return result;
-}
-
-static void
-account_chooser_setup (EmpathyAccountChooser *self)
-{
-  EmpathyAccountChooserPriv *priv;
-  GtkListStore *store;
-  GtkCellRenderer *renderer;
-  GtkComboBox *combobox;
-
-  priv = GET_PRIV (self);
-
-  /* Set up combo box with new store */
-  combobox = GTK_COMBO_BOX (self);
-
-  gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
-
-  store = gtk_list_store_new (COL_ACCOUNT_COUNT,
-      GDK_TYPE_PIXBUF,  /* Image */
-      G_TYPE_STRING,    /* Name */
-      G_TYPE_BOOLEAN,   /* Enabled */
-      G_TYPE_UINT,      /* Row type */
-      TP_TYPE_ACCOUNT);
-
-  gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
-    account_cmp, self, NULL);
-  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
-    GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
-
-  gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (store));
-
-  renderer = gtk_cell_renderer_pixbuf_new ();
-  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
-  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
-      "pixbuf", COL_ACCOUNT_IMAGE,
-      "sensitive", COL_ACCOUNT_ENABLED,
-      NULL);
-
-  renderer = gtk_cell_renderer_text_new ();
-  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
-  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
-      "text", COL_ACCOUNT_TEXT,
-      "sensitive", COL_ACCOUNT_ENABLED,
-      NULL);
-
-  /* Populate accounts */
-  tp_proxy_prepare_async (priv->manager, NULL, account_manager_prepared_cb,
-      self);
-
-  g_object_unref (store);
-}
-
 static void
 account_chooser_account_validity_changed_cb (TpAccountManager *manager,
     TpAccount *account,
@@ -783,7 +787,7 @@ account_chooser_find_account_foreach (GtkTreeModel *model,
 static gboolean
 account_chooser_find_account (EmpathyAccountChooser *self,
     TpAccount *account,
-    GtkTreeIter*iter)
+    GtkTreeIter *iter)
 {
   GtkListStore *store;
   GtkComboBox *combobox;
@@ -822,7 +826,6 @@ account_chooser_filter_ready_cb (gboolean is_enabled,
 {
   FilterResultCallbackData *fr_data = data;
   EmpathyAccountChooser *self;
-  EmpathyAccountChooserPriv *priv;
   TpAccount *account;
   GtkTreeIter *iter;
   GtkListStore *store;
@@ -831,7 +834,6 @@ account_chooser_filter_ready_cb (gboolean is_enabled,
   GdkPixbuf *pixbuf;
 
   self = fr_data->self;
-  priv = GET_PRIV (self);
   account = fr_data->account;
   iter = fr_data->iter;
   combobox = GTK_COMBO_BOX (self);
@@ -851,10 +853,10 @@ account_chooser_filter_ready_cb (gboolean is_enabled,
     g_object_unref (pixbuf);
 
   /* set first connected account as active account */
-  if (priv->account_manually_set == FALSE &&
-      priv->set_active_item == FALSE && is_enabled)
+  if (self->priv->account_manually_set == FALSE &&
+      self->priv->set_active_item == FALSE && is_enabled)
     {
-      priv->set_active_item = TRUE;
+      self->priv->set_active_item = TRUE;
       gtk_combo_box_set_active_iter (combobox, iter);
     }
 
@@ -865,14 +867,11 @@ static void
 account_chooser_update_iter (EmpathyAccountChooser *self,
     GtkTreeIter *iter)
 {
-  EmpathyAccountChooserPriv *priv;
   GtkListStore *store;
   GtkComboBox *combobox;
   TpAccount *account;
   FilterResultCallbackData *data;
 
-  priv = GET_PRIV (self);
-
   combobox = GTK_COMBO_BOX (self);
   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
 
@@ -886,15 +885,25 @@ account_chooser_update_iter (EmpathyAccountChooser *self,
 
   data = filter_result_callback_data_new (self, account, iter);
 
-  if (priv->filter)
-    priv->filter (account, account_chooser_filter_ready_cb,
-            (gpointer) data, priv->filter_data);
+  if (self->priv->filter)
+    self->priv->filter (account, account_chooser_filter_ready_cb,
+            (gpointer) data, self->priv->filter_data);
   else
     account_chooser_filter_ready_cb (TRUE, (gpointer) data);
 
   g_object_unref (account);
 }
 
+static void
+update_account (EmpathyAccountChooser *self,
+    TpAccount *account)
+{
+  GtkTreeIter iter;
+
+  if (account_chooser_find_account (self, account, &iter))
+    account_chooser_update_iter (self, &iter);
+}
+
 static void
 account_chooser_status_changed_cb (TpAccount *account,
     guint old_status,
@@ -905,10 +914,8 @@ account_chooser_status_changed_cb (TpAccount *account,
     gpointer user_data)
 {
   EmpathyAccountChooser *self = user_data;
-  GtkTreeIter iter;
 
-  if (account_chooser_find_account (self, account, &iter))
-    account_chooser_update_iter (self, &iter);
+  update_account (self, account);
 }
 
 static gboolean
@@ -961,6 +968,16 @@ account_chooser_filter_foreach (GtkTreeModel *model,
   return FALSE;
 }
 
+void
+empathy_account_chooser_refilter (EmpathyAccountChooser *self)
+{
+  GtkTreeModel *model;
+
+  self->priv->set_active_item = FALSE;
+  model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
+  gtk_tree_model_foreach (model, account_chooser_filter_foreach, self);
+}
+
 /**
  * empathy_account_chooser_set_filter:
  * @self: an #EmpathyAccountChooser
@@ -975,20 +992,13 @@ empathy_account_chooser_set_filter (EmpathyAccountChooser *self,
     EmpathyAccountChooserFilterFunc filter,
     gpointer user_data)
 {
-  EmpathyAccountChooserPriv *priv;
-  GtkTreeModel *model;
-
   g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
 
-  priv = GET_PRIV (self);
-
-  priv->filter = filter;
-  priv->filter_data = user_data;
+  self->priv->filter = filter;
+  self->priv->filter_data = user_data;
 
   /* Refilter existing data */
-  priv->set_active_item = FALSE;
-  model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
-  gtk_tree_model_foreach (model, account_chooser_filter_foreach, self);
+  empathy_account_chooser_refilter (self);
 }
 
 /**
@@ -1002,6 +1012,34 @@ empathy_account_chooser_set_filter (EmpathyAccountChooser *self,
  * Return value: whether the account indicated by @account is visible.
  */
 
+gboolean
+empathy_account_chooser_is_ready (EmpathyAccountChooser *self)
+{
+  return self->priv->ready;
+}
+
+TpAccount *
+empathy_account_chooser_get_account (EmpathyAccountChooser *self)
+{
+  TpAccount *account;
+
+  account = empathy_account_chooser_dup_account (self);
+  if (account == NULL)
+    return NULL;
+
+  g_object_unref (account);
+
+  return account;
+}
+
+TpAccountManager *
+empathy_account_chooser_get_account_manager (EmpathyAccountChooser *self)
+{
+  return self->priv->manager;
+}
+
+/* Pre-defined filters */
+
 /**
  * empathy_account_chooser_filter_is_connected:
  * @account: a #TpAccount
@@ -1064,33 +1102,3 @@ empathy_account_chooser_filter_supports_chatrooms (TpAccount *account,
 out:
   callback (supported, callback_data);
 }
-
-gboolean
-empathy_account_chooser_is_ready (EmpathyAccountChooser *self)
-{
-  EmpathyAccountChooserPriv *priv = GET_PRIV (self);
-
-  return priv->ready;
-}
-
-TpAccount *
-empathy_account_chooser_get_account (EmpathyAccountChooser *self)
-{
-  TpAccount *account;
-
-  account = empathy_account_chooser_dup_account (self);
-  if (account == NULL)
-    return NULL;
-
-  g_object_unref (account);
-
-  return account;
-}
-
-TpAccountManager *
-empathy_account_chooser_get_account_manager (EmpathyAccountChooser *self)
-{
-  EmpathyAccountChooserPriv *priv = GET_PRIV (self);
-
-  return priv->manager;
-}