diff --git a/src/main.c b/src/main.c index a5ccfd4..0390bd1 100644 --- a/src/main.c +++ b/src/main.c @@ -13,7 +13,7 @@ int main(int argc, char *argv[]) mainWindow = mainWindow_new(); gtk_widget_show(mainWindow->mainWindow); - mdiary_scan_to_store("/", mainWindow->entryListStore); + mdiary_scan_to_store(argv[1], mainWindow->entryListStore); gtk_main(); diff --git a/src/mdiary.c b/src/mdiary.c index 2da4076..25426e6 100644 --- a/src/mdiary.c +++ b/src/mdiary.c @@ -1,21 +1,49 @@ #include "mdiary.h" -void mdiary_scan_to_store(gchar *base_dir, GtkListStore *entryListStore) +static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListStore) +{ + g_print("Add file: %s\n", filename); +} + +static void mdiary_recurse_and_collect(gchar *base_dir, GtkListStore *entryListStore, guint max_level) { GError *error; - GDir *dir = g_dir_open("/", 0, &error); + GDir *dir = g_dir_open(base_dir, 0, &error); gchar *dirname; + gchar *fullPath; + GRegex *regex; + GMatchInfo *match_info; if (!dir) { g_print("Error, could not open base directory.\n"); } else { while (dirname = (gchar *)g_dir_read_name(dir)) { - g_print("EL=%s\n", dirname); + fullPath = g_strdup_printf("%s/%s", base_dir, dirname); + if (g_file_test(fullPath, G_FILE_TEST_IS_REGULAR)) { + regex = g_regex_new("\\.md$", G_REGEX_CASELESS, 0, NULL); + if (g_regex_match(regex, fullPath, 0, &match_info) && + g_match_info_matches(match_info)) + mdiary_add_file_to_store(fullPath, entryListStore); + g_regex_unref(regex); + } else if (g_file_test(fullPath, G_FILE_TEST_IS_DIR) && max_level) { + mdiary_recurse_and_collect(fullPath, entryListStore, max_level--); + } + g_free(fullPath); } g_dir_close(dir); } } +/** + * @brief mdiary_scan_to_store recursively (max. 5 levels) scans the base_dir into the entryListStore. + * @param base_dir The base directory to start scanning in + * @param entryListStore Target GtkListStore + */ +void mdiary_scan_to_store(gchar *base_dir, GtkListStore *entryListStore) +{ + mdiary_recurse_and_collect(base_dir, entryListStore, 5); +} + /** * @brief mainWindow_taglist_to_string concatenates a list with commas * @param list is a GList of strings to concatenate