[gnome-photos/wip/uajain/de_dup] WIP: Migrate insert_remote_object and _set_metadata to share_point_online



commit 649497896c63fd1def049883d0d112983135e1a5
Author: Umang Jain <mailumangjain gmail com>
Date:   Mon Aug 22 04:52:04 2016 +0530

    WIP: Migrate insert_remote_object and _set_metadata to share_point_online

 src/photos-share-point-google.c |  182 ++++--------------------------
 src/photos-share-point-online.c |  240 +++++++++++++++++++++++++++++++++++++++
 src/photos-share-point-online.h |   13 ++
 3 files changed, 273 insertions(+), 162 deletions(-)
---
diff --git a/src/photos-share-point-google.c b/src/photos-share-point-google.c
index 7979adb..479f7eb 100644
--- a/src/photos-share-point-google.c
+++ b/src/photos-share-point-google.c
@@ -24,7 +24,6 @@
 #include <gio/gio.h>
 #include <gdata/gdata.h>
 #include <glib/gi18n.h>
-#include <gexiv2/gexiv2.h>
 
 #include "photos-base-item.h"
 #include "photos-filterable.h"
@@ -33,17 +32,14 @@
 #include "photos-search-context.h"
 #include "photos-share-point-google.h"
 #include "photos-source.h"
-#include "photos-tracker-queue.h"
 #include "photos-utils.h"
 
 
 struct _PhotosSharePointGoogle
 {
   PhotosSharePointOnline parent_instance;
-  PhotosTrackerQueue *queue;
   GDataGoaAuthorizer *authorizer;
   GDataPicasaWebService *service;
-  GError *queue_error;
 };
 
 
@@ -99,176 +95,52 @@ photos_share_point_google_parse_error (PhotosSharePoint *self, GError *error)
 }
 
 
+
 static void
-photos_share_point_google_set_metadata (PhotosBaseItem *item, const gchar *title, const gchar *id, gpointer 
user_data)
+photos_share_point_google_entry_callback (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
-  GExiv2Metadata *meta;
-  GFile *file;
-  GTask *task;
-  const gchar *uri;
-  gchar *identifier;
-  gchar *path;
+  GTask *task = G_TASK (user_data);
   GError *error = NULL;
 
-  meta = gexiv2_metadata_new ();
-  task = G_TASK (user_data);
-
-  /*Can we do something better to get path ?*/
-  uri = photos_base_item_get_uri (item);
-  file = g_file_new_for_uri (uri);
-  path = g_file_get_path (file);
-  gexiv2_metadata_open_path (meta, path, &error);
+  photos_share_point_online_tracker_entry_finish (PHOTOS_SHARE_POINT_ONLINE (source_object), res, &error);
   if (error != NULL)
     {
+      g_warning ("Failed to create tracker entry and embed metadata: %s", error->message);
       g_task_return_error (task, error);
+      goto out;
     }
-  else
-    {
-      if (gexiv2_metadata_has_tag (meta, "Xmp.xmp.gnome-photos.google.title"))
-       gexiv2_metadata_clear_tag (meta, "Xmp.xmp.gnome-photos.google.title");
 
-      if (gexiv2_metadata_has_tag (meta, "Xmp.xmp.gnome-photos.google.id"))
-       gexiv2_metadata_clear_tag (meta, "Xmp.xmp.gnome-photos.google.id");
-
-      gexiv2_metadata_set_tag_string (meta, "Xmp.xmp.gnome-photos.google.title", title);
-
-      identifier = g_strconcat ("google:picasaweb:", id, NULL);
-      gexiv2_metadata_set_tag_string (meta, "Xmp.xmp.gnome-photos.google.id", identifier);
-      gexiv2_metadata_save_file (meta, path, &error);
-
-      if (error)
-       g_task_return_error (task, error);
-      }
+  g_task_return_boolean (task, TRUE);
+  g_print ("Everything was done cleanly!\n");
 
-  gexiv2_metadata_free (meta);
-  g_free (identifier);
-  g_object_unref (file);
+out:
   g_object_unref (task);
-  g_free (path);
-}
-
-
-static void
-photos_share_point_google_relate_objects (PhotosSharePointGoogle *self, GCancellable *cancellable, const 
gchar *obj1, const gchar *obj2)
-{
-  PhotosSearchContextState *state;
-  PhotosQuery *query;
-  GApplication *app;
-
-  app = g_application_get_default ();
-  state = photos_search_context_get_state (PHOTOS_SEARCH_CONTEXT (app));
-
-  query = photos_query_builder_relate_objects (state, obj1, obj2);
-  printf("Related query 1 :%s \n", query->sparql);
-  photos_tracker_queue_update (self->queue,
-                               query->sparql,
-                               cancellable,
-                               NULL,
-                               NULL,
-                               NULL);
-
-  query = photos_query_builder_relate_objects (state, obj2, obj1);
-  printf("Related query 2 :%s \n", query->sparql);
-  photos_tracker_queue_update (self->queue,
-                               query->sparql,
-                               cancellable,
-                               NULL,
-                               NULL,
-                               NULL);
-  photos_query_free (query);
 }
 
 
 static void
