Compare commits

...

2 Commits

2 changed files with 90 additions and 14 deletions

View File

@ -1,6 +1,14 @@
#include "mainwindow.h"
#include "mdiary.h"
const gchar *default_template = "\
# Title\n\
Date: 01.01.2000 00:00\n\
Summary: \n\
Tags: \n\
\n\
Text";
static gboolean mainWindow_workspace_entry_visible(GtkTreeModel *model,
GtkTreeIter *iter,
struct mainWindow *mainWindow)
@ -780,6 +788,27 @@ void mainWindow_entrySelected(GtkWidget *widget, gpointer user_data)
}
}
/**
* @brief mainWindow_openExternalEditor opens the supplied path with the default system editor
* @param mainWindow
* @param path is the file to open
*/
void mainWindow_openExternalEditor(gchar *path)
{
GFile *file;
GAppInfo *info;
GList *files = NULL;
g_info("Opening entry in external editor.");
file = g_file_new_for_path(path);
info = g_file_query_default_handler(file, NULL, NULL);
files = g_list_append(files, (gpointer) file);
g_app_info_launch(info, files, NULL, NULL);
g_list_free(files); // Content is freed individually
g_object_unref(info);
g_object_unref(file);
}
/**
* @brief mainWindow_entryActivated is the callback when the user doubleclicks or presses enter on an entry
* @param widget is the entryListView
@ -787,9 +816,7 @@ void mainWindow_entrySelected(GtkWidget *widget, gpointer user_data)
*/
void mainWindow_entryActivated(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer user_data)
{
GFile *file;
GAppInfo *info;
GList *files = NULL;
GtkTreeModel *model;
GtkTreeIter iter;
GtkTreeView *treeview = (GtkTreeView*) widget;
@ -798,16 +825,9 @@ void mainWindow_entryActivated(GtkWidget *widget, GtkTreePath *path, GtkTreeView
model = gtk_tree_view_get_model(treeview);
if (gtk_tree_model_get_iter(model, &iter, path)) {
g_info("Opening entry in external editor.");
gtk_tree_model_get(model, &iter, COL_URL, &name, -1);
file = g_file_new_for_path(name);
info = g_file_query_default_handler(file, NULL, NULL);
files = g_list_append(files, (gpointer) file);
g_app_info_launch(info, files, NULL, NULL);
mainWindow_openExternalEditor(name);
g_free(name);
g_list_free(files); // Content is freed individually
g_object_unref(info);
g_object_unref(file);
}
}
@ -947,13 +967,15 @@ void mainWindow_switch_workspace(struct mainWindow *mainWindow, gchar *path, gbo
mdiary_reset_store(mainWindow->entryListStore, mainWindow->completionListStore);
mdiary_scanner = mdiary_scanner_new(gpg_enabled);
g_free(mainWindow->currentWorkspaceUrl);
mainWindow->currentWorkspaceUrl = g_strdup(path);
if (mdiary_scan_to_store(mdiary_scanner, path, mainWindow->entryListStore, mainWindow->completionListStore)) {
mainWindow_set_meta_information(mainWindow,
mdiary_scanner->time_earliest, mdiary_scanner->time_latest,
FALSE);
mainWindow_add_recent_workspace(mainWindow, path, FALSE);
g_free(mainWindow->currentWorkspaceUrl);
mainWindow->currentWorkspaceUrl = g_strdup(path);
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);
@ -1035,8 +1057,61 @@ void mainWindow_otherWorkspaceClicked(GtkWidget *widget, gpointer user_data)
*/
void mainWindow_new_entry_clicked(GtkWidget *widget, gpointer user_data)
{
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
void close_dialog(GtkWidget *widget, gpointer dlg)
{
gtk_dialog_response(GTK_DIALOG(dlg), GTK_RESPONSE_OK);
}
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
GtkWidget *dialog;
GtkWidget *textInput;
gchar *path;
FILE *fd;
gchar *default_path;
GDateTime *datetime;
dialog = gtk_message_dialog_new(GTK_WINDOW(mainWindow->mainWindow),
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
"Please enter the filename of the entry");
textInput = gtk_entry_new();
datetime = g_date_time_new_now_local();
default_path = g_date_time_format(datetime, "%Y/%Y-%m-%d");
gtk_entry_set_text(GTK_ENTRY(textInput), default_path);
g_free(default_path);
g_date_time_unref(datetime);
gtk_box_pack_end(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), textInput, true, true, 4);
g_signal_connect(textInput,
"activate",
(GCallback) close_dialog,
dialog);
gtk_widget_show(textInput);
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
path = (gchar*) gtk_entry_get_text(GTK_ENTRY(textInput));
if (strlen(path) > 0) {
if (path[0] != '/')
path = g_strdup_printf("%s/%s.md", mainWindow->currentWorkspaceUrl, path);
else
path = g_strdup_printf("%s.md", path);
fd = fopen(path, "r");
if (fd) {
fclose(fd);
g_warning("File '%s' already exists.", path);
} else {
fd = fopen(path, "w");
if (fd) {
fwrite(default_template, sizeof(char), strlen(default_template), fd);
fclose(fd);
mainWindow_openExternalEditor(path);
} else {
g_warning("Could not open file '%s' for writing.", path);
}
}
}
}
gtk_widget_destroy(GTK_WIDGET(dialog));
// Get ws path
// Ask user for filename
// Create file and paste template

View File

@ -101,6 +101,7 @@ void mainWindow_workspaceListClicked(GtkWidget *widget,
void mainWindow_clearSearch(GtkWidget *widget, gpointer user_data);
void mainWindow_calendarSelected(GtkWidget *widget, gpointer user_data);
void mainWindow_entrySelected(GtkWidget *widget, gpointer user_data);
void mainWindow_openExternalEditor(gchar *path);
void mainWindow_entryActivated(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer user_data);
void mainWindow_filterChanged(GtkWidget *widget, gpointer user_data);
void mainWindow_workspace_search_changed(GtkWidget *widget, gpointer user_data);