2017-02-01 18:20:48 +01:00
|
|
|
#include "mdiary.h"
|
2017-02-04 13:37:56 +01:00
|
|
|
|
2017-02-04 19:55:08 +01:00
|
|
|
static gint64 time_earliest;
|
|
|
|
static gint64 time_latest;
|
|
|
|
|
2017-02-04 16:12:46 +01:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
},
|
|
|
|
{
|
2017-02-04 17:47:37 +01:00
|
|
|
.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... */
|
2017-02-04 16:12:46 +01:00
|
|
|
.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;
|
|
|
|
}
|
|
|
|
|
2017-02-04 17:01:09 +01:00
|
|
|
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;
|
2017-02-04 19:55:08 +01:00
|
|
|
gboolean ret = 0;
|
2017-02-04 17:01:09 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-04 16:12:46 +01:00
|
|
|
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 {
|
2017-02-04 19:57:02 +01:00
|
|
|
if (*ptr == ',' || *ptr == '\0') {
|
2017-02-04 16:12:46 +01:00
|
|
|
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;
|
|
|
|
}
|
2017-02-04 19:57:02 +01:00
|
|
|
} else if (*ptr == ' ') {
|
|
|
|
if (!collected)
|
|
|
|
beg_ptr = ptr + 1;
|
2017-02-04 16:12:46 +01:00
|
|
|
} else {
|
|
|
|
collected = 1;
|
|
|
|
}
|
|
|
|
} while (*(ptr++));
|
|
|
|
g_free(orig_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_regex_unref(regex);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-04 14:22:01 +01:00
|
|
|
static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListStore)
|
|
|
|
{
|
2017-02-04 16:12:46 +01:00
|
|
|
GFile *file;
|
|
|
|
GError *err = NULL;
|
|
|
|
GFileInputStream *stream;
|
|
|
|
GDataInputStream *dstream;
|
|
|
|
gchar *line;
|
|
|
|
|
|
|
|
GDateTime *datetime = NULL;
|
|
|
|
gchar *title = NULL;
|
|
|
|
GList *tagList = NULL;
|
2017-02-04 17:01:09 +01:00
|
|
|
gchar *summary = NULL;
|
|
|
|
GString *text = NULL;
|
|
|
|
guint header_state = 0;
|
2017-02-04 16:12:46 +01:00
|
|
|
|
2017-02-04 14:22:01 +01:00
|
|
|
g_print("Add file: %s\n", filename);
|
2017-02-04 16:12:46 +01:00
|
|
|
|
|
|
|
file = g_file_new_for_path(filename);
|
|
|
|
stream = g_file_read(file, NULL, &err);
|
|
|
|
if (err != NULL) {
|
2017-02-04 17:01:09 +01:00
|
|
|
g_error("PARSER: Could not open %s for reading: %s\n", filename, err->message);
|
2017-02-04 16:12:46 +01:00
|
|
|
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)) {
|
2017-02-04 17:01:09 +01:00
|
|
|
if (header_state == 0) {
|
2017-02-04 16:12:46 +01:00
|
|
|
if (!datetime) {
|
|
|
|
datetime = mdiary_get_date_from_string_ext(line, "Date: .*", "");
|
|
|
|
if (datetime)
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-04 17:01:09 +01:00
|
|
|
if (!summary) {
|
|
|
|
summary = mdiary_get_summary_from_string(line);
|
|
|
|
if (summary)
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-04 16:12:46 +01:00
|
|
|
if (!title) {
|
|
|
|
title = mdiary_get_title_from_string(line);
|
|
|
|
if (title)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!tagList) {
|
|
|
|
tagList = mdiary_add_tags_from_string(line);
|
|
|
|
if (tagList)
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-04 17:01:09 +01:00
|
|
|
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);
|
|
|
|
}
|
2017-02-04 16:12:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-04 17:01:09 +01:00
|
|
|
if (!datetime) {
|
|
|
|
g_warning("PARSER: Could not detect date in file!\n");
|
|
|
|
datetime = g_date_time_new_from_unix_local(0);
|
|
|
|
}
|
2017-02-04 16:12:46 +01:00
|
|
|
if (!tagList) {
|
2017-02-04 17:01:09 +01:00
|
|
|
g_warning("PARSER: Could not detect tags in file!\n");
|
2017-02-04 16:12:46 +01:00
|
|
|
tagList = g_list_append(tagList, "untagged");
|
|
|
|
}
|
2017-02-04 17:01:09 +01:00
|
|
|
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.");
|
2017-02-04 16:12:46 +01:00
|
|
|
}
|
2017-02-04 17:01:09 +01:00
|
|
|
if (!title) {
|
|
|
|
g_warning("PARSER: Could not detect title in file!\n");
|
2017-02-04 16:12:46 +01:00
|
|
|
title = g_strdup("Untitled");
|
2017-02-04 17:01:09 +01:00
|
|
|
}
|
2017-02-04 16:12:46 +01:00
|
|
|
|
|
|
|
mdiary_add_entry_to_store(entryListStore,
|
|
|
|
title,
|
|
|
|
datetime,
|
|
|
|
tagList,
|
2017-02-04 17:01:09 +01:00
|
|
|
summary,
|
|
|
|
text->str);
|
2017-02-04 16:12:46 +01:00
|
|
|
|
2017-02-04 19:55:08 +01:00
|
|
|
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);
|
|
|
|
|
2017-02-04 16:12:46 +01:00
|
|
|
g_input_stream_close(G_INPUT_STREAM(dstream), NULL, NULL);
|
|
|
|
g_input_stream_close(G_INPUT_STREAM(stream), NULL, NULL);
|
2017-02-04 17:01:09 +01:00
|
|
|
g_string_free(text, 0);
|
|
|
|
g_free(summary);
|
2017-02-04 16:12:46 +01:00
|
|
|
g_free(title);
|
|
|
|
g_date_time_unref(datetime);
|
|
|
|
g_object_unref(file);
|
2017-02-04 14:22:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mdiary_recurse_and_collect(gchar *base_dir, GtkListStore *entryListStore, guint max_level)
|
2017-02-04 13:37:56 +01:00
|
|
|
{
|
|
|
|
GError *error;
|
2017-02-04 14:22:01 +01:00
|
|
|
GDir *dir = g_dir_open(base_dir, 0, &error);
|
2017-02-04 13:37:56 +01:00
|
|
|
gchar *dirname;
|
2017-02-04 14:22:01 +01:00
|
|
|
gchar *fullPath;
|
|
|
|
GRegex *regex;
|
|
|
|
GMatchInfo *match_info;
|
2017-02-04 13:37:56 +01:00
|
|
|
|
|
|
|
if (!dir) {
|
|
|
|
g_print("Error, could not open base directory.\n");
|
|
|
|
} else {
|
|
|
|
while (dirname = (gchar *)g_dir_read_name(dir)) {
|
2017-02-04 14:22:01 +01:00
|
|
|
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);
|
2017-02-04 13:37:56 +01:00
|
|
|
}
|
|
|
|
g_dir_close(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-04 14:22:01 +01:00
|
|
|
/**
|
|
|
|
* @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)
|
|
|
|
{
|
2017-02-04 19:55:08 +01:00
|
|
|
time_earliest = G_MAXINT64;
|
|
|
|
time_latest = 0;
|
2017-02-04 14:22:01 +01:00
|
|
|
mdiary_recurse_and_collect(base_dir, entryListStore, 5);
|
|
|
|
}
|
|
|
|
|
2017-02-04 19:55:08 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-04 13:37:56 +01:00
|
|
|
/**
|
|
|
|
* @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,
|
2017-02-04 17:01:09 +01:00
|
|
|
gchar *summary,
|
2017-02-04 13:37:56 +01:00
|
|
|
gchar *text)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GDateTime *datetime_copy;
|
2017-02-04 17:01:09 +01:00
|
|
|
GList *taglist_copy;
|
2017-02-04 13:37:56 +01:00
|
|
|
gchar *date_text;
|
|
|
|
gchar *taglistString;
|
|
|
|
|
|
|
|
datetime_copy = g_date_time_add(datetime, 0);
|
2017-02-04 17:01:09 +01:00
|
|
|
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);
|
|
|
|
|
2017-02-04 13:37:56 +01:00
|
|
|
|
|
|
|
gtk_list_store_append(entryListStore, &iter);
|
|
|
|
gtk_list_store_set(entryListStore, &iter,
|
2017-02-04 17:01:09 +01:00
|
|
|
COL_TITLE, g_strdup(title),
|
2017-02-04 13:37:56 +01:00
|
|
|
COL_DATE_TEXT, date_text,
|
|
|
|
COL_TAGS_TEXT, taglistString,
|
|
|
|
COL_TIMESTAMP, datetime_copy,
|
2017-02-04 17:01:09 +01:00
|
|
|
COL_TAGLIST, taglist_copy, /* TODO: Verify that the duplication worked! */
|
|
|
|
COL_SUMMARY, summary, /* Automatically strdupd */
|
|
|
|
COL_TEXT, text, /* Automatically strdupd */
|
2017-02-04 13:37:56 +01:00
|
|
|
-1);
|
|
|
|
g_free(date_text);
|
|
|
|
}
|