[gimp] libgimpconfig: add API operating on GFiles instead of filenames



commit 6ec5fb80e41393581cff54b8470703cf4d2321bf
Author: Michael Natterer <mitch gimp org>
Date:   Tue Jul 1 01:54:31 2014 +0200

    libgimpconfig: add API operating on GFiles instead of filenames
    
    to GimpConfigInterface's wrappers, to GimpConfigWriter and to
    GimpScanner.

 app/core/gimpcontainer.c         |    1 +
 libgimpconfig/gimpconfig-iface.c |   89 ++++++++++++++++++++++++++++++++++++++
 libgimpconfig/gimpconfig-iface.h |   10 ++++
 libgimpconfig/gimpconfig.def     |    4 ++
 libgimpconfig/gimpconfigwriter.c |   36 +++++++++++++++
 libgimpconfig/gimpconfigwriter.h |    4 ++
 libgimpconfig/gimpscanner.c      |   27 +++++++++++
 libgimpconfig/gimpscanner.h      |    2 +
 8 files changed, 173 insertions(+), 0 deletions(-)
---
diff --git a/app/core/gimpcontainer.c b/app/core/gimpcontainer.c
index 1e4942e..38c98c4 100644
--- a/app/core/gimpcontainer.c
+++ b/app/core/gimpcontainer.c
@@ -20,6 +20,7 @@
 
 #include "config.h"
 
+#include <gio/gio.h>
 #include <gegl.h>
 
 #include "libgimpconfig/gimpconfig.h"
diff --git a/libgimpconfig/gimpconfig-iface.c b/libgimpconfig/gimpconfig-iface.c
index 4defe87..09f5a87 100644
--- a/libgimpconfig/gimpconfig-iface.c
+++ b/libgimpconfig/gimpconfig-iface.c
@@ -298,6 +298,47 @@ gimp_config_serialize_to_file (GimpConfig   *config,
 }
 
 /**
+ * gimp_config_serialize_to_gfile:
+ * @config: a #GObject that implements the #GimpConfigInterface.
+ * @file:   the #GFile to write the configuration to.
+ * @header: optional file header (must be ASCII only)
+ * @footer: optional file footer (must be ASCII only)
+ * @data: user data passed to the serialize implementation.
+ * @error: return location for a possible error
+ *
+ * Serializes the object properties of @config to the file specified
+ * by @file. If a file with that name already exists, it is
+ * overwritten. Basically this function opens @file for you and calls
+ * the serialize function of the @config's #GimpConfigInterface.
+ *
+ * Return value: %TRUE if serialization succeeded, %FALSE otherwise.
+ *
+ * Since: GIMP 2.10
+ **/
+gboolean
+gimp_config_serialize_to_gfile (GimpConfig   *config,
+                                GFile        *file,
+                                const gchar  *header,
+                                const gchar  *footer,
+                                gpointer      data,
+                                GError      **error)
+{
+  GimpConfigWriter *writer;
+
+  g_return_val_if_fail (GIMP_IS_CONFIG (config), FALSE);
+  g_return_val_if_fail (G_IS_FILE (file), FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  writer = gimp_config_writer_new_gfile (file, TRUE, header, error);
+  if (!writer)
+    return FALSE;
+
+  GIMP_CONFIG_GET_INTERFACE (config)->serialize (config, writer, data);
+
+  return gimp_config_writer_finish (writer, footer, error);
+}
+
+/**
  * gimp_config_serialize_to_fd:
  * @config: a #GObject that implements the #GimpConfigInterface.
  * @fd: a file descriptor, opened for writing
@@ -408,6 +449,54 @@ gimp_config_deserialize_file (GimpConfig   *config,
 }
 
 /**
+ * gimp_config_deserialize_gfile:
+ * @config: a #GObject that implements the #GimpConfigInterface.
+ * @file: the #GFile to read configuration from.
+ * @data: user data passed to the deserialize implementation.
+ * @error: return location for a possible error
+ *
+ * Opens the file specified by @file, reads configuration data from it
+ * and configures @config accordingly. Basically this function creates
+ * a properly configured #GScanner for you and calls the deserialize
+ * function of the @config's #GimpConfigInterface.
+ *
+ * Return value: %TRUE if deserialization succeeded, %FALSE otherwise.
+ *
+ * Since: GIMP 2.10
+ **/
+gboolean
+gimp_config_deserialize_gfile (GimpConfig  *config,
+                               GFile       *file,
+                               gpointer     data,
+                               GError     **error)
+{
+  GScanner *scanner;
+  gboolean  success;
+
+  g_return_val_if_fail (GIMP_IS_CONFIG (config), FALSE);
+  g_return_val_if_fail (G_IS_FILE (file), FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  scanner = gimp_scanner_new_gfile (file, error);
+  if (! scanner)
+    return FALSE;
+
+  g_object_freeze_notify (G_OBJECT (config));
+
+  success = GIMP_CONFIG_GET_INTERFACE (config)->deserialize (config,
+                                                             scanner, 0, data);
+
+  g_object_thaw_notify (G_OBJECT (config));
+
+  gimp_scanner_destroy (scanner);
+
+  if (! success)
+    g_assert (error == NULL || *error != NULL);
+
+  return success;
+}
+
+/**
  * gimp_config_deserialize_string:
  * @config:   a #GObject that implements the #GimpConfigInterface.
  * @text:     string to deserialize (in UTF-8 encoding)
diff --git a/libgimpconfig/gimpconfig-iface.h b/libgimpconfig/gimpconfig-iface.h
index e9ccf0b..b26c34b 100644
--- a/libgimpconfig/gimpconfig-iface.h
+++ b/libgimpconfig/gimpconfig-iface.h
@@ -79,6 +79,12 @@ gboolean      gimp_config_serialize_to_file     (GimpConfig       *config,
                                                  const gchar      *footer,
                                                  gpointer          data,
                                                  GError          **error);
+gboolean      gimp_config_serialize_to_gfile    (GimpConfig       *config,
+                                                 GFile            *file,
+                                                 const gchar      *header,
+                                                 const gchar      *footer,
+                                                 gpointer          data,
+                                                 GError          **error);
 gboolean      gimp_config_serialize_to_fd       (GimpConfig       *config,
                                                  gint              fd,
                                                  gpointer          data);
@@ -88,6 +94,10 @@ gboolean      gimp_config_deserialize_file      (GimpConfig       *config,
                                                  const gchar      *filename,
                                                  gpointer          data,
                                                  GError          **error);
+gboolean      gimp_config_deserialize_gfile     (GimpConfig       *config,
+                                                 GFile            *file,
+                                                 gpointer          data,
+                                                 GError          **error);
 gboolean      gimp_config_deserialize_string    (GimpConfig       *config,
                                                  const gchar      *text,
                                                  gint              text_len,
diff --git a/libgimpconfig/gimpconfig.def b/libgimpconfig/gimpconfig.def
index c29106e..292beec 100644
--- a/libgimpconfig/gimpconfig.def
+++ b/libgimpconfig/gimpconfig.def
@@ -8,6 +8,7 @@ EXPORTS
        gimp_config_copy
        gimp_config_deserialize
        gimp_config_deserialize_file
+       gimp_config_deserialize_gfile
        gimp_config_deserialize_properties
        gimp_config_deserialize_property
        gimp_config_deserialize_return
@@ -29,6 +30,7 @@ EXPORTS
        gimp_config_serialize_property_by_name
        gimp_config_serialize_to_fd
        gimp_config_serialize_to_file
+       gimp_config_serialize_to_gfile
        gimp_config_serialize_to_string
        gimp_config_serialize_value
        gimp_config_string_append_escaped
@@ -42,6 +44,7 @@ EXPORTS
        gimp_config_writer_linefeed
        gimp_config_writer_new_fd
        gimp_config_writer_new_file
+       gimp_config_writer_new_gfile
        gimp_config_writer_new_string
        gimp_config_writer_open
        gimp_config_writer_print
@@ -53,6 +56,7 @@ EXPORTS
        gimp_param_spec_config_path_type
        gimp_scanner_destroy
        gimp_scanner_new_file
+       gimp_scanner_new_gfile
        gimp_scanner_new_string
        gimp_scanner_parse_boolean
        gimp_scanner_parse_color
diff --git a/libgimpconfig/gimpconfigwriter.c b/libgimpconfig/gimpconfigwriter.c
index 1f455be..6aaafeb 100644
--- a/libgimpconfig/gimpconfigwriter.c
+++ b/libgimpconfig/gimpconfigwriter.c
@@ -178,6 +178,42 @@ gimp_config_writer_new_file (const gchar  *filename,
 }
 
 /**
+ * gimp_config_writer_new_gfile:
+ * @file: a #GFile
+ * @atomic: if %TRUE the file is written atomically
+ * @header: text to include as comment at the top of the file
+ * @error: return location for errors
+ *
+ * Creates a new #GimpConfigWriter and sets it up to write to
+ * @file. If @atomic is %TRUE, a temporary file is used to avoid
+ * possible race conditions. The temporary file is then moved to @file
+ * when the writer is closed.
+ *
+ * Return value: a new #GimpConfigWriter or %NULL in case of an error
+ *
+ * Since: GIMP 2.10
+ **/
+GimpConfigWriter *
+gimp_config_writer_new_gfile (GFile        *file,
+                              gboolean      atomic,
+                              const gchar  *header,
+                              GError      **error)
+{
+  GimpConfigWriter *writer;
+  gchar            *path;
+
+  g_return_val_if_fail (G_IS_FILE (file), NULL);
+
+  path = g_file_get_path (file);
+
+  writer = gimp_config_writer_new_file (path, atomic, header, error);
+
+  g_free (path);
+
+  return writer;
+}
+
+/**
  * gimp_config_writer_new_fd:
  * @fd:
  *
diff --git a/libgimpconfig/gimpconfigwriter.h b/libgimpconfig/gimpconfigwriter.h
index 801ec52..3e430c1 100644
--- a/libgimpconfig/gimpconfigwriter.h
+++ b/libgimpconfig/gimpconfigwriter.h
@@ -31,6 +31,10 @@ GimpConfigWriter * gimp_config_writer_new_file     (const gchar       *filename,
                                                     gboolean           atomic,
                                                     const gchar       *header,
                                                     GError           **error);
+GimpConfigWriter * gimp_config_writer_new_gfile    (GFile             *file,
+                                                    gboolean           atomic,
+                                                    const gchar       *header,
+                                                    GError           **error);
 GimpConfigWriter * gimp_config_writer_new_fd       (gint               fd);
 GimpConfigWriter * gimp_config_writer_new_string   (GString           *string);
 
diff --git a/libgimpconfig/gimpscanner.c b/libgimpconfig/gimpscanner.c
index b94d74d..2f8777c 100644
--- a/libgimpconfig/gimpscanner.c
+++ b/libgimpconfig/gimpscanner.c
@@ -113,6 +113,33 @@ gimp_scanner_new_file (const gchar  *filename,
 }
 
 /**
+ * gimp_scanner_new_gfile:
+ * @file: a #GFile
+ * @error: return location for #GError, or %NULL
+ *
+ * Return value: The new #GScanner.
+ *
+ * Since: GIMP 2.10
+ **/
+GScanner *
+gimp_scanner_new_gfile (GFile  *file,
+                        GError **error)
+{
+  GScanner *scanner;
+  gchar    *path;
+
+  g_return_val_if_fail (G_IS_FILE (file), NULL);
+
+  path = g_file_get_path (file);
+
+  scanner = gimp_scanner_new_file (path, error);
+
+  g_free (path);
+
+  return scanner;
+}
+
+/**
  * gimp_scanner_new_string:
  * @text:
  * @text_len:
diff --git a/libgimpconfig/gimpscanner.h b/libgimpconfig/gimpscanner.h
index 5833f42..6592dba 100644
--- a/libgimpconfig/gimpscanner.h
+++ b/libgimpconfig/gimpscanner.h
@@ -30,6 +30,8 @@
 
 GScanner * gimp_scanner_new_file                 (const gchar  *filename,
                                                   GError      **error);
+GScanner * gimp_scanner_new_gfile                (GFile        *file,
+                                                  GError      **error);
 GScanner * gimp_scanner_new_string               (const gchar  *text,
                                                   gint          text_len,
                                                   GError      **error);


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