[gtksourceview/wip/loader-saver] FileSaver: new constructor new_with_target()



commit 365e09e6f881d16a8a8ec2eee8c41b66897d5772
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Tue Jul 8 17:18:35 2014 +0200

    FileSaver: new constructor new_with_target()

 docs/reference/gtksourceview-3.0-sections.txt |    1 +
 gtksourceview/gtksourcefilesaver.c            |   70 ++++++++++++++++++++++---
 gtksourceview/gtksourcefilesaver.h            |    5 ++-
 tests/test-file-saver.c                       |    2 +-
 4 files changed, 69 insertions(+), 9 deletions(-)
---
diff --git a/docs/reference/gtksourceview-3.0-sections.txt b/docs/reference/gtksourceview-3.0-sections.txt
index d188cd6..fe3c0b4 100644
--- a/docs/reference/gtksourceview-3.0-sections.txt
+++ b/docs/reference/gtksourceview-3.0-sections.txt
@@ -297,6 +297,7 @@ GtkSourceFileSaverError
 GtkSourceFileSaverFlags
 <SUBSECTION>
 gtk_source_file_saver_new
+gtk_source_file_saver_new_with_target
 gtk_source_file_saver_get_buffer
 gtk_source_file_saver_get_file
 gtk_source_file_saver_get_location
diff --git a/gtksourceview/gtksourcefilesaver.c b/gtksourceview/gtksourcefilesaver.c
index 115ea7d..539eef5 100644
--- a/gtksourceview/gtksourcefilesaver.c
+++ b/gtksourceview/gtksourcefilesaver.c
@@ -289,6 +289,21 @@ gtk_source_file_saver_constructed (GObject *object)
 
                compression_type = gtk_source_file_get_compression_type (saver->priv->file);
                gtk_source_file_saver_set_compression_type (saver, compression_type);
+
+               if (saver->priv->location == NULL)
+               {
+                       saver->priv->location = gtk_source_file_get_location (saver->priv->file);
+
+                       if (saver->priv->location != NULL)
+                       {
+                               g_object_ref (saver->priv->location);
+                       }
+                       else
+                       {
+                               g_warning ("GtkSourceFileSaver: GtkSourceFile's location is NULL. "
+                                          "Use gtk_source_file_saver_new_with_target().");
+                       }
+               }
        }
 
        G_OBJECT_CLASS (gtk_source_file_saver_parent_class)->constructed (object);
@@ -343,7 +358,9 @@ gtk_source_file_saver_class_init (GtkSourceFileSaverClass *klass)
        /**
         * GtkSourceFileSaver:location:
         *
-        * The #GFile where to save the buffer.
+        * The #GFile where to save the buffer. If set to %NULL, the
+        * #GtkSourceFile's #GtkSourceFile:location is taken at construction
+        * time.
         *
         * Since: 3.14
         */
@@ -980,24 +997,62 @@ gtk_source_file_saver_error_quark (void)
  * gtk_source_file_saver_new:
  * @buffer: the #GtkSourceBuffer to save.
  * @file: the #GtkSourceFile.
- * @location: the #GFile where to save the buffer to.
+ *
+ * Creates a new #GtkSourceFileSaver object. The @buffer will be saved to the
+ * @file's #GtkSourceFile:location.
+ *
+ * The file saver's #GtkSourceFileSaver:location is set at construction time
+ * with the @file's #GtkSourceFile:location value.
+ *
+ * This constructor is suitable for a simple "save" operation, when the @file
+ * already contains a non-%NULL #GtkSourceFile:location.
  *
  * Returns: a new #GtkSourceFileSaver object.
  * Since: 3.14
  */
 GtkSourceFileSaver *
 gtk_source_file_saver_new (GtkSourceBuffer *buffer,
-                          GtkSourceFile   *file,
-                          GFile           *location)
+                          GtkSourceFile   *file)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
+       g_return_val_if_fail (GTK_SOURCE_IS_FILE (file), NULL);
+
+       return g_object_new (GTK_SOURCE_TYPE_FILE_SAVER,
+                            "buffer", buffer,
+                            "file", file,
+                            NULL);
+}
+
+/**
+ * gtk_source_file_saver_new_with_target:
+ * @buffer: the #GtkSourceBuffer to save.
+ * @file: the #GtkSourceFile.
+ * @target_location: the #GFile where to save the buffer to.
+ *
+ * Creates a new #GtkSourceFileSaver object with a target location. When the
+ * file saving is finished successfully, @target_location is set to the @file's
+ * #GtkSourceFile:location property. If an error occurs, the previous valid
+ * location is still available in #GtkSourceFile.
+ *
+ * This constructor is suitable for a "save as" operation, or for saving for the
+ * first time a new buffer.
+ *
+ * Returns: a new #GtkSourceFileSaver object.
+ * Since: 3.14
+ */
+GtkSourceFileSaver *
+gtk_source_file_saver_new_with_target (GtkSourceBuffer *buffer,
+                                      GtkSourceFile   *file,
+                                      GFile           *target_location)
 {
        g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), NULL);
        g_return_val_if_fail (GTK_SOURCE_IS_FILE (file), NULL);
-       g_return_val_if_fail (G_IS_FILE (location), NULL);
+       g_return_val_if_fail (G_IS_FILE (target_location), NULL);
 
        return g_object_new (GTK_SOURCE_TYPE_FILE_SAVER,
                             "buffer", buffer,
                             "file", file,
-                            "location", location,
+                            "location", target_location,
                             NULL);
 }
 
@@ -1247,7 +1302,8 @@ gtk_source_file_saver_save_async (GtkSourceFileSaver     *saver,
        g_return_if_fail (saver->priv->task == NULL);
 
        if (saver->priv->source_buffer == NULL ||
-           saver->priv->file == NULL)
+           saver->priv->file == NULL ||
+           saver->priv->location == NULL)
        {
                return;
        }
diff --git a/gtksourceview/gtksourcefilesaver.h b/gtksourceview/gtksourcefilesaver.h
index 6040064..c818be4 100644
--- a/gtksourceview/gtksourcefilesaver.h
+++ b/gtksourceview/gtksourcefilesaver.h
@@ -95,8 +95,11 @@ GType                         gtk_source_file_saver_get_type         (void) G_GNUC_CONST;
 GQuark                  gtk_source_file_saver_error_quark      (void);
 
 GtkSourceFileSaver     *gtk_source_file_saver_new              (GtkSourceBuffer          *buffer,
+                                                                GtkSourceFile            *file);
+
+GtkSourceFileSaver     *gtk_source_file_saver_new_with_target  (GtkSourceBuffer          *buffer,
                                                                 GtkSourceFile            *file,
-                                                                GFile                    *location);
+                                                                GFile                    *target_location);
 
 GtkSourceBuffer                *gtk_source_file_saver_get_buffer       (GtkSourceFileSaver       *saver);
 
diff --git a/tests/test-file-saver.c b/tests/test-file-saver.c
index 19178fb..0349482 100644
--- a/tests/test-file-saver.c
+++ b/tests/test-file-saver.c
@@ -199,7 +199,7 @@ test_saver (const gchar            *filename_or_uri,
        gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), buffer_contents, -1);
 
        file = gtk_source_file_new ();
-       saver = gtk_source_file_saver_new (buffer, file, location);
+       saver = gtk_source_file_saver_new_with_target (buffer, file, location);
 
        gtk_source_file_saver_set_newline_type (saver, newline_type);
        gtk_source_file_saver_set_encoding (saver, gtk_source_encoding_get_utf8 ());


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