From c628684c5b463933a9b1e2d2fdae7b96ef598be2 Mon Sep 17 00:00:00 2001 From: Markus Koch Date: Sat, 4 Feb 2017 19:55:08 +0100 Subject: [PATCH] Added time span detection --- src/main.c | 1 + src/mainwindow.c | 34 +++++++++++++++++++++++++++++----- src/mainwindow.h | 4 ++++ src/mdiary.c | 31 ++++++++++++++++++++++++++++++- src/mdiary.h | 2 ++ 5 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index 0390bd1..dbbf5fb 100644 --- a/src/main.c +++ b/src/main.c @@ -14,6 +14,7 @@ int main(int argc, char *argv[]) gtk_widget_show(mainWindow->mainWindow); mdiary_scan_to_store(argv[1], mainWindow->entryListStore); + mainWindow_set_meta_information(mainWindow, mdiary_get_time_earliest(), mdiary_get_time_latest()); gtk_main(); diff --git a/src/mainwindow.c b/src/mainwindow.c index 0503960..86ab21d 100644 --- a/src/mainwindow.c +++ b/src/mainwindow.c @@ -280,6 +280,8 @@ struct mainWindow *mainWindow_new() mainWindow->filterSettings.time_start = 0; mainWindow->filterSettings.time_end = -1; + mainWindow->time_earliest = 0; + mainWindow->time_latest = 0; gtk_builder_connect_signals(builder, NULL); @@ -295,6 +297,20 @@ struct mainWindow *mainWindow_new() return mainWindow; } +/** + * @brief mainWindow_set_meta_information sets some meta info for a nice GUI. It also resets the search. + * @param mainWindow + * @param time_earliest is the reset value for the filter start date + * @param time_latest is the reset value for the filter end date + */ +void mainWindow_set_meta_information(struct mainWindow *mainWindow, guint time_earliest, guint time_latest) +{ + mainWindow->time_earliest = time_earliest; + mainWindow->time_latest = time_latest; + + mainWindow_clearSearch(NULL, mainWindow); +} + /** * @brief mainWindow_chooseWorkspaceClicked is called when the user clicks the Choose Workspace button. * @param widget N/A @@ -464,12 +480,20 @@ void mainWindow_checkDate(GtkWidget *widget, gint event, gpointer user_data) void mainWindow_clearSearch(GtkWidget *widget, gpointer user_data) { struct mainWindow *mainWindow = (struct mainWindow *)user_data; + GDateTime *datetime; + gchar *temp; gtk_entry_set_text(GTK_ENTRY(mainWindow->searchEntry), ""); - /** - * These need to be set to oldest and newest after indexing date - */ - gtk_entry_set_text(GTK_ENTRY(mainWindow->dateStart), "2016-01-01"); - gtk_entry_set_text(GTK_ENTRY(mainWindow->dateEnd), "2018-01-01"); + datetime = g_date_time_new_from_unix_local(mainWindow->time_earliest); + temp = g_date_time_format(datetime, "%Y-%m-%d"); + gtk_entry_set_text(GTK_ENTRY(mainWindow->dateStart), temp); + g_date_time_unref(datetime); + g_free(temp); + + datetime = g_date_time_new_from_unix_local(mainWindow->time_latest); + temp = g_date_time_format(datetime, "%Y-%m-%d"); + gtk_entry_set_text(GTK_ENTRY(mainWindow->dateEnd), temp); + g_date_time_unref(datetime); + g_free(temp); } diff --git a/src/mainwindow.h b/src/mainwindow.h index 8b22401..5470013 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -28,6 +28,9 @@ struct mainWindow { GtkWidget *selectedDateEntry; GtkTreeModelFilter *entryListFiltered; + guint time_earliest; + guint time_latest; + struct filterSettings { guint time_start; guint time_end; @@ -39,6 +42,7 @@ struct mainWindow { }; struct mainWindow *mainWindow_new(); +void mainWindow_set_meta_information(struct mainWindow *mainWindow, guint time_earliest, guint time_latest); /* Slots */ void mainWindow_showPopover(GtkWidget *widget, GdkEvent *event, gpointer user_data); diff --git a/src/mdiary.c b/src/mdiary.c index b7f18f8..6028bc2 100644 --- a/src/mdiary.c +++ b/src/mdiary.c @@ -1,5 +1,8 @@ #include "mdiary.h" +static gint64 time_earliest; +static gint64 time_latest; + /** * @brief The dateFormats struct contains regex/date-info pairs to parse different date formats. */ @@ -154,7 +157,7 @@ static gboolean mdiary_is_empty_line(gchar *string) { GRegex *regex; GMatchInfo *match_info; - gchar *ret = 0; + gboolean ret = 0; regex = g_regex_new("^\\s*$", G_REGEX_RAW, 0, NULL); if (g_regex_match(regex, string, 0, &match_info) && @@ -302,6 +305,11 @@ static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListSto summary, text->str); + if (g_date_time_to_unix(datetime) > time_latest) + time_latest = g_date_time_to_unix(datetime); + if (g_date_time_to_unix(datetime) < time_earliest) + time_earliest = g_date_time_to_unix(datetime); + g_input_stream_close(G_INPUT_STREAM(dstream), NULL, NULL); g_input_stream_close(G_INPUT_STREAM(stream), NULL, NULL); g_string_free(text, 0); @@ -347,9 +355,30 @@ static void mdiary_recurse_and_collect(gchar *base_dir, GtkListStore *entryListS */ void mdiary_scan_to_store(gchar *base_dir, GtkListStore *entryListStore) { + time_earliest = G_MAXINT64; + time_latest = 0; mdiary_recurse_and_collect(base_dir, entryListStore, 5); } +/** + * @brief mdiary_get_time_earliest getter function for time_earliest. Call mdiary_scan_to_store before using. + * @return time_earliest + */ +guint mdiary_get_time_earliest(void) +{ + return time_earliest; +} + +/** + * @brief mdiary_get_time_earliest getter function for time_latest. Call mdiary_scan_to_store before using. + * @return time_latest + */ +guint mdiary_get_time_latest(void) +{ + return time_latest; +} + + /** * @brief mainWindow_taglist_to_string concatenates a list with commas * @param list is a GList of strings to concatenate diff --git a/src/mdiary.h b/src/mdiary.h index b72325c..9391b15 100644 --- a/src/mdiary.h +++ b/src/mdiary.h @@ -26,5 +26,7 @@ void mdiary_add_entry_to_store(GtkListStore *entryListStore, gchar *text); GDateTime *mdiary_get_date_from_string_ext(gchar *string, gchar *prefix, gchar *suffix); GDateTime *mdiary_get_date_from_string(gchar *string); +guint mdiary_get_time_earliest(void); +guint mdiary_get_time_latest(void); #endif /* MDIARY_H */