[gimp] script-fu: port script loading to GFileEnumerator



commit 48e6199a4f8e2a80f266e2dbf9f9cd9b16be399a
Author: Michael Natterer <mitch gimp org>
Date:   Sun Aug 3 21:49:20 2014 +0200

    script-fu: port script loading to GFileEnumerator

 plug-ins/script-fu/scheme-wrapper.c    |   21 ++++---
 plug-ins/script-fu/scheme-wrapper.h    |    2 +-
 plug-ins/script-fu/script-fu-scripts.c |  107 ++++++++++++++++++++++----------
 plug-ins/script-fu/script-fu-scripts.h |   10 ++--
 plug-ins/script-fu/script-fu.c         |   21 +++---
 5 files changed, 101 insertions(+), 60 deletions(-)
---
diff --git a/plug-ins/script-fu/scheme-wrapper.c b/plug-ins/script-fu/scheme-wrapper.c
index 14fa9a3..c84152d 100644
--- a/plug-ins/script-fu/scheme-wrapper.c
+++ b/plug-ins/script-fu/scheme-wrapper.c
@@ -177,8 +177,8 @@ static scheme sc;
 
 
 void
-tinyscheme_init (const gchar *path,
-                 gboolean     register_scripts)
+tinyscheme_init (GList    *path,
+                 gboolean  register_scripts)
 {
   /* init the interpreter */
   if (! scheme_init (&sc))
@@ -201,31 +201,34 @@ tinyscheme_init (const gchar *path,
 
   if (path)
     {
-      GList *dir_list = gimp_path_parse (path, 256, TRUE, NULL);
       GList *list;
 
-      for (list = dir_list; list; list = g_list_next (list))
+      for (list = path; list; list = g_list_next (list))
         {
-          if (ts_load_file (list->data, "script-fu.init"))
+          gchar *dir = g_file_get_path (list->data);
+
+          if (ts_load_file (dir, "script-fu.init"))
             {
               /*  To improve compatibility with older Script-Fu scripts,
                *  load script-fu-compat.init from the same directory.
                */
-              ts_load_file (list->data, "script-fu-compat.init");
+              ts_load_file (dir, "script-fu-compat.init");
 
               /*  To improve compatibility with older GIMP version,
                *  load plug-in-compat.init from the same directory.
                */
-              ts_load_file (list->data, "plug-in-compat.init");
+              ts_load_file (dir, "plug-in-compat.init");
+
+              g_free (dir);
 
               break;
             }
+
+          g_free (dir);
         }
 
       if (list == NULL)
         g_printerr ("Unable to read initialization file script-fu.init\n");
-
-      gimp_path_free (dir_list);
     }
 }
 
diff --git a/plug-ins/script-fu/scheme-wrapper.h b/plug-ins/script-fu/scheme-wrapper.h
index 9ca416d..ec4fdc3 100644
--- a/plug-ins/script-fu/scheme-wrapper.h
+++ b/plug-ins/script-fu/scheme-wrapper.h
@@ -20,7 +20,7 @@
 
 #include "tinyscheme/scheme.h"
 
-void          tinyscheme_init         (const gchar  *path,
+void          tinyscheme_init         (GList        *path,
                                        gboolean      register_scripts);
 
 void          ts_set_run_mode         (GimpRunMode   run_mode);
diff --git a/plug-ins/script-fu/script-fu-scripts.c b/plug-ins/script-fu/script-fu-scripts.c
index 1606ec5..521a4c2 100644
--- a/plug-ins/script-fu/script-fu-scripts.c
+++ b/plug-ins/script-fu/script-fu-scripts.c
@@ -54,28 +54,28 @@ typedef struct
  *  Local Functions
  */
 
-static gboolean  script_fu_run_command    (const gchar             *command,
-                                           GError                 **error);
-static void      script_fu_load_script    (const GimpDatafileData  *file_data,
-                                           gpointer                 user_data);
-static gboolean  script_fu_install_script (gpointer                 foo,
-                                           GList                   *scripts,
-                                           gpointer                 bar);
-static void      script_fu_install_menu   (SFMenu                  *menu);
-static gboolean  script_fu_remove_script  (gpointer                 foo,
-                                           GList                   *scripts,
-                                           gpointer                 bar);
-static void      script_fu_script_proc    (const gchar             *name,
-                                           gint                     nparams,
-                                           const GimpParam         *params,
-                                           gint                    *nreturn_vals,
-                                           GimpParam              **return_vals);
-
-static SFScript *script_fu_find_script    (const gchar             *name);
-
-static gchar *   script_fu_menu_map       (const gchar             *menu_path);
-static gint      script_fu_menu_compare   (gconstpointer            a,
-                                           gconstpointer            b);
+static gboolean  script_fu_run_command    (const gchar      *command,
+                                           GError          **error);
+static void      script_fu_load_directory (GFile            *directory);
+static void      script_fu_load_script    (GFile            *file);
+static gboolean  script_fu_install_script (gpointer          foo,
+                                           GList            *scripts,
+                                           gpointer          bar);
+static void      script_fu_install_menu   (SFMenu           *menu);
+static gboolean  script_fu_remove_script  (gpointer          foo,
+                                           GList            *scripts,
+                                           gpointer          bar);
+static void      script_fu_script_proc    (const gchar      *name,
+                                           gint              nparams,
+                                           const GimpParam  *params,
+                                           gint             *nreturn_vals,
+                                           GimpParam       **return_vals);
+
+static SFScript *script_fu_find_script    (const gchar      *name);
+
+static gchar *   script_fu_menu_map       (const gchar      *menu_path);
+static gint      script_fu_menu_compare   (gconstpointer     a,
+                                           gconstpointer     b);
 
 
 /*
@@ -91,8 +91,10 @@ static GList *script_menu_list = NULL;
  */
 
 void
-script_fu_find_scripts (const gchar *path)
+script_fu_find_scripts (GList *path)
 {
+  GList *list;
+
   /*  Make sure to clear any existing scripts  */
   if (script_tree != NULL)
     {
@@ -107,9 +109,10 @@ script_fu_find_scripts (const gchar *path)
 
   script_tree = g_tree_new ((GCompareFunc) g_utf8_collate);
 
-  gimp_datafiles_read_directories (path, G_FILE_TEST_IS_REGULAR,
-                                   script_fu_load_script,
-                                   NULL);
+  for (list = path; list; list = g_list_next (list))
+    {
+      script_fu_load_directory (list->data);
+    }
 
   /*  Now that all scripts are read in and sorted, tell gimp about them  */
   g_tree_foreach (script_tree,
@@ -587,12 +590,49 @@ script_fu_run_command (const gchar  *command,
 }
 
 static void
-script_fu_load_script (const GimpDatafileData *file_data,
-                       gpointer                user_data)
+script_fu_load_directory (GFile *directory)
+{
+  GFileEnumerator *enumerator;
+
+  enumerator = g_file_enumerate_children (directory,
+                                          G_FILE_ATTRIBUTE_STANDARD_NAME ","
+                                          G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN ","
+                                          G_FILE_ATTRIBUTE_STANDARD_TYPE,
+                                          G_FILE_QUERY_INFO_NONE,
+                                          NULL, NULL);
+
+  if (enumerator)
+    {
+      GFileInfo *info;
+
+      while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)))
+        {
+          GFileType file_type = g_file_info_get_file_type (info);
+
+         if (file_type == G_FILE_TYPE_REGULAR &&
+             ! g_file_info_get_is_hidden (info))
+            {
+              GFile *child = g_file_enumerator_get_child (enumerator, info);
+
+              script_fu_load_script (child);
+
+              g_object_unref (child);
+            }
+
+          g_object_unref (info);
+        }
+
+      g_object_unref (enumerator);
+    }
+}
+
+static void
+script_fu_load_script (GFile *file)
 {
-  if (gimp_datafiles_check_extension (file_data->filename, ".scm"))
+  if (gimp_file_has_extension (file, ".scm"))
     {
-      gchar  *escaped = script_fu_strescape (file_data->filename);
+      gchar  *path    = g_file_get_path (file);
+      gchar  *escaped = script_fu_strescape (path);
       gchar  *command;
       GError *error   = NULL;
 
@@ -601,15 +641,13 @@ script_fu_load_script (const GimpDatafileData *file_data,
 
       if (! script_fu_run_command (command, &error))
         {
-          gchar *display_name = g_filename_display_name (file_data->filename);
-          gchar *message      = g_strdup_printf (_("Error while loading %s:"),
-                                                 display_name);
+          gchar *message = g_strdup_printf (_("Error while loading %s:"),
+                                            gimp_file_get_utf8_name (file));
 
           g_message ("%s\n\n%s", message, error->message);
 
           g_clear_error (&error);
           g_free (message);
-          g_free (display_name);
         }
 
 #ifdef G_OS_WIN32
@@ -620,6 +658,7 @@ script_fu_load_script (const GimpDatafileData *file_data,
 #endif
 
       g_free (command);
+      g_free (path);
     }
 }
 
diff --git a/plug-ins/script-fu/script-fu-scripts.h b/plug-ins/script-fu/script-fu-scripts.h
index 07f5030..a2e3cce 100644
--- a/plug-ins/script-fu/script-fu-scripts.h
+++ b/plug-ins/script-fu/script-fu-scripts.h
@@ -19,11 +19,11 @@
 #define __SCRIPT_FU_SCRIPTS_H__
 
 
-void      script_fu_find_scripts  (const gchar *path);
-pointer   script_fu_add_script    (scheme      *sc,
-                                   pointer      a);
-pointer   script_fu_add_menu      (scheme      *sc,
-                                   pointer      a);
+void      script_fu_find_scripts  (GList   *path);
+pointer   script_fu_add_script    (scheme  *sc,
+                                   pointer  a);
+pointer   script_fu_add_menu      (scheme  *sc,
+                                   pointer  a);
 
 
 #endif /*  __SCRIPT_FU_SCRIPTS__  */
diff --git a/plug-ins/script-fu/script-fu.c b/plug-ins/script-fu/script-fu.c
index 70f95ef..e1ea492 100644
--- a/plug-ins/script-fu/script-fu.c
+++ b/plug-ins/script-fu/script-fu.c
@@ -46,7 +46,7 @@ static void    script_fu_run            (const gchar      *name,
                                          const GimpParam  *params,
                                          gint             *nreturn_vals,
                                          GimpParam       **return_vals);
-static gchar * script_fu_search_path    (void);
+static GList * script_fu_search_path    (void);
 static void    script_fu_extension_init (void);
 static void    script_fu_refresh_proc   (const gchar      *name,
                                          gint              nparams,
@@ -178,7 +178,7 @@ script_fu_run (const gchar      *name,
                gint             *nreturn_vals,
                GimpParam       **return_vals)
 {
-  gchar *path;
+  GList *path;
 
   INIT_I18N();
 
@@ -207,7 +207,7 @@ script_fu_run (const gchar      *name,
   /*  Load all of the available scripts  */
   script_fu_find_scripts (path);
 
-  g_free (path);
+  g_list_free_full (path, (GDestroyNotify) g_object_unref);
 
   if (strcmp (name, "extension-script-fu") == 0)
     {
@@ -269,11 +269,11 @@ script_fu_run (const gchar      *name,
     }
 }
 
-static gchar *
+static GList *
 script_fu_search_path (void)
 {
-  gchar  *path_str;
-  gchar  *path  = NULL;
+  gchar *path_str;
+  GList *path  = NULL;
 
   path_str = gimp_gimprc_query ("script-fu-path");
 
@@ -281,15 +281,14 @@ script_fu_search_path (void)
     {
       GError *error = NULL;
 
-      path = g_filename_from_utf8 (path_str, -1, NULL, NULL, &error);
-
+      path = gimp_config_path_expand_to_files (path_str, &error);
       g_free (path_str);
 
       if (! path)
         {
           g_warning ("Can't convert script-fu-path to filesystem encoding: %s",
                      error->message);
-          g_error_free (error);
+          g_clear_error (&error);
         }
     }
 
@@ -369,11 +368,11 @@ script_fu_refresh_proc (const gchar      *name,
   else
     {
       /*  Reload all of the available scripts  */
-      gchar *path = script_fu_search_path ();
+      GList *path = script_fu_search_path ();
 
       script_fu_find_scripts (path);
 
-      g_free (path);
+      g_list_free_full (path, (GDestroyNotify) g_object_unref);
 
       status = GIMP_PDB_SUCCESS;
     }


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