From 246832231856f3873d12b0702c02019bab281938 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sun, 1 Apr 2018 21:00:27 +0200 Subject: Use GFile to save presentations --- src/narrative_window.c | 21 ++++++++++++++++++--- src/presentation.c | 24 ++++++++++++------------ src/sc_parse.c | 14 +++++++++++--- src/sc_parse.h | 3 ++- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/narrative_window.c b/src/narrative_window.c index 42084b8..1b26b62 100644 --- a/src/narrative_window.c +++ b/src/narrative_window.c @@ -58,6 +58,22 @@ struct _narrative_window }; +static void show_error(NarrativeWindow *nw, const char *err) +{ + GtkWidget *mw; + + mw = gtk_message_dialog_new(GTK_WINDOW(nw->window), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_ERROR, + GTK_BUTTONS_CLOSE, err); + + g_signal_connect_swapped(mw, "response", + G_CALLBACK(gtk_widget_destroy), mw); + + gtk_widget_show(mw); +} + + static void update_toolbar(NarrativeWindow *nw) { int cur_para; @@ -89,7 +105,7 @@ static gint saveas_response_sig(GtkWidget *d, gint response, GFile *file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(d)); if ( save_presentation(nw->p, file) ) { - //show_error(sw, "Failed to save presentation"); + show_error(nw, "Failed to save presentation"); } g_object_unref(file); @@ -132,8 +148,7 @@ static void save_sig(GSimpleAction *action, GVariant *parameter, gpointer vp) return saveas_sig(NULL, NULL, nw); } - /* FIXME: Do this properly with GFile */ - file = g_file_new_for_path(nw->p->filename); + file = g_file_new_for_uri(nw->p->filename); save_presentation(nw->p, file); g_object_unref(file); } diff --git a/src/presentation.c b/src/presentation.c index d338a1a..bc0089f 100644 --- a/src/presentation.c +++ b/src/presentation.c @@ -127,22 +127,22 @@ struct presentation *new_presentation(const char *imagestore) int save_presentation(struct presentation *p, GFile *file) { - FILE *fh; - char *filename; - - /* FIXME: Do this properly using GFile */ - filename = g_file_get_path(file); - printf("Saving to %s\n", filename); + GFileOutputStream *fh; + int r; + GError *error = NULL; - fh = fopen(filename, "w"); - if ( fh == NULL ) 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 = save_sc_block(G_OUTPUT_STREAM(fh), p->scblocks); + g_object_unref(fh); - save_sc_block(fh, p->scblocks); + if ( r ) return 1; imagestore_set_parent(p->is, g_file_get_parent(file)); - p->filename = strdup(filename); - - fclose(fh); + p->filename = g_file_get_uri(file); p->saved = 1; update_titlebar(p->narrative_window); return 0; diff --git a/src/sc_parse.c b/src/sc_parse.c index 5a653cf..3212872 100644 --- a/src/sc_parse.c +++ b/src/sc_parse.c @@ -31,6 +31,7 @@ #include #include #include +#include #include "sc_parse.h" @@ -410,18 +411,25 @@ char *serialise_sc_block(const SCBlock *bl) } -void save_sc_block(FILE *fh, const SCBlock *bl) +int save_sc_block(GOutputStream *fh, const SCBlock *bl) { while ( bl != NULL ) { + GError *error = NULL; char *a = serialise_sc_block(bl); + gssize r; if ( a == NULL ) { fprintf(stderr, "Failed to serialise block\n"); - return; + return 1; + } + r = g_output_stream_write(fh, a, strlen(a), NULL, &error); + if ( r == -1 ) { + fprintf(stderr, "Write failed: %s\n", error->message); + return 1; } - fputs(a, fh); free(a); bl = bl->next; } + return 0; } diff --git a/src/sc_parse.h b/src/sc_parse.h index 6e23d97..981299f 100644 --- a/src/sc_parse.h +++ b/src/sc_parse.h @@ -28,6 +28,7 @@ #endif #include +#include typedef struct _scblock SCBlock; @@ -83,7 +84,7 @@ extern void show_sc_blocks(const SCBlock *bl); extern void show_sc_block(const SCBlock *bl, const char *prefix); extern char *serialise_sc_block(const SCBlock *bl); -extern void save_sc_block(FILE *fh, const SCBlock *bl); +extern int save_sc_block(GOutputStream *fh, const SCBlock *bl); extern size_t scblock_delete_text(SCBlock *b, ssize_t o1, ssize_t o2); -- cgit v1.2.3