with Image File acts as a cover for Sub Folder in the gallery but I can’t think of a way to find it. Please give me some hints as to how I could do it.
One thing that I noticed from your sample is that it only works with a single directory within Main Folder. In my case, Main Folder contains multiple Sub Folder, each with their own unique cover.png, which I should have specified more clearly.
If you don’t mind can you tweak it a bit to better fit my case?
Ah, you may want to enumerate subdirectories then! Consider the following sample code:
#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
/**
* utility_print_error:
*
* @function_name: the name of the function that failed
* @ptr_error: GError containing additional informations
*
* Utility function to print a detailed error report
* when a function fails.
*/
static void
utility_print_error (const char *function_name,
GError **ptr_error)
{
GError *error = ptr_error ? *ptr_error : NULL;
g_print ("%s failed", function_name);
if (error)
{
g_print (": %s", error->message);
g_clear_error (ptr_error);
}
g_print ("\n");
}
/**
* enumerate_child_directories:
*
* @directory: path to an existing directory
*
* Return value:
* A GList containing the names of all the subdirectories
* found within directory. The caller must free the result
* with `g_list_free_full (result, g_free)`
*/
static GList *
enumerate_child_directories (const char *directory)
{
GFile *dir = NULL;
GFileEnumerator *direnum = NULL;
GList *results = NULL;
GError *error = NULL;
dir = g_file_new_for_path (directory);
direnum = g_file_enumerate_children (dir,
"standard::*",
G_FILE_QUERY_INFO_NONE,
NULL, &error);
if (!direnum)
{
utility_print_error ("g_file_enumerate_children", &error);
goto cleanup;
}
while (TRUE)
{
GFileInfo *info;
if (!g_file_enumerator_iterate (direnum, &info,
NULL, NULL, &error))
{
utility_print_error ("g_file_enumerator_iterate", &error);
goto cleanup;
}
if (!info)
{
/* Done! */
break;
}
if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
results = g_list_prepend (results, g_strdup (g_file_info_get_name (info)));
}
cleanup:
g_clear_object (&direnum); /* Note: frees the last @info */
g_clear_object (&dir);
return g_list_reverse (results);
}
int main()
{
/* Instead of "/usr/include" one may pass anything else, e.g "C:\\" on Windows */
GList *child_dirs = enumerate_child_directories ("/usr/include");
for (GList *l = child_dirs; l != NULL; l = l->next)
{
g_print ("%s\n", (const char*) l->data);
}
g_list_free_full (child_dirs, g_free);
}
On Unix systems the file name may not be printable, neither on screen nor on a graphical UI. You may also want to acquire the display name of files using g_file_info_get_display_name(). But that’s generally usable as-is