Compare commits
8 Commits
9bac55c4ce
...
a3c4b3164d
Author | SHA1 | Date | |
---|---|---|---|
a3c4b3164d | |||
8de6eda287 | |||
ee16f15a98 | |||
e39c0ed7da | |||
909a84c16a | |||
ac0d8c843d | |||
d3b0dce208 | |||
6a47f95675 |
@ -5,6 +5,8 @@
|
|||||||
<object class="GtkTextBuffer" id="entryTextBuffer"/>
|
<object class="GtkTextBuffer" id="entryTextBuffer"/>
|
||||||
<object class="GtkEntryCompletion" id="searchEntryCompletion"/>
|
<object class="GtkEntryCompletion" id="searchEntryCompletion"/>
|
||||||
<object class="GtkApplicationWindow" id="mainWindow">
|
<object class="GtkApplicationWindow" id="mainWindow">
|
||||||
|
<property name="width_request">720</property>
|
||||||
|
<property name="height_request">405</property>
|
||||||
<property name="can_focus">False</property>
|
<property name="can_focus">False</property>
|
||||||
<signal name="destroy" handler="gtk_main_quit" swapped="no"/>
|
<signal name="destroy" handler="gtk_main_quit" swapped="no"/>
|
||||||
<child>
|
<child>
|
||||||
@ -206,14 +208,23 @@
|
|||||||
</packing>
|
</packing>
|
||||||
</child>
|
</child>
|
||||||
<child>
|
<child>
|
||||||
<object class="GtkTextView" id="entryText">
|
<object class="GtkScrolledWindow">
|
||||||
<property name="width_request">640</property>
|
|
||||||
<property name="height_request">320</property>
|
|
||||||
<property name="visible">True</property>
|
<property name="visible">True</property>
|
||||||
<property name="can_focus">True</property>
|
<property name="can_focus">True</property>
|
||||||
<property name="editable">False</property>
|
<property name="shadow_type">in</property>
|
||||||
<property name="wrap_mode">word</property>
|
<child>
|
||||||
<property name="buffer">entryTextBuffer</property>
|
<object class="GtkTextView" id="entryText">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">True</property>
|
||||||
|
<property name="editable">False</property>
|
||||||
|
<property name="wrap_mode">word-char</property>
|
||||||
|
<property name="left_margin">4</property>
|
||||||
|
<property name="right_margin">4</property>
|
||||||
|
<property name="top_margin">4</property>
|
||||||
|
<property name="bottom_margin">4</property>
|
||||||
|
<property name="buffer">entryTextBuffer</property>
|
||||||
|
</object>
|
||||||
|
</child>
|
||||||
</object>
|
</object>
|
||||||
<packing>
|
<packing>
|
||||||
<property name="resize">True</property>
|
<property name="resize">True</property>
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include <gtk/gtk.h>
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
|
#include "mdiary.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@ -12,6 +13,8 @@ int main(int argc, char *argv[])
|
|||||||
mainWindow = mainWindow_new();
|
mainWindow = mainWindow_new();
|
||||||
gtk_widget_show(mainWindow->mainWindow);
|
gtk_widget_show(mainWindow->mainWindow);
|
||||||
|
|
||||||
|
mdiary_scan_to_store(argv[1], mainWindow->entryListStore);
|
||||||
|
|
||||||
gtk_main();
|
gtk_main();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
215
src/mainwindow.c
215
src/mainwindow.c
@ -1,178 +1,6 @@
|
|||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
#include "mdiary.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
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief The dateFormats struct contains regex/date-info pairs to parse different date formats.
|
|
||||||
*/
|
|
||||||
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 mainWindow_get_date_from_string 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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.
|
* @brief mainWindow_list_entry_visible checks whether the entry should be displayed.
|
||||||
* @param model N/A
|
* @param model N/A
|
||||||
@ -283,6 +111,7 @@ static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
|
|||||||
G_TYPE_STRING,
|
G_TYPE_STRING,
|
||||||
G_TYPE_POINTER,
|
G_TYPE_POINTER,
|
||||||
G_TYPE_POINTER,
|
G_TYPE_POINTER,
|
||||||
|
G_TYPE_STRING,
|
||||||
G_TYPE_STRING);
|
G_TYPE_STRING);
|
||||||
mainWindow->entryListFiltered = 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),
|
GTK_TREE_MODEL(mainWindow->entryListStore),
|
||||||
@ -312,8 +141,8 @@ static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
|
|||||||
"text", COL_DATE_TEXT, NULL);
|
"text", COL_DATE_TEXT, NULL);
|
||||||
col = gtk_tree_view_get_column(mainWindow->entryListView, COL_DATE_TEXT);
|
col = gtk_tree_view_get_column(mainWindow->entryListView, COL_DATE_TEXT);
|
||||||
gtk_tree_view_column_set_resizable(col, 1);
|
gtk_tree_view_column_set_resizable(col, 1);
|
||||||
gtk_tree_sortable_set_sort_func(sortable, COL_TIMESTAMP, mainWindow_sort_date_compare_func,
|
gtk_tree_sortable_set_sort_func(sortable, COL_DATE_TEXT, mainWindow_sort_date_compare_func,
|
||||||
GINT_TO_POINTER(COL_TIMESTAMP), NULL);
|
GINT_TO_POINTER(COL_DATE_TEXT), NULL);
|
||||||
gtk_tree_view_column_set_sort_column_id(col, COL_DATE_TEXT);
|
gtk_tree_view_column_set_sort_column_id(col, COL_DATE_TEXT);
|
||||||
|
|
||||||
renderer = gtk_cell_renderer_text_new();
|
renderer = gtk_cell_renderer_text_new();
|
||||||
@ -324,28 +153,6 @@ static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
|
|||||||
gtk_tree_view_column_set_sort_column_id(col, COL_TAGS_TEXT);
|
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);
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -450,9 +257,9 @@ struct mainWindow *mainWindow_new()
|
|||||||
mainWindow_configure_treeView(mainWindow);
|
mainWindow_configure_treeView(mainWindow);
|
||||||
mainWindow_connect_signals(mainWindow);
|
mainWindow_connect_signals(mainWindow);
|
||||||
|
|
||||||
gtk_toggle_button_set_active(mainWindow->checkSearchTitle, 1);
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mainWindow->checkSearchTitle), 1);
|
||||||
gtk_toggle_button_set_active(mainWindow->checkSearchTags, 1);
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mainWindow->checkSearchTags), 1);
|
||||||
gtk_toggle_button_set_active(mainWindow->checkSearchText, 0);
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mainWindow->checkSearchText), 0);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* These need to be set to oldest and newest after indexing date
|
* These need to be set to oldest and newest after indexing date
|
||||||
@ -560,16 +367,16 @@ void mainWindow_filterChanged(GtkWidget *widget, gpointer user_data)
|
|||||||
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
|
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
|
||||||
|
|
||||||
if (!widget || widget == mainWindow->dateStart) {
|
if (!widget || widget == mainWindow->dateStart) {
|
||||||
datetime = mainWindow_get_date_from_string((gchar *)gtk_entry_get_text(
|
datetime = mdiary_get_date_from_string((gchar *)gtk_entry_get_text(
|
||||||
GTK_ENTRY(mainWindow->dateStart)));
|
GTK_ENTRY(mainWindow->dateStart)));
|
||||||
if (datetime) {
|
if (datetime) {
|
||||||
mainWindow->filterSettings.time_start = g_date_time_to_unix(datetime);
|
mainWindow->filterSettings.time_start = g_date_time_to_unix(datetime);
|
||||||
g_date_time_unref(datetime);
|
g_date_time_unref(datetime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!widget || widget == mainWindow->dateEnd) {
|
if (!widget || widget == mainWindow->dateEnd) {
|
||||||
datetime = mainWindow_get_date_from_string((gchar *)gtk_entry_get_text(
|
datetime = mdiary_get_date_from_string((gchar *)gtk_entry_get_text(
|
||||||
GTK_ENTRY(mainWindow->dateEnd)));
|
GTK_ENTRY(mainWindow->dateEnd)));
|
||||||
if (datetime) {
|
if (datetime) {
|
||||||
mainWindow->filterSettings.time_end = g_date_time_to_unix(datetime);
|
mainWindow->filterSettings.time_end = g_date_time_to_unix(datetime);
|
||||||
g_date_time_unref(datetime);
|
g_date_time_unref(datetime);
|
||||||
@ -597,7 +404,7 @@ void mainWindow_checkDate(GtkWidget *widget, gint event, gpointer user_data)
|
|||||||
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
|
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
|
||||||
GDateTime *datetime;
|
GDateTime *datetime;
|
||||||
|
|
||||||
datetime = mainWindow_get_date_from_string((char *)gtk_entry_get_text(GTK_ENTRY(widget)));
|
datetime = mdiary_get_date_from_string((char *)gtk_entry_get_text(GTK_ENTRY(widget)));
|
||||||
if (!datetime) {
|
if (!datetime) {
|
||||||
datetime = g_date_time_new_from_unix_local(widget == mainWindow->dateStart
|
datetime = g_date_time_new_from_unix_local(widget == mainWindow->dateStart
|
||||||
? mainWindow->filterSettings.time_start
|
? mainWindow->filterSettings.time_start
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <gtk/gtk.h>
|
#include <gtk/gtk.h>
|
||||||
|
#include "mdiary.h"
|
||||||
|
|
||||||
struct mainWindow {
|
struct mainWindow {
|
||||||
GtkWidget *mainWindow;
|
GtkWidget *mainWindow;
|
||||||
|
415
src/mdiary.c
415
src/mdiary.c
@ -1 +1,416 @@
|
|||||||
#include "mdiary.h"
|
#include "mdiary.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The dateFormats struct contains regex/date-info pairs to parse different date formats.
|
||||||
|
*/
|
||||||
|
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{2,2})\\.(\\d{1,2})\\.(\\d{1,4})\\s*(\\d{1,2}):(\\d{1,2}).*",
|
||||||
|
/* The regex above should begin with (\d{1,2}). But that seems to break the regex... */
|
||||||
|
.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 mdiary_get_date_from_string 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.
|
||||||
|
*/
|
||||||
|
GDateTime *mdiary_get_date_from_string(gchar *string)
|
||||||
|
{
|
||||||
|
return mdiary_get_date_from_string_ext(string, "", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief mdiary_get_date_from_string_ext 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.
|
||||||
|
*/
|
||||||
|
GDateTime *mdiary_get_date_from_string_ext(gchar *string, gchar *prefix, gchar *suffix)
|
||||||
|
{
|
||||||
|
GRegex *regex;
|
||||||
|
GMatchInfo *match_info;
|
||||||
|
struct dateFormats *dateFormats;
|
||||||
|
GDateTime *datetime = NULL;
|
||||||
|
guint year = 0;
|
||||||
|
guint month = 0;
|
||||||
|
guint day = 0;
|
||||||
|
guint hour = 0;
|
||||||
|
guint minute = 0;
|
||||||
|
gchar *regex_string;
|
||||||
|
|
||||||
|
dateFormats = dateFormats_list;
|
||||||
|
do {
|
||||||
|
regex_string = g_strdup_printf("%s%s%s", prefix, dateFormats->regex, suffix);
|
||||||
|
regex = g_regex_new(regex_string, 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);
|
||||||
|
g_free(regex_string);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g_free(regex_string);
|
||||||
|
g_regex_unref(regex);
|
||||||
|
} while ((++dateFormats)->regex);
|
||||||
|
|
||||||
|
return datetime;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gchar *mdiary_get_title_from_string(gchar *string)
|
||||||
|
{
|
||||||
|
GRegex *regex;
|
||||||
|
GMatchInfo *match_info;
|
||||||
|
gchar *ret = NULL;
|
||||||
|
|
||||||
|
regex = g_regex_new("\\# (.*)", G_REGEX_RAW, 0, NULL);
|
||||||
|
if (g_regex_match(regex, string, 0, &match_info) &&
|
||||||
|
g_regex_get_capture_count(regex) > 0 &&
|
||||||
|
g_match_info_matches(match_info)) {
|
||||||
|
ret = g_strdup(g_match_info_fetch(match_info, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
g_regex_unref(regex);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gchar *mdiary_get_summary_from_string(gchar *string)
|
||||||
|
{
|
||||||
|
GRegex *regex;
|
||||||
|
GMatchInfo *match_info;
|
||||||
|
gchar *ret = NULL;
|
||||||
|
|
||||||
|
regex = g_regex_new("^\\s*Summary: (.*)", G_REGEX_RAW, 0, NULL);
|
||||||
|
if (g_regex_match(regex, string, 0, &match_info) &&
|
||||||
|
g_regex_get_capture_count(regex) > 0 &&
|
||||||
|
g_match_info_matches(match_info)) {
|
||||||
|
ret = g_strdup(g_match_info_fetch(match_info, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
g_regex_unref(regex);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean mdiary_is_empty_line(gchar *string)
|
||||||
|
{
|
||||||
|
GRegex *regex;
|
||||||
|
GMatchInfo *match_info;
|
||||||
|
gchar *ret = 0;
|
||||||
|
|
||||||
|
regex = g_regex_new("^\\s*$", G_REGEX_RAW, 0, NULL);
|
||||||
|
if (g_regex_match(regex, string, 0, &match_info) &&
|
||||||
|
g_match_info_matches(match_info)) {
|
||||||
|
ret = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_regex_unref(regex);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static GList *mdiary_add_tags_from_string(gchar *string)
|
||||||
|
{
|
||||||
|
GRegex *regex;
|
||||||
|
GMatchInfo *match_info;
|
||||||
|
GList *ret = NULL;
|
||||||
|
gchar *ptr;
|
||||||
|
gchar *beg_ptr;
|
||||||
|
gchar *orig_ptr;
|
||||||
|
gboolean collected = 0;
|
||||||
|
gchar bak;
|
||||||
|
|
||||||
|
regex = g_regex_new("^\\s*Tags: (.*)", G_REGEX_RAW, 0, NULL);
|
||||||
|
if (g_regex_match(regex, string, 0, &match_info) &&
|
||||||
|
g_regex_get_capture_count(regex) > 0 &&
|
||||||
|
g_match_info_matches(match_info)) {
|
||||||
|
/**
|
||||||
|
* TODO: This function should be rewritten to fully use GRegex...
|
||||||
|
*/
|
||||||
|
ptr = beg_ptr = orig_ptr = g_strdup(g_match_info_fetch(match_info, 1));
|
||||||
|
do {
|
||||||
|
if (*ptr == ',' || *ptr == ' ' || *ptr == '\0') {
|
||||||
|
bak = *ptr;
|
||||||
|
if (collected) {
|
||||||
|
collected = 0;
|
||||||
|
*ptr = '\0';
|
||||||
|
ret = g_list_append(ret, g_strdup(beg_ptr));
|
||||||
|
beg_ptr = ptr + 1;
|
||||||
|
*ptr = bak;
|
||||||
|
} else {
|
||||||
|
beg_ptr = ptr + 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
collected = 1;
|
||||||
|
}
|
||||||
|
} while (*(ptr++));
|
||||||
|
g_free(orig_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
g_regex_unref(regex);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListStore)
|
||||||
|
{
|
||||||
|
GFile *file;
|
||||||
|
GError *err = NULL;
|
||||||
|
GFileInputStream *stream;
|
||||||
|
GDataInputStream *dstream;
|
||||||
|
gchar *line;
|
||||||
|
|
||||||
|
GDateTime *datetime = NULL;
|
||||||
|
gchar *title = NULL;
|
||||||
|
GList *tagList = NULL;
|
||||||
|
gchar *summary = NULL;
|
||||||
|
GString *text = NULL;
|
||||||
|
guint header_state = 0;
|
||||||
|
|
||||||
|
g_print("Add file: %s\n", filename);
|
||||||
|
|
||||||
|
file = g_file_new_for_path(filename);
|
||||||
|
stream = g_file_read(file, NULL, &err);
|
||||||
|
if (err != NULL) {
|
||||||
|
g_error("PARSER: Could not open %s for reading: %s\n", filename, err->message);
|
||||||
|
g_error_free(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
dstream = g_data_input_stream_new(G_INPUT_STREAM(stream));
|
||||||
|
|
||||||
|
while (line = g_data_input_stream_read_line(G_DATA_INPUT_STREAM(dstream), NULL, NULL, NULL)) {
|
||||||
|
if (header_state == 0) {
|
||||||
|
if (!datetime) {
|
||||||
|
datetime = mdiary_get_date_from_string_ext(line, "Date: .*", "");
|
||||||
|
if (datetime)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!summary) {
|
||||||
|
summary = mdiary_get_summary_from_string(line);
|
||||||
|
if (summary)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!title) {
|
||||||
|
title = mdiary_get_title_from_string(line);
|
||||||
|
if (title)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!tagList) {
|
||||||
|
tagList = mdiary_add_tags_from_string(line);
|
||||||
|
if (tagList)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
header_state = mdiary_is_empty_line(line);
|
||||||
|
} else if (header_state == 1) {
|
||||||
|
header_state += !mdiary_is_empty_line(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (header_state == 2) {
|
||||||
|
if (text) {
|
||||||
|
text = g_string_append(text, "\n");
|
||||||
|
text = g_string_append(text, line);
|
||||||
|
} else {
|
||||||
|
text = g_string_new(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!datetime) {
|
||||||
|
g_warning("PARSER: Could not detect date in file!\n");
|
||||||
|
datetime = g_date_time_new_from_unix_local(0);
|
||||||
|
}
|
||||||
|
if (!tagList) {
|
||||||
|
g_warning("PARSER: Could not detect tags in file!\n");
|
||||||
|
tagList = g_list_append(tagList, "untagged");
|
||||||
|
}
|
||||||
|
if (!text) {
|
||||||
|
g_warning("PARSER: Could not find any text in file!\n");
|
||||||
|
text = g_string_new("No content found.");
|
||||||
|
}
|
||||||
|
if (!summary) {
|
||||||
|
g_warning("PARSER: Could not detect summary in file!\n");
|
||||||
|
summary = g_strdup("No summary found.");
|
||||||
|
}
|
||||||
|
if (!title) {
|
||||||
|
g_warning("PARSER: Could not detect title in file!\n");
|
||||||
|
title = g_strdup("Untitled");
|
||||||
|
}
|
||||||
|
|
||||||
|
mdiary_add_entry_to_store(entryListStore,
|
||||||
|
title,
|
||||||
|
datetime,
|
||||||
|
tagList,
|
||||||
|
summary,
|
||||||
|
text->str);
|
||||||
|
|
||||||
|
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);
|
||||||
|
g_free(summary);
|
||||||
|
g_free(title);
|
||||||
|
g_date_time_unref(datetime);
|
||||||
|
g_object_unref(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mdiary_recurse_and_collect(gchar *base_dir, GtkListStore *entryListStore, guint max_level)
|
||||||
|
{
|
||||||
|
GError *error;
|
||||||
|
GDir *dir = g_dir_open(base_dir, 0, &error);
|
||||||
|
gchar *dirname;
|
||||||
|
gchar *fullPath;
|
||||||
|
GRegex *regex;
|
||||||
|
GMatchInfo *match_info;
|
||||||
|
|
||||||
|
if (!dir) {
|
||||||
|
g_print("Error, could not open base directory.\n");
|
||||||
|
} else {
|
||||||
|
while (dirname = (gchar *)g_dir_read_name(dir)) {
|
||||||
|
fullPath = g_strdup_printf("%s/%s", base_dir, dirname);
|
||||||
|
if (g_file_test(fullPath, G_FILE_TEST_IS_REGULAR)) {
|
||||||
|
regex = g_regex_new("\\.md$", G_REGEX_CASELESS, 0, NULL);
|
||||||
|
if (g_regex_match(regex, fullPath, 0, &match_info) &&
|
||||||
|
g_match_info_matches(match_info))
|
||||||
|
mdiary_add_file_to_store(fullPath, entryListStore);
|
||||||
|
g_regex_unref(regex);
|
||||||
|
} else if (g_file_test(fullPath, G_FILE_TEST_IS_DIR) && max_level) {
|
||||||
|
mdiary_recurse_and_collect(fullPath, entryListStore, max_level--);
|
||||||
|
}
|
||||||
|
g_free(fullPath);
|
||||||
|
}
|
||||||
|
g_dir_close(dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief mdiary_scan_to_store recursively (max. 5 levels) scans the base_dir into the entryListStore.
|
||||||
|
* @param base_dir The base directory to start scanning in
|
||||||
|
* @param entryListStore Target GtkListStore
|
||||||
|
*/
|
||||||
|
void mdiary_scan_to_store(gchar *base_dir, GtkListStore *entryListStore)
|
||||||
|
{
|
||||||
|
mdiary_recurse_and_collect(base_dir, entryListStore, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 *summary,
|
||||||
|
gchar *text)
|
||||||
|
{
|
||||||
|
GtkTreeIter iter;
|
||||||
|
GDateTime *datetime_copy;
|
||||||
|
GList *taglist_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");
|
||||||
|
taglist_copy = g_list_copy_deep(tags, (GCopyFunc) g_strdup, NULL);
|
||||||
|
taglistString = mdiary_taglist_to_string(taglist_copy);
|
||||||
|
|
||||||
|
|
||||||
|
gtk_list_store_append(entryListStore, &iter);
|
||||||
|
gtk_list_store_set(entryListStore, &iter,
|
||||||
|
COL_TITLE, g_strdup(title),
|
||||||
|
COL_DATE_TEXT, date_text,
|
||||||
|
COL_TAGS_TEXT, taglistString,
|
||||||
|
COL_TIMESTAMP, datetime_copy,
|
||||||
|
COL_TAGLIST, taglist_copy, /* TODO: Verify that the duplication worked! */
|
||||||
|
COL_SUMMARY, summary, /* Automatically strdupd */
|
||||||
|
COL_TEXT, text, /* Automatically strdupd */
|
||||||
|
-1);
|
||||||
|
g_free(date_text);
|
||||||
|
}
|
||||||
|
26
src/mdiary.h
26
src/mdiary.h
@ -1,4 +1,30 @@
|
|||||||
#ifndef MDIARY_H
|
#ifndef MDIARY_H
|
||||||
#define MDIARY_H
|
#define MDIARY_H
|
||||||
|
|
||||||
|
#include <glib.h>
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
#include <stdlib.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_SUMMARY, /* Summary of the entry */
|
||||||
|
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 *summary,
|
||||||
|
gchar *text);
|
||||||
|
GDateTime *mdiary_get_date_from_string_ext(gchar *string, gchar *prefix, gchar *suffix);
|
||||||
|
GDateTime *mdiary_get_date_from_string(gchar *string);
|
||||||
|
|
||||||
#endif /* MDIARY_H */
|
#endif /* MDIARY_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user