Added date filtering
This commit is contained in:
parent
e7f3513cb9
commit
8ac7436182
139
src/mainwindow.c
139
src/mainwindow.c
@ -11,6 +11,99 @@ enum {
|
||||
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().
|
||||
*/
|
||||
@ -71,10 +164,8 @@ static gboolean mainWindow_list_entry_visible(GtkTreeModel *model, GtkTreeIter *
|
||||
gtk_tree_model_get(model, iter, COL_TIMESTAMP, &datetime, -1);
|
||||
if (datetime) {
|
||||
time = g_date_time_to_unix(datetime);
|
||||
g_print("TIME=%d\n", time);
|
||||
return time >= mainWindow->filterSettings.time_start && time <= mainWindow->filterSettings.time_end;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@ -108,7 +199,6 @@ static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
|
||||
GtkCellRenderer *renderer;
|
||||
GtkTreeSortable *sortable;
|
||||
GtkTreeViewColumn *col;
|
||||
GtkTreeModelFilter *filtered;
|
||||
GtkTreeModelSort *sorted;
|
||||
GtkTreeModel *model;
|
||||
|
||||
@ -119,12 +209,13 @@ static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
|
||||
G_TYPE_POINTER,
|
||||
G_TYPE_POINTER,
|
||||
G_TYPE_STRING);
|
||||
filtered = GTK_TREE_MODEL_FILTER(gtk_tree_model_filter_new(
|
||||
GTK_TREE_MODEL(mainWindow->entryListStore), NULL));
|
||||
mainWindow->entryListFiltered = GTK_TREE_MODEL_FILTER(gtk_tree_model_filter_new(
|
||||
GTK_TREE_MODEL(mainWindow->entryListStore),
|
||||
NULL));
|
||||
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,
|
||||
mainWindow, NULL);
|
||||
model = GTK_TREE_MODEL(sorted);
|
||||
@ -202,6 +293,16 @@ static void mainWindow_connect_signals(struct mainWindow *mainWindow)
|
||||
"changed",
|
||||
(GCallback) mainWindow_entrySelected,
|
||||
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()
|
||||
@ -233,6 +334,9 @@ struct mainWindow *mainWindow_new()
|
||||
mainWindow_configure_treeView(mainWindow);
|
||||
mainWindow_connect_signals(mainWindow);
|
||||
|
||||
mainWindow_get_date_from_string("pointless foo");
|
||||
mainWindow_get_date_from_string("2017-02-11 12:44");
|
||||
|
||||
return mainWindow;
|
||||
}
|
||||
|
||||
@ -283,3 +387,22 @@ void mainWindow_entrySelected(GtkWidget *widget, gpointer user_data)
|
||||
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);
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ struct mainWindow {
|
||||
|
||||
GtkListStore *entryListStore;
|
||||
GtkWidget *selectedDateEntry;
|
||||
GtkTreeModelFilter *entryListFiltered;
|
||||
|
||||
struct filterSettings {
|
||||
guint time_start;
|
||||
@ -32,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_calendarSelected(GtkWidget *widget, gpointer user_data);
|
||||
void mainWindow_entrySelected(GtkWidget *widget, gpointer user_data);
|
||||
void mainWindow_dateChanged(GtkWidget *widget, gpointer user_data);
|
||||
|
||||
#endif /* MAINWINDOW_H */
|
||||
|
Loading…
Reference in New Issue
Block a user