diff --git a/src/main.c b/src/main.c index 3c1b0f2..a5ccfd4 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ #include #include "mainwindow.h" +#include "mdiary.h" int main(int argc, char *argv[]) { @@ -12,6 +13,8 @@ int main(int argc, char *argv[]) mainWindow = mainWindow_new(); gtk_widget_show(mainWindow->mainWindow); + mdiary_scan_to_store("/", mainWindow->entryListStore); + gtk_main(); return 0; diff --git a/src/mainwindow.c b/src/mainwindow.c index 442b22b..4705e64 100644 --- a/src/mainwindow.c +++ b/src/mainwindow.c @@ -1,16 +1,6 @@ #include "mainwindow.h" #include "mdiary.h" -enum { - COL_TITLE = 0, /* Title of the entry */ - COL_DATE_TEXT, /* Textual representation of the date (auto generated) */ - COL_TAGS_TEXT, /* Textual representation of the tags (auto generated) */ - COL_TIMESTAMP, /* GDateTime of the entry */ - COL_TAGLIST, /* GList of the tags */ - COL_TEXT, /* Raw text from the file */ - COL_COUNT -}; - /** * @brief The dateFormats struct contains regex/date-info pairs to parse different date formats. */ @@ -114,65 +104,6 @@ static GDateTime *mainWindow_get_date_from_string(gchar *string) return datetime; } -/** - * @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 *mainWindow_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 mainWindow_list_add_entry adds a diary entry to the main store. All parameters are duplicated. - * @param mainWindow struct mainWindow * - * @param title entry title - * @param datetime entry date time - * @param tags string list of tags - * @param text MD source of the entry - */ -static void mainWindow_list_add_entry(struct mainWindow *mainWindow, - 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 = mainWindow_taglist_to_string(tags); - - gtk_list_store_append(mainWindow->entryListStore, &iter); - gtk_list_store_set(mainWindow->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); -} - /** * @brief mainWindow_list_entry_visible checks whether the entry should be displayed. * @param model N/A @@ -336,15 +267,15 @@ static void mainWindow_configure_treeView(struct mainWindow *mainWindow) list = g_list_append(list, "Tag2"); dt = g_date_time_new_local(2017, 11, 21, 02, 03, 0); - mainWindow_list_add_entry(mainWindow, "Title", dt, list, "Text text text"); + mdiary_add_entry_to_store(mainWindow->entryListStore, "Title", dt, list, "Text text text"); g_date_time_unref(dt); dt = g_date_time_new_local(2017, 2, 3, 6, 7, 1); - mainWindow_list_add_entry(mainWindow, "AAA 2", dt, list, "fdfd Text text text"); + mdiary_add_entry_to_store(mainWindow->entryListStore, "AAA 2", dt, list, "fdfd Text text text"); g_date_time_unref(dt); dt = g_date_time_new_local(2016, 4, 18, 13, 44, 2); - mainWindow_list_add_entry(mainWindow, "Yey entries 3", dt, list, "The content is content."); + mdiary_add_entry_to_store(mainWindow->entryListStore, "Yey entries 3", dt, list, "The content is content."); g_date_time_unref(dt); } diff --git a/src/mainwindow.h b/src/mainwindow.h index d7ebed0..d172e84 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -3,6 +3,7 @@ #include #include +#include "mdiary.h" struct mainWindow { GtkWidget *mainWindow; diff --git a/src/mdiary.c b/src/mdiary.c index ec9b65e..2da4076 100644 --- a/src/mdiary.c +++ b/src/mdiary.c @@ -1 +1,77 @@ #include "mdiary.h" + +void mdiary_scan_to_store(gchar *base_dir, GtkListStore *entryListStore) +{ + GError *error; + GDir *dir = g_dir_open("/", 0, &error); + gchar *dirname; + + 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); + } + g_dir_close(dir); + } +} + +/** + * @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); +} diff --git a/src/mdiary.h b/src/mdiary.h index 3772938..9e124b3 100644 --- a/src/mdiary.h +++ b/src/mdiary.h @@ -1,4 +1,24 @@ #ifndef MDIARY_H #define MDIARY_H +#include +#include + +enum { + COL_TITLE = 0, /* Title of the entry */ + COL_DATE_TEXT, /* Textual representation of the date (auto generated) */ + COL_TAGS_TEXT, /* Textual representation of the tags (auto generated) */ + COL_TIMESTAMP, /* GDateTime of the entry */ + COL_TAGLIST, /* GList of the tags */ + COL_TEXT, /* Raw text from the file */ + COL_COUNT +}; + +void mdiary_scan_to_store(gchar *base_dir, + GtkListStore *entryListStore); +void mdiary_add_entry_to_store(GtkListStore *entryListStore, + gchar *title, + GDateTime *datetime, + GList *tags, + gchar *text); #endif /* MDIARY_H */