]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-account-chooser.c
Use a flat namespace for internal includes
[empathy.git] / libempathy-gtk / empathy-account-chooser.c
index c0df6a6a60b7313558889edeadf2d3cb92bdf793..a272309e589f222fdcaf9832b568d2eba9d03722 100644 (file)
 
 #include "config.h"
 
-#include <string.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
@@ -65,6 +57,8 @@ struct _EmpathyAccountChooserPriv
   EmpathyAccountChooserFilterFunc filter;
   gpointer filter_data;
   gboolean ready;
+
+  TpAccount *select_when_ready;
 };
 
 typedef struct
@@ -88,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);
@@ -135,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,
@@ -154,6 +152,8 @@ static gboolean account_chooser_set_account_foreach (GtkTreeModel *model,
     SetAccountData *data);
 static void update_account (EmpathyAccountChooser *self,
     TpAccount *account);
+static gboolean select_account (EmpathyAccountChooser *self,
+    TpAccount *account);
 
 enum {
   PROP_0,
@@ -247,6 +247,14 @@ account_cmp (GtkTreeModel *model,
   return result;
 }
 
+static void
+account_connection_notify_cb (TpAccount *account,
+    GParamSpec *spec,
+    EmpathyAccountChooser *self)
+{
+  update_account (self, account);
+}
+
 static void
 account_manager_prepared_cb (GObject *source_object,
     GAsyncResult *result,
@@ -264,7 +272,7 @@ account_manager_prepared_cb (GObject *source_object,
       return;
     }
 
-  accounts = tp_account_manager_get_valid_accounts (manager);
+  accounts = tp_account_manager_dup_valid_accounts (manager);
 
   for (l = accounts; l != NULL; l = l->next)
     {
@@ -275,9 +283,24 @@ account_manager_prepared_cb (GObject *source_object,
       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 (accounts);
+  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);
@@ -338,6 +361,7 @@ account_chooser_dispose (GObject *object)
   EmpathyAccountChooser *self = EMPATHY_ACCOUNT_CHOOSER (object);
 
   g_clear_object (&self->priv->manager);
+  g_clear_object (&self->priv->select_when_ready);
 
   G_OBJECT_CLASS (empathy_account_chooser_parent_class)->dispose (object);
 }
@@ -513,17 +537,8 @@ 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)
 {
   GtkComboBox *combobox;
@@ -550,6 +565,32 @@ empathy_account_chooser_set_account (EmpathyAccountChooser *self,
   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)
 {
@@ -746,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;