diff options
author | Thomas White <taw@bitwiz.org.uk> | 2017-11-26 20:02:53 +0100 |
---|---|---|
committer | Thomas White <taw@bitwiz.org.uk> | 2017-11-26 20:02:53 +0100 |
commit | 2bf28d3b9de3ae3af076c569ffaccb66e13857f1 (patch) | |
tree | dd2ef413583e63ea589019fe687b0b599059bd34 | |
parent | 86e80d221809201f4cab608c5312bce8c76618a8 (diff) |
Use config file for image store pathname
-rw-r--r-- | src/colloquium.c | 92 | ||||
-rw-r--r-- | src/colloquium.h | 4 | ||||
-rw-r--r-- | src/imagestore.c | 50 | ||||
-rw-r--r-- | src/imagestore.h | 2 | ||||
-rw-r--r-- | src/narrative_window.c | 12 | ||||
-rw-r--r-- | src/presentation.c | 4 | ||||
-rw-r--r-- | src/presentation.h | 2 | ||||
-rw-r--r-- | src/print.c | 6 | ||||
-rw-r--r-- | src/sc_editor.c | 4 | ||||
-rw-r--r-- | src/sc_editor.h | 2 | ||||
-rw-r--r-- | src/slide_window.c | 9 |
11 files changed, 156 insertions, 31 deletions
diff --git a/src/colloquium.c b/src/colloquium.c index 18f3b00..c5716d9 100644 --- a/src/colloquium.c +++ b/src/colloquium.c @@ -28,6 +28,7 @@ #include <gtk/gtk.h> #include <getopt.h> #include <string.h> +#include <stdlib.h> #include <sys/stat.h> #include <glib.h> #include <glib/gstdio.h> @@ -42,6 +43,7 @@ struct _colloquium GtkApplication parent_instance; char *mydir; int first_run; + char *imagestore; }; @@ -56,7 +58,7 @@ static void colloquium_activate(GApplication *papp) Colloquium *app = COLLOQUIUM(papp); if ( !app->first_run ) { struct presentation *p; - p = new_presentation(); + p = new_presentation(app->imagestore); narrative_window_new(p, papp); } } @@ -116,17 +118,18 @@ GActionEntry app_entries[] = { }; -static void colloquium_open(GApplication *app, GFile **files, gint n_files, +static void colloquium_open(GApplication *papp, GFile **files, gint n_files, const gchar *hint) { int i; + Colloquium *app = COLLOQUIUM(papp); for ( i = 0; i<n_files; i++ ) { struct presentation *p; char *uri = g_file_get_path(files[i]); - p = new_presentation(); + p = new_presentation(app->imagestore); if ( load_presentation(p, uri) == 0 ) { - narrative_window_new(p, app); + narrative_window_new(p, papp); } else { fprintf(stderr, "Failed to load '%s'\n", uri); } @@ -141,11 +144,75 @@ static void colloquium_finalize(GObject *object) } +static void create_config(const char *filename) +{ + + FILE *fh; + fh = fopen(filename, "w"); + if ( fh == NULL ) { + fprintf(stderr, "Failed to create config\n"); + return; + } + + fprintf(fh, "imagestore: %s\n", + g_get_user_special_dir(G_USER_DIRECTORY_PICTURES)); + + fclose(fh); +} + + +static void chomp(char *s) +{ + size_t i; + + if ( !s ) return; + + for ( i=0; i<strlen(s); i++ ) { + if ( (s[i] == '\n') || (s[i] == '\r') ) { + s[i] = '\0'; + return; + } + } +} + + +static void read_config(const char *filename, Colloquium *app) +{ + FILE *fh; + char line[1024]; + + fh = fopen(filename, "r"); + if ( fh == NULL ) { + fprintf(stderr, "Failed to open %s\n", filename); + return; + } + + if ( fgets(line, 1024, fh) == NULL ) { + fprintf(stderr, "Failed to read from config\n"); + return; + } + chomp(line); + + if ( strncmp(line, "imagestore: ", 11) == 0 ) { + app->imagestore = strdup(line+12); + } + + fclose(fh); +} + + +const char *colloquium_get_imagestore(Colloquium *app) +{ + return app->imagestore; +} + + static void colloquium_startup(GApplication *papp) { Colloquium *app = COLLOQUIUM(papp); GtkBuilder *builder; const char *configdir; + char *tmp; G_APPLICATION_CLASS(colloquium_parent_class)->startup(papp); @@ -324,6 +391,23 @@ static void colloquium_startup(GApplication *papp) fprintf(stderr, "Failed to create folder\n"); } } + + /* Read config file */ + tmp = malloc(strlen(app->mydir)+32); + if ( tmp != NULL ) { + + tmp[0] = '\0'; + strcat(tmp, app->mydir); + strcat(tmp, "/config"); + + /* Create default config file if it doesn't exist */ + if ( !g_file_test(tmp, G_FILE_TEST_EXISTS) ) { + create_config(tmp); + } + + read_config(tmp, app); + free(tmp); + } } diff --git a/src/colloquium.h b/src/colloquium.h index a9b9245..5b4677c 100644 --- a/src/colloquium.h +++ b/src/colloquium.h @@ -1,7 +1,7 @@ /* * colloquium.h * - * Copyright © 2014 Thomas White <taw@bitwiz.org.uk> + * Copyright © 2014-2017 Thomas White <taw@bitwiz.org.uk> * * This file is part of Colloquium. * @@ -36,4 +36,6 @@ typedef struct _colloquium Colloquium; GTK_TYPE_APPLICATION, Colloquium)) +const char *colloquium_get_imagestore(Colloquium *app); + #endif /* COLLOQUIUM_H */ diff --git a/src/imagestore.c b/src/imagestore.c index e84406f..2831d38 100644 --- a/src/imagestore.c +++ b/src/imagestore.c @@ -52,6 +52,7 @@ struct _imagestore struct image_record *images; int max_images; char *dname; + const char *storename; }; @@ -69,7 +70,7 @@ static int alloc_images(ImageStore *is, int new_max_images) } -ImageStore *imagestore_new() +ImageStore *imagestore_new(const char *storename) { ImageStore *is; @@ -78,6 +79,7 @@ ImageStore *imagestore_new() is->images = NULL; is->dname = NULL; + is->storename = storename; is->n_images = 0; is->max_images = 0; if ( alloc_images(is, 32) ) { @@ -127,6 +129,8 @@ static cairo_surface_t *pixbuf_to_surface(GdkPixbuf *t) cairo_t *cr; int w, h; + if ( t == NULL ) return NULL; + w = gdk_pixbuf_get_width(t); h = gdk_pixbuf_get_height(t); @@ -147,23 +151,49 @@ static cairo_surface_t *pixbuf_to_surface(GdkPixbuf *t) } -static cairo_surface_t *try_all_locations(const char *filename, int w) +static cairo_surface_t *try_all_locations(const char *filename, + const char *dname, const char *iname, + int w) { GError *error = NULL; GdkPixbuf *t; t = gdk_pixbuf_new_from_file_at_size(filename, w, -1, &error); - if ( t == NULL ) return NULL; - /* FIXME: Restore searching in pre-defined folder, - * hence (or otherwise) providing for relocation of presentation files */ + /* Try the file prefixed with the directory the presentation is in */ + if ( (t == NULL) && (dname != NULL) ) { + char *tmp; + error = NULL; + tmp = malloc(strlen(filename) + strlen(dname) + 2); + if ( tmp == NULL ) return NULL; + strcpy(tmp, dname); + strcat(tmp, "/"); + strcat(tmp, filename); + t = gdk_pixbuf_new_from_file_at_size(tmp, w, -1, &error); + free(tmp); + } + + /* Try prefixing with imagestore folder */ + if ( (t == NULL) && (iname != NULL) ) { + char *tmp; + error = NULL; + tmp = malloc(strlen(filename) + strlen(iname) + 2); + if ( tmp == NULL ) return NULL; + strcpy(tmp, iname); + strcat(tmp, "/"); + strcat(tmp, filename); + t = gdk_pixbuf_new_from_file_at_size(tmp, w, -1, &error); + free(tmp); + } return pixbuf_to_surface(t); } static cairo_surface_t *add_image_size(struct image_record *im, - const char *filename, int w) + const char *filename, + const char *dname, const char *iname, + int w) { cairo_surface_t *surf; @@ -173,7 +203,7 @@ static cairo_surface_t *add_image_size(struct image_record *im, return NULL; } - surf = try_all_locations(filename, w); + surf = try_all_locations(filename, dname, iname, w); if ( surf == NULL ) return NULL; /* Add surface to list */ @@ -201,7 +231,8 @@ static cairo_surface_t *add_new_image(ImageStore *is, const char *filename, int is->images[idx].n_sizes = 0; is->images[idx].filename = strdup(filename); - return add_image_size(&is->images[idx], filename, w); + return add_image_size(&is->images[idx], filename, is->dname, + is->storename, w); } @@ -241,6 +272,7 @@ cairo_surface_t *lookup_image(ImageStore *is, const char *filename, int w) } /* We don't have this size yet */ - surf = add_image_size(&is->images[i], filename, w); + surf = add_image_size(&is->images[i], filename, is->dname, + is->storename, w); return surf; } diff --git a/src/imagestore.h b/src/imagestore.h index d2ab7f7..016b2e3 100644 --- a/src/imagestore.h +++ b/src/imagestore.h @@ -32,7 +32,7 @@ typedef struct _imagestore ImageStore; -extern ImageStore *imagestore_new(void); +extern ImageStore *imagestore_new(const char *storename); extern void imagestore_destroy(ImageStore *is); diff --git a/src/narrative_window.c b/src/narrative_window.c index 72968c9..1552845 100644 --- a/src/narrative_window.c +++ b/src/narrative_window.c @@ -30,6 +30,7 @@ #include <string.h> #include <stdlib.h> +#include "colloquium.h" #include "presentation.h" #include "narrative_window.h" #include "sc_editor.h" @@ -692,7 +693,7 @@ void update_titlebar(NarrativeWindow *nw) } -NarrativeWindow *narrative_window_new(struct presentation *p, GApplication *app) +NarrativeWindow *narrative_window_new(struct presentation *p, GApplication *papp) { NarrativeWindow *nw; GtkWidget *vbox; @@ -702,6 +703,7 @@ NarrativeWindow *narrative_window_new(struct presentation *p, GApplication *app) SCBlock **stylesheets; SCCallbackList *cbl; GtkWidget *image; + Colloquium *app = COLLOQUIUM(papp); if ( p->narrative_window != NULL ) { fprintf(stderr, "Narrative window is already open!\n"); @@ -711,7 +713,7 @@ NarrativeWindow *narrative_window_new(struct presentation *p, GApplication *app) nw = calloc(1, sizeof(NarrativeWindow)); if ( nw == NULL ) return NULL; - nw->app = app; + nw->app = papp; nw->p = p; nw->window = gtk_application_window_new(GTK_APPLICATION(app)); @@ -734,7 +736,8 @@ NarrativeWindow *narrative_window_new(struct presentation *p, GApplication *app) nw->p->scblocks = sc_parse(""); } - nw->sceditor = sc_editor_new(nw->p->scblocks, stylesheets, p->lang); + nw->sceditor = sc_editor_new(nw->p->scblocks, stylesheets, p->lang, + colloquium_get_imagestore(app)); free(stylesheets); cbl = sc_callback_list_new(); sc_callback_list_add_callback(cbl, "sthumb", create_thumbnail, @@ -823,8 +826,7 @@ NarrativeWindow *narrative_window_new(struct presentation *p, GApplication *app) GTK_WIDGET(nw->sceditor)); gtk_widget_show_all(nw->window); - nw->app = app; - g_application_hold(app); + g_application_hold(papp); return nw; } diff --git a/src/presentation.c b/src/presentation.c index efdf694..01cbe1a 100644 --- a/src/presentation.c +++ b/src/presentation.c @@ -98,7 +98,7 @@ char *get_titlebar_string(struct presentation *p) } -struct presentation *new_presentation() +struct presentation *new_presentation(const char *imagestore) { struct presentation *new; @@ -116,7 +116,7 @@ struct presentation *new_presentation() new->completely_empty = 1; new->saved = 1; new->stylesheet = NULL; - new->is = imagestore_new(); + new->is = imagestore_new(imagestore); /* FIXME: Hardcoded */ new->lang = pango_language_from_string("en_GB"); diff --git a/src/presentation.h b/src/presentation.h index a639c59..bf2ea81 100644 --- a/src/presentation.h +++ b/src/presentation.h @@ -68,7 +68,7 @@ struct presentation }; -extern struct presentation *new_presentation(void); +extern struct presentation *new_presentation(const char *imagestore); extern char *load_everything(const char *filename); extern SCBlock *find_stylesheet(SCBlock *bl); extern int replace_stylesheet(struct presentation *p, SCBlock *ss); diff --git a/src/print.c b/src/print.c index e33bc39..0f3e7ff 100644 --- a/src/print.c +++ b/src/print.c @@ -51,8 +51,10 @@ struct print_stuff /* When printing narrative */ int nar_line; struct frame *top; - ImageStore *is; int start_paras[256]; + + ImageStore *is; + const char *storename; }; @@ -188,7 +190,7 @@ static void begin_narrative_print(GtkPrintOperation *op, GtkPrintContext *ctx, sc_callback_list_add_callback(cbl, "sthumb", create_thumbnail, render_thumbnail, NULL, ps->p); - ps->is = imagestore_new(); + ps->is = imagestore_new(ps->storename); if ( ps->p->stylesheet != NULL ) { stylesheets[0] = ps->p->stylesheet; diff --git a/src/sc_editor.c b/src/sc_editor.c index bd9a7b3..15ac49b 100644 --- a/src/sc_editor.c +++ b/src/sc_editor.c @@ -1963,7 +1963,7 @@ int sc_editor_get_num_paras(SCEditor *e) SCEditor *sc_editor_new(SCBlock *scblocks, SCBlock **stylesheets, - PangoLanguage *lang) + PangoLanguage *lang, const char *storename) { SCEditor *sceditor; GtkTargetEntry targets[1]; @@ -1977,7 +1977,7 @@ SCEditor *sc_editor_new(SCBlock *scblocks, SCBlock **stylesheets, sceditor->log_h = 100; sceditor->border_offs_x = 0; sceditor->border_offs_y = 0; - sceditor->is = imagestore_new(); + sceditor->is = imagestore_new(storename); sceditor->slidenum = 0; sceditor->min_border = 0.0; sceditor->top_editable = 0; diff --git a/src/sc_editor.h b/src/sc_editor.h index b40207d..3c836a6 100644 --- a/src/sc_editor.h +++ b/src/sc_editor.h @@ -172,7 +172,7 @@ extern void sc_editor_set_stylesheets(SCEditor *e, SCBlock **stylesheets); extern SCBlock *sc_editor_get_scblock(SCEditor *e); extern GtkWidget *sc_editor_get_widget(SCEditor *e); extern SCEditor *sc_editor_new(SCBlock *scblocks, SCBlock **stylesheets, - PangoLanguage *lang); + PangoLanguage *lang, const char *storename); extern void sc_editor_set_size(SCEditor *e, int w, int h); extern void sc_editor_set_logical_size(SCEditor *e, double w, double h); extern void sc_editor_set_flow(SCEditor *e, int flow); diff --git a/src/slide_window.c b/src/slide_window.c index 7201c0a..415caeb 100644 --- a/src/slide_window.c +++ b/src/slide_window.c @@ -1,7 +1,7 @@ /* * slide_window.c * - * Copyright © 2013-2016 Thomas White <taw@bitwiz.org.uk> + * Copyright © 2013-2017 Thomas White <taw@bitwiz.org.uk> * * This file is part of Colloquium. * @@ -33,6 +33,7 @@ #include <gdk-pixbuf/gdk-pixbuf.h> #include <math.h> +#include "colloquium.h" #include "presentation.h" #include "slide_window.h" #include "render.h" @@ -214,13 +215,14 @@ GActionEntry sw_entries[] = { SlideWindow *slide_window_open(struct presentation *p, SCBlock *scblocks, - GApplication *app) + GApplication *papp) { GtkWidget *window; GtkWidget *scroll; SlideWindow *sw; SCBlock *stylesheets[2]; SCBlock *ch; + Colloquium *app = COLLOQUIUM(papp); sw = calloc(1, sizeof(SlideWindow)); if ( sw == NULL ) return NULL; @@ -244,7 +246,8 @@ SlideWindow *slide_window_open(struct presentation *p, SCBlock *scblocks, if ( ch == NULL ) { ch = sc_block_append_inside(scblocks, NULL, NULL, ""); } - sw->sceditor = sc_editor_new(ch, stylesheets, p->lang); + sw->sceditor = sc_editor_new(ch, stylesheets, p->lang, + colloquium_get_imagestore(app)); sc_editor_set_slidenum(sw->sceditor, slide_number(sw->p, scblocks)); scroll = gtk_scrolled_window_new(NULL, NULL); |