]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-account-chooser.c
use the 48x48 version of the local-xmpp icon
[empathy.git] / libempathy-gtk / empathy-account-chooser.c
index c075768b9d2b3cd01fb07dd109f8e99b2ac4bc25..ae3f1dd154e3538ac0c5eda34f73ef0dd09a2c52 100644 (file)
@@ -65,6 +65,8 @@ struct _EmpathyAccountChooserPriv
   EmpathyAccountChooserFilterFunc filter;
   gpointer filter_data;
   gboolean ready;
+
+  TpAccount *select_when_ready;
 };
 
 typedef struct
@@ -88,6 +90,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 +141,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,
@@ -152,6 +158,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,
@@ -171,6 +181,8 @@ G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser,
 static void
 empathy_account_chooser_init (EmpathyAccountChooser *self)
 {
+  TpSimpleClientFactory *factory;
+
   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
     EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
 
@@ -186,6 +198,14 @@ empathy_account_chooser_init (EmpathyAccountChooser *self)
 
   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
@@ -235,6 +255,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,
@@ -263,10 +291,25 @@ 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);
 
+  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);
 }
@@ -321,13 +364,14 @@ account_chooser_constructed (GObject *object)
 }
 
 static void
-account_chooser_finalize (GObject *object)
+account_chooser_dispose (GObject *object)
 {
-  EmpathyAccountChooser *self = (EmpathyAccountChooser *) object;
+  EmpathyAccountChooser *self = EMPATHY_ACCOUNT_CHOOSER (object);
 
-  g_object_unref (self->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
@@ -373,7 +417,7 @@ empathy_account_chooser_class_init (EmpathyAccountChooserClass *klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
   object_class->constructed = account_chooser_constructed;
-  object_class->finalize = account_chooser_finalize;
+  object_class->dispose = account_chooser_dispose;
   object_class->get_property = account_chooser_get_property;
   object_class->set_property = account_chooser_set_property;
 
@@ -501,17 +545,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;
@@ -538,6 +573,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)
 {
@@ -734,7 +795,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;
@@ -841,6 +902,16 @@ account_chooser_update_iter (EmpathyAccountChooser *self,
   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,
@@ -851,10 +922,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
@@ -907,6 +976,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
@@ -921,17 +1000,13 @@ empathy_account_chooser_set_filter (EmpathyAccountChooser *self,
     EmpathyAccountChooserFilterFunc filter,
     gpointer user_data)
 {
-  GtkTreeModel *model;
-
   g_return_if_fail (EMPATHY_IS_ACCOUNT_CHOOSER (self));
 
   self->priv->filter = filter;
   self->priv->filter_data = user_data;
 
   /* Refilter existing data */
-  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_refilter (self);
 }
 
 /**
@@ -945,6 +1020,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
@@ -1007,29 +1110,3 @@ empathy_account_chooser_filter_supports_chatrooms (TpAccount *account,
 out:
   callback (supported, callback_data);
 }
-
-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;
-}