[gnome-online-accounts] tplinker: sync the enabled status between Telepathy and GOA accounts



commit 620d7e3850f6c6e98369e34438368beb9a2a67c7
Author: Marco Barisione <marco barisione collabora co uk>
Date:   Tue Jul 9 16:44:50 2013 +0100

    tplinker: sync the enabled status between Telepathy and GOA accounts
    
    https://bugzilla.gnome.org/show_bug.cgi?id=696267

 src/daemon/goatpaccountlinker.c |   88 +++++++++++++++++++++++++++++++++++++-
 1 files changed, 85 insertions(+), 3 deletions(-)
---
diff --git a/src/daemon/goatpaccountlinker.c b/src/daemon/goatpaccountlinker.c
index 665931d..df31c9f 100644
--- a/src/daemon/goatpaccountlinker.c
+++ b/src/daemon/goatpaccountlinker.c
@@ -72,6 +72,72 @@ is_telepathy_account (GoaAccount *goa_account)
 }
 
 static void
+goa_account_chat_disabled_changed_cb (GoaAccount         *goa_account,
+                                      GParamSpec         *spec,
+                                      GoaTpAccountLinker *self)
+{
+  GoaTpAccountLinkerPrivate *priv = self->priv;
+  const gchar *id;
+  TpAccount *tp_account;
+  gboolean tp_enabled;
+  gboolean goa_enabled;
+
+  id = get_id_from_goa_account (goa_account);
+  tp_account = g_hash_table_lookup (priv->tp_accounts, id);
+  if (tp_account == NULL)
+    return;
+
+  goa_enabled = !goa_account_get_chat_disabled (goa_account);
+  tp_enabled = tp_account_is_enabled (tp_account);
+  if (tp_enabled != goa_enabled)
+    {
+      goa_info ("The GOA account %s (Telepathy object path: %s) has been %s, "
+          "propagating to Telepathy",
+          goa_account_get_id (goa_account), id,
+          goa_enabled ? "enabled" : "disabled");
+      tp_account_set_enabled_async (tp_account, goa_enabled, NULL, NULL);
+    }
+}
+
+static void
+tp_account_chat_enabled_changed_cb (TpAccount          *tp_account,
+                                    GParamSpec         *spec,
+                                    GoaTpAccountLinker *self)
+{
+  GoaTpAccountLinkerPrivate *priv = self->priv;
+  const gchar *id;
+  GoaObject *goa_object;
+  GoaAccount *goa_account;
+  gboolean tp_enabled;
+  gboolean goa_enabled;
+
+  id = get_id_from_tp_account (tp_account);
+  goa_object = g_hash_table_lookup (priv->goa_accounts, id);
+  if (goa_object == NULL)
+    return;
+
+  goa_account = goa_object_peek_account (goa_object);
+  goa_enabled = !goa_account_get_chat_disabled (goa_account);
+  tp_enabled = tp_account_is_enabled (tp_account);
+  if (tp_enabled != goa_enabled)
+    {
+      goa_info ("The Telepathy account %s has been %s, propagating to GOA",
+          id, tp_enabled ? "enabled" : "disabled");
+      /* When we set this property, the autogenerated code emits a notify
+       * signal immediately even if the property hasn't changed, so
+       * goa_account_chat_disabled_changed_cb() thinks that the property
+       * changed back to the old value and a cycle starts.
+       * The right notify signal will be emitted later when the property is
+       * actually changed. */
+      g_signal_handlers_block_by_func (goa_account,
+          goa_account_chat_disabled_changed_cb, self);
+      goa_account_set_chat_disabled (goa_account, !tp_enabled);
+      g_signal_handlers_unblock_by_func (goa_account,
+          goa_account_chat_disabled_changed_cb, self);
+    }
+}
+
+static void
 goa_account_created_cb (GoaManager   *manager,
                         GAsyncResult *res,
                         gpointer      user_data)
@@ -114,8 +180,8 @@ create_goa_account (GoaTpAccountLinker *self,
   g_variant_builder_init (&credentials, G_VARIANT_TYPE_VARDICT);
 
   g_variant_builder_init (&details, G_VARIANT_TYPE ("a{ss}"));
-  /* FIXME: we need to sync the enable status */
-  g_variant_builder_add (&details, "{ss}", "ChatEnabled", "true");
+  g_variant_builder_add (&details, "{ss}", "ChatEnabled",
+      tp_account_is_enabled (tp_account) ? "true" : "false");
 
   provider = g_strdup_printf ("telepathy/%s",
       tp_account_get_protocol_name (tp_account));
@@ -146,6 +212,10 @@ tp_account_added (GoaTpAccountLinker *self,
   g_hash_table_replace (priv->tp_accounts, g_strdup (id),
       g_object_ref (tp_account));
 
+  g_signal_connect_object (tp_account, "notify::enabled",
+      G_CALLBACK (tp_account_chat_enabled_changed_cb),
+      self, 0);
+
   goa_object = g_hash_table_lookup (priv->goa_accounts, id);
   if (goa_object == NULL)
     {
@@ -157,6 +227,8 @@ tp_account_added (GoaTpAccountLinker *self,
     {
       goa_debug ("Found a Telepathy account with a matching "
           "GOA account: %s", id);
+      /* Make sure the initial state is synced. */
+      tp_account_chat_enabled_changed_cb (tp_account, NULL, self);
     }
 }
 
@@ -242,6 +314,7 @@ goa_account_added_cb (GoaClient *client,
   GoaTpAccountLinkerPrivate *priv = self->priv;
   GoaAccount *goa_account = goa_object_peek_account (goa_object);
   const gchar *id = NULL;
+  TpAccount *tp_account;
 
   if (!is_telepathy_account (goa_account))
     return;
@@ -250,10 +323,19 @@ goa_account_added_cb (GoaClient *client,
   goa_debug ("GOA account %s for Telepathy account %s added",
       goa_account_get_id (goa_account), id);
 
-  /* FIXME: we need to track when the chat part gets enabled/disabled. */
+  g_signal_connect_object (goa_account, "notify::chat-disabled",
+      G_CALLBACK (goa_account_chat_disabled_changed_cb), self, 0);
 
   g_hash_table_insert (priv->goa_accounts, g_strdup (id),
       g_object_ref (goa_object));
+
+  tp_account = g_hash_table_lookup (priv->tp_accounts, id);
+  if (tp_account != NULL)
+    {
+      /* The chat enabled status may have changed during the creation of the
+       * GOA account, so we need to make sure it's synced. */
+      tp_account_chat_enabled_changed_cb (tp_account, NULL, self);
+    }
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]