A viewer for a file based diary with entries written in markdown.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

465 lines
14 KiB

#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<gchar *> of the tags */
COL_TEXT, /* Raw text from the file */
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})\\s*(\\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,2})\\.(\\d{1,2})\\.(\\d{1,4})\\s*(\\d{1,2}):(\\d{1,2}).*",
.index_count = 5,
.index_year = 3,
.index_month = 2,
.index_day = 1,
.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, hour, minute, 0);
g_regex_unref(regex);
break;
}
}
g_regex_unref(regex);
} while ((++dateFormats)->regex);
if (!datetime)
g_print("Warning! Could not match date in \"%s\"!\n", string);
return datetime;
}
/**
* Returns: The string, 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;
}
/**
* Note: This function creates a copyof each argument.
*/
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);
}
static gboolean mainWindow_list_entry_visible(GtkTreeModel *model, GtkTreeIter *iter, struct mainWindow *mainWindow)
{
GDateTime *datetime;
guint time;
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,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer userdata)
{
GDateTime *datetime1;
GDateTime *datetime2;
guint time1;
guint time2;
gtk_tree_model_get(model, a, COL_TIMESTAMP, &datetime1, -1);
gtk_tree_model_get(model, b, COL_TIMESTAMP, &datetime2, -1);
time1 = g_date_time_to_unix(datetime1);
time2 = g_date_time_to_unix(datetime2);
if (time1 == time2)
return 0;
else if (time1 > time2)
return 1;
else
return -1;
}
static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
{
GtkCellRenderer *renderer;
GtkTreeSortable *sortable;
GtkTreeViewColumn *col;
GtkTreeModelSort *sorted;
GtkTreeModel *model;
mainWindow->entryListStore = gtk_list_store_new(COL_COUNT,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_POINTER,
G_TYPE_POINTER,
G_TYPE_STRING);
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(mainWindow->entryListFiltered)));
gtk_tree_model_filter_set_visible_func(mainWindow->entryListFiltered,
(GtkTreeModelFilterVisibleFunc) mainWindow_list_entry_visible,
mainWindow, NULL);
model = GTK_TREE_MODEL(sorted);
gtk_tree_view_set_model(mainWindow->entryListView, model);
/* TODO: Unref mainWindow->entryListStore and filtered, sorted and model? */
sortable = GTK_TREE_SORTABLE(sorted);
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(mainWindow->entryListView, -1, "Title", renderer,
"text", COL_TITLE, NULL);
col = gtk_tree_view_get_column(mainWindow->entryListView, COL_TITLE);
gtk_tree_view_column_set_resizable(col, 1);
gtk_tree_view_column_set_sort_column_id(col, COL_TITLE);
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(mainWindow->entryListView, -1, "Date", renderer,
"text", COL_DATE_TEXT, NULL);
col = gtk_tree_view_get_column(mainWindow->entryListView, COL_DATE_TEXT);
gtk_tree_view_column_set_resizable(col, 1);
gtk_tree_sortable_set_sort_func(sortable, COL_TIMESTAMP, mainWindow_sort_date_compare_func,
GINT_TO_POINTER(COL_TIMESTAMP), NULL);
gtk_tree_view_column_set_sort_column_id(col, COL_DATE_TEXT);
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(mainWindow->entryListView, -1, "Tags", renderer,
"text", COL_TAGS_TEXT, NULL);
col = gtk_tree_view_get_column(mainWindow->entryListView, COL_TAGS_TEXT);
gtk_tree_view_column_set_resizable(col, 1);
gtk_tree_view_column_set_sort_column_id(col, COL_TAGS_TEXT);
gtk_tree_sortable_set_sort_column_id(sortable, COL_DATE_TEXT, GTK_SORT_DESCENDING);
/*
* Test entries for debugging only
*/
GDateTime *dt;
GList *list = NULL;
list = g_list_append(list, "Tag0");
list = g_list_append(list, "Tag1");
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");
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");
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)
{
g_signal_connect(mainWindow->dateStart,
"focus-in-event",
(GCallback) mainWindow_showPopover,
mainWindow);
g_signal_connect(mainWindow->dateEnd,
"focus-in-event",
(GCallback) mainWindow_showPopover,
mainWindow);
g_signal_connect(mainWindow->calendarRange,
"day-selected",
(GCallback) mainWindow_calendarSelected,
mainWindow);
g_signal_connect_swapped(mainWindow->calendarRange,
"day-selected-double-click",
(GCallback) gtk_popover_popdown,
mainWindow->popoverDate);
g_signal_connect(mainWindow->entryListSelection,
"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);
g_signal_connect(mainWindow->dateStart,
"focus-out-event",
(GCallback) mainWindow_checkDate,
mainWindow);
g_signal_connect(mainWindow->dateEnd,
"focus-out-event",
(GCallback) mainWindow_checkDate,
mainWindow);
}
struct mainWindow *mainWindow_new()
{
GtkBuilder *builder;
struct mainWindow *mainWindow;
builder = gtk_builder_new_from_file("glade/mainWindow.glade");
mainWindow = malloc(sizeof(struct mainWindow));
mainWindow->mainWindow = GTK_WIDGET(gtk_builder_get_object(builder, "mainWindow"));
mainWindow->buttonChooseWorkspace = GTK_WIDGET(gtk_builder_get_object(builder, "buttonChooseWorkspace"));
mainWindow->searchEntry = GTK_WIDGET(gtk_builder_get_object(builder, "searchEntry"));
mainWindow->dateStart = GTK_WIDGET(gtk_builder_get_object(builder, "dateStart"));
mainWindow->dateEnd = GTK_WIDGET(gtk_builder_get_object(builder, "dateEnd"));
mainWindow->entryText = GTK_WIDGET(gtk_builder_get_object(builder, "entryText"));
mainWindow->popoverDate = GTK_WIDGET(gtk_builder_get_object(builder, "popoverDate"));
mainWindow->calendarRange = GTK_WIDGET(gtk_builder_get_object(builder, "calendarRange"));
mainWindow->entryListView = GTK_TREE_VIEW(gtk_builder_get_object(builder, "entryListView"));
mainWindow->entryListSelection = GTK_TREE_SELECTION(gtk_builder_get_object(builder, "entryListSelection"));
mainWindow->textBuffer = GTK_TEXT_BUFFER(gtk_builder_get_object(builder, "entryTextBuffer"));
mainWindow->filterSettings.time_start = 0;
mainWindow->filterSettings.time_end = -1;
gtk_builder_connect_signals(builder, NULL);
g_object_unref(G_OBJECT(builder));
mainWindow_configure_treeView(mainWindow);
mainWindow_connect_signals(mainWindow);
/**
* 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");
return mainWindow;
}
void mainWindow_chooseWorkspaceClicked(GtkWidget *widget, gint event, gpointer user_data)
{
printf("Choose workspace.\n");
fflush(stdout);
}
void mainWindow_showPopover(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
GDateTime *datetime;
mainWindow->selectedDateEntry = NULL;
datetime = g_date_time_new_from_unix_local((widget == mainWindow->dateStart
? mainWindow->filterSettings.time_start
: mainWindow->filterSettings.time_end));
if (datetime) {
gtk_calendar_select_month(GTK_CALENDAR(mainWindow->calendarRange),
g_date_time_get_month(datetime) - 1,
g_date_time_get_year(datetime));
gtk_calendar_select_day(GTK_CALENDAR(mainWindow->calendarRange),
g_date_time_get_day_of_month(datetime));
g_date_time_unref(datetime);
}
/* gtk_editable_select_region(GTK_EDITABLE(widget), 0, 255); */
mainWindow->selectedDateEntry = widget;
if (!gtk_widget_get_visible(mainWindow->popoverDate))
gtk_popover_popup(GTK_POPOVER(mainWindow->popoverDate));
gtk_popover_set_relative_to(GTK_POPOVER(mainWindow->popoverDate), widget);
}
void mainWindow_calendarSelected(GtkWidget *widget, gpointer user_data)
{
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
guint year, month, day;
gchar *text;
if (mainWindow->selectedDateEntry) {
gtk_calendar_get_date(GTK_CALENDAR(mainWindow->calendarRange), &year, &month, &day);
text = g_strdup_printf("%d-%02d-%02d", year, month + 1, day);
gtk_entry_set_text(GTK_ENTRY(mainWindow->selectedDateEntry), text);
g_free(text);
}
}
void mainWindow_entrySelected(GtkWidget *widget, gpointer user_data)
{
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
GtkTreeSelection *selection;
GtkTreeModel *model;
GtkTreeIter iter;
gchar *temp;
selection = gtk_tree_view_get_selection(mainWindow->entryListView);
if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
gtk_tree_model_get(model, &iter, COL_TEXT, &temp, -1);
gtk_text_buffer_set_text(mainWindow->textBuffer, temp, g_utf8_strlen(temp, -1));
fflush(stdout);
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((gchar *)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((gchar *)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);
}
void mainWindow_checkDate(GtkWidget *widget, gint event, gpointer user_data)
{
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
GDateTime *datetime;
datetime = mainWindow_get_date_from_string((char *)gtk_entry_get_text(GTK_ENTRY(widget)));
if (!datetime) {
datetime = g_date_time_new_from_unix_local(widget == mainWindow->dateStart
? mainWindow->filterSettings.time_start
: mainWindow->filterSettings.time_end);
gtk_entry_set_text(GTK_ENTRY(widget),
g_date_time_format(datetime, "%Y-%m-%d"));
}
g_date_time_unref(datetime);
}