[gnome-photos/wip/rishi/revert-to-original: 4/6] base-item, pipeline: Add is_edited and revert_to_original API



commit 08efbbb59578b69557da2408b42f44ee1d01601b
Author: Rafael Fonseca <r4f4rfs gmail com>
Date:   Mon Apr 4 14:31:17 2016 +0200

    base-item, pipeline: Add is_edited and revert_to_original API
    
    These new functions will be needed to implement the option to revert
    all edits in the UI.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=763156

 src/photos-base-item.c |  103 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/photos-base-item.h |   18 ++++++++
 src/photos-pipeline.c  |   61 ++++++++++++++++++++++++++++
 src/photos-pipeline.h  |    4 ++
 4 files changed, 186 insertions(+), 0 deletions(-)
---
diff --git a/src/photos-base-item.c b/src/photos-base-item.c
index f8ae974..634ba10 100644
--- a/src/photos-base-item.c
+++ b/src/photos-base-item.c
@@ -941,6 +941,32 @@ photos_base_item_guess_save_sizes_load (GObject *source_object, GAsyncResult *re
 }
 
 
+static void
+photos_base_item_pipeline_is_edited_load (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+  PhotosBaseItem *self = PHOTOS_BASE_ITEM (source_object);
+  GError *error;
+  GTask *task = G_TASK (user_data);
+  GeglNode *graph = NULL;
+  gboolean is_edited;
+
+  error = NULL;
+  graph = photos_base_item_load_finish (self, res, &error);
+  if (error != NULL)
+    {
+      g_task_return_error (task, error);
+      goto out;
+    }
+
+  is_edited = photos_pipeline_is_edited (self->priv->pipeline);
+  g_task_return_boolean (task, is_edited);
+
+ out:
+  g_clear_object (&graph);
+  g_object_unref (task);
+}
+
+
 static gboolean
 photos_base_item_process_idle (gpointer user_data)
 {
@@ -2692,6 +2718,83 @@ photos_base_item_operations_revert_finish (PhotosBaseItem *self, GAsyncResult *r
 
 
 void
+photos_base_item_pipeline_is_edited_async (PhotosBaseItem *self,
+                                           GCancellable *cancellable,
+                                           GAsyncReadyCallback callback,
+                                           gpointer user_data)
+{
+  PhotosBaseItemPrivate *priv;
+  GTask *task;
+
+  g_return_if_fail (PHOTOS_IS_BASE_ITEM (self));
+  priv = self->priv;
+
+  g_return_if_fail (!priv->collection);
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, photos_base_item_pipeline_is_edited_async);
+
+  photos_base_item_load_async (self, cancellable, photos_base_item_pipeline_is_edited_load, g_object_ref 
(task));
+
+  g_object_unref (task);
+}
+
+
+gboolean
+photos_base_item_pipeline_is_edited_finish (PhotosBaseItem *self, GAsyncResult *res, GError **error)
+{
+  GTask *task = G_TASK (res);
+
+  g_return_val_if_fail (PHOTOS_IS_BASE_ITEM (self), FALSE);
+  g_return_val_if_fail (g_task_is_valid (res, self), FALSE);
+  g_return_val_if_fail (g_task_get_source_tag (task) == photos_base_item_pipeline_is_edited_async, FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  return g_task_propagate_boolean (task, error);
+}
+
+
+void
+photos_base_item_pipeline_revert_to_original_async (PhotosBaseItem *self,
+                                                    GCancellable *cancellable,
+                                                    GAsyncReadyCallback callback,
+                                                    gpointer user_data)
+
+{
+  PhotosBaseItemPrivate *priv;
+  GTask *task;
+
+  g_return_if_fail (PHOTOS_IS_BASE_ITEM (self));
+  priv = self->priv;
+
+  g_return_if_fail (!priv->collection);
+
+  photos_pipeline_revert_to_original (priv->pipeline);
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, photos_base_item_pipeline_revert_to_original_async);
+
+  photos_base_item_process_async (self, cancellable, photos_base_item_common_process, g_object_ref (task));
+
+  g_object_unref (task);
+}
+
+
+gboolean
+photos_base_item_pipeline_revert_to_original_finish (PhotosBaseItem *self, GAsyncResult *res, GError **error)
+{
+  GTask *task = G_TASK (res);
+
+  g_return_val_if_fail (PHOTOS_IS_BASE_ITEM (self), FALSE);
+  g_return_val_if_fail (g_task_is_valid (res, self), FALSE);
+  g_return_val_if_fail (g_task_get_source_tag (task) == photos_base_item_pipeline_revert_to_original_async, 
FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  return g_task_propagate_boolean (task, error);
+}
+
+
+void
 photos_base_item_pipeline_save_async (PhotosBaseItem *self,
                                       GCancellable *cancellable,
                                       GAsyncReadyCallback callback,
diff --git a/src/photos-base-item.h b/src/photos-base-item.h
index b2c454f..5d57792 100644
--- a/src/photos-base-item.h
+++ b/src/photos-base-item.h
@@ -243,6 +243,24 @@ gboolean            photos_base_item_operations_revert_finish(PhotosBaseItem *se
                                                               GAsyncResult *res,
                                                               GError **error);
 
+void                photos_base_item_pipeline_is_edited_async          (PhotosBaseItem *self,
+                                                                        GCancellable *cancellable,
+                                                                        GAsyncReadyCallback callback,
+                                                                        gpointer user_data);
+
+gboolean            photos_base_item_pipeline_is_edited_finish         (PhotosBaseItem *self,
+                                                                        GAsyncResult *res,
+                                                                        GError **error);
+
+void                photos_base_item_pipeline_revert_to_original_async (PhotosBaseItem *self,
+                                                                        GCancellable *cancellable,
+                                                                        GAsyncReadyCallback callback,
+                                                                        gpointer user_data);
+
+gboolean            photos_base_item_pipeline_revert_to_original_finish (PhotosBaseItem *self,
+                                                                         GAsyncResult *res,
+                                                                         GError **error);
+
 void                photos_base_item_pipeline_save_async     (PhotosBaseItem *self,
                                                               GCancellable *cancellable,
                                                               GAsyncReadyCallback callback,
diff --git a/src/photos-pipeline.c b/src/photos-pipeline.c
index 6ad2cdd..dc634d2 100644
--- a/src/photos-pipeline.c
+++ b/src/photos-pipeline.c
@@ -495,6 +495,49 @@ photos_pipeline_get_output (PhotosPipeline *self)
 }
 
 
+gboolean
+photos_pipeline_is_edited (PhotosPipeline *self)
+{
+  GSList *children = NULL;
+  GSList *l;
+  guint n_operations = 0;
+
+  children = gegl_node_get_children (self->graph);
+  if (children == NULL)
+    goto out;
+
+  for (l = children; l != NULL && n_operations == 0; l = l->next)
+    {
+      GeglNode *node = GEGL_NODE (l->data);
+      const char *operation;
+
+      if (gegl_node_get_passthrough (node))
+        continue;
+
+      operation = gegl_node_get_operation (node);
+
+      if (g_strcmp0 (operation, "gegl:nop") == 0)
+        {
+          continue;
+        }
+      else if (g_strcmp0 (operation, "photos:magic-filter") == 0)
+        {
+          gint preset;
+
+          gegl_node_get (node, "preset", &preset, NULL);
+          if (preset == PHOTOS_OPERATION_INSTA_PRESET_NONE)
+            continue;
+        }
+
+      n_operations++;
+    }
+
+ out:
+  g_slist_free (children);
+  return n_operations > 0;
+}
+
+
 GeglProcessor *
 photos_pipeline_new_processor (PhotosPipeline *self)
 {
@@ -613,6 +656,24 @@ photos_pipeline_revert (PhotosPipeline *self)
 
 
 void
+photos_pipeline_revert_to_original (PhotosPipeline *self)
+{
+  const gchar *empty_xml = "<?xml version='1.0' encoding='UTF-8'?><gegl></gegl>";
+  gchar *xml;
+
+  if (!photos_pipeline_create_graph_from_xml (self, empty_xml))
+    g_warning ("Unable to revert to original");
+
+  g_clear_pointer (&self->snapshot, g_free);
+
+  xml = gegl_node_to_xml_full (self->graph, self->graph, "/");
+  photos_debug (PHOTOS_DEBUG_GEGL, "Pipeline: %s", xml);
+
+  g_free (xml);
+}
+
+
+void
 photos_pipeline_snapshot (PhotosPipeline *self)
 {
   g_free (self->snapshot);
diff --git a/src/photos-pipeline.h b/src/photos-pipeline.h
index 63f9a19..15aa88e 100644
--- a/src/photos-pipeline.h
+++ b/src/photos-pipeline.h
@@ -66,6 +66,8 @@ GeglNode              *photos_pipeline_get_graph         (PhotosPipeline *self);
 
 GeglNode              *photos_pipeline_get_output        (PhotosPipeline *self);
 
+gboolean               photos_pipeline_is_edited         (PhotosPipeline *self);
+
 GeglProcessor         *photos_pipeline_new_processor     (PhotosPipeline *self);
 
 void                   photos_pipeline_save_async        (PhotosPipeline *self,
@@ -79,6 +81,8 @@ gboolean               photos_pipeline_remove            (PhotosPipeline *self,
 
 void                   photos_pipeline_revert            (PhotosPipeline *self);
 
+void                   photos_pipeline_revert_to_original(PhotosPipeline *self);
+
 void                   photos_pipeline_snapshot          (PhotosPipeline *self);
 
 G_END_DECLS


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