[gimp] libgimpconfig: add gimp_file_new_for_config_path() and _get_config_path()



commit d36d9567709c8dfe1810ef6fc39cb5cc24e4a8f5
Author: Michael Natterer <mitch gimp org>
Date:   Fri Sep 30 22:03:34 2016 +0200

    libgimpconfig: add gimp_file_new_for_config_path() and _get_config_path()
    
    Which turn a UTF-8 encoded config path directly into a GFile and back,
    using gimp_config_path_expand() and _unexpand().

 libgimpconfig/gimpconfig-path.c |   79 +++++++++++++++++++++++++++++++++++++++
 libgimpconfig/gimpconfig-path.h |    5 ++
 libgimpconfig/gimpconfig.def    |    2 +
 3 files changed, 86 insertions(+), 0 deletions(-)
---
diff --git a/libgimpconfig/gimpconfig-path.c b/libgimpconfig/gimpconfig-path.c
index 05479c8..95205de 100644
--- a/libgimpconfig/gimpconfig-path.c
+++ b/libgimpconfig/gimpconfig-path.c
@@ -410,6 +410,85 @@ gimp_config_path_unexpand (const gchar  *path,
   return gimp_config_path_unexpand_only (path);
 }
 
+/**
+ * gimp_file_new_for_config_path:
+ * @path:   a NUL-terminated string in UTF-8 encoding
+ * @error:  return location for errors
+ *
+ * Expands @path using gimp_config_path_expand() and returns a #GFile
+ * for the expanded path.
+ *
+ * To reverse the expansion, use gimp_file_get_config_path().
+ *
+ * Return value: a newly allocated #GFile, or %NULL if the expansion failed.
+ *
+ * Since: 2.10
+ **/
+GFile *
+gimp_file_new_for_config_path (const gchar  *path,
+                               GError      **error)
+{
+  GFile *file = NULL;
+  gchar *expanded;
+
+  g_return_val_if_fail (path != NULL, NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  expanded = gimp_config_path_expand (path, TRUE, error);
+
+  if (expanded)
+    {
+      file = g_file_new_for_path (expanded);
+      g_free (expanded);
+    }
+
+  return file;
+}
+
+/**
+ * gimp_file_new_for_config_path:
+ * @file:   a #GFile
+ * @error:  return location for errors
+ *
+ * Unexpands @file's path using gimp_config_path_unexpand() and
+ * returns the unexpanded path.
+ *
+ * The inverse operation of gimp_file_new_for_config_path().
+ *
+ * Return value: a newly allocated NUL-terminated UTF-8 string, or %NULL if
+ *               unexpanding failed.
+ *
+ * Since: 2.10
+ **/
+gchar *
+gimp_file_get_config_path (GFile   *file,
+                           GError **error)
+{
+  gchar *unexpanded = NULL;
+  gchar *path;
+
+  g_return_val_if_fail (G_IS_FILE (file), NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  path = g_file_get_path (file);
+
+  if (path)
+    {
+      unexpanded = gimp_config_path_unexpand (path, TRUE, error);
+      g_free (path);
+    }
+  else
+    {
+      g_set_error_literal (error, 0, 0,
+                           _("File has no path represantation"));
+    }
+
+  return unexpanded;
+}
+
+
+/*  private functions  */
+
 #define SUBSTS_ALLOC 4
 
 static gchar *
diff --git a/libgimpconfig/gimpconfig-path.h b/libgimpconfig/gimpconfig-path.h
index 783124a..eae9872 100644
--- a/libgimpconfig/gimpconfig-path.h
+++ b/libgimpconfig/gimpconfig-path.h
@@ -84,6 +84,11 @@ gchar             * gimp_config_path_unexpand        (const gchar  *path,
                                                       gboolean      recode,
                                                       GError      **error) G_GNUC_MALLOC;
 
+GFile             * gimp_file_new_for_config_path    (const gchar  *path,
+                                                      GError      **error) G_GNUC_MALLOC;
+gchar             * gimp_file_get_config_path        (GFile        *file,
+                                                      GError      **error) G_GNUC_MALLOC;
+
 gchar             * gimp_config_build_data_path      (const gchar  *name) G_GNUC_MALLOC;
 gchar             * gimp_config_build_writable_path  (const gchar  *name) G_GNUC_MALLOC;
 gchar             * gimp_config_build_plug_in_path   (const gchar  *name) G_GNUC_MALLOC;
diff --git a/libgimpconfig/gimpconfig.def b/libgimpconfig/gimpconfig.def
index f4c4946..5e4fc5b 100644
--- a/libgimpconfig/gimpconfig.def
+++ b/libgimpconfig/gimpconfig.def
@@ -70,6 +70,8 @@ EXPORTS
        gimp_config_writer_printf
        gimp_config_writer_revert
        gimp_config_writer_string
+       gimp_file_get_config_path
+       gimp_file_new_for_config_path
        gimp_param_config_path_get_type
        gimp_param_spec_config_path
        gimp_param_spec_config_path_type


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