[gtksourceview/wip/loader-saver] FileSaver: location -> target-location



commit 2ed7270d05ca15915d8def82b2e30a5c7c11e759
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Mon Jul 7 22:47:39 2014 +0200

    FileSaver: location -> target-location
    
    gtk_source_file_saver_get_location() returns the location where the
    contents is saved. It is either the target-location, or the
    GtkSourceFile's location. In GeditTab this function is used at several
    places, and a get_target_location() would not be convenient.

 docs/reference/gtksourceview-3.0-sections.txt |    1 +
 gtksourceview/gtksourcefilesaver.c            |   98 +++++++++++++++++++++----
 gtksourceview/gtksourcefilesaver.h            |    6 +-
 tests/test-file-saver.c                       |    2 +-
 4 files changed, 89 insertions(+), 18 deletions(-)
---
diff --git a/docs/reference/gtksourceview-3.0-sections.txt b/docs/reference/gtksourceview-3.0-sections.txt
index 2c898c9..88e11c7 100644
--- a/docs/reference/gtksourceview-3.0-sections.txt
+++ b/docs/reference/gtksourceview-3.0-sections.txt
@@ -296,6 +296,7 @@ GtkSourceFileSaverError
 GtkSourceFileSaverFlags
 <SUBSECTION>
 gtk_source_file_saver_new
+gtk_source_file_saver_new_with_target_location
 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..b046725 100644
--- a/gtksourceview/gtksourcefilesaver.c
+++ b/gtksourceview/gtksourcefilesaver.c
@@ -62,7 +62,7 @@ enum
        PROP_0,
        PROP_BUFFER,
        PROP_FILE,
-       PROP_LOCATION,
+       PROP_TARGET_LOCATION,
        PROP_ENCODING,
        PROP_NEWLINE_TYPE,
        PROP_COMPRESSION_TYPE,
@@ -83,6 +83,12 @@ struct _GtkSourceFileSaverPrivate
         */
        GtkSourceFile *file;
 
+       GFile *target_location;
+
+       /* The location used for the file saving. It is either the
+        * target_location, or the GtkSourceFile's location if target_location
+        * is NULL.
+        */
        GFile *location;
 
        const GtkSourceEncoding *encoding;
@@ -152,9 +158,9 @@ gtk_source_file_saver_set_property (GObject      *object,
                                                   (gpointer *)&saver->priv->file);
                        break;
 
-               case PROP_LOCATION:
-                       g_assert (saver->priv->location == NULL);
-                       saver->priv->location = g_value_dup_object (value);
+               case PROP_TARGET_LOCATION:
+                       g_assert (saver->priv->target_location == NULL);
+                       saver->priv->target_location = g_value_dup_object (value);
                        break;
 
                case PROP_ENCODING:
@@ -197,8 +203,8 @@ gtk_source_file_saver_get_property (GObject    *object,
                        g_value_set_object (value, saver->priv->file);
                        break;
 
-               case PROP_LOCATION:
-                       g_value_set_object (value, saver->priv->location);
+               case PROP_TARGET_LOCATION:
+                       g_value_set_object (value, saver->priv->target_location);
                        break;
 
                case PROP_ENCODING:
@@ -265,6 +271,7 @@ gtk_source_file_saver_dispose (GObject *object)
                saver->priv->file = NULL;
        }
 
+       g_clear_object (&saver->priv->target_location);
        g_clear_object (&saver->priv->location);
 
        G_OBJECT_CLASS (gtk_source_file_saver_parent_class)->dispose (object);
@@ -289,6 +296,24 @@ 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->target_location != NULL)
+               {
+                       saver->priv->location = g_object_ref (saver->priv->target_location);
+               }
+               else
+               {
+                       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 ("FileSaver: the GtkSourceFile's location is NULL.");
+                       }
+               }
        }
 
        G_OBJECT_CLASS (gtk_source_file_saver_parent_class)->constructed (object);
@@ -341,16 +366,17 @@ gtk_source_file_saver_class_init (GtkSourceFileSaverClass *klass)
                                                              G_PARAM_STATIC_STRINGS));
 
        /**
-        * GtkSourceFileSaver:location:
+        * GtkSourceFileSaver:target-location:
         *
-        * The #GFile where to save the buffer.
+        * The #GFile where to save the buffer. If %NULL, the #GtkSourceFile's
+        * "location" property is used.
         *
         * Since: 3.14
         */
        g_object_class_install_property (object_class,
-                                        PROP_LOCATION,
-                                        g_param_spec_object ("location",
-                                                             _("Location"),
+                                        PROP_TARGET_LOCATION,
+                                        g_param_spec_object ("target-location",
+                                                             _("Target Location"),
                                                              "",
                                                              G_TYPE_FILE,
                                                              G_PARAM_READWRITE |
@@ -980,24 +1006,59 @@ 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 #GtkSourceFileSaver object. The @buffer contents will be saved to
+ * the @file's #GtkSourceFile:location.
+ *
+ * This function should be used only for a save operation to the same location.
+ * You should not call gtk_source_file_set_location() before creating a file
+ * saver. For a "save as" operation, use
+ * gtk_source_file_saver_new_with_target_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_location:
+ * @buffer: the #GtkSourceBuffer to save.
+ * @file: the #GtkSourceFile.
+ * @target_location: the target #GFile where to save the contents.
+ *
+ * Creates a #GtkSourceFileSaver object. The @buffer contents will be saved to
+ * the @target_location. This function should be used for a "save as" operation.
+ * When the file saving is finished successfully, the @target_location will be
+ * set to the #GtkSourceFile's "location" property. If an error occurs, you
+ * still have the previous valid location in #GtkSourceFile.
+ *
+ * Returns: a new #GtkSourceFileSaver object.
+ * Since: 3.14
+ */
+GtkSourceFileSaver *
+gtk_source_file_saver_new_with_target_location (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,
+                            "target-location", target_location,
                             NULL);
 }
 
@@ -1035,6 +1096,10 @@ gtk_source_file_saver_get_file (GtkSourceFileSaver *saver)
  * gtk_source_file_saver_get_location:
  * @saver: a #GtkSourceFileSaver.
  *
+ * Gets the #GFile where the buffer is saved. If the
+ * #GtkSourceFileSaver:target-location property is not %NULL, return it. Else,
+ * the #GtkSourceFile's "location" property is returned.
+ *
  * Returns: (transfer none): the #GFile where to save the buffer to.
  * Since: 3.14
  */
@@ -1245,6 +1310,7 @@ gtk_source_file_saver_save_async (GtkSourceFileSaver     *saver,
 
        g_return_if_fail (GTK_SOURCE_IS_FILE_SAVER (saver));
        g_return_if_fail (saver->priv->task == NULL);
+       g_return_if_fail (saver->priv->location != NULL);
 
        if (saver->priv->source_buffer == NULL ||
            saver->priv->file == NULL)
diff --git a/gtksourceview/gtksourcefilesaver.h b/gtksourceview/gtksourcefilesaver.h
index 6040064..254226a 100644
--- a/gtksourceview/gtksourcefilesaver.h
+++ b/gtksourceview/gtksourcefilesaver.h
@@ -95,8 +95,12 @@ 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_location
+                                                               (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..872f934 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_location (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]