Reworked mdiary_scanner + added GUI support for gpg encrypted files

newfile
Markus Koch 2017-02-15 19:16:55 +01:00
parent 0640ec612d
commit 71a2370b91
5 changed files with 111 additions and 56 deletions

View File

@ -406,6 +406,10 @@ static void mainWindow_connect_signals(struct mainWindow *mainWindow)
"changed",
(GCallback) mainWindow_workspace_search_changed,
mainWindow);
g_signal_connect(mainWindow->buttonDecrypt,
"clicked",
(GCallback) mainWindow_decrypt_gpg_clicked,
mainWindow);
}
/**
@ -715,23 +719,31 @@ static void mainWindow_show_error(struct mainWindow *mainWindow, gchar *text)
gtk_widget_destroy(dialog);
}
void mainWindow_switch_workspace(struct mainWindow *mainWindow, gchar *path)
void mainWindow_switch_workspace(struct mainWindow *mainWindow, gchar *path, gboolean gpg_enabled)
{
struct mdiary_scanner *mdiary_scanner;
g_print("Switching workspace to %s.\n", path);
gtk_popover_popdown(GTK_POPOVER(mainWindow->popoverWorkspace));
mainWindow_set_meta_information(mainWindow, 0, 0, TRUE);
mdiary_reset_store(mainWindow->entryListStore, mainWindow->completionListStore);
if (mdiary_scan_to_store(path, mainWindow->entryListStore, mainWindow->completionListStore)) {
mdiary_scanner = mdiary_scanner_new(gpg_enabled);
if (mdiary_scan_to_store(mdiary_scanner, path, mainWindow->entryListStore, mainWindow->completionListStore)) {
mainWindow_set_meta_information(mainWindow,
mdiary_get_time_earliest(), mdiary_get_time_latest(),
mdiary_scanner->time_earliest, mdiary_scanner->time_latest,
FALSE);
mainWindow_add_recent_workspace(mainWindow, path, FALSE);
gtk_header_bar_set_subtitle(GTK_HEADER_BAR(mainWindow->headerBar), path);
if (mdiary_scanner->entries_encrypted > 0 && !gpg_enabled)
gtk_widget_set_visible(mainWindow->buttonDecrypt, TRUE);
else
gtk_widget_set_visible(mainWindow->buttonDecrypt, FALSE);
} else {
gtk_header_bar_set_subtitle(GTK_HEADER_BAR(mainWindow->headerBar), "No workspace opened.");
mainWindow_show_error(mainWindow, "Could not open workspace or no entries were found in it.");
}
mdiary_scanner_free(mdiary_scanner);
}
void mainWindow_workspaceListClicked(GtkWidget *widget,
@ -750,7 +762,7 @@ void mainWindow_workspaceListClicked(GtkWidget *widget,
selection = gtk_tree_view_get_selection(mainWindow->workspaceListView);
if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
gtk_tree_model_get(model, &iter, WSCOL_PATH, &path, -1);
mainWindow_switch_workspace(mainWindow, path);
mainWindow_switch_workspace(mainWindow, path, FALSE);
g_free(path);
}
}
@ -778,9 +790,26 @@ void mainWindow_otherWorkspaceClicked(GtkWidget *widget, gpointer user_data)
char *directory;
directory = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
mainWindow_switch_workspace(mainWindow, directory);
mainWindow_switch_workspace(mainWindow, directory, FALSE);
g_free(directory);
}
gtk_widget_destroy (dialog);
}
void mainWindow_decrypt_gpg_clicked(GtkWidget *widget, gpointer user_data)
{
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
GtkTreeIter iter;
gchar *temp;
if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter)) {
gtk_tree_model_get(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter,
WSCOL_PATH, &temp,
-1);
mainWindow_switch_workspace(mainWindow, temp, TRUE);
g_free(temp);
} else {
g_print("ERROR: This should never happen...\n");
}
}

View File

@ -83,6 +83,7 @@ void mainWindow_workspace_search_changed(GtkWidget *widget, gpointer user_data);
void mainWindow_checkDate(GtkWidget *widget, gint event, gpointer user_data);
void mainWindow_dateIconPress(GtkWidget *widget, gint icon_pos, gint event, gpointer user_data);
void mainWindow_add_recent_workspace(struct mainWindow *mainWindow, gchar *path, gboolean append);
void mainWindow_switch_workspace(struct mainWindow *mainWindow, gchar *path);
void mainWindow_switch_workspace(struct mainWindow *mainWindow, gchar *path, gboolean gpg_enabled);
void mainWindow_decrypt_gpg_clicked(GtkWidget *widget, gpointer user_data);
#endif /* MAINWINDOW_H */

View File

@ -1,8 +1,5 @@
#include "mdiary.h"
static gint64 time_earliest;
static gint64 time_latest;
/**
* @brief The dateFormats struct contains regex/date-info pairs to parse different date formats.
*/
@ -250,15 +247,16 @@ static gchar *mdiary_get_line_from_string(gchar **str_ptr)
* @param entryListStore
* @param autoCompletion
*/
static void mdiary_add_file_to_store(gchar *filename, gchar *content,
static void mdiary_add_file_to_store(struct mdiary_scanner *mdiary_scanner,
gchar *filename, gchar *content,
GtkListStore *entryListStore, GtkListStore *autoCompletion)
{
GFile *file;
GFile *file = NULL;
GError *err = NULL;
GFileInputStream *stream;
GDataInputStream *dstream;
gchar *line;
gchar *content_ptr;
GFileInputStream *stream = NULL;
GDataInputStream *dstream = NULL;
gchar *line = NULL;
gchar *content_ptr = NULL;
GDateTime *datetime = NULL;
gchar *title = NULL;
@ -341,6 +339,7 @@ static void mdiary_add_file_to_store(gchar *filename, gchar *content,
title = g_strdup("Untitled");
}
mdiary_scanner->entries_added++;
mdiary_add_entry_to_store(entryListStore,
autoCompletion,
title,
@ -349,10 +348,10 @@ static void mdiary_add_file_to_store(gchar *filename, gchar *content,
summary,
text->str);
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);
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);
g_input_stream_close(G_INPUT_STREAM(dstream), NULL, NULL);
g_input_stream_close(G_INPUT_STREAM(stream), NULL, NULL);
@ -363,7 +362,9 @@ static void mdiary_add_file_to_store(gchar *filename, gchar *content,
g_object_unref(file);
}
static void mdiary_add_gpg_to_store(gchar *filename, GtkListStore *entryListStore, GtkListStore *autoCompletion)
static void mdiary_add_gpg_to_store(struct mdiary_scanner *mdiary_scanner,
gchar *filename,
GtkListStore *entryListStore, GtkListStore *autoCompletion)
{
gchar *cmd = NULL;
gchar *cmd_stdout = NULL;
@ -374,7 +375,7 @@ static void mdiary_add_gpg_to_store(gchar *filename, GtkListStore *entryListStor
if (g_spawn_command_line_sync(cmd, &cmd_stdout, &cmd_stderr, &exit_status, NULL) && exit_status == 0) {
g_print("GPG: Decryption OK!\n");
mdiary_add_file_to_store(filename, cmd_stdout, entryListStore, autoCompletion);
mdiary_add_file_to_store(mdiary_scanner, filename, cmd_stdout, entryListStore, autoCompletion);
} else {
g_print("GPG: Decryption of message failed!\n"
"---------- SNIP ----------\n"
@ -388,7 +389,8 @@ static void mdiary_add_gpg_to_store(gchar *filename, GtkListStore *entryListStor
g_free(cmd_stderr);
}
static void mdiary_recurse_and_collect(gchar *base_dir,
static void mdiary_recurse_and_collect(struct mdiary_scanner *mdiary_scanner,
gchar *base_dir,
GtkListStore *entryListStore,
GtkListStore *autoCompletion,
guint max_level)
@ -408,16 +410,28 @@ static void mdiary_recurse_and_collect(gchar *base_dir,
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, NULL, entryListStore, autoCompletion);
mdiary_add_file_to_store(mdiary_scanner,
fullPath, NULL,
entryListStore, autoCompletion);
g_regex_unref(regex);
regex = g_regex_new("\\.md.gpg$", G_REGEX_CASELESS, 0, NULL);
if (g_regex_match(regex, fullPath, 0, &match_info) &&
g_match_info_matches(match_info))
mdiary_add_gpg_to_store(fullPath, entryListStore, autoCompletion);
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);
}
g_regex_unref(regex);
} else if (g_file_test(fullPath, G_FILE_TEST_IS_DIR) && max_level) {
mdiary_recurse_and_collect(fullPath, entryListStore, autoCompletion, max_level--);
mdiary_recurse_and_collect(mdiary_scanner,
fullPath,
entryListStore, autoCompletion,
max_level--);
}
g_free(fullPath);
}
@ -425,40 +439,41 @@ static void mdiary_recurse_and_collect(gchar *base_dir,
}
}
struct mdiary_scanner *mdiary_scanner_new(gboolean gpg_enabled)
{
struct mdiary_scanner *mdiary_scanner;
mdiary_scanner = malloc(sizeof(struct mdiary_scanner));
if (mdiary_scanner) {
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_encrypted = 0;
}
}
void *mdiary_scanner_free(struct mdiary_scanner *mdiary_scanner)
{
free(mdiary_scanner);
}
/**
* @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 in the list (total across multiple scans)
* @return The amount of entries added in total across all scans
*/
gint mdiary_scan_to_store(gchar *base_dir, GtkListStore *entryListStore, GtkListStore *autoCompletion)
gint mdiary_scan_to_store(struct mdiary_scanner *mdiary_scanner,
gchar *base_dir,
GtkListStore *entryListStore, GtkListStore *autoCompletion)
{
time_earliest = G_MAXINT64;
time_latest = 0;
mdiary_recurse_and_collect(base_dir, entryListStore, autoCompletion, 5);
return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(entryListStore), NULL);
mdiary_recurse_and_collect(mdiary_scanner, base_dir, entryListStore, autoCompletion, 5);
return mdiary_scanner->entries_added;
}
/**
* @brief mdiary_get_time_earliest getter function for time_earliest. Call mdiary_scan_to_store before using.
* @return time_earliest
*/
gint64 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
*/
gint64 mdiary_get_time_latest(void)
{
return time_latest;
}
/**
* @brief mainWindow_taglist_to_string concatenates a list with commas
* @param list is a GList of strings to concatenate

View File

@ -16,7 +16,19 @@ enum {
COL_COUNT
};
gint mdiary_scan_to_store(gchar *base_dir, GtkListStore *entryListStore, GtkListStore *autoCompletion);
struct mdiary_scanner {
gboolean gpg_enabled;
gint64 time_earliest;
gint64 time_latest;
gint entries_added;
gint entries_encrypted;
};
struct mdiary_scanner *mdiary_scanner_new(gboolean gpg_enabled);
void *mdiary_scanner_free(struct mdiary_scanner *mdiary_scanner);
gint mdiary_scan_to_store(struct mdiary_scanner *mdiary_scanner,
gchar *base_dir,
GtkListStore *entryListStore, GtkListStore *autoCompletion);
void mdiary_add_entry_to_store(GtkListStore *entryListStore,
GtkListStore *autoCompletion,
gchar *title,
@ -26,8 +38,6 @@ void mdiary_add_entry_to_store(GtkListStore *entryListStore,
gchar *text);
GDateTime *mdiary_get_date_from_string_ext(gchar *string, gchar *prefix, gchar *suffix);
GDateTime *mdiary_get_date_from_string(gchar *string);
gint64 mdiary_get_time_earliest(void);
gint64 mdiary_get_time_latest(void);
void mdiary_reset_store(GtkListStore *entryListStore, GtkListStore *autoCompletion);
#endif /* MDIARY_H */

View File

@ -48,7 +48,7 @@ gboolean mdiary_load_settings(gchar *filename, struct mdiary_settings *settings)
gtk_tree_model_get(GTK_TREE_MODEL(settings->mainWindow->workspaceListStore), &iter,
WSCOL_PATH, &temp,
-1);
mainWindow_switch_workspace(settings->mainWindow, temp);
mainWindow_switch_workspace(settings->mainWindow, temp, FALSE);
g_free(temp);
}
}