2017-02-01 18:20:48 +01:00
|
|
|
#include "mdiary.h"
|
2017-02-04 13:37:56 +01:00
|
|
|
|
2017-02-04 14:22:01 +01:00
|
|
|
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)
|
2017-02-04 13:37:56 +01:00
|
|
|
{
|
|
|
|
GError *error;
|
2017-02-04 14:22:01 +01:00
|
|
|
GDir *dir = g_dir_open(base_dir, 0, &error);
|
2017-02-04 13:37:56 +01:00
|
|
|
gchar *dirname;
|
2017-02-04 14:22:01 +01:00
|
|
|
gchar *fullPath;
|
|
|
|
GRegex *regex;
|
|
|
|
GMatchInfo *match_info;
|
2017-02-04 13:37:56 +01:00
|
|
|
|
|
|
|
if (!dir) {
|
|
|
|
g_print("Error, could not open base directory.\n");
|
|
|
|
} else {
|
|
|
|
while (dirname = (gchar *)g_dir_read_name(dir)) {
|
2017-02-04 14:22:01 +01:00
|
|
|
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);
|
2017-02-04 13:37:56 +01:00
|
|
|
}
|
|
|
|
g_dir_close(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-04 14:22:01 +01:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
2017-02-04 13:37:56 +01:00
|
|
|
/**
|
|
|
|
* @brief mainWindow_taglist_to_string concatenates a list with commas
|
|
|
|
* @param list is a GList of strings to concatenate
|
|
|
|
* @return The string, it needs to be freed using g_free().
|
|
|
|
*/
|
|
|
|
static gchar *mdiary_taglist_to_string(GList *list)
|
|
|
|
{
|
|
|
|
GString *str = NULL;
|
|
|
|
gchar *ret = NULL;
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
for (l = list; l != NULL; l = l->next) {
|
|
|
|
if (str == NULL)
|
|
|
|
str = g_string_new("");
|
|
|
|
else
|
|
|
|
str = g_string_append(str, ", ");
|
|
|
|
str = g_string_append(str, l->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = str->str;
|
|
|
|
g_string_free(str, 0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief mdiary_add_entry_to_store adds the specified entry to the store. All params are copied.
|
|
|
|
* @param entryListStore target store
|
|
|
|
* @param title Entry title
|
|
|
|
* @param datetime Entry GDateTime
|
|
|
|
* @param tags Entry tags
|
|
|
|
* @param text Entry MD text (with header)
|
|
|
|
*/
|
|
|
|
void mdiary_add_entry_to_store(GtkListStore *entryListStore,
|
|
|
|
gchar *title,
|
|
|
|
GDateTime *datetime,
|
|
|
|
GList *tags,
|
|
|
|
gchar *text)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GDateTime *datetime_copy;
|
|
|
|
gchar *date_text;
|
|
|
|
gchar *taglistString;
|
|
|
|
|
|
|
|
datetime_copy = g_date_time_add(datetime, 0);
|
|
|
|
date_text = g_date_time_format(datetime_copy, "%A, %e %B %Y %R");
|
|
|
|
taglistString = mdiary_taglist_to_string(tags);
|
|
|
|
|
|
|
|
gtk_list_store_append(entryListStore, &iter);
|
|
|
|
gtk_list_store_set(entryListStore, &iter,
|
|
|
|
COL_TITLE, title,
|
|
|
|
COL_DATE_TEXT, date_text,
|
|
|
|
COL_TAGS_TEXT, taglistString,
|
|
|
|
COL_TIMESTAMP, datetime_copy,
|
|
|
|
COL_TAGLIST, NULL,
|
|
|
|
COL_TEXT, g_strdup(text),
|
|
|
|
-1);
|
|
|
|
g_free(date_text);
|
|
|
|
}
|