-photos_google_share_point_tracker_entry_created (GObject *source_object, GAsyncResult *res, gpointer 
user_data)
+photos_share_point_google_create_tracker_entry (PhotosSharePointGoogle *self, GDataPicasaWebFile 
*file_entry, gpointer user_data)
 {
-  PhotosSharePointGoogle *self;
   PhotosSharePointGoogleShareData *data;
-  TrackerSparqlConnection *connection = TRACKER_SPARQL_CONNECTION (source_object);
-  GCancellable *cancellable;
-  GError *error;
   GTask *task = G_TASK (user_data);
-  GVariant *variant;
-  GVariant *child;
-  const gchar *item_urn;
-  const gchar *remote_urn;
-
-  self = PHOTOS_SHARE_POINT_GOOGLE (g_task_get_source_object (task));
-  cancellable = g_task_get_cancellable (task);
-  data = (PhotosSharePointGoogleShareData *) g_task_get_task_data (task);
-
-  error = NULL;
-  variant = tracker_sparql_connection_update_blank_finish (connection, res, &error);
-  if (error != NULL)
-    {
-      g_task_return_error (task, error);
-      return;
-    }
-
-  child = g_variant_get_child_value (variant, 0); /* variant is now aa{ss} */
-  g_variant_unref (variant);
-  variant = child;
-
-  child = g_variant_get_child_value (variant, 0); /* variant is now s{ss} */
-  g_variant_unref (variant);
-  variant = child;
-
-  child = g_variant_get_child_value (variant, 0); /* variant is now {ss} */
-  g_variant_unref (variant);
-  variant = child;
-
-  child = g_variant_get_child_value (variant, 1);
-  remote_urn = g_variant_dup_string (child, NULL);/*urn of inserted object*/
-  g_variant_unref (child);
-  printf("remote image urn: %s\n", remote_urn);
-
-  item_urn = photos_filterable_get_id (PHOTOS_FILTERABLE (data->item));
-  printf("local image urn: %s\n", item_urn);
-
-  /* We do, remote urn relatedTo base urn mapping here.
-   * In the model, we see if any urn has set its related to property then,
-   * it is definitely a remote urn and it does not show up in overview.
-   */
-
-  photos_share_point_google_relate_objects (self, cancellable, remote_urn, item_urn);
-}
-
-
-static void
-photos_share_point_google_create_tracker_entry (PhotosSharePointGoogle *self,
-                                                GTask *task,
-                                                GDataPicasaWebFile *file_entry,
-                                                GError **error)
-{
-  PhotosSearchContextState *state;
-  PhotosSharePointGoogleShareData *data;
-  PhotosQuery *query;
-  GApplication *app;
   GCancellable *cancellable;
   const gchar *id;
   const gchar *title;
 
-  app = g_application_get_default ();
-  state = photos_search_context_get_state (PHOTOS_SEARCH_CONTEXT (app));
-
   cancellable = g_task_get_cancellable (task);
   data = (PhotosSharePointGoogleShareData *) g_task_get_task_data (task);
 
   id = gdata_entry_get_id (GDATA_ENTRY (file_entry));
   title = gdata_entry_get_title (GDATA_ENTRY (file_entry));
 
-  query = photos_query_builder_insert_remote_object (state, title, id);
-  photos_tracker_queue_update_blank (self->queue,
-                                     query->sparql,
-                                     cancellable,
-                                     photos_google_share_point_tracker_entry_created,
-                                     g_object_ref (task),
-                                     g_object_unref);
-  photos_query_free (query);
-
-  /*Metadata Embed logic flows from here*/
-  data->item = PHOTOS_BASE_ITEM (g_object_ref (data->item));
-  photos_share_point_google_set_metadata (data->item, title, id, g_object_ref (task));
+  photos_share_point_online_tracker_entry_async (PHOTOS_SHARE_POINT_ONLINE (self),
+                                                cancellable,
+                                                data->item,
+                                                title,
+                                                id,
+                                                photos_share_point_google_entry_callback,
+                                                g_object_ref (task));
+  g_object_unref (task);
 }
 
 
@@ -302,16 +174,8 @@ photos_share_point_google_share_save_to_stream (GObject *source_object, GAsyncRe
       goto out;
     }
 
-    /* adding remote object logic flows from here*/
-  error = NULL;
-  photos_share_point_google_create_tracker_entry (self, task, file_entry, &error);
-  if (error != NULL)
-    {
-      g_task_return_error (task, error);
-      goto out;
-    }
-
-  g_task_return_boolean (task, TRUE);
+  /* Map remote to local object and vice-versa */
+  photos_share_point_google_create_tracker_entry (self, file_entry, g_object_ref (task));
 
  out:
   g_clear_object (&file_entry);
@@ -449,7 +313,6 @@ photos_share_point_google_dispose (GObject *object)
   PhotosSharePointGoogle *self = PHOTOS_SHARE_POINT_GOOGLE (object);
 
   g_clear_object (&self->authorizer);
-  g_clear_object (&self->queue);
   g_clear_object (&self->service);
 
   G_OBJECT_CLASS (photos_share_point_google_parent_class)->dispose (object);
@@ -459,17 +322,12 @@ photos_share_point_google_dispose (GObject *object)
 static void
 photos_share_point_google_finalize (GObject *object)
 {
-  PhotosSharePointGoogle *self = PHOTOS_SHARE_POINT_GOOGLE (object);
-
-  g_clear_error (&self->queue_error);
-
   G_OBJECT_CLASS (photos_share_point_google_parent_class)->dispose (object);
 }
 
 static void
 photos_share_point_google_init (PhotosSharePointGoogle *self)
 {
-  self->queue = photos_tracker_queue_dup_singleton (NULL, &self->queue_error);
 }
 
 
diff --git a/src/photos-share-point-online.c b/src/photos-share-point-online.c
index ac6105d..088e5e4 100644
--- a/src/photos-share-point-online.c
+++ b/src/photos-share-point-online.c
@@ -24,17 +24,25 @@
 
 
 #include "config.h"
+#include <tracker-sparql.h>
+#include <gexiv2/gexiv2.h>
 
 #include "photos-filterable.h"
 #include "photos-share-point-online.h"
+#include "photos-tracker-queue.h"
+#include "photos-query.h"
+#include "photos-query-builder.h"
+#include "photos-search-context.h"
 
 
 struct _PhotosSharePointOnlinePrivate
 {
   GIcon *icon;
+  PhotosTrackerQueue *queue;
   PhotosSource *source;
   gchar *id;
   gchar *name;
+  GError *queue_error;
 };
 
 enum
@@ -50,6 +58,38 @@ G_DEFINE_ABSTRACT_TYPE_WITH_CODE (PhotosSharePointOnline, photos_share_point_onl
                                   G_ADD_PRIVATE (PhotosSharePointOnline)
                                   G_IMPLEMENT_INTERFACE (PHOTOS_TYPE_FILTERABLE, 
photos_filterable_interface_init));
 
+typedef struct _PhotosSharePointOnlineData PhotosSharePointOnlineData;
+
+struct _PhotosSharePointOnlineData
+{
+  const gchar *remote_title;
+  const gchar *remote_id;
+  PhotosBaseItem *item;
+};
+
+static PhotosSharePointOnlineData *
+photos_share_point_online_data_new (PhotosBaseItem *item, const gchar *title, const gchar *id)
+{
+  PhotosSharePointOnlineData *data;
+
+  data = g_slice_new0 (PhotosSharePointOnlineData);
+  data->item = g_object_ref (item);
+  data->remote_id = g_strdup (id);
+  data->remote_title = g_strdup (title);
+
+  return data;
+}
+
+
+static void
+photos_share_point_online_data_free (PhotosSharePointOnlineData *data)
+{
+  g_free (data->remote_id);
+  g_free (data->remote_title);
+  g_clear_object (&data->item);
+  g_slice_free (PhotosSharePointOnlineData, data);
+}
+
 
 static GIcon *
 photos_share_point_online_get_icon (PhotosSharePoint *share_point)
@@ -176,6 +216,8 @@ photos_share_point_online_set_property (GObject *object, guint prop_id, const GV
 static void
 photos_share_point_online_init (PhotosSharePointOnline *self)
 {
+  PhotosSharePointOnlinePrivate *priv = photos_share_point_online_get_instance_private (self);
+  priv->queue = photos_tracker_queue_dup_singleton (NULL, &priv->queue_error);
 }
 
 
@@ -217,3 +259,201 @@ photos_share_point_online_get_source (PhotosSharePointOnline *self)
   priv = photos_share_point_online_get_instance_private (self);
   return priv->source;
 }
+
+
+static void
+photos_share_point_online_relate_objects (PhotosSharePointOnline *self, GCancellable *cancellable, const 
gchar *obj1, const gchar *obj2)
+{
+  PhotosSharePointOnlinePrivate *priv = photos_share_point_online_get_instance_private (self);
+  PhotosSearchContextState *state;
+  PhotosQuery *query;
+  GApplication *app;
+
+  app = g_application_get_default ();
+  state = photos_search_context_get_state (PHOTOS_SEARCH_CONTEXT (app));
+
+  query = photos_query_builder_relate_objects (state, obj1, obj2);
+  photos_tracker_queue_update (priv->queue,
+                               query->sparql,
+                               cancellable,
+                               NULL,
+                               NULL,
+                               NULL);
+
+  query = photos_query_builder_relate_objects (state, obj2, obj1);
+
+  photos_tracker_queue_update (priv->queue,
+                               query->sparql,
+                               cancellable,
+                               NULL,
+                               NULL,
+                               NULL);
+  photos_query_free (query);
+  printf("\n\n%s and %s are related now! \n", obj1, obj2);
+}
+
+
+static void
+photos_share_point_online_set_metadata (PhotosSharePointOnlineData *data, gpointer user_data)
+{
+  GExiv2Metadata *meta;
+  GFile *file;
+  GTask *task;
+  const gchar *uri;
+  gchar *identifier;
+  gchar *path;
+  GError *error = NULL;
+
+  meta = gexiv2_metadata_new ();
+  task = G_TASK (user_data);
+
+  /* Can we do something better to get path? */
+  uri = photos_base_item_get_uri (data->item);
+  file = g_file_new_for_uri (uri);
+  path = g_file_get_path (file);
+  gexiv2_metadata_open_path (meta, path, &error);
+  if (error != NULL)
+    {
+      g_task_return_error (task, error);
+      goto out;
+    }
+  else
+    {
+      if (gexiv2_metadata_has_tag (meta, "Xmp.xmp.gnome-photos.google.title"))
+       gexiv2_metadata_clear_tag (meta, "Xmp.xmp.gnome-photos.google.title");
+
+      if (gexiv2_metadata_has_tag (meta, "Xmp.xmp.gnome-photos.google.id"))
+       gexiv2_metadata_clear_tag (meta, "Xmp.xmp.gnome-photos.google.id");
+
+      gexiv2_metadata_set_tag_string (meta, "Xmp.xmp.gnome-photos.google.title", data->remote_title);
+
+      identifier = g_strconcat ("google:picasaweb:", data->remote_id, NULL);
+      gexiv2_metadata_set_tag_string (meta, "Xmp.xmp.gnome-photos.google.id", identifier);
+      gexiv2_metadata_save_file (meta, path, &error);
+
+      if (error)
+       g_task_return_error (task, error);
+      }
+out:
+  gexiv2_metadata_free (meta);
+  g_free (identifier);
+  g_object_unref (file);
+  g_object_unref (task);
+  g_free (path);
+}
+
+
+static void
+photos_share_point_online_tracker_entry_created (GObject *source_object, GAsyncResult *res, gpointer 
user_data)
+{
+  PhotosSharePointOnline *self;
+  PhotosSharePointOnlineData *data;
+  TrackerSparqlConnection *connection = TRACKER_SPARQL_CONNECTION (source_object);
+  GCancellable *cancellable;
+  GError *error = NULL;
+  GTask *task = G_TASK (user_data);
+  GVariant *variant;
+  GVariant *child;
+  const gchar *item_urn;
+  const gchar *remote_urn;
+
+  self = PHOTOS_SHARE_POINT_ONLINE (g_task_get_source_object (task));
+  data = (PhotosSharePointOnlineData *) (g_task_get_task_data (task));
+  cancellable = g_task_get_cancellable (task);
+
+  variant = tracker_sparql_connection_update_blank_finish (connection, res, &error);
+  if (error != NULL)
+    {
+      g_warning ("Could not insert remote object: %s", error->message);
+      g_task_return_error (task, error);
+      return;
+    }
+
+  child = g_variant_get_child_value (variant, 0); /* variant is now aa{ss} */
+  g_variant_unref (variant);
+  variant = child;
+
+  child = g_variant_get_child_value (variant, 0); /* variant is now s{ss} */
+  g_variant_unref (variant);
+  variant = child;
+
+  child = g_variant_get_child_value (variant, 0); /* variant is now {ss} */
+  g_variant_unref (variant);
+  variant = child;
+
+  child = g_variant_get_child_value (variant, 1);
+  remote_urn = g_variant_dup_string (child, NULL);/*urn of inserted object*/
+  g_variant_unref (child);
+
+  item_urn = photos_filterable_get_id (PHOTOS_FILTERABLE (data->item));
+  photos_share_point_online_relate_objects (self, cancellable, remote_urn, item_urn);
+
+  /* Metadata Embed logic flows from here */
+  photos_share_point_online_set_metadata (data, g_object_ref (task));
+}
+
+
+static void
+photos_share_point_online_create_entry_in_thread_func (GTask *task,
+                                                      gpointer source_object,
+                                                      gpointer task_data,
+                                                      GCancellable *cancellable)
+{
+  PhotosSharePointOnline *self = PHOTOS_SHARE_POINT_ONLINE (source_object);
+  PhotosSharePointOnlinePrivate *priv;
+  PhotosSearchContextState *state;
+  PhotosSharePointOnlineData *data;
+  PhotosQuery *query;
+  GApplication *app;
+
+  app = g_application_get_default ();
+  state = photos_search_context_get_state (PHOTOS_SEARCH_CONTEXT (app));
+  data = (PhotosSharePointOnlineData *) (task_data);
+
+  priv = photos_share_point_online_get_instance_private (self);
+  query = photos_query_builder_insert_remote_object (state, data->remote_title, data->remote_id);
+  photos_tracker_queue_update_blank (priv->queue,
+                                     query->sparql,
+                                     cancellable,
+                                     photos_share_point_online_tracker_entry_created,
+                                     g_object_ref (task),
+                                     g_object_unref);
+  photos_query_free (query);
+}
+
+
+gboolean
+photos_share_point_online_tracker_entry_finish (PhotosSharePointOnline *self, GAsyncResult *res, GError 
**error)
+{
+  GTask *task = G_TASK (res);
+
+  g_return_val_if_fail (g_task_is_valid (res, self), FALSE);
+  g_return_val_if_fail (g_task_get_source_tag (task) == photos_share_point_online_tracker_entry_async, 
FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  return g_task_propagate_boolean (task, error);
+}
+
+
+void
+photos_share_point_online_tracker_entry_async (PhotosSharePointOnline *self,
+                                              GCancellable *cancellable,
+                                              PhotosBaseItem *item,
+                                              const gchar *title,
+                                              const gchar *id,
+                                              GAsyncReadyCallback callback,
+                                              gpointer user_data)
+{
+  GTask *task;
+  PhotosSharePointOnlineData *data;
+
+  data = photos_share_point_online_data_new (item, title, id);
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, photos_share_point_online_tracker_entry_async);
+  g_task_set_task_data (task, data, (GDestroyNotify) photos_share_point_online_data_free);
+
+  g_task_run_in_thread (task, photos_share_point_online_create_entry_in_thread_func);
+  g_object_unref (task);
+}
+
diff --git a/src/photos-share-point-online.h b/src/photos-share-point-online.h
index f197e69..17171c6 100644
--- a/src/photos-share-point-online.h
+++ b/src/photos-share-point-online.h
@@ -25,6 +25,7 @@
 #ifndef PHOTOS_SHARE_POINT_ONLINE_H
 #define PHOTOS_SHARE_POINT_ONLINE_H
 
+#include "photos-base-item.h"
 #include "photos-share-point.h"
 #include "photos-source.h"
 
@@ -45,6 +46,18 @@ struct _PhotosSharePointOnlineClass
 
 PhotosSource           *photos_share_point_online_get_source             (PhotosSharePointOnline *self);
 
+void                    photos_share_point_online_tracker_entry_async    (PhotosSharePointOnline *self,
+                                                                         GCancellable *cancellable,
+                                                                         PhotosBaseItem *item,
+                                                                         const gchar *title,
+                                                                         const gchar *id,
+                                                                         GAsyncReadyCallback callback,
+                                                                         gpointer user_data);
+
+gboolean               photos_share_point_online_tracker_entry_finish    (PhotosSharePointOnline *self,
+                                                                         GAsyncResult *res,
+                                                                         GError **error);
+
 G_END_DECLS
 
 #endif /* PHOTOS_SHARE_POINT_ONLINE_H */


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