[gnome-photos/wip/uajain/de_dup: 2/3] share-point-online: Add remote object in tracker



commit add29425ad5593c9ea7061c6b66b12d2ddc3ce4b
Author: Umang Jain <mailumangjain gmail com>
Date:   Wed Aug 24 15:40:22 2016 +0530

    share-point-online: Add remote object in tracker
    
    https://bugzilla.gnome.org/show_bug.cgi?id=770267

 src/photos-share-point-online.c |  240 +++++++++++++++++++++++++++++++++++++++
 src/photos-share-point-online.h |   13 ++
 2 files changed, 253 insertions(+), 0 deletions(-)
---
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]