Compare commits

..

2 Commits

Author SHA1 Message Date
8ac7436182 Added date filtering 2017-02-02 21:40:49 +01:00
e7f3513cb9 Added filter function code 2017-02-02 20:20:22 +01:00
2 changed files with 154 additions and 8 deletions

View File

@ -11,6 +11,99 @@ enum {
COL_COUNT COL_COUNT
}; };
struct dateFormats {
gchar *regex;
guint index_count;
guint index_year;
guint index_month;
guint index_day;
guint index_hour;
guint index_minute;
} dateFormats_list[] = {
{
.regex = "(\\d{1,4})-(\\d{1,2})-(\\d{1,2}) (\\d{1,2}):(\\d{1,2}).*",
.index_count = 5,
.index_year = 1,
.index_month = 2,
.index_day = 3,
.index_hour = 4,
.index_minute = 5
},
{
.regex = "(\\d{1,4})-(\\d{1,2})-(\\d{1,2}).*",
.index_count = 3,
.index_year = 1,
.index_month = 2,
.index_day = 3,
},
{
.regex = "(\\d{1,2})\\.(\\d{1,2})\\.(\\d{1,4}).*",
.index_count = 3,
.index_year = 3,
.index_month = 2,
.index_day = 1,
},
{
.regex = NULL
}
};
/**
* @brief Tries to guess the date format used and converts it to GDateTime.
* @param Input string.
* @return A GDateTime object or NULL on error. You need to free it after use.
*/
static GDateTime *mainWindow_get_date_from_string(gchar *string)
{
GRegex *regex;
GMatchInfo *match_info;
GRegexError *regex_error;
struct dateFormats *dateFormats;
GDateTime *datetime = NULL;
guint year = 0;
guint month = 0;
guint day = 0;
guint hour = 0;
guint minute = 0;
dateFormats = dateFormats_list;
do {
regex = g_regex_new(dateFormats->regex, G_REGEX_RAW, 0, NULL);
if (g_regex_match(regex, string, 0, &match_info)
&& g_regex_get_capture_count(regex) >= dateFormats->index_count &&
g_match_info_matches(match_info)) {
if (dateFormats->index_year)
year = atoi(g_match_info_fetch(match_info, dateFormats->index_year));
if (dateFormats->index_month)
month = atoi(g_match_info_fetch(match_info, dateFormats->index_month));
if (dateFormats->index_day)
day = atoi(g_match_info_fetch(match_info, dateFormats->index_day));
if (dateFormats->index_hour)
hour = atoi(g_match_info_fetch(match_info, dateFormats->index_hour));
if (dateFormats->index_minute)
minute = atoi(g_match_info_fetch(match_info, dateFormats->index_minute));
if (year >= 0 &&
month >=1 && month <=12 &&
day >= 1 && day <= 31 &&
hour >= 0 && hour <= 23 &&
minute >= 0 && minute <= 59) {
datetime = g_date_time_new_local(year, month, day, 0, 0, 0);
g_regex_unref(regex);
break;
}
}
g_regex_unref(regex);
} while ((++dateFormats)->regex);
if (!datetime)
g_print("Warning! Could not match date \"%s\"!\n", string);
return datetime;
}
/** /**
* Returns: The string, needs to be freed using g_free(). * Returns: The string, needs to be freed using g_free().
*/ */
@ -65,9 +158,16 @@ static void mainWindow_list_add_entry(struct mainWindow *mainWindow,
static gboolean mainWindow_list_entry_visible(GtkTreeModel *model, GtkTreeIter *iter, struct mainWindow *mainWindow) static gboolean mainWindow_list_entry_visible(GtkTreeModel *model, GtkTreeIter *iter, struct mainWindow *mainWindow)
{ {
/* gtk_tree_model_get (model, iter, COLUMN_NAME, &target, -1); */ GDateTime *datetime;
guint time;
return 1; gtk_tree_model_get(model, iter, COL_TIMESTAMP, &datetime, -1);
if (datetime) {
time = g_date_time_to_unix(datetime);
return time >= mainWindow->filterSettings.time_start && time <= mainWindow->filterSettings.time_end;
} else {
return 0;
}
} }
static gint mainWindow_sort_date_compare_func(GtkTreeModel *model, static gint mainWindow_sort_date_compare_func(GtkTreeModel *model,
@ -99,7 +199,6 @@ static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
GtkCellRenderer *renderer; GtkCellRenderer *renderer;
GtkTreeSortable *sortable; GtkTreeSortable *sortable;
GtkTreeViewColumn *col; GtkTreeViewColumn *col;
GtkTreeModelFilter *filtered;
GtkTreeModelSort *sorted; GtkTreeModelSort *sorted;
GtkTreeModel *model; GtkTreeModel *model;
@ -110,12 +209,13 @@ static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
G_TYPE_POINTER, G_TYPE_POINTER,
G_TYPE_POINTER, G_TYPE_POINTER,
G_TYPE_STRING); G_TYPE_STRING);
filtered = GTK_TREE_MODEL_FILTER(gtk_tree_model_filter_new( mainWindow->entryListFiltered = GTK_TREE_MODEL_FILTER(gtk_tree_model_filter_new(
GTK_TREE_MODEL(mainWindow->entryListStore), NULL)); GTK_TREE_MODEL(mainWindow->entryListStore),
NULL));
sorted = GTK_TREE_MODEL_SORT(gtk_tree_model_sort_new_with_model( sorted = GTK_TREE_MODEL_SORT(gtk_tree_model_sort_new_with_model(
GTK_TREE_MODEL(filtered))); GTK_TREE_MODEL(mainWindow->entryListFiltered)));
gtk_tree_model_filter_set_visible_func(filtered, gtk_tree_model_filter_set_visible_func(mainWindow->entryListFiltered,
(GtkTreeModelFilterVisibleFunc) mainWindow_list_entry_visible, (GtkTreeModelFilterVisibleFunc) mainWindow_list_entry_visible,
mainWindow, NULL); mainWindow, NULL);
model = GTK_TREE_MODEL(sorted); model = GTK_TREE_MODEL(sorted);
@ -165,9 +265,13 @@ static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
mainWindow_list_add_entry(mainWindow, "Title", dt, list, "Text text text"); mainWindow_list_add_entry(mainWindow, "Title", dt, list, "Text text text");
g_date_time_unref(dt); g_date_time_unref(dt);
dt = g_date_time_new_local(2017, 2, 3, 6, 7, 0); 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"); mainWindow_list_add_entry(mainWindow, "AAA 2", dt, list, "fdfd Text text text");
g_date_time_unref(dt); 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.");
g_date_time_unref(dt);
} }
static void mainWindow_connect_signals(struct mainWindow *mainWindow) static void mainWindow_connect_signals(struct mainWindow *mainWindow)
@ -189,6 +293,16 @@ static void mainWindow_connect_signals(struct mainWindow *mainWindow)
"changed", "changed",
(GCallback) mainWindow_entrySelected, (GCallback) mainWindow_entrySelected,
mainWindow); mainWindow);
g_signal_connect(mainWindow->dateStart,
"changed",
(GCallback) mainWindow_dateChanged,
mainWindow);
g_signal_connect(mainWindow->dateEnd,
"changed",
(GCallback) mainWindow_dateChanged,
mainWindow);
} }
struct mainWindow *mainWindow_new() struct mainWindow *mainWindow_new()
@ -210,6 +324,9 @@ struct mainWindow *mainWindow_new()
mainWindow->entryListView = GTK_TREE_VIEW(gtk_builder_get_object(builder, "entryListView")); mainWindow->entryListView = GTK_TREE_VIEW(gtk_builder_get_object(builder, "entryListView"));
mainWindow->entryListSelection = GTK_TREE_SELECTION(gtk_builder_get_object(builder, "entryListSelection")); mainWindow->entryListSelection = GTK_TREE_SELECTION(gtk_builder_get_object(builder, "entryListSelection"));
mainWindow->filterSettings.time_start = 0;
mainWindow->filterSettings.time_end = -1;
gtk_builder_connect_signals(builder, NULL); gtk_builder_connect_signals(builder, NULL);
g_object_unref(G_OBJECT(builder)); g_object_unref(G_OBJECT(builder));
@ -217,6 +334,9 @@ struct mainWindow *mainWindow_new()
mainWindow_configure_treeView(mainWindow); mainWindow_configure_treeView(mainWindow);
mainWindow_connect_signals(mainWindow); mainWindow_connect_signals(mainWindow);
mainWindow_get_date_from_string("pointless foo");
mainWindow_get_date_from_string("2017-02-11 12:44");
return mainWindow; return mainWindow;
} }
@ -267,3 +387,22 @@ void mainWindow_entrySelected(GtkWidget *widget, gpointer user_data)
g_free(temp); g_free(temp);
} }
} }
void mainWindow_dateChanged(GtkWidget *widget, gpointer user_data)
{
GDateTime *datetime;
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
datetime = mainWindow_get_date_from_string(gtk_entry_get_text(GTK_ENTRY(mainWindow->dateStart)));
if (datetime) {
mainWindow->filterSettings.time_start = g_date_time_to_unix(datetime);
g_date_time_unref(datetime);
}
datetime = mainWindow_get_date_from_string(gtk_entry_get_text(GTK_ENTRY(mainWindow->dateEnd)));
if (datetime) {
mainWindow->filterSettings.time_end = g_date_time_to_unix(datetime);
g_date_time_unref(datetime);
}
gtk_tree_model_filter_refilter(mainWindow->entryListFiltered);
}

View File

@ -18,6 +18,12 @@ struct mainWindow {
GtkListStore *entryListStore; GtkListStore *entryListStore;
GtkWidget *selectedDateEntry; GtkWidget *selectedDateEntry;
GtkTreeModelFilter *entryListFiltered;
struct filterSettings {
guint time_start;
guint time_end;
} filterSettings;
}; };
struct mainWindow *mainWindow_new(); struct mainWindow *mainWindow_new();
@ -27,5 +33,6 @@ void mainWindow_showPopover(GtkWidget *widget, GdkEvent *event, gpointer user_da
void mainWindow_chooseWorkspaceClicked(GtkWidget *widget, gint event, gpointer user_data); void mainWindow_chooseWorkspaceClicked(GtkWidget *widget, gint event, gpointer user_data);
void mainWindow_calendarSelected(GtkWidget *widget, gpointer user_data); void mainWindow_calendarSelected(GtkWidget *widget, gpointer user_data);
void mainWindow_entrySelected(GtkWidget *widget, gpointer user_data); void mainWindow_entrySelected(GtkWidget *widget, gpointer user_data);
void mainWindow_dateChanged(GtkWidget *widget, gpointer user_data);
#endif /* MAINWINDOW_H */ #endif /* MAINWINDOW_H */