diff --git a/src/mainwindow.c b/src/mainwindow.c index 47ca81a..fd38543 100644 --- a/src/mainwindow.c +++ b/src/mainwindow.c @@ -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) @@ -959,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); @@ -1047,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