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 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) (limited to 'libstorycode/narrative.c') 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; } -- cgit v1.2.3