[epiphany/wip/ephy-sync: 48/48] sync-service: Remove the source ID at sign out and finalize



commit 87909010278d5f5326c4ef0ed7fe1f36a192bea2
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Sat Aug 20 15:00:37 2016 +0300

    sync-service: Remove the source ID at sign out and finalize

 src/ephy-shell.c        |   13 ++++------
 src/ephy-sync-service.c |   56 +++++++++++++++++++++++++++-------------------
 src/ephy-sync-service.h |    7 ++---
 src/prefs-dialog.c      |    5 +--
 4 files changed, 43 insertions(+), 38 deletions(-)
---
diff --git a/src/ephy-shell.c b/src/ephy-shell.c
index 910b170..8d3301c 100644
--- a/src/ephy-shell.c
+++ b/src/ephy-shell.c
@@ -555,6 +555,7 @@ static void
 ephy_shell_init (EphyShell *shell)
 {
   EphyShell **ptr = &ephy_shell;
+  EphySyncService *service;
 
   /* globally accessible singleton */
   g_assert (ephy_shell == NULL);
@@ -562,14 +563,10 @@ ephy_shell_init (EphyShell *shell)
   g_object_add_weak_pointer (G_OBJECT (ephy_shell),
                              (gpointer *)ptr);
 
-  ephy_shell->sync_service = ephy_sync_service_new ();
-  /* Do a start up sync and set a periodical sync afterwards. */
-  if (ephy_sync_service_is_signed_in (ephy_shell->sync_service) == TRUE) {
-    ephy_sync_service_do_periodical_sync (ephy_shell->sync_service);
-    g_timeout_add_seconds (ephy_sync_service_get_sync_frequency (ephy_shell->sync_service),
-                           (GSourceFunc)ephy_sync_service_do_periodical_sync,
-                           ephy_shell->sync_service);
-  }
+  /* Start the periodical sync now. */
+  service = ephy_sync_service_new ();
+  ephy_sync_service_start_periodical_sync (service, TRUE);
+  ephy_shell->sync_service = service;
 }
 
 static void
diff --git a/src/ephy-sync-service.c b/src/ephy-sync-service.c
index 606fcd8..c9d8d16 100644
--- a/src/ephy-sync-service.c
+++ b/src/ephy-sync-service.c
@@ -34,12 +34,13 @@
 #define MOZILLA_FXA_SERVER_URL    "https://api.accounts.firefox.com/v1/";
 #define EPHY_BOOKMARKS_COLLECTION "ephy-bookmarks"
 #define EMAIL_REGEX               "^[a-zA-Z0-9_]([a-zA-Z0-9._]+[a-zA-Z0-9_])?@[a-z0-9.-]+$"
+#define SYNC_FREQUENCY            (15 * 60)
 
 struct _EphySyncService {
   GObject      parent_instance;
 
   SoupSession *session;
-  guint        sync_frequency;
+  guint        source_id;
 
   char        *uid;
   char        *sessionToken;
@@ -560,6 +561,7 @@ ephy_sync_service_finalize (GObject *object)
   if (self->keypair != NULL)
     ephy_sync_crypto_rsa_key_pair_free (self->keypair);
 
+  ephy_sync_service_stop_periodical_sync (self);
   g_queue_free_full (self->storage_queue, (GDestroyNotify) storage_server_request_async_data_free);
 
   G_OBJECT_CLASS (ephy_sync_service_parent_class)->finalize (object);
@@ -594,7 +596,6 @@ ephy_sync_service_init (EphySyncService *self)
 
   self->session = soup_session_new ();
   self->storage_queue = g_queue_new ();
-  self->sync_frequency = 15 * 60;
 
   email = g_settings_get_string (EPHY_SETTINGS_MAIN, EPHY_PREFS_SYNC_USER);
 
@@ -663,23 +664,6 @@ ephy_sync_service_set_sync_time (EphySyncService *self,
   g_settings_set_double (EPHY_SETTINGS_MAIN, EPHY_PREFS_SYNC_TIME, time);
 }
 
-guint
-ephy_sync_service_get_sync_frequency (EphySyncService *self)
-{
-  g_return_val_if_fail (EPHY_IS_SYNC_SERVICE (self), G_MAXUINT);
-
-  return self->sync_frequency;
-}
-
-void
-ephy_sync_service_set_sync_frequency (EphySyncService *self,
-                                      guint            sync_frequency)
-{
-  g_return_if_fail (EPHY_IS_SYNC_SERVICE (self));
-
-  self->sync_frequency = sync_frequency;
-}
-
 char *
 ephy_sync_service_get_token (EphySyncService   *self,
                              EphySyncTokenType  type)
@@ -1384,12 +1368,38 @@ ephy_sync_service_sync_bookmarks (EphySyncService *self,
   g_free (endpoint);
 }
 
-gboolean
-ephy_sync_service_do_periodical_sync (EphySyncService *self)
+static gboolean
+do_periodical_sync (gpointer user_data)
 {
-  g_return_val_if_fail (EPHY_IS_SYNC_SERVICE (self), G_SOURCE_REMOVE);
+  EphySyncService *service = EPHY_SYNC_SERVICE (user_data);
 
-  ephy_sync_service_sync_bookmarks (self, FALSE);
+  ephy_sync_service_sync_bookmarks (service, FALSE);
 
   return G_SOURCE_CONTINUE;
 }
+
+void
+ephy_sync_service_start_periodical_sync (EphySyncService *self,
+                                         gboolean         now)
+{
+  g_return_if_fail (EPHY_IS_SYNC_SERVICE (self));
+
+  if (ephy_sync_service_is_signed_in (self) == FALSE)
+    return;
+
+  if (now == TRUE)
+    do_periodical_sync (self);
+
+  self->source_id = g_timeout_add_seconds (SYNC_FREQUENCY, do_periodical_sync, self);
+}
+
+void
+ephy_sync_service_stop_periodical_sync (EphySyncService *self)
+{
+  g_return_if_fail (EPHY_IS_SYNC_SERVICE (self));
+
+  if (ephy_sync_service_is_signed_in (self) == FALSE)
+    return;
+
+  g_source_remove (self->source_id);
+}
diff --git a/src/ephy-sync-service.h b/src/ephy-sync-service.h
index 0cb8d6b..b50c9a2 100644
--- a/src/ephy-sync-service.h
+++ b/src/ephy-sync-service.h
@@ -39,9 +39,6 @@ void             ephy_sync_service_set_user_email               (EphySyncService
 double           ephy_sync_service_get_sync_time                (EphySyncService *self);
 void             ephy_sync_service_set_sync_time                (EphySyncService *self,
                                                                  double           time);
-guint            ephy_sync_service_get_sync_frequency           (EphySyncService *self);
-void             ephy_sync_service_set_sync_frequency           (EphySyncService *self,
-                                                                 guint            sync_frequency);
 char            *ephy_sync_service_get_token                    (EphySyncService   *self,
                                                                  EphySyncTokenType  type);
 void             ephy_sync_service_set_token                    (EphySyncService   *self,
@@ -78,7 +75,9 @@ void             ephy_sync_service_delete_bookmark              (EphySyncService
                                                                  gboolean         conditional);
 void             ephy_sync_service_sync_bookmarks               (EphySyncService *self,
                                                                  gboolean         first);
-gboolean         ephy_sync_service_do_periodical_sync           (EphySyncService *self);
+void             ephy_sync_service_start_periodical_sync        (EphySyncService *self,
+                                                                 gboolean         now);
+void             ephy_sync_service_stop_periodical_sync         (EphySyncService *self);
 
 G_END_DECLS
 
diff --git a/src/prefs-dialog.c b/src/prefs-dialog.c
index ff306d9..6a12728 100644
--- a/src/prefs-dialog.c
+++ b/src/prefs-dialog.c
@@ -272,9 +272,7 @@ server_message_received_cb (WebKitUserContentManager *manager,
 
     /* Do a first time sync and set a periodical sync afterwards. */
     ephy_sync_service_sync_bookmarks (service, TRUE);
-    g_timeout_add_seconds (ephy_sync_service_get_sync_frequency (service),
-                           (GSourceFunc) ephy_sync_service_do_periodical_sync,
-                           service);
+    ephy_sync_service_start_periodical_sync (service, FALSE);
 
     /* Translators: the %s refers to the email of the currently logged in user. */
     text = g_strdup_printf (_("Currently logged in as <b>%s</b>"), email);
@@ -340,6 +338,7 @@ on_sync_sign_out_button_clicked (GtkWidget   *button,
   service = ephy_shell_get_sync_service (ephy_shell_get_default ());
 
   /* Destroy session and delete tokens. */
+  ephy_sync_service_stop_periodical_sync (service);
   ephy_sync_service_destroy_session (service, NULL);
   ephy_sync_service_clear_storage_credentials (service);
   ephy_sync_service_clear_tokens (service);


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