Automatically add tags for directory names

This commit is contained in:
Markus Koch 2018-07-07 09:31:13 +02:00
parent 16c3dcac15
commit 843329331d

View File

@ -246,7 +246,7 @@ static gchar *mdiary_get_line_from_string(gchar **str_ptr)
* @param autoCompletion * @param autoCompletion
*/ */
static void mdiary_add_file_to_store(struct mdiary_scanner *mdiary_scanner, static void mdiary_add_file_to_store(struct mdiary_scanner *mdiary_scanner,
gchar *filename, gchar *content, gchar *filename, gchar *content, GList *autotags,
GtkListStore *entryListStore, GtkListStore *autoCompletion) GtkListStore *entryListStore, GtkListStore *autoCompletion)
{ {
GFile *file = NULL; GFile *file = NULL;
@ -263,6 +263,8 @@ static void mdiary_add_file_to_store(struct mdiary_scanner *mdiary_scanner,
GString *text = NULL; GString *text = NULL;
guint header_state = 0; guint header_state = 0;
GList *l;
g_message("Add file: %s\n", filename); g_message("Add file: %s\n", filename);
mdiary_scanner->entries_failed++; /* Will be decremented again when entry was successfully added. */ mdiary_scanner->entries_failed++; /* Will be decremented again when entry was successfully added. */
@ -338,6 +340,11 @@ static void mdiary_add_file_to_store(struct mdiary_scanner *mdiary_scanner,
title = g_strdup("Untitled"); title = g_strdup("Untitled");
} }
for (l = autotags; l != NULL; l = l->next)
{
tagList = g_list_append(tagList, g_strdup(l->data));
}
mdiary_scanner->entries_failed--; mdiary_scanner->entries_failed--;
mdiary_scanner->entries_added++; mdiary_scanner->entries_added++;
mdiary_add_entry_to_store(entryListStore, mdiary_add_entry_to_store(entryListStore,
@ -366,7 +373,7 @@ static void mdiary_add_file_to_store(struct mdiary_scanner *mdiary_scanner,
} }
static void mdiary_add_gpg_to_store(struct mdiary_scanner *mdiary_scanner, static void mdiary_add_gpg_to_store(struct mdiary_scanner *mdiary_scanner,
gchar *filename, gchar *filename, GList **add_tags,
GtkListStore *entryListStore, GtkListStore *autoCompletion) GtkListStore *entryListStore, GtkListStore *autoCompletion)
{ {
gchar *cmd = NULL; gchar *cmd = NULL;
@ -378,7 +385,7 @@ static void mdiary_add_gpg_to_store(struct mdiary_scanner *mdiary_scanner,
if (g_spawn_command_line_sync(cmd, &cmd_stdout, &cmd_stderr, &exit_status, NULL) && exit_status == 0) { if (g_spawn_command_line_sync(cmd, &cmd_stdout, &cmd_stderr, &exit_status, NULL) && exit_status == 0) {
g_message("GPG: Decryption OK!\n"); g_message("GPG: Decryption OK!\n");
mdiary_add_file_to_store(mdiary_scanner, filename, cmd_stdout, entryListStore, autoCompletion); mdiary_add_file_to_store(mdiary_scanner, filename, cmd_stdout, *add_tags, entryListStore, autoCompletion);
} else { } else {
mdiary_scanner->entries_failed++; mdiary_scanner->entries_failed++;
mdiary_scanner->error_code |= MDS_ERROR_GPG; mdiary_scanner->error_code |= MDS_ERROR_GPG;
@ -396,6 +403,7 @@ static void mdiary_add_gpg_to_store(struct mdiary_scanner *mdiary_scanner,
static void mdiary_recurse_and_collect(struct mdiary_scanner *mdiary_scanner, static void mdiary_recurse_and_collect(struct mdiary_scanner *mdiary_scanner,
gchar *base_dir, gchar *base_dir,
GList **autotags,
GtkListStore *entryListStore, GtkListStore *entryListStore,
GtkListStore *autoCompletion, GtkListStore *autoCompletion,
guint max_level) guint max_level)
@ -417,27 +425,28 @@ static void mdiary_recurse_and_collect(struct mdiary_scanner *mdiary_scanner,
if (g_regex_match(regex, fullPath, 0, &match_info) && if (g_regex_match(regex, fullPath, 0, &match_info) &&
g_match_info_matches(match_info)) g_match_info_matches(match_info))
mdiary_add_file_to_store(mdiary_scanner, mdiary_add_file_to_store(mdiary_scanner,
fullPath, NULL, fullPath, NULL, *autotags,
entryListStore, autoCompletion); entryListStore, autoCompletion);
g_regex_unref(regex); g_regex_unref(regex);
regex = g_regex_new("\\.md.gpg$", G_REGEX_CASELESS, 0, NULL); regex = g_regex_new("\\.md.gpg$", G_REGEX_CASELESS, 0, NULL);
if (g_regex_match(regex, fullPath, 0, &match_info) && if (g_regex_match(regex, fullPath, 0, &match_info) &&
g_match_info_matches(match_info)) { g_match_info_matches(match_info)) {
mdiary_scanner->entries_encrypted++; mdiary_scanner->entries_encrypted++;
if (mdiary_scanner->gpg_enabled) if (mdiary_scanner->gpg_enabled)
mdiary_add_gpg_to_store(mdiary_scanner, mdiary_add_gpg_to_store(mdiary_scanner,
fullPath, fullPath, autotags,
entryListStore, autoCompletion); entryListStore, autoCompletion);
} }
g_regex_unref(regex); g_regex_unref(regex);
} else if (g_file_test(fullPath, G_FILE_TEST_IS_DIR) && max_level) { } else if (g_file_test(fullPath, G_FILE_TEST_IS_DIR) && max_level) {
*autotags = g_list_append(*autotags, dirname);
mdiary_recurse_and_collect(mdiary_scanner, mdiary_recurse_and_collect(mdiary_scanner,
fullPath, fullPath, autotags,
entryListStore, autoCompletion, entryListStore, autoCompletion,
max_level--); max_level--);
*autotags = g_list_remove(*autotags, g_list_last(*autotags)->data);
} }
g_free(fullPath); g_free(fullPath);
} }
@ -478,8 +487,11 @@ gint mdiary_scan_to_store(struct mdiary_scanner *mdiary_scanner,
gchar *base_dir, gchar *base_dir,
GtkListStore *entryListStore, GtkListStore *autoCompletion) GtkListStore *entryListStore, GtkListStore *autoCompletion)
{ {
GList *autotags = NULL;
mdiary_recurse_and_collect(mdiary_scanner, base_dir, entryListStore, autoCompletion, 5); mdiary_recurse_and_collect(mdiary_scanner, base_dir, &autotags, entryListStore, autoCompletion, 5);
g_list_free(autotags);
return mdiary_scanner->entries_added; return mdiary_scanner->entries_added;
} }