]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-account-chooser.c
Center the 'smiley images' inside the menu items
[empathy.git] / libempathy-gtk / empathy-account-chooser.c
index 5cbe6d0844449c8bcf6276d9993e6f9728fe2554..1d6a491c075efe0b9140015b06d138ac1b74f4f1 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"
+#include <tp-account-widgets/tpaw-pixbuf-utils.h>
 
 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
-#include <libempathy/empathy-debug.h>
+#include "empathy-debug.h"
 
 /**
  * SECTION:empathy-account-chooser
@@ -56,9 +47,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 +56,9 @@ typedef struct
   EmpathyAccountChooserFilterFunc filter;
   gpointer filter_data;
   gboolean ready;
-} EmpathyAccountChooserPriv;
+
+  TpAccount *select_when_ready;
+};
 
 typedef struct
 {
@@ -90,6 +81,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);
@@ -137,7 +132,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 +149,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,
@@ -173,23 +172,31 @@ G_DEFINE_TYPE (EmpathyAccountChooser, empathy_account_chooser,
 static void
 empathy_account_chooser_init (EmpathyAccountChooser *self)
 {
-  EmpathyAccountChooserPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+  TpSimpleClientFactory *factory;
+
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
     EMPATHY_TYPE_ACCOUNT_CHOOSER, EmpathyAccountChooserPriv);
 
-  self->priv = priv;
-  priv->set_active_item = FALSE;
-  priv->account_manually_set = FALSE;
-  priv->filter = NULL;
-  priv->filter_data = NULL;
+  self->priv->set_active_item = FALSE;
+  self->priv->account_manually_set = FALSE;
+  self->priv->filter = NULL;
+  self->priv->filter_data = NULL;
+
+  self->priv->manager = tp_account_manager_dup ();
 
-  priv->manager = tp_account_manager_dup ();
+  tp_g_signal_connect_object (self->priv->manager, "account-validity-changed",
+      G_CALLBACK (account_chooser_account_validity_changed_cb), self, 0);
 
-  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);
+  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
@@ -239,6 +246,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,
@@ -247,7 +262,6 @@ account_manager_prepared_cb (GObject *source_object,
   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))
@@ -257,7 +271,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)
     {
@@ -268,11 +282,26 @@ 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);
+    }
 
-  priv->ready = TRUE;
+  self->priv->ready = TRUE;
   g_signal_emit (self, signals[READY], 0);
 }
 
@@ -280,12 +309,13 @@ static void
 account_chooser_constructed (GObject *object)
 {
   EmpathyAccountChooser *self = (EmpathyAccountChooser *) object;
-  EmpathyAccountChooserPriv *priv;
   GtkListStore *store;
   GtkCellRenderer *renderer;
   GtkComboBox *combobox;
 
-  priv = GET_PRIV (self);
+  if (G_OBJECT_CLASS (empathy_account_chooser_parent_class)->constructed)
+    G_OBJECT_CLASS
+      (empathy_account_chooser_parent_class)->constructed (object);
 
   /* Set up combo box with new store */
   combobox = GTK_COMBO_BOX (self);
@@ -321,27 +351,22 @@ account_chooser_constructed (GObject *object)
       NULL);
 
   /* Populate accounts */
-  tp_proxy_prepare_async (priv->manager, NULL, account_manager_prepared_cb,
-      self);
+  tp_proxy_prepare_async (self->priv->manager, NULL,
+      account_manager_prepared_cb, self);
 
   g_object_unref (store);
 
 }
 
 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
@@ -350,14 +375,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);
@@ -389,7 +412,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;
 
@@ -439,16 +462,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))
@@ -520,20 +540,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;
@@ -541,8 +551,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);
@@ -555,24 +563,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);
@@ -581,7 +612,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;
     }
 }
 
@@ -598,13 +629,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;
 }
 
 /**
@@ -619,7 +646,6 @@ void
 empathy_account_chooser_set_has_all_option (EmpathyAccountChooser *self,
     gboolean has_all_option)
 {
-  EmpathyAccountChooserPriv *priv;
   GtkComboBox *combobox;
   GtkListStore *store;
   GtkTreeModel *model;
@@ -627,16 +653,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
@@ -766,7 +790,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;
@@ -805,7 +829,6 @@ account_chooser_filter_ready_cb (gboolean is_enabled,
 {
   FilterResultCallbackData *fr_data = data;
   EmpathyAccountChooser *self;
-  EmpathyAccountChooserPriv *priv;
   TpAccount *account;
   GtkTreeIter *iter;
   GtkListStore *store;
@@ -814,14 +837,13 @@ 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);
   store = GTK_LIST_STORE (gtk_combo_box_get_model (combobox));
 
   icon_name = tp_account_get_icon_name (account);
-  pixbuf = empathy_pixbuf_from_icon_name (icon_name,
+  pixbuf = tpaw_pixbuf_from_icon_name (icon_name,
     GTK_ICON_SIZE_BUTTON);
 
   gtk_list_store_set (store, iter,
@@ -834,10 +856,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);
     }
 
@@ -848,14 +870,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));
 
@@ -869,15 +888,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,
@@ -888,10 +917,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
@@ -944,6 +971,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
@@ -958,20 +995,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);
 }
 
 /**
@@ -985,6 +1015,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
@@ -1047,33 +1105,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;
-}