Compare commits

..

3 Commits

6 changed files with 207 additions and 60 deletions

View File

@ -3,6 +3,11 @@
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkTextBuffer" id="entryTextBuffer"/>
<object class="GtkImage" id="imageDecrypt">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">changes-prevent-symbolic.symbolic</property>
</object>
<object class="GtkPopover" id="popoverWorkspace">
<property name="can_focus">False</property>
<child>
@ -401,7 +406,7 @@
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">4</property>
<property name="spacing">6</property>
<child>
<object class="GtkMenuButton">
<property name="visible">True</property>
@ -446,6 +451,20 @@
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="buttonDecrypt">
<property name="label" translatable="yes">Decrypt GPG</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="image">imageDecrypt</property>
<property name="always_show_image">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>

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);
}
/**
@ -448,6 +452,7 @@ struct mainWindow *mainWindow_new()
mainWindow->buttonClearSearch = GTK_WIDGET(gtk_builder_get_object(builder, "buttonClearSearch"));
mainWindow->headerBar = GTK_WIDGET(gtk_builder_get_object(builder, "headerBar"));
mainWindow->labelDateToDate = GTK_WIDGET(gtk_builder_get_object(builder, "label_date_to_date"));
mainWindow->buttonDecrypt = GTK_WIDGET(gtk_builder_get_object(builder, "buttonDecrypt"));
mainWindow->entryListView = GTK_TREE_VIEW(gtk_builder_get_object(builder, "entryListView"));
mainWindow->entryListSelection = GTK_TREE_SELECTION(gtk_builder_get_object(builder, "entryListSelection"));
mainWindow->textBuffer = GTK_TEXT_BUFFER(gtk_builder_get_object(builder, "entryTextBuffer"));
@ -714,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,
@ -749,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);
}
}
@ -777,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

@ -31,6 +31,7 @@ struct mainWindow {
GtkWidget *buttonClearSearch;
GtkWidget *headerBar;
GtkWidget *labelDateToDate;
GtkWidget *buttonDecrypt;
GtkTreeView *entryListView;
GtkTreeSelection *entryListSelection;
GtkTextBuffer *textBuffer;
@ -82,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.
*/
@ -217,13 +214,49 @@ static GList *mdiary_add_tags_from_string(gchar *string)
return ret;
}
static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListStore, GtkListStore *autoCompletion)
/**
* @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)
{
GFile *file;
gchar *start;
gchar *str;
if (!str_ptr || !*str_ptr | !**str_ptr)
return NULL;
str = *str_ptr;
start = str;
while (*str != '\n' && *str != '\0') {
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
*/
static void mdiary_add_file_to_store(struct mdiary_scanner *mdiary_scanner,
gchar *filename, gchar *content,
GtkListStore *entryListStore, GtkListStore *autoCompletion)
{
GFile *file = NULL;
GError *err = NULL;
GFileInputStream *stream;
GDataInputStream *dstream;
gchar *line;
GFileInputStream *stream = NULL;
GDataInputStream *dstream = NULL;
gchar *line = NULL;
gchar *content_ptr = NULL;
GDateTime *datetime = NULL;
gchar *title = NULL;
@ -234,16 +267,21 @@ static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListSto
g_message("Add file: %s\n", filename);
file = g_file_new_for_path(filename);
stream = g_file_read(file, NULL, &err);
if (err != NULL) {
g_print("PARSER: %s\n", filename, err->message);
g_error_free(err);
return;
if (!content) {
file = g_file_new_for_path(filename);
stream = g_file_read(file, NULL, &err);
if (err != NULL) {
g_print("PARSER: %s\n", filename, err->message);
g_error_free(err);
return;
}
dstream = g_data_input_stream_new(G_INPUT_STREAM(stream));
}
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)) {
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))) {
if (header_state == 0) {
if (!datetime) {
datetime = mdiary_get_date_from_string_ext(line, "Date: .*", "");
@ -301,6 +339,7 @@ static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListSto
title = g_strdup("Untitled");
}
mdiary_scanner->entries_added++;
mdiary_add_entry_to_store(entryListStore,
autoCompletion,
title,
@ -309,10 +348,10 @@ static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListSto
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);
@ -323,7 +362,35 @@ static void mdiary_add_file_to_store(gchar *filename, GtkListStore *entryListSto
g_object_unref(file);
}
static void mdiary_recurse_and_collect(gchar *base_dir,
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;
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) {
g_print("GPG: Decryption OK!\n");
mdiary_add_file_to_store(mdiary_scanner, filename, cmd_stdout, entryListStore, autoCompletion);
} else {
g_print("GPG: Decryption of message failed!\n"
"---------- SNIP ----------\n"
"%s"
"---------- SNIP ----------\n",
cmd_stderr ? cmd_stderr : "Unknown error.");
}
g_free(cmd);
g_free(cmd_stdout);
g_free(cmd_stderr);
}
static void mdiary_recurse_and_collect(struct mdiary_scanner *mdiary_scanner,
gchar *base_dir,
GtkListStore *entryListStore,
GtkListStore *autoCompletion,
guint max_level)
@ -343,10 +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, 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_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);
}
@ -354,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);
}
}