[gnome-photos/wip/uajain/de_dup: 2/3] share-point-google: Add remote object and relate with local resource



commit f25f8ac16fbbd24d806a5d30336577b2cdc32d16
Author: Umang Jain <mailumangjain gmail com>
Date:   Fri Aug 26 05:47:19 2016 +0530

    share-point-google: Add remote object and relate with local resource
    
    https://bugzilla.gnome.org/show_bug.cgi?id=770267

 src/photos-share-point-google.c |  268 ++++++++++++++++++++++++++++++++++++++-
 1 files changed, 267 insertions(+), 1 deletions(-)
---
diff --git a/src/photos-share-point-google.c b/src/photos-share-point-google.c
index 94bd7ed..8c70517 100644
--- a/src/photos-share-point-google.c
+++ b/src/photos-share-point-google.c
@@ -26,16 +26,23 @@
 #include <glib/gi18n.h>
 
 #include "photos-base-item.h"
+#include "photos-filterable.h"
+#include "photos-query.h"
+#include "photos-query-builder.h"
+#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;
 };
 
 
@@ -52,6 +59,7 @@ typedef struct _PhotosSharePointGoogleShareData PhotosSharePointGoogleShareData;
 struct _PhotosSharePointGoogleShareData
 {
   GDataUploadStream *stream;
+  GDataPicasaWebFile *file_entry;
   PhotosBaseItem *item;
 };
 
@@ -63,6 +71,7 @@ photos_share_point_google_share_data_new (PhotosBaseItem *item)
 
   data = g_slice_new0 (PhotosSharePointGoogleShareData);
   data->item = g_object_ref (item);
+  data->file_entry = NULL;
 
   return data;
 }
@@ -73,6 +82,7 @@ photos_share_point_google_share_data_free (PhotosSharePointGoogleShareData *data
 {
   g_clear_object (&data->stream);
   g_clear_object (&data->item);
+  g_clear_object (&data->file_entry);
   g_slice_free (PhotosSharePointGoogleShareData, data);
 }
 
@@ -92,6 +102,246 @@ photos_share_point_google_parse_error (PhotosSharePoint *self, GError *error)
 
 
 static void
+photos_share_point_google_relate_objects (PhotosSharePointGoogle *self, GCancellable *cancellable, const 
gchar *domain_urn, const gchar *range_urn)
+{
+  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, domain_urn, range_urn);
+  photos_tracker_queue_update (self->queue,
+                               query->sparql,
+                               cancellable,
+                               NULL,
+                               NULL,
+                               NULL);
+
+  query = photos_query_builder_relate_objects (state, range_urn, domain_urn);
+  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)
+{
+  PhotosSharePointGoogle *self;
+  PhotosSearchContextState *state;
+  PhotosSharePointGoogleShareData *data;
+  PhotosQuery *query;
+  TrackerSparqlConnection *connection = TRACKER_SPARQL_CONNECTION (source_object);
+  GApplication *app;
+  GCancellable *cancellable;
+  GDataLink *alternate;
+  GError *error;
+  GList *media_contents;
+  GTask *task = G_TASK (user_data);
+  GTimeVal tv;
+  GVariant *variant;
+  GVariant *child;
+  gint64 timestamp;
+  gboolean flash;
+
+  const gchar *credit;
+  const gchar *make;
+  const gchar *model;
+  const gchar *title;
+  const gchar *summary;
+  const gchar *mime;
+  const gchar *item_urn;
+  const gchar *remote_urn;
+  const gchar *flash_off;
+  const gchar *flash_on;
+  const gchar *alternate_uri;
+
+  gchar *exposure;
+  gchar *focal_length;
+  gchar *fstop;
+  gchar *iso;
+  gchar *width;
+  gchar *height;
+  gchar *time;
+
+  flash_off = "http://www.tracker-project.org/temp/nmm#flash-off";;
+  flash_on = "http://www.tracker-project.org/temp/nmm#flash-on";;
+
+  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);
+  g_variant_unref (child);
+
+  app = g_application_get_default ();
+  state = photos_search_context_get_state (PHOTOS_SEARCH_CONTEXT (app));
+
+  alternate = gdata_entry_look_up_link (GDATA_ENTRY (data->file_entry), GDATA_LINK_ALTERNATE);
+  alternate_uri = gdata_link_get_uri (alternate);
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nie:url", alternate_uri);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+
+  summary = gdata_entry_get_summary (GDATA_ENTRY (data->file_entry));
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nie:description", summary);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+
+  media_contents = gdata_picasaweb_file_get_contents (data->file_entry);
+  mime = gdata_media_content_get_content_type (GDATA_MEDIA_CONTENT (media_contents->data));
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nie:mimeType", mime);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+
+  title = gdata_entry_get_title (GDATA_ENTRY (data->file_entry));
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nie:title", title);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+
+  credit = gdata_picasaweb_file_get_credit (data->file_entry);
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nco:creator", credit);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+
+  exposure = g_strdup_printf ("%f", gdata_picasaweb_file_get_exposure (data->file_entry));
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nmm:exposureTime", exposure);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+  g_free (exposure);
+
+  focal_length = g_strdup_printf ("%f", gdata_picasaweb_file_get_focal_length (data->file_entry));
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nmm:focalLength", focal_length);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+  g_free (focal_length);
+
+  fstop = g_strdup_printf ("%f", gdata_picasaweb_file_get_fstop (data->file_entry));
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nmm:fnumber", fstop);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+  g_free (fstop);
+
+  iso = g_strdup_printf ("%d", gdata_picasaweb_file_get_iso (data->file_entry));
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nmm:isoSpeed", iso);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+  g_free (iso);
+
+  flash = gdata_picasaweb_file_get_flash (data->file_entry);
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nmm:flash", flash ? flash_on : 
flash_off);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+
+  /* Not sure how to do this with Equipment
+  make = gdata_picasaweb_file_get_make (data->file_entry);
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nfo:manufacturer", make);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+
+  model = gdata_picasaweb_file_get_model (data->file_entry);
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nfo:model", model);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+   * */
+
+  width = g_strdup_printf ("%u", gdata_picasaweb_file_get_width (data->file_entry));
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nfo:width", width);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+  g_free (width);
+
+  height = g_strdup_printf ("%u", gdata_picasaweb_file_get_height (data->file_entry));
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nfo:height", height);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+  g_free (height);
+
+  /* borrowed from query_builder_mtime_update_query */
+  timestamp = gdata_picasaweb_file_get_timestamp (data->file_entry) / G_USEC_PER_SEC;
+  tv.tv_sec = timestamp;
+  tv.tv_usec = 0;
+  time = g_time_val_to_iso8601 (&tv);
+  query = photos_query_builder_insert_or_replace (state, remote_urn, "nie:contentCreated", time);
+  photos_tracker_queue_update (self->queue, query->sparql, cancellable, NULL, NULL, NULL);
+  g_free (time);
+  photos_query_free (query);
+
+  item_urn = photos_filterable_get_id (PHOTOS_FILTERABLE (data->item));
+  photos_share_point_google_relate_objects (self, cancellable, remote_urn, item_urn);
+}
+
+
+static void
+photos_share_point_google_metadata_added (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+  PhotosBaseItem *item = PHOTOS_BASE_ITEM (source_object);
+  GError *error = NULL;
+
+  if (!photos_base_item_add_metadata_finish (item, res, &error))
+    {
+      g_warning ("Could not remote link as metadata: %s", error->message);
+      g_error_free (error);
+    }
+
+ g_object_unref (item);
+}
+
+
+static void
+photos_share_point_google_create_tracker_entry (PhotosSharePointGoogle *self, GTask *task)
+{
+  PhotosSearchContextState *state;
+  PhotosSharePointGoogleShareData *data;
+  PhotosQuery *query;
+  GApplication *app;
+  GCancellable *cancellable;
+  const gchar *id, *tag;
+  gchar *identifier;
+
+  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 (data->file_entry));
+  query = photos_query_builder_insert_remote_object (state, 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);
+
+  identifier = g_strdup_printf ("google:picasaweb:%s", id);
+  tag = "Xmp.xmp.gnome-photos.google.identifier";
+  g_object_set_data (G_OBJECT (data->item), "tag", (gpointer *) tag);
+  photos_base_item_add_metadata_async (data->item,
+                                       identifier,
+                                       cancellable,
+                                       photos_share_point_google_metadata_added,
+                                       NULL);
+
+  g_object_unref (task);
+}
+
+
+static void
 photos_share_point_google_share_save_to_stream (GObject *source_object, GAsyncResult *res, gpointer 
user_data)
 {
   PhotosSharePointGoogle *self;
@@ -123,8 +373,11 @@ photos_share_point_google_share_save_to_stream (GObject *source_object, GAsyncRe
 
   g_task_return_boolean (task, TRUE);
 
+  data->file_entry = GDATA_PICASAWEB_FILE (g_object_ref (file_entry));
+  photos_share_point_google_create_tracker_entry (self, G_TASK (g_object_ref (task)));
+
  out:
-  g_clear_object (&file_entry);
+  g_object_unref (file_entry);
   g_object_unref (task);
 }
 
@@ -259,6 +512,7 @@ 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);
@@ -266,8 +520,19 @@ 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)->finalize (object);
+}
+
+static void
 photos_share_point_google_init (PhotosSharePointGoogle *self)
 {
+  self->queue = photos_tracker_queue_dup_singleton (NULL, &self->queue_error);
 }
 
 
@@ -280,6 +545,7 @@ photos_share_point_google_class_init (PhotosSharePointGoogleClass *class)
   object_class->constructed = photos_share_point_google_constructed;
   object_class->dispose = photos_share_point_google_dispose;
   share_point_class->parse_error = photos_share_point_google_parse_error;
+  object_class->finalize = photos_share_point_google_finalize;
   share_point_class->share_async = photos_share_point_google_share_async;
   share_point_class->share_finish = photos_share_point_google_share_finish;
 }


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