From d92d4d2767585e1956f2ce616ed795f2b1af3cc3 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sun, 31 Mar 2019 13:53:42 +0200 Subject: Saving hooks --- libstorycode/narrative.c | 52 ++++++++++++++++++++++++++++++++++++++++++++---- src/colloquium.c | 4 ++-- src/narrative_window.c | 32 +++++++++++++++++++++++++---- src/narrative_window.h | 3 ++- 4 files changed, 80 insertions(+), 11 deletions(-) diff --git a/libstorycode/narrative.c b/libstorycode/narrative.c index 19797e3..287e181 100644 --- a/libstorycode/narrative.c +++ b/libstorycode/narrative.c @@ -31,6 +31,9 @@ #include #include +#include +#define _(x) gettext(x) + #ifdef HAVE_PANGO #include #endif @@ -54,7 +57,7 @@ Narrative *narrative_new() n->n_items = 0; n->items = NULL; n->stylesheet = NULL; - n->imagestore = NULL; + n->imagestore = imagestore_new("."); /* FIXME: From app config */ n->saved = 1; #ifdef HAVE_PANGO n->language = pango_language_to_string(pango_language_get_default()); @@ -107,16 +110,57 @@ Narrative *narrative_load(GFile *file) g_bytes_unref(bytes); if ( n == NULL ) return NULL; - n->imagestore = imagestore_new("."); /* FIXME: From app config */ imagestore_set_parent(n->imagestore, g_file_get_parent(file)); return n; } +static int write_narrative(GOutputStream *fh, Narrative *n) +{ + int i; + + for ( i=0; in_items; i++ ) { + + gssize r; + GError *error = NULL; + char *a = "Hello"; + + r = g_output_stream_write(fh, a, strlen(a), NULL, &error); + if ( r == -1 ) { + fprintf(stderr, "Write failed: %s\n", error->message); + return 1; + } + } + return 0; +} + + int narrative_save(Narrative *n, GFile *file) { - /* FIXME: Implementation */ - return 1; + GFileOutputStream *fh; + int r; + GError *error = NULL; + + if ( file == NULL ) { + fprintf(stderr, "Saving to NULL!\n"); + return 1; + } + + fh = g_file_replace(file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error); + if ( fh == NULL ) { + fprintf(stderr, _("Open failed: %s\n"), error->message); + return 1; + } + r = write_narrative(G_OUTPUT_STREAM(fh), n); + if ( r ) { + fprintf(stderr, _("Couldn't save presentation\n")); + } + g_object_unref(fh); + + imagestore_set_parent(n->imagestore, g_file_get_parent(file)); + + n->saved = 1; + return 0; } diff --git a/src/colloquium.c b/src/colloquium.c index 65647ad..d3ada61 100644 --- a/src/colloquium.c +++ b/src/colloquium.c @@ -67,7 +67,7 @@ static void colloquium_activate(GApplication *papp) ss = stylesheet_new(); narrative_add_text(n, strdup("")); narrative_add_stylesheet(n, ss); - narrative_window_new(n, papp); + narrative_window_new(n, NULL, papp); } } @@ -231,7 +231,7 @@ static void colloquium_open(GApplication *papp, GFile **files, gint n_files, Narrative *n; n = narrative_load(files[i]); if ( n != NULL ) { - narrative_window_new(n, papp); + narrative_window_new(n, files[i], papp); } else { char *uri = g_file_get_uri(files[i]); fprintf(stderr, _("Failed to load presentation '%s'\n"), diff --git a/src/narrative_window.c b/src/narrative_window.c index 772c886..2df2c1e 100644 --- a/src/narrative_window.c +++ b/src/narrative_window.c @@ -80,12 +80,24 @@ static void show_error(NarrativeWindow *nw, const char *err) } +static char *get_titlebar_string(NarrativeWindow *nw) +{ + if ( nw == NULL || nw->file == NULL ) { + return strdup(_("(untitled)")); + } else { + char *bn = g_file_get_basename(nw->file); + return bn; + } +} + + + static void update_titlebar(NarrativeWindow *nw) { char *title; char *title_new; - title = strdup("test"); // FIXME get_titlebar_string(nw->p); + title = get_titlebar_string(nw); title_new = realloc(title, strlen(title)+16); if ( title_new == NULL ) { free(title); @@ -139,9 +151,15 @@ static gint saveas_response_sig(GtkWidget *d, gint response, if ( narrative_save(nw->n, file) ) { show_error(nw, _("Failed to save presentation")); + } else { + update_titlebar(nw); } - /* save_narrative keeps a reference to both of these */ + if ( nw->file != file ) { + if ( nw->file != NULL ) g_object_unref(nw->file); + nw->file = file; + g_object_ref(nw->file); + } g_object_unref(file); } @@ -190,7 +208,11 @@ static void save_sig(GSimpleAction *action, GVariant *parameter, gpointer vp) return saveas_sig(NULL, NULL, nw); } - narrative_save(nw->n, nw->file); + if ( narrative_save(nw->n, nw->file) ) { + show_error(nw, _("Failed to save presentation")); + } else { + update_titlebar(nw); + } } @@ -688,7 +710,7 @@ GActionEntry nw_entries[] = { //} -NarrativeWindow *narrative_window_new(Narrative *n, GApplication *papp) +NarrativeWindow *narrative_window_new(Narrative *n, GFile *file, GApplication *papp) { NarrativeWindow *nw; GtkWidget *vbox; @@ -704,6 +726,8 @@ NarrativeWindow *narrative_window_new(Narrative *n, GApplication *papp) nw->app = papp; nw->n = n; nw->n_slidewindows = 0; + nw->file = file; + g_object_ref(file); nw->window = gtk_application_window_new(GTK_APPLICATION(app)); update_titlebar(nw); diff --git a/src/narrative_window.h b/src/narrative_window.h index a2c769c..5a7646d 100644 --- a/src/narrative_window.h +++ b/src/narrative_window.h @@ -29,6 +29,7 @@ typedef struct _narrative_window NarrativeWindow; -extern NarrativeWindow *narrative_window_new(Narrative *n, GApplication *app); +extern NarrativeWindow *narrative_window_new(Narrative *n, GFile *file, + GApplication *papp); #endif /* NARRATIVE_WINDOW_H */ -- cgit v1.2.3