[gnome-commander] Add function for loading directory files, use it for setting up the file popup menu with scripts



commit f13ddbe3f90dd60ae053221f5ffb8309a6de1b1a
Author: Uwe Scholz <u scholz83 gmx de>
Date:   Sat Oct 9 11:11:08 2021 +0200

    Add function for loading directory files, use it for setting up the file popup menu with scripts

 src/dirlist.cc                | 58 +++++++++++++++++++++++++++++++++++++++++++
 src/dirlist.h                 |  1 +
 src/gnome-cmd-file-popmenu.cc | 43 +++++++++++++++++---------------
 src/gnome-cmd-file-popmenu.h  |  2 ++
 4 files changed, 84 insertions(+), 20 deletions(-)
---
diff --git a/src/dirlist.cc b/src/dirlist.cc
index c94300f9..2e36ad33 100644
--- a/src/dirlist.cc
+++ b/src/dirlist.cc
@@ -282,3 +282,61 @@ void dirlist_cancel (GnomeCmdDir *dir)
     // ToDo: Add a cancel-trigger for the async dir listing
     DEBUG('l', "Cancel dir-listing not implemented yet...\n");
 }
+
+
+GList* sync_dir_list (const gchar *absDirPath)
+{
+    g_return_val_if_fail (absDirPath != nullptr, nullptr);
+
+    GError *error = nullptr;
+
+    DEBUG('l', "sync_dir_list: %s\n", absDirPath);
+
+    GList *gFileInfoList = nullptr;
+
+    auto gFile = g_file_new_for_path(absDirPath);
+
+    if (!g_file_query_exists(gFile, nullptr))
+    {
+        g_warning("sync_dir_list error: \"%s\" does not exist", absDirPath);
+        g_object_unref(gFile);
+        return nullptr;
+    }
+
+    auto gFileEnumerator = g_file_enumerate_children (gFile,
+                            "*",
+                            G_FILE_QUERY_INFO_NONE,
+                            nullptr,
+                            &error);
+    if(error)
+    {
+        g_critical("sync_dir_list: Unable to enumerate children, error: %s", error->message);
+        g_error_free(error);
+        return nullptr;
+    }
+
+    GFileInfo *gFileInfoTmp = nullptr;
+    do
+    {
+        gFileInfoTmp = g_file_enumerator_next_file(gFileEnumerator, nullptr, &error);
+        if(error)
+        {
+            g_critical("sync_dir_list: Unable to enumerate next file, error: %s", error->message);
+            break;
+        }
+        if (gFileInfoTmp)
+        {
+            gFileInfoList = g_list_append(gFileInfoList, gFileInfoTmp);
+        }
+    }
+    while (gFileInfoTmp && !error);
+
+    if (error)
+    {
+        g_error_free(error);
+    }
+
+    g_file_enumerator_close (gFileEnumerator, nullptr, nullptr);
+    g_object_unref(gFileEnumerator);
+    return gFileInfoList;
+}
diff --git a/src/dirlist.h b/src/dirlist.h
index 664d7420..f6ca758c 100644
--- a/src/dirlist.h
+++ b/src/dirlist.h
@@ -25,3 +25,4 @@
 
 void dirlist_list (GnomeCmdDir *dir, gboolean visprog);
 void dirlist_cancel (GnomeCmdDir *dir);
+GList* sync_dir_list (const gchar *absDirPath);
diff --git a/src/gnome-cmd-file-popmenu.cc b/src/gnome-cmd-file-popmenu.cc
index 1a58ec8f..ebd8e0dc 100644
--- a/src/gnome-cmd-file-popmenu.cc
+++ b/src/gnome-cmd-file-popmenu.cc
@@ -35,6 +35,7 @@
 #include "gnome-cmd-plain-path.h"
 #include "gnome-cmd-xfer.h"
 #include "imageloader.h"
+#include "dirlist.h"
 
 #include <fnmatch.h>
 
@@ -501,6 +502,8 @@ static void destroy (GtkObject *object)
     g_list_foreach (menu->priv->data_list, (GFunc) g_free, nullptr);
     g_list_free (menu->priv->data_list);
 
+    g_free(menu->scriptsDir);
+
     g_free (menu->priv);
 
     if (GTK_OBJECT_CLASS (parent_class)->destroy)
@@ -531,37 +534,37 @@ static void init (GnomeCmdFilePopmenu *menu)
 {
     menu->priv = g_new0 (GnomeCmdFilePopmenuPrivate, 1);
 
+    menu->scriptsDir = g_build_filename (g_get_user_config_dir (), PACKAGE SCRIPT_DIRECTORY, nullptr);
+
     // Create scripts directory if needed
-    gchar *scripts_dir = g_build_filename (g_get_user_config_dir (), PACKAGE "/scripts", nullptr);
-    create_dir_if_needed(scripts_dir);
-    g_free (scripts_dir);
+    if (!is_dir_existing(menu->scriptsDir))
+    {
+        create_dir(menu->scriptsDir);
+    }
 }
 
 
 inline GList *get_list_of_action_script_file_names(const gchar* scriptsDir)
 {
-    DIR *dp = opendir (scriptsDir);
+    g_return_val_if_fail (scriptsDir != nullptr, nullptr);
+
+    auto gFileInfoList = sync_dir_list(scriptsDir);
+
+    if (g_list_length (gFileInfoList) == 0)
+        return nullptr;
+
     GList *scriptList = nullptr;
-    if (dp != nullptr)
+    for (auto gFileInfoListItem = gFileInfoList; gFileInfoListItem; gFileInfoListItem = 
gFileInfoListItem->next)
     {
-        struct dirent *directoryEntry;
-        while ((directoryEntry = readdir (dp)))
+        auto gFileInfo = (GFileInfo*) gFileInfoListItem->data;
+        if (g_file_info_get_file_type(gFileInfo) == G_FILE_TYPE_REGULAR)
         {
-            char *fileName = directoryEntry->d_name;
-            struct stat buf;
-            string scriptPath (scriptsDir);
-            scriptPath.append ("/").append (fileName);
-            if (stat (scriptPath.c_str(), &buf) == 0)
-            {
-                if (buf.st_mode & S_IFREG)
-                {
-                    DEBUG('p', "Adding \'%s\' to the list of scripts.\n", scriptPath.c_str());
-                    scriptList = g_list_append (scriptList, g_strdup(fileName));
-                }
-            }
+            DEBUG('p', "Adding \'%s\' to the list of scripts.\n", g_file_info_get_name(gFileInfo));
+            scriptList = g_list_append (scriptList, g_strdup(g_file_info_get_name(gFileInfo)));
         }
-        closedir (dp);
+
     }
+    g_list_free_full(gFileInfoList, g_object_unref);
     return scriptList;
 }
 
diff --git a/src/gnome-cmd-file-popmenu.h b/src/gnome-cmd-file-popmenu.h
index 64b301d6..dde87665 100644
--- a/src/gnome-cmd-file-popmenu.h
+++ b/src/gnome-cmd-file-popmenu.h
@@ -40,6 +40,8 @@ struct GnomeCmdFilePopmenu
     GtkMenu parent;
 
     GnomeCmdFilePopmenuPrivate *priv;
+
+    gchar* scriptsDir;
 };
 
 


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