[epiphany/wip/sync: 8/11] synchronizable: Add default implementation for _to_bso()



commit 56934e7995a7df97daab6af3998fe8dd039a73e6
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Wed Apr 19 16:19:52 2017 +0300

    synchronizable: Add default implementation for _to_bso()

 src/bookmarks/ephy-bookmark.c  |   20 +----------------
 src/sync/ephy-synchronizable.c |   45 +++++++++++++++++++++++++++++++++++++++-
 src/sync/ephy-synchronizable.h |    6 +++-
 3 files changed, 49 insertions(+), 22 deletions(-)
---
diff --git a/src/bookmarks/ephy-bookmark.c b/src/bookmarks/ephy-bookmark.c
index 1fc8c28..e3a7e35 100644
--- a/src/bookmarks/ephy-bookmark.c
+++ b/src/bookmarks/ephy-bookmark.c
@@ -404,25 +404,7 @@ static JsonNode *
 ephy_bookmark_synchronizable_to_bso (EphySynchronizable  *synchronizable,
                                      SyncCryptoKeyBundle *bundle)
 {
-  EphyBookmark *bookmark = EPHY_BOOKMARK (synchronizable);
-  JsonNode *bso;
-  JsonObject *object;
-  char *serialized;
-  char *payload;
-
-  serialized = json_gobject_to_data (G_OBJECT (bookmark), NULL);
-  payload = ephy_sync_crypto_encrypt_record (serialized, bundle);
-  bso = json_node_new (JSON_NODE_OBJECT);
-  object = json_object_new ();
-  json_object_set_string_member (object, "id", bookmark->id);
-  json_object_set_string_member (object, "payload", payload);
-  json_node_set_object (bso, object);
-
-  json_object_unref (object);
-  g_free (payload);
-  g_free (serialized);
-
-  return bso;
+  return ephy_synchronizable_default_to_bso (synchronizable, bundle);
 }
 
 static void
diff --git a/src/sync/ephy-synchronizable.c b/src/sync/ephy-synchronizable.c
index 077c784..250a686 100644
--- a/src/sync/ephy-synchronizable.c
+++ b/src/sync/ephy-synchronizable.c
@@ -188,7 +188,8 @@ ephy_synchronizable_to_bso (EphySynchronizable  *synchronizable,
  * @gtype: the #GType of object to construct
  * @bundle: a %SyncCryptoKeyBundle holding the encryption key and the HMAC key
  *          used to validate and decrypt the Basic Storage Object
- * @is_deleted: return value for a flag that shows whether the object is marked as deleted
+ * @is_deleted: return value for a flag that says whether the object
+ *              was marked as deleted
  *
  * Converts a JSON object representing the Basic Storage Object
  * from the server's point of view (i.e. the %modified field is present)
@@ -267,3 +268,45 @@ out:
 
   return object;
 }
+
+/**
+ * ephy_synchronizable_default_to_bso:
+ * @synchronizable: an #EphySynchronizable
+ * @bundle: a %SyncCryptoKeyBundle holding the encryption key and the HMAC key
+ *          used to validate and encrypt the Basic Storage Object
+ *
+ * Calls the default implementation of the #EphySynchronizable
+ * #EphySynchronizableInterface.to_bso() virtual function.
+ *
+ * This function can be used inside a custom implementation of the
+ * #EphySynchronizableInterface.to_bso() virtual function in lieu of
+ * calling the default implementation through g_type_default_interface_peek().
+ *
+ * Return value: (transfer full): @synchronizable's BSO representation as a #JsonNode
+ **/
+JsonNode *
+ephy_synchronizable_default_to_bso (EphySynchronizable  *synchronizable,
+                                    SyncCryptoKeyBundle *bundle)
+{
+  JsonNode *bso;
+  JsonObject *object;
+  char *serialized;
+  char *payload;
+
+  g_return_val_if_fail (EPHY_IS_SYNCHRONIZABLE (synchronizable), NULL);
+  g_return_val_if_fail (bundle, NULL);
+
+  serialized = json_gobject_to_data (G_OBJECT (synchronizable), NULL);
+  payload = ephy_sync_crypto_encrypt_record (serialized, bundle);
+  bso = json_node_new (JSON_NODE_OBJECT);
+  object = json_object_new ();
+  json_object_set_string_member (object, "id", ephy_synchronizable_get_id (synchronizable));
+  json_object_set_string_member (object, "payload", payload);
+  json_node_set_object (bso, object);
+
+  json_object_unref (object);
+  g_free (payload);
+  g_free (serialized);
+
+  return bso;
+}
diff --git a/src/sync/ephy-synchronizable.h b/src/sync/ephy-synchronizable.h
index cac6b04..ef677b5 100644
--- a/src/sync/ephy-synchronizable.h
+++ b/src/sync/ephy-synchronizable.h
@@ -65,11 +65,13 @@ void                 ephy_synchronizable_set_is_uploaded        (EphySynchroniza
                                                                  gboolean             uploaded);
 JsonNode            *ephy_synchronizable_to_bso                 (EphySynchronizable  *synchronizable,
                                                                  SyncCryptoKeyBundle *bundle);
-/* This can't be an interface method because we lack the EphySynchronizable object.
- * Think of it as more of an utility function. */
+/* This can't be an interface method because we lack the EphySynchronizable object. */
 GObject             *ephy_synchronizable_from_bso               (JsonNode            *bso,
                                                                  GType                gtype,
                                                                  SyncCryptoKeyBundle *bundle,
                                                                  gboolean            *is_deleted);
+/* Default implementations. */
+JsonNode            *ephy_synchronizable_default_to_bso         (EphySynchronizable  *synchronizable,
+                                                                 SyncCryptoKeyBundle *bundle);
 
 G_END_DECLS


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