2017-02-01 18:20:48 +01:00
|
|
|
#include "mdiary.h"
|
2017-02-04 13:37:56 +01:00
|
|
|
|
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-15 20:23:08 +01:00
|
|
|
.regex = "(\\d{1,2})\\.(\\d{1,2})\\.(\\d{1,4})\\s*(\\d{1,2}):(\\d{1,2}).*",
|
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 {
|
2017-02-15 20:23:08 +01:00
|
|
|
regex_string = g_strdup_printf("^\\s*%s%s%s", prefix, dateFormats->regex, suffix);
|
2017-02-04 16:12:46 +01:00
|
|
|
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 &&
|
2017-02-15 19:25:42 +01:00
|
|
|
month >= 1 && month <= 12 &&
|
2017-02-04 16:12:46 +01:00
|
|
|
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-15 17:50:23 +01:00
|
|
|
/**
|
|
|
|
* @brief mdiary_get_line_from_string WARNING: This does modify str and the content of str!
|
|
|
|
* @param str The pointer to the string where to search the linebreak
|
|
|
|
* @return The current line (null terminated). Do not free, this is a direct pointer into the original string.
|
|
|
|
*/
|
|
|
|
static gchar *mdiary_get_line_from_string(gchar **str_ptr)
|
|
|
|
{
|
|
|
|
gchar *start;
|
|
|
|
gchar *str;
|
|
|
|
|
2017-02-15 22:49:04 +01:00
|
|
|
if (!str_ptr || !*str_ptr || !**str_ptr)
|
2017-02-15 17:50:23 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
str = *str_ptr;
|
|
|
|
start = str;
|
|
|
|
|
2017-02-15 19:25:42 +01:00
|
|
|
while (*str != '\n' && *str != '\0')
|
2017-02-15 17:50:23 +01:00
|
|
|
str++;
|
|
|
|
|
|
|
|
*str = '\0';
|
|
|
|
*str_ptr = ++str;
|
|
|
|
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief mdiary_add_file_to_store
|
|
|
|
* @param filename
|
|
|
|
* @param content NULL when the file shall be read by the function. Otherwise the file content.
|
|
|
|
* @param entryListStore
|
|
|
|
* @param autoCompletion
|
|
|
|
*/
|
2017-02-15 19:16:55 +01:00
|
|
|
static void mdiary_add_file_to_store(struct mdiary_scanner *mdiary_scanner,
|
|
|
|
gchar *filename, gchar *content,
|
2017-02-15 17:50:23 +01:00
|
|
|
GtkListStore *entryListStore, GtkListStore *autoCompletion)
|
2017-02-04 14:22:01 +01:00
|
|
|
{
|
2017-02-15 19:16:55 +01:00
|
|
|
GFile *file = NULL;
|
2017-02-04 16:12:46 +01:00
|
|
|
GError *err = NULL;
|
2017-02-15 19:16:55 +01:00
|
|
|
GFileInputStream *stream = NULL;
|
|
|
|
GDataInputStream *dstream = NULL;
|
|
|
|
gchar *line = NULL;
|
|
|
|
gchar *content_ptr = NULL;
|
2017-02-04 16:12:46 +01:00
|
|
|
|
|
|
|
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-05 17:38:36 +01:00
|
|
|
g_message("Add file: %s\n", filename);
|
2017-02-15 19:38:04 +01:00
|
|
|
mdiary_scanner->entries_failed++; /* Will be decremented again when entry was successfully added. */
|
2017-02-04 16:12:46 +01:00
|
|
|
|
2017-02-15 17:50:23 +01:00
|
|
|
if (!content) {
|
|
|
|
file = g_file_new_for_path(filename);
|
|
|
|
stream = g_file_read(file, NULL, &err);
|
|
|
|
if (err != NULL) {
|
2017-03-24 20:49:10 +01:00
|
|
|
g_print("PARSER: %s: %s\n", filename, err->message);
|
2017-02-15 17:50:23 +01:00
|
|
|
g_error_free(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dstream = g_data_input_stream_new(G_INPUT_STREAM(stream));
|
2017-02-04 16:12:46 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 17:50:23 +01:00
|
|
|
content_ptr = content;
|
|
|
|
|
|
|
|
while (line = (content ? mdiary_get_line_from_string(&content_ptr) :
|
|
|
|
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) {
|
2017-02-15 20:23:08 +01:00
|
|
|
datetime = mdiary_get_date_from_string_ext(line, "Date: ", "");
|
2017-02-04 16:12:46 +01:00
|
|
|
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
|
|
|
|
2017-02-15 19:38:04 +01:00
|
|
|
mdiary_scanner->entries_failed--;
|
2017-02-15 19:16:55 +01:00
|
|
|
mdiary_scanner->entries_added++;
|
2017-02-04 16:12:46 +01:00
|
|
|
mdiary_add_entry_to_store(entryListStore,
|
2017-02-05 18:45:54 +01:00
|
|
|
autoCompletion,
|
2017-02-04 16:12:46 +01:00
|
|
|
title,
|
|
|
|
datetime,
|
|
|
|
tagList,
|
2017-02-04 17:01:09 +01:00
|
|
|
summary,
|
2017-02-19 12:50:28 +01:00
|
|
|
text->str,
|
|
|
|
filename);
|
2017-02-04 16:12:46 +01:00
|
|
|
|
2017-02-15 19:16:55 +01:00
|
|
|
if (g_date_time_to_unix(datetime) > mdiary_scanner->time_latest)
|
|
|
|
mdiary_scanner->time_latest = g_date_time_to_unix(datetime);
|
|
|
|
if (g_date_time_to_unix(datetime) < mdiary_scanner->time_earliest)
|
|
|
|
mdiary_scanner->time_earliest = g_date_time_to_unix(datetime);
|
2017-02-04 19:55:08 +01:00
|
|
|
|
2017-02-15 19:21:55 +01:00
|
|
|
if (!content) {
|
|
|
|
g_input_stream_close(G_INPUT_STREAM(dstream), NULL, NULL);
|
|
|
|
g_input_stream_close(G_INPUT_STREAM(stream), NULL, NULL);
|
|
|
|
g_object_unref(file);
|
|
|
|
}
|
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);
|
2017-02-04 14:22:01 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 19:16:55 +01:00
|
|
|
static void mdiary_add_gpg_to_store(struct mdiary_scanner *mdiary_scanner,
|
|
|
|
gchar *filename,
|
|
|
|
GtkListStore *entryListStore, GtkListStore *autoCompletion)
|
2017-02-15 17:50:23 +01:00
|
|
|
{
|
|
|
|
gchar *cmd = NULL;
|
|
|
|
gchar *cmd_stdout = NULL;
|
|
|
|
gchar *cmd_stderr = NULL;
|
|
|
|
gint exit_status = 0;
|
|
|
|
|
|
|
|
cmd = g_strdup_printf("gpg --decrypt %s", filename);
|
|
|
|
|
|
|
|
if (g_spawn_command_line_sync(cmd, &cmd_stdout, &cmd_stderr, &exit_status, NULL) && exit_status == 0) {
|
2017-02-15 19:21:55 +01:00
|
|
|
g_message("GPG: Decryption OK!\n");
|
2017-02-15 19:16:55 +01:00
|
|
|
mdiary_add_file_to_store(mdiary_scanner, filename, cmd_stdout, entryListStore, autoCompletion);
|
2017-02-15 17:50:23 +01:00
|
|
|
} else {
|
2017-02-15 19:38:04 +01:00
|
|
|
mdiary_scanner->entries_failed++;
|
2018-03-18 13:54:47 +01:00
|
|
|
mdiary_scanner->error_code |= MDS_ERROR_GPG;
|
2017-02-15 19:21:55 +01:00
|
|
|
g_warning("GPG: Decryption of message failed!\n"
|
|
|
|
"---------- SNIP ----------\n"
|
|
|
|
"%s"
|
|
|
|
"---------- SNIP ----------\n",
|
|
|
|
cmd_stderr ? cmd_stderr : "Unknown error.");
|
2017-02-15 17:50:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free(cmd);
|
|
|
|
g_free(cmd_stdout);
|
|
|
|
g_free(cmd_stderr);
|
|
|
|
}
|
|
|
|
|
2017-02-15 19:16:55 +01:00
|
|
|
static void mdiary_recurse_and_collect(struct mdiary_scanner *mdiary_scanner,
|
|
|
|
gchar *base_dir,
|
2017-02-05 18:45:54 +01:00
|
|
|
GtkListStore *entryListStore,
|
|
|
|
GtkListStore *autoCompletion,
|
|
|
|
guint max_level)
|
2017-02-04 13:37:56 +01:00
|
|
|
{
|
2017-02-07 19:53:51 +01:00
|
|
|
GDir *dir = g_dir_open(base_dir, 0, NULL);
|
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) {
|
2017-02-07 19:53:51 +01:00
|
|
|
g_print("Could not open base directory.\n");
|
2018-03-18 13:54:47 +01:00
|
|
|
mdiary_scanner->error_code |= MDS_ERROR_BASE_DIR;
|
2017-02-04 13:37:56 +01:00
|
|
|
} 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))
|
2017-02-15 19:16:55 +01:00
|
|
|
mdiary_add_file_to_store(mdiary_scanner,
|
|
|
|
fullPath, NULL,
|
|
|
|
entryListStore, autoCompletion);
|
2017-02-15 17:50:23 +01:00
|
|
|
g_regex_unref(regex);
|
|
|
|
|
2017-02-15 19:16:55 +01:00
|
|
|
|
2017-02-15 17:50:23 +01:00
|
|
|
regex = g_regex_new("\\.md.gpg$", G_REGEX_CASELESS, 0, NULL);
|
|
|
|
if (g_regex_match(regex, fullPath, 0, &match_info) &&
|
2017-02-15 19:16:55 +01:00
|
|
|
g_match_info_matches(match_info)) {
|
|
|
|
mdiary_scanner->entries_encrypted++;
|
|
|
|
if (mdiary_scanner->gpg_enabled)
|
|
|
|
mdiary_add_gpg_to_store(mdiary_scanner,
|
|
|
|
fullPath,
|
|
|
|
entryListStore, autoCompletion);
|
|
|
|
}
|
2017-02-04 14:22:01 +01:00
|
|
|
g_regex_unref(regex);
|
2017-02-15 19:16:55 +01:00
|
|
|
|
2017-02-04 14:22:01 +01:00
|
|
|
} else if (g_file_test(fullPath, G_FILE_TEST_IS_DIR) && max_level) {
|
2017-02-15 19:16:55 +01:00
|
|
|
mdiary_recurse_and_collect(mdiary_scanner,
|
|
|
|
fullPath,
|
|
|
|
entryListStore, autoCompletion,
|
|
|
|
max_level--);
|
2017-02-04 14:22:01 +01:00
|
|
|
}
|
|
|
|
g_free(fullPath);
|
2017-02-04 13:37:56 +01:00
|
|
|
}
|
|
|
|
g_dir_close(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 19:16:55 +01:00
|
|
|
struct mdiary_scanner *mdiary_scanner_new(gboolean gpg_enabled)
|
2017-02-04 14:22:01 +01:00
|
|
|
{
|
2017-02-15 19:16:55 +01:00
|
|
|
struct mdiary_scanner *mdiary_scanner;
|
|
|
|
|
|
|
|
mdiary_scanner = malloc(sizeof(struct mdiary_scanner));
|
|
|
|
if (mdiary_scanner) {
|
2017-02-15 19:38:04 +01:00
|
|
|
mdiary_scanner->time_earliest = G_MAXINT64;
|
|
|
|
mdiary_scanner->time_latest = 0;
|
|
|
|
mdiary_scanner->entries_added = 0;
|
|
|
|
mdiary_scanner->gpg_enabled = gpg_enabled;
|
|
|
|
mdiary_scanner->entries_failed = 0;
|
2017-02-15 19:16:55 +01:00
|
|
|
mdiary_scanner->entries_encrypted = 0;
|
2018-03-18 13:54:47 +01:00
|
|
|
mdiary_scanner->error_code = 0;
|
2017-02-15 19:16:55 +01:00
|
|
|
}
|
2017-03-24 20:18:33 +01:00
|
|
|
|
|
|
|
return mdiary_scanner;
|
2017-02-04 14:22:01 +01:00
|
|
|
}
|
|
|
|
|
2017-03-24 20:49:10 +01:00
|
|
|
void mdiary_scanner_free(struct mdiary_scanner *mdiary_scanner)
|
2017-02-04 19:55:08 +01:00
|
|
|
{
|
2017-02-15 19:16:55 +01:00
|
|
|
free(mdiary_scanner);
|
2017-02-04 19:55:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-15 19:16:55 +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
|
|
|
|
* @return The amount of entries added in total across all scans
|
2017-02-04 19:55:08 +01:00
|
|
|
*/
|
2017-02-15 19:16:55 +01:00
|
|
|
gint mdiary_scan_to_store(struct mdiary_scanner *mdiary_scanner,
|
|
|
|
gchar *base_dir,
|
|
|
|
GtkListStore *entryListStore, GtkListStore *autoCompletion)
|
2017-02-04 19:55:08 +01:00
|
|
|
{
|
|
|
|
|
2017-02-15 19:16:55 +01:00
|
|
|
mdiary_recurse_and_collect(mdiary_scanner, base_dir, entryListStore, autoCompletion, 5);
|
|
|
|
|
|
|
|
return mdiary_scanner->entries_added;
|
|
|
|
}
|
2017-02-04 19:55:08 +01:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-05 18:45:54 +01:00
|
|
|
struct tag_compare_struct {
|
|
|
|
gchar *text;
|
|
|
|
gboolean result;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
gboolean mdiary_test_duplicate(GtkTreeModel *model,
|
|
|
|
GtkTreePath *path,
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
gpointer data) {
|
|
|
|
gchar *temp = NULL;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
struct tag_compare_struct *tag_compare_struct = (struct tag_compare_struct *)data;
|
|
|
|
|
|
|
|
gtk_tree_model_get(model, iter, 0, &temp, -1);
|
|
|
|
if (!g_strcmp0(temp, tag_compare_struct->text)) {
|
|
|
|
ret = TRUE;
|
|
|
|
tag_compare_struct->result = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(temp);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-04 13:37:56 +01:00
|
|
|
/**
|
|
|
|
* @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,
|
2017-02-05 18:45:54 +01:00
|
|
|
GtkListStore *autoCompletion,
|
2017-02-04 13:37:56 +01:00
|
|
|
gchar *title,
|
|
|
|
GDateTime *datetime,
|
|
|
|
GList *tags,
|
2017-02-04 17:01:09 +01:00
|
|
|
gchar *summary,
|
2017-02-19 12:50:28 +01:00
|
|
|
gchar *text,
|
|
|
|
gchar *file_url)
|
2017-02-04 13:37:56 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2017-02-05 18:45:54 +01:00
|
|
|
GList *l;
|
|
|
|
struct tag_compare_struct tag_compare_struct = { .text = "", .result = FALSE };
|
|
|
|
|
2017-02-04 13:37:56 +01:00
|
|
|
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-05 15:02:18 +01:00
|
|
|
COL_TITLE, 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-19 12:50:28 +01:00
|
|
|
COL_URL, file_url, /* Automatically strdupd */
|
2017-02-04 13:37:56 +01:00
|
|
|
-1);
|
|
|
|
g_free(date_text);
|
2017-02-05 18:45:54 +01:00
|
|
|
|
|
|
|
for (l = tags; l != NULL; l = l->next) {
|
|
|
|
tag_compare_struct.text = l->data;
|
|
|
|
gtk_tree_model_foreach(GTK_TREE_MODEL(autoCompletion), mdiary_test_duplicate, &tag_compare_struct);
|
|
|
|
if (!tag_compare_struct.result) {
|
|
|
|
gtk_list_store_append(autoCompletion, &iter);
|
|
|
|
gtk_list_store_set(autoCompletion, &iter,
|
|
|
|
0, l->data,
|
|
|
|
-1);
|
|
|
|
}
|
|
|
|
}
|
2017-02-05 18:54:42 +01:00
|
|
|
|
|
|
|
tag_compare_struct.result = FALSE;
|
|
|
|
|
|
|
|
tag_compare_struct.text = title;
|
|
|
|
gtk_tree_model_foreach(GTK_TREE_MODEL(autoCompletion), mdiary_test_duplicate, &tag_compare_struct);
|
|
|
|
if (!tag_compare_struct.result) {
|
|
|
|
gtk_list_store_append(autoCompletion, &iter);
|
|
|
|
gtk_list_store_set(autoCompletion, &iter,
|
|
|
|
0, title,
|
|
|
|
-1);
|
|
|
|
}
|
2017-02-04 13:37:56 +01:00
|
|
|
}
|
2017-02-07 19:03:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief mdiary_free_entry_elements frees all non-GtkListView-managed parts of the mdiary entry
|
|
|
|
* @param model
|
|
|
|
* @param path
|
|
|
|
* @param iter
|
|
|
|
* @param data
|
|
|
|
* @return FALSE
|
|
|
|
*/
|
2017-02-15 19:25:42 +01:00
|
|
|
gboolean mdiary_free_entry_elements(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
|
|
|
|
{
|
2017-02-07 19:03:05 +01:00
|
|
|
gpointer ptr;
|
|
|
|
|
|
|
|
gtk_tree_model_get(model, iter, COL_TAGLIST, &ptr, -1);
|
|
|
|
g_list_free_full((GList *)ptr, g_free);
|
|
|
|
|
|
|
|
gtk_tree_model_get(model, iter, COL_TIMESTAMP, &ptr, -1);
|
|
|
|
g_date_time_unref((GDateTime *)ptr);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief mdiary_reset_store clears the two GtkListStores and frees all of their respective elements. (Make sure that
|
|
|
|
* no other part of the program tries to access the elements of the store while executing this function.)
|
|
|
|
* @param entryListStore
|
|
|
|
* @param autoCompletion
|
|
|
|
*/
|
|
|
|
void mdiary_reset_store(GtkListStore *entryListStore, GtkListStore *autoCompletion)
|
|
|
|
{
|
|
|
|
gtk_tree_model_foreach(GTK_TREE_MODEL(entryListStore), mdiary_free_entry_elements, NULL);
|
|
|
|
gtk_list_store_clear(entryListStore);
|
|
|
|
gtk_list_store_clear(autoCompletion);
|
|
|
|
}
|