Automatically fill current date and time when creating a new entry

This commit is contained in:
Markus Koch 2018-03-17 12:47:52 +01:00
parent 247ea6977f
commit 871669f904

View File

@ -1,13 +1,14 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "mdiary.h" #include "mdiary.h"
const gchar *default_template = "\ const gchar *default_template[] = {"# Title\n"
# Title\n\ "Date: ", "\0",
Date: 01.01.2000 00:00\n\ "\n"
Summary: \n\ "Summary: \n"
Tags: \n\ "Tags: \n"
\n\ "\n"
Text"; "Text",
NULL};
static gboolean mainWindow_workspace_entry_visible(GtkTreeModel *model, static gboolean mainWindow_workspace_entry_visible(GtkTreeModel *model,
GtkTreeIter *iter, GtkTreeIter *iter,
@ -1072,6 +1073,7 @@ void mainWindow_new_entry_clicked(GtkWidget *widget, gpointer user_data)
FILE *fd; FILE *fd;
gchar *default_path; gchar *default_path;
GDateTime *datetime; GDateTime *datetime;
int i;
dialog = gtk_message_dialog_new(GTK_WINDOW(mainWindow->mainWindow), dialog = gtk_message_dialog_new(GTK_WINDOW(mainWindow->mainWindow),
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
@ -1082,7 +1084,6 @@ void mainWindow_new_entry_clicked(GtkWidget *widget, gpointer user_data)
default_path = g_date_time_format(datetime, "%Y/%Y-%m-%d"); default_path = g_date_time_format(datetime, "%Y/%Y-%m-%d");
gtk_entry_set_text(GTK_ENTRY(textInput), default_path); gtk_entry_set_text(GTK_ENTRY(textInput), default_path);
g_free(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); gtk_box_pack_end(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), textInput, true, true, 4);
g_signal_connect(textInput, g_signal_connect(textInput,
"activate", "activate",
@ -1104,7 +1105,15 @@ void mainWindow_new_entry_clicked(GtkWidget *widget, gpointer user_data)
} else { } else {
fd = fopen(path, "w"); fd = fopen(path, "w");
if (fd) { if (fd) {
fwrite(default_template, sizeof(char), strlen(default_template), fd); for (i = 0; default_template[i] != NULL; ++i) {
if (default_template[i][0] == '\0') {
default_path = g_date_time_format(datetime, "%d.%m.%Y %H:%M");
fwrite(default_path, sizeof(char), strlen(default_path), fd);
g_free(default_path);
} else {
fwrite(default_template[i], sizeof(char), strlen(default_template[i]), fd);
}
}
fclose(fd); fclose(fd);
mainWindow_openExternalEditor(path); mainWindow_openExternalEditor(path);
} else { } else {
@ -1114,6 +1123,8 @@ void mainWindow_new_entry_clicked(GtkWidget *widget, gpointer user_data)
} }
} }
g_date_time_unref(datetime);
gtk_widget_destroy(GTK_WIDGET(dialog)); gtk_widget_destroy(GTK_WIDGET(dialog));
// Get ws path // Get ws path
// Ask user for filename // Ask user for filename