[epiphany/wip/ephy-sync: 125/126] sync-service: Fix merge_local_and_remote_bookmarks_response_cb()



commit 8ccb03fba514d8205240e57710c0cbc85d9f1f19
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Tue Aug 16 17:17:40 2016 +0300

    sync-service: Fix merge_local_and_remote_bookmarks_response_cb()

 src/ephy-sync-service.c |  253 ++++++++++++++++++++++++----------------------
 1 files changed, 132 insertions(+), 121 deletions(-)
---
diff --git a/src/ephy-sync-service.c b/src/ephy-sync-service.c
index 962a5ac..6c9fc2e 100644
--- a/src/ephy-sync-service.c
+++ b/src/ephy-sync-service.c
@@ -1061,146 +1061,157 @@ merge_local_and_remote_bookmarks_response_cb (SoupSession *session,
                                               SoupMessage *message,
                                               gpointer     user_data)
 {
-    EphySyncService *service;
-    EphyBookmarksManager *manager;
-    GSequence *bookmarks;
-    JsonParser *parser;
-    JsonArray *array;
-    GList *remotes = NULL;
-    GList *locals = NULL;
-    gboolean *marked_locals = NULL;
-    gboolean *marked_remotes = NULL;
-    gsize i;
-
-    service = ephy_shell_get_global_sync_service (ephy_shell_get_default ());
-    manager = ephy_shell_get_bookmarks_manager (ephy_shell_get_default ());
-    bookmarks = ephy_bookmarks_manager_get_bookmarks (manager);
-    parser = json_parser_new ();
-    json_parser_load_from_data (parser, message->response_body->data, -1, NULL);
-    array = json_node_get_array (json_parser_get_root (parser));
-
-    /* Convert all BSOs to EphyBookmark objects. */
-    for (i = 0; i < json_array_get_length (array); i++) {
-      JsonObject *bso = json_array_get_object_element (array, i);
-      EphyBookmark *bookmark = ephy_bookmark_from_bso (bso);
+  EphySyncService *service;
+  EphyBookmarksManager *manager;
+  GSequence *bookmarks;
+  JsonParser *parser;
+  JsonArray *array;
+  GList *remotes = NULL;
+  GList *locals = NULL;
+  const gchar *timestamp;
+  gboolean *marked_locals = NULL;
+  gboolean *marked_remotes = NULL;
+  double server_time;
+  gsize i;
 
-      if (g_strcmp0 (ephy_bookmark_get_id (bookmark), DUMMY_BOOKMARK_ID) == 0) {
-        g_object_unref (bookmark);
-        continue;
-      }
+  service = ephy_shell_get_global_sync_service (ephy_shell_get_default ());
+  manager = ephy_shell_get_bookmarks_manager (ephy_shell_get_default ());
+  bookmarks = ephy_bookmarks_manager_get_bookmarks (manager);
+  parser = json_parser_new ();
+  json_parser_load_from_data (parser, message->response_body->data, -1, NULL);
 
-      remotes = g_list_append (remotes, bookmark);
-    }
+  if (message->status_code != 200) {
+    LOG ("Failed to merge local and remote bookmarks. Status code: %u, response: %s",
+         message->status_code, message->response_body->data);
+    goto out;
+  }
 
-    /* If we have no remote bookmarks, then we directly upload all the local
-     * bookmarks to the server.
-     */
-    if (g_list_length (remotes) == 0) {
-      for (GSequenceIter *iter = g_sequence_get_begin_iter (bookmarks);
-           !g_sequence_iter_is_end (iter); iter = g_sequence_iter_next (iter))
-        ephy_sync_service_upload_bookmark_to_server (service, g_sequence_get (iter), TRUE);
-      goto out;
-    }
+  /* Convert all BSOs to EphyBookmark objects. */
+  array = json_node_get_array (json_parser_get_root (parser));
+  for (i = 0; i < json_array_get_length (array); i++) {
+    JsonObject *bso = json_array_get_object_element (array, i);
+    EphyBookmark *bookmark = ephy_bookmark_from_bso (bso);
 
-    /* If we have no local bookmarks, then we add all the remote bookmarks to the
-     * local instance.
-     */
-    if (g_sequence_is_empty (bookmarks) == TRUE) {
-      for (GList *r = remotes; r != NULL; r = r->next)
-        ephy_bookmarks_manager_add_bookmark (manager, EPHY_BOOKMARK (r->data));
-      goto out;
-    }
+    if (g_strcmp0 (ephy_bookmark_get_id (bookmark), DUMMY_BOOKMARK_ID) == 0)
+      g_object_unref (bookmark);
+    else
+      remotes = g_list_append (remotes, bookmark);
+  }
 
-    /* If we have both local and remote bookmarks, then we merge them in the
-     * nicest way possible (see the two cases below).
-     */
+  /* If we have no remote bookmarks, then we directly upload all the local
+   * bookmarks to the server.
+   */
+  if (g_list_length (remotes) == 0) {
     for (GSequenceIter *iter = g_sequence_get_begin_iter (bookmarks);
          !g_sequence_iter_is_end (iter); iter = g_sequence_iter_next (iter))
-      locals = g_list_append (locals, g_sequence_get (iter));
-
-    marked_locals = g_malloc0 (g_list_length (locals) * sizeof (gboolean));
-    marked_remotes = g_malloc0 (g_list_length (remotes) * sizeof (gboolean));
-
-    i = 0;
-    for (GList *l = locals; l != NULL; l = l->next, i++) {
-      EphyBookmark *local = EPHY_BOOKMARK (l->data);
-      const gchar *local_id = ephy_bookmark_get_id (local);
-      double local_modified = ephy_bookmark_get_modified (local);
-      const gchar *local_url = ephy_bookmark_get_url (local);
-      gsize j = 0;
-
-      for (GList *r = remotes; r != NULL; r = r->next, j++) {
-        if (marked_remotes[j] == FALSE) {
-          EphyBookmark *remote = EPHY_BOOKMARK (r->data);
-          const gchar *remote_id = ephy_bookmark_get_id (remote);
-          double remote_modified = ephy_bookmark_get_modified (remote);
-          const gchar *remote_url = ephy_bookmark_get_url (remote);
-
-          /* Case 1: same ids.
-           * Since ids are globally unique, having a bookmark on the server with
-           * the same id means that the bookmark has been synced before in the
-           * past. Keep the one with most recent modified timestamp.
-           */
-          if (g_strcmp0 (local_id, remote_id) == 0) {
-            if (local_modified > remote_modified) {
-              ephy_sync_service_upload_bookmark_to_server (service, local, TRUE);
-            } else if (remote_modified > local_modified) {
-              ephy_bookmarks_manager_remove_bookmark (manager, local);
-              ephy_bookmarks_manager_add_bookmark (manager, remote);
-            } else {
-              g_object_unref (remote);
-            }
-            marked_locals[i] = marked_remotes[j] = TRUE;
-            break;
-          }
+      ephy_sync_service_upload_bookmark_to_server (service, g_sequence_get (iter), TRUE);
+    goto out;
+  }
 
-          /* Case 2: different ids, same urls.
-           * Merge tags into the local bookmark, keep the remote id and upload it
-           * to server. The add_tag functions will ignore the duplicates themselves.
-           */
-          if (g_strcmp0 (local_url, remote_url) == 0) {
-            GSequence *tags = ephy_bookmark_get_tags (remote);
-
-            for (GSequenceIter *it = g_sequence_get_begin_iter (tags);
-                 !g_sequence_iter_is_end (it); it = g_sequence_iter_next (it)) {
-              const gchar *tag = g_sequence_get (it);
-              ephy_bookmark_add_tag (local, tag);
-              ephy_bookmarks_manager_add_tag (manager, tag);
-            }
-
-            ephy_bookmark_set_id (local, remote_id);
+  /* If we have no local bookmarks, then we add all the remote bookmarks to the
+   * local instance.
+   */
+  if (g_sequence_is_empty (bookmarks) == TRUE) {
+    for (GList *r = remotes; r != NULL; r = r->next)
+      ephy_bookmarks_manager_add_bookmark (manager, EPHY_BOOKMARK (r->data));
+    goto out;
+  }
+
+  /* If we have both local and remote bookmarks, then we merge them in the
+   * nicest way possible (see the two cases below).
+   */
+  for (GSequenceIter *iter = g_sequence_get_begin_iter (bookmarks);
+       !g_sequence_iter_is_end (iter); iter = g_sequence_iter_next (iter))
+    locals = g_list_append (locals, g_sequence_get (iter));
+
+  marked_locals = g_malloc0 (g_list_length (locals) * sizeof (gboolean));
+  marked_remotes = g_malloc0 (g_list_length (remotes) * sizeof (gboolean));
+
+  i = 0;
+  for (GList *l = locals; l != NULL; l = l->next, i++) {
+    EphyBookmark *local = EPHY_BOOKMARK (l->data);
+    const gchar *local_id = ephy_bookmark_get_id (local);
+    double local_modified = ephy_bookmark_get_modified (local);
+    const gchar *local_url = ephy_bookmark_get_url (local);
+    gsize j = 0;
+
+    for (GList *r = remotes; r != NULL; r = r->next, j++) {
+      if (marked_remotes[j] == FALSE) {
+        EphyBookmark *remote = EPHY_BOOKMARK (r->data);
+        const gchar *remote_id = ephy_bookmark_get_id (remote);
+        double remote_modified = ephy_bookmark_get_modified (remote);
+        const gchar *remote_url = ephy_bookmark_get_url (remote);
+
+        /* Case 1: same ids.
+         * Since ids are globally unique, having a bookmark on the server with
+         * the same id means that the bookmark has been synced before in the
+         * past. Keep the one with most recent modified timestamp.
+         */
+        if (g_strcmp0 (local_id, remote_id) == 0) {
+          if (local_modified > remote_modified) {
             ephy_sync_service_upload_bookmark_to_server (service, local, TRUE);
+          } else if (remote_modified > local_modified) {
+            ephy_bookmarks_manager_remove_bookmark (manager, local);
+            ephy_bookmarks_manager_add_bookmark (manager, remote);
+          } else {
             g_object_unref (remote);
-            marked_locals[i] = marked_remotes[j] = TRUE;
-            break;
           }
+          marked_locals[i] = marked_remotes[j] = TRUE;
+          break;
+        }
+
+        /* Case 2: different ids, same urls.
+         * Merge tags into the local bookmark, keep the remote id and upload it
+         * to server. The add_tag functions will ignore the duplicates themselves.
+         */
+        if (g_strcmp0 (local_url, remote_url) == 0) {
+          GSequence *tags = ephy_bookmark_get_tags (remote);
+
+          for (GSequenceIter *it = g_sequence_get_begin_iter (tags);
+               !g_sequence_iter_is_end (it); it = g_sequence_iter_next (it)) {
+            const gchar *tag = g_sequence_get (it);
+            ephy_bookmark_add_tag (local, tag);
+            ephy_bookmarks_manager_add_tag (manager, tag);
+          }
+
+          ephy_bookmark_set_id (local, remote_id);
+          ephy_sync_service_upload_bookmark_to_server (service, local, TRUE);
+          g_object_unref (remote);
+          marked_locals[i] = marked_remotes[j] = TRUE;
+          break;
         }
       }
     }
+  }
 
-    /* The remaining unmarked local bookmarks will be directly uploaded to the server. */
-    i = 0;
-    for (GList *l = locals; l != NULL; l = l->next, i++)
-      if (marked_locals[i] == FALSE)
-        ephy_sync_service_upload_bookmark_to_server (service, EPHY_BOOKMARK (l->data), TRUE);
+  /* The remaining unmarked local bookmarks will be directly uploaded to the server. */
+  i = 0;
+  for (GList *l = locals; l != NULL; l = l->next, i++)
+    if (marked_locals[i] == FALSE)
+      ephy_sync_service_upload_bookmark_to_server (service, EPHY_BOOKMARK (l->data), TRUE);
 
-    /* The remaining unmarked remote bookmarks will be added to the local instance. */
-    i = 0;
-    for (GList *r = remotes; r != NULL; r = r->next, i++)
-      if (marked_remotes[i] == FALSE)
-        ephy_bookmarks_manager_add_bookmark (manager, EPHY_BOOKMARK (r->data));
+  /* The remaining unmarked remote bookmarks will be added to the local instance. */
+  i = 0;
+  for (GList *r = remotes; r != NULL; r = r->next, i++)
+    if (marked_remotes[i] == FALSE)
+      ephy_bookmarks_manager_add_bookmark (manager, EPHY_BOOKMARK (r->data));
 
-    /* Save changes to file. */
-    ephy_bookmarks_manager_save_to_file_async (manager, NULL, NULL, NULL);
+  /* Save changes to file. */
+  ephy_bookmarks_manager_save_to_file_async (manager, NULL, NULL, NULL);
 
-  out:
-    g_object_unref (parser);
-    g_list_free (locals);
-    g_list_free (remotes);
-    g_free (marked_locals);
-    g_free (marked_remotes);
+  /* Set the sync time. */
+  timestamp = soup_message_headers_get_one (message->response_headers, "X-Weave-Timestamp");
+  server_time = g_ascii_strtod (timestamp, NULL);
+  ephy_sync_service_set_sync_time (service, server_time);
 
-    ephy_sync_service_release_next_storage_message (service);
+out:
+  g_object_unref (parser);
+  g_list_free (locals);
+  g_list_free (remotes);
+  g_free (marked_locals);
+  g_free (marked_remotes);
+
+  ephy_sync_service_release_next_storage_message (service);
 }
 
 void


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