[gnome-photos/wip/rishi/misc-fixes: 9/24] pipeline: Add photos_pipeline_save_async



commit 1e0fa544a2a77cccd3fcc830c3a8103ab7b65320
Author: Debarshi Ray <debarshir gnome org>
Date:   Fri Dec 18 13:40:50 2015 +0100

    pipeline: Add photos_pipeline_save_async
    
    https://bugzilla.gnome.org/show_bug.cgi?id=759363

 src/photos-pipeline.c |   74 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/photos-pipeline.h |    8 +++++
 2 files changed, 82 insertions(+), 0 deletions(-)
---
diff --git a/src/photos-pipeline.c b/src/photos-pipeline.c
index eed5e9c..150d858 100644
--- a/src/photos-pipeline.c
+++ b/src/photos-pipeline.c
@@ -21,7 +21,10 @@
 
 #include "config.h"
 
+#include <string.h>
+
 #include <glib.h>
+#include <gio/gio.h>
 
 #include "egg-counter.h"
 #include "photos-debug.h"
@@ -82,6 +85,28 @@ photos_pipeline_to_xml (PhotosPipeline *self)
   return ret_val;
 }
 
+
+static void
+photos_pipeline_save_replace_contents (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+  GTask *task = G_TASK (user_data);
+  GError *error;
+  GFile *file = G_FILE (source_object);
+
+  error = NULL;
+  if (!g_file_replace_contents_finish (file, res, NULL, &error))
+    {
+      g_task_return_error (task, error);
+      goto out;
+    }
+
+  g_task_return_boolean (task, TRUE);
+
+ out:
+  g_object_unref (task);
+}
+
+
 static void
 photos_pipeline_constructed (GObject *object)
 {
@@ -263,6 +288,7 @@ photos_pipeline_new_processor (PhotosPipeline *self)
 
   output = gegl_node_get_output_proxy (self->graph, "output");
   processor = gegl_node_new_processor (output, NULL);
+  g_object_set_data_full (G_OBJECT (processor), "pipeline-graph", g_object_ref (self->graph), 
g_object_unref);
   return processor;
 }
 
@@ -288,6 +314,54 @@ photos_pipeline_reset (PhotosPipeline *self)
 }
 
 
+void
+photos_pipeline_save_async (PhotosPipeline *self,
+                            const gchar *uri,
+                            GCancellable *cancellable,
+                            GAsyncReadyCallback callback,
+                            gpointer user_data)
+{
+  GFile *file;
+  GTask *task;
+  gchar *xml;
+  gsize len;
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, photos_pipeline_save_async);
+
+  xml = photos_pipeline_to_xml (self);
+  g_task_set_task_data (task, xml, g_free);
+
+  file = g_file_new_for_uri (uri);
+  len = strlen (xml);
+  g_file_replace_contents_async (file,
+                                 xml,
+                                 len,
+                                 NULL,
+                                 FALSE,
+                                 G_FILE_CREATE_REPLACE_DESTINATION,
+                                 cancellable,
+                                 photos_pipeline_save_replace_contents,
+                                 g_object_ref (task));
+
+  g_object_unref (file);
+  g_object_unref (task);
+}
+
+
+gboolean
+photos_pipeline_save_finish (PhotosPipeline *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_pipeline_save_async, FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  return g_task_propagate_boolean (task, error);
+}
+
+
 gboolean
 photos_pipeline_undo (PhotosPipeline *self)
 {
diff --git a/src/photos-pipeline.h b/src/photos-pipeline.h
index e64e672..69dd81a 100644
--- a/src/photos-pipeline.h
+++ b/src/photos-pipeline.h
@@ -74,6 +74,14 @@ GeglProcessor         *photos_pipeline_new_processor     (PhotosPipeline *self);
 
 void                   photos_pipeline_reset             (PhotosPipeline *self);
 
+void                   photos_pipeline_save_async        (PhotosPipeline *self,
+                                                          const gchar *uri,
+                                                          GCancellable *cancellable,
+                                                          GAsyncReadyCallback callback,
+                                                          gpointer user_data);
+
+gboolean               photos_pipeline_save_finish       (PhotosPipeline *self, GAsyncResult *res, GError 
**error);
+
 gboolean               photos_pipeline_undo              (PhotosPipeline *self);
 
 G_END_DECLS


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