Save the stylesheet
authorThomas White <taw@bitwiz.me.uk>
Mon, 29 Oct 2018 21:43:54 +0000 (22:43 +0100)
committerThomas White <taw@bitwiz.me.uk>
Mon, 29 Oct 2018 21:43:54 +0000 (22:43 +0100)
data/savepresentation.ui
src/narrative_window.c
src/presentation.c
src/presentation.h
src/stylesheet.c
src/stylesheet.h

index 5597377..32f43be 100644 (file)
@@ -74,7 +74,7 @@
               </packing>
             </child>
             <child>
-              <object class="GtkRadioButton" id="radiobutton1">
+              <object class="GtkRadioButton" id="privatess">
                 <property name="label" translatable="yes">Create/update private stylesheet for this presentation</property>
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
               </packing>
             </child>
             <child>
-              <object class="GtkRadioButton">
+              <object class="GtkRadioButton" id="folderss">
                 <property name="label" translatable="yes">Create/update default stylesheet for presentations in folder</property>
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
                 <property name="receives_default">False</property>
                 <property name="active">True</property>
                 <property name="draw_indicator">True</property>
-                <property name="group">radiobutton1</property>
+                <property name="group">privatess</property>
               </object>
               <packing>
                 <property name="expand">False</property>
               </packing>
             </child>
             <child>
-              <object class="GtkRadioButton">
+              <object class="GtkRadioButton" id="noss">
                 <property name="label" translatable="yes">Don't save stylesheet at all</property>
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
                 <property name="receives_default">False</property>
                 <property name="active">True</property>
                 <property name="draw_indicator">True</property>
-                <property name="group">radiobutton1</property>
+                <property name="group">privatess</property>
               </object>
               <packing>
                 <property name="expand">False</property>
index 123535d..3ff369c 100644 (file)
@@ -103,6 +103,11 @@ struct saveas_info
 {
        NarrativeWindow *nw;
        GtkWidget *filechooser;
+
+       /* Radio buttons for how to save stylesheet */
+       GtkWidget *privatess;
+       GtkWidget *folderss;
+       GtkWidget *noss;
 };
 
 
@@ -112,12 +117,38 @@ static gint saveas_response_sig(GtkWidget *d, gint response,
        if ( response == 1 ) {  /* hard-coded number in Glade file */
 
                GFile *file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(si->filechooser));
+               GFile *ssfile = NULL;
+
+               if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(si->privatess)) ) {
+                       gchar *ssuri;
+                       ssuri = g_file_get_uri(file);
+                       if ( ssuri != NULL ) {
+                               size_t l = strlen(ssuri);
+                               if ( ssuri[l-3] == '.' && ssuri[l-2] == 's' && ssuri[l-1] =='c' ) {
+                                       ssuri[l-1] = 's';
+                                       ssfile = g_file_new_for_uri(ssuri);
+                                       g_free(ssuri);
+                               }
+                       }
+               } else if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(si->folderss)) ) {
+                       GFile *parent;
+                       parent = g_file_get_parent(file);
+                       if ( parent != NULL ) {
+                               ssfile = g_file_get_child(parent, "stylesheet.ss");
+                               g_object_unref(parent);
+                       }
+               } else if ( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(si->noss)) ) {
+                       /* Do nothing */
+               } else {
+                       fprintf(stderr, _("Couldn't determine how to save stylesheet!\n"));
+               }
 
-               if ( save_presentation(si->nw->p, file) ) {
+               if ( save_presentation(si->nw->p, file, ssfile) ) {
                        show_error(si->nw, _("Failed to save presentation"));
                }
 
                g_object_unref(file);
+               g_object_unref(ssfile);
 
        }
 
@@ -146,6 +177,9 @@ static void saveas_sig(GSimpleAction *action, GVariant *parameter, gpointer vp)
        gtk_builder_connect_signals(builder, si);
        d = GTK_WIDGET(gtk_builder_get_object(builder, "savepresentation"));
        si->filechooser = GTK_WIDGET(gtk_builder_get_object(builder, "filechooser"));
+       si->privatess = GTK_WIDGET(gtk_builder_get_object(builder, "privatess"));
+       si->folderss = GTK_WIDGET(gtk_builder_get_object(builder, "folderss"));
+       si->noss = GTK_WIDGET(gtk_builder_get_object(builder, "noss"));
        gtk_window_set_transient_for(GTK_WINDOW(d), GTK_WINDOW(nw->window));
 
        gtk_widget_show_all(d);
@@ -169,7 +203,7 @@ static void save_sig(GSimpleAction *action, GVariant *parameter, gpointer vp)
        }
 
        file = g_file_new_for_uri(nw->p->uri);
-       save_presentation(nw->p, file);
+       save_presentation(nw->p, file, nw->p->stylesheet_from);
        g_object_unref(file);
 }
 
index eab0f5a..810293d 100644 (file)
@@ -83,7 +83,7 @@ static void find_and_load_stylesheet(struct presentation *p, GFile *file)
                                ssuri[l-1] = 's';
                                ssfile = g_file_new_for_uri(ssuri);
                                p->stylesheet = stylesheet_load(ssfile);
-                               g_object_unref(ssfile);
+                               p->stylesheet_from = ssfile;
                                g_free(ssuri);
                        }
                }
@@ -95,8 +95,9 @@ static void find_and_load_stylesheet(struct presentation *p, GFile *file)
                                ssfile = g_file_get_child(parent, "stylesheet.ss");
                                if ( ssfile != NULL ) {
                                        p->stylesheet = stylesheet_load(ssfile);
-                                       g_object_unref(ssfile);
+                                       p->stylesheet_from = ssfile;
                                }
+                               g_object_unref(parent);
                        }
                }
 
@@ -106,13 +107,14 @@ static void find_and_load_stylesheet(struct presentation *p, GFile *file)
        if ( p->stylesheet == NULL ) {
                ssfile = g_file_new_for_path("./stylesheet.ss");
                p->stylesheet = stylesheet_load(ssfile);
-               g_object_unref(ssfile);
+               p->stylesheet_from = ssfile;
        }
 
        /* Fourth choice: internal default stylesheet */
        if ( p->stylesheet == NULL ) {
                ssfile = g_file_new_for_uri("resource:///uk/me/bitwiz/Colloquium/default.ss");
                p->stylesheet = stylesheet_load(ssfile);
+               p->stylesheet_from = NULL;
                g_object_unref(ssfile);
        }
 
@@ -148,10 +150,11 @@ struct presentation *new_presentation(const char *imagestore)
 }
 
 
-int save_presentation(struct presentation *p, GFile *file)
+int save_presentation(struct presentation *p, GFile *file, GFile *ssfile)
 {
        GFileOutputStream *fh;
        int r;
+       int sr;
        GError *error = NULL;
 
        fh = g_file_replace(file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
@@ -160,9 +163,22 @@ int save_presentation(struct presentation *p, GFile *file)
                return 1;
        }
        r = save_sc_block(G_OUTPUT_STREAM(fh), p->scblocks);
+       if ( r ) {
+               fprintf(stderr, _("Couldn't save presentation\n"));
+       }
        g_object_unref(fh);
 
-       if ( r ) return 1;
+       if ( ssfile != NULL ) {
+               sr = stylesheet_save(p->stylesheet, ssfile);
+               if ( sr ) {
+                       fprintf(stderr, _("Couldn't save stylesheet\n"));
+               }
+       } else {
+               fprintf(stderr, _("Not updating default stylesheet\n"));
+               sr = 0;
+       }
+
+       if ( r || sr ) return 1;
 
        imagestore_set_parent(p->is, g_file_get_parent(file));
        p->uri = g_file_get_uri(file);
index ea3780e..57542ca 100644 (file)
@@ -41,6 +41,7 @@ struct menu_pl;
 struct presentation
 {
        char             *uri;
+       GFile            *stylesheet_from;
        int               completely_empty;
        int               saved;
        PangoLanguage    *lang;
@@ -59,7 +60,6 @@ struct presentation
 
        SCBlock          *scblocks;
        Stylesheet       *stylesheet;
-
 };
 
 
@@ -76,7 +76,7 @@ extern SCBlock *next_slide(struct presentation *p, SCBlock *sl);
 extern SCBlock *prev_slide(struct presentation *p, SCBlock *sl);
 
 extern int load_presentation(struct presentation *p, GFile *file);
-extern int save_presentation(struct presentation *p, GFile *file);
+extern int save_presentation(struct presentation *p, GFile *file, GFile *ssfile);
 
 #define UNUSED __attribute__((unused))
 
index 96172a4..dbafd8c 100644 (file)
@@ -232,3 +232,28 @@ void stylesheet_free(Stylesheet *ss)
        free(ss);
 }
 
+
+int stylesheet_save(Stylesheet *ss, GFile *file)
+{
+       JsonGenerator *gen;
+       GError *error = NULL;
+       GFileOutputStream *fh;
+
+       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;
+       }
+
+       gen = json_generator_new();
+       json_generator_set_root(gen, ss->root);
+       json_generator_set_pretty(gen, TRUE);
+       json_generator_set_indent(gen, 1);
+       json_generator_set_indent_char(gen, '\t');
+       if ( !json_generator_to_stream(gen, G_OUTPUT_STREAM(fh), NULL, &error) ) {
+               fprintf(stderr, _("Open failed: %s\n"), error->message);
+               return 1;
+       }
+       g_object_unref(fh);
+       return 0;
+}
index c24c62c..537b2f9 100644 (file)
@@ -34,6 +34,8 @@ typedef struct _stylesheet Stylesheet;
 
 extern Stylesheet *stylesheet_load(GFile *file);
 
+extern int stylesheet_save(Stylesheet *ss, GFile *file);
+
 extern int parse_colour_duo(const char *a, GdkRGBA *col1, GdkRGBA *col2);
 
 extern char *stylesheet_lookup(Stylesheet *ss, const char *path, const char *key);