[epiphany/wip/ephy-sync: 108/126] sync-bookmarks: Function to upload a bookmark to the server



commit aa442369566cd2db8944e44f28e849377d3e29f5
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Mon Aug 8 21:13:56 2016 +0300

    sync-bookmarks: Function to upload a bookmark to the server

 src/ephy-sync-bookmarks.c |   60 +++++++++++++++++++++++++++++++++++++++++++++
 src/ephy-sync-bookmarks.h |    4 +++
 2 files changed, 64 insertions(+), 0 deletions(-)
---
diff --git a/src/ephy-sync-bookmarks.c b/src/ephy-sync-bookmarks.c
index 4d7d340..742f27a 100644
--- a/src/ephy-sync-bookmarks.c
+++ b/src/ephy-sync-bookmarks.c
@@ -51,6 +51,16 @@ warning_alert (const gchar *text,
              text, message->status_code, message->response_body->data);
 }
 
+static gchar *
+get_storage_endpoint_for_bookmark (EphyBookmark *bookmark)
+{
+  g_return_val_if_fail (EPHY_IS_BOOKMARK (bookmark), NULL);
+
+  return g_strdup_printf ("storage/%s/%s",
+                          EPHY_BOOKMARKS_COLLECTION,
+                          ephy_bookmark_get_id (bookmark));
+}
+
 static void
 create_bso_collection_response_cb (SoupSession *session,
                                    SoupMessage *message,
@@ -102,6 +112,56 @@ ephy_sync_bookmarks_create_storage_collection (void)
 }
 
 static void
+upload_bookmark_to_server_response_cb (SoupSession *session,
+                                       SoupMessage *message,
+                                       gpointer     user_data)
+{
+  EphySyncService *service;
+  EphyBookmark *bookmark;
+  double last_modified;
+
+  bookmark = EPHY_BOOKMARK (user_data);
+
+  if (message->status_code == 200) {
+    last_modified = g_ascii_strtod (message->response_body->data, NULL);
+    ephy_bookmark_set_modified (bookmark, last_modified);
+  } else if (message->status_code == 412) {
+    /* FIXME: A more recent value is on the server. See how to handle this. */
+  } else {
+    warning_alert ("Failed to upload bookmark to the storage server", message);
+  }
+
+  service = ephy_shell_get_global_sync_service (ephy_shell_get_default ());
+  ephy_sync_service_release_next_storage_message (service);
+}
+
+void
+ephy_sync_bookmarks_upload_bookmark_to_server (EphyBookmark *bookmark,
+                                               gboolean      force_upload)
+{
+  EphySyncService *service;
+  gchar *endpoint;
+  gchar *bso;
+  double modified;
+
+  g_return_if_fail (EPHY_IS_BOOKMARK (bookmark));
+
+  service = ephy_shell_get_global_sync_service (ephy_shell_get_default ());
+  endpoint = get_storage_endpoint_for_bookmark (bookmark);
+  bso = ephy_bookmark_to_bso (bookmark);
+  modified = ephy_bookmark_get_modified (bookmark);
+
+  ephy_sync_service_send_storage_message (service, endpoint,
+                                          SOUP_METHOD_PUT, bso, -1,
+                                          force_upload ? -1 : modified,
+                                          upload_bookmark_to_server_response_cb,
+                                          bookmark);
+
+  g_free (endpoint);
+  g_free (bso);
+}
+
+static void
 storage_server_response_default_cb (SoupSession *session,
                                     SoupMessage *message,
                                     gpointer     user_data)
diff --git a/src/ephy-sync-bookmarks.h b/src/ephy-sync-bookmarks.h
index 47f0ccc..db64bdf 100644
--- a/src/ephy-sync-bookmarks.h
+++ b/src/ephy-sync-bookmarks.h
@@ -19,6 +19,7 @@
 #ifndef EPHY_SYNC_BOOKMARKS_H
 #define EPHY_SYNC_BOOKMARKS_H
 
+#include "ephy-bookmark.h"
 #include "ephy-sync-service.h"
 
 #include <glib-object.h>
@@ -27,6 +28,9 @@ G_BEGIN_DECLS
 
 void ephy_sync_bookmarks_create_storage_collection (void);
 
+void ephy_sync_bookmarks_upload_bookmark_to_server (EphyBookmark *bookmark,
+                                                    gboolean      force_upload);
+
 void ephy_sync_bookmarks_check_storage_collection  (void);
 
 void ephy_sync_bookmarks_delete_storage_collection (void);


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