diff options
author | Thomas White <taw@bitwiz.org.uk> | 2013-08-26 23:18:27 +0200 |
---|---|---|
committer | Thomas White <taw@bitwiz.org.uk> | 2013-08-26 23:18:27 +0200 |
commit | 62709b5f100733de9d1cb8862606ea98048f0210 (patch) | |
tree | dce01aa8fc01c18562dd4bae5d283410cda07394 | |
parent | ba5c48c7729d92216df12166c643ecbd4470808e (diff) |
Try other locations for images as well
-rw-r--r-- | src/imagestore.c | 63 | ||||
-rw-r--r-- | src/imagestore.h | 3 | ||||
-rw-r--r-- | src/presentation.c | 2 |
3 files changed, 57 insertions, 11 deletions
diff --git a/src/imagestore.c b/src/imagestore.c index fbf123a..7712e65 100644 --- a/src/imagestore.c +++ b/src/imagestore.c @@ -28,6 +28,7 @@ #include <stdlib.h> #include <string.h> #include <assert.h> +#include <libgen.h> #include "imagestore.h" @@ -44,6 +45,7 @@ struct _imagestore int n_images; struct image_record *images; int max_images; + char *dname; }; @@ -69,6 +71,7 @@ ImageStore *imagestore_new() if ( is == NULL ) return NULL; is->images = NULL; + is->dname = NULL; is->n_images = 0; is->max_images = 0; if ( alloc_images(is, 32) ) { @@ -80,6 +83,20 @@ ImageStore *imagestore_new() } +void imagestore_set_presentation_file(ImageStore *is, const char *filename) +{ + const char *dname; + char *cpy; + + /* dirname() is yukky */ + cpy = strdup(filename); + dname = dirname(cpy); + free(is->dname); + is->dname = strdup(dname); + free(cpy); +} + + void imagestore_destroy(ImageStore *is) { int i; @@ -98,17 +115,41 @@ void imagestore_destroy(ImageStore *is) } -static GdkPixbuf *add_pixbuf(struct image_record *im, const char *filename, - int w, enum is_size isz) +static GdkPixbuf *try_all_locations(const char *filename, int w, ImageStore *is) { GError *error = NULL; - printf("Loading '%s' at width %i\n", filename, w); - im->pixbuf[isz] = gdk_pixbuf_new_from_file_at_size(filename, w, -1, - &error); - if ( im->pixbuf[isz] == NULL ) { - fprintf(stderr, "Failed to load image '%s'\n", filename); - return NULL; - } + GdkPixbuf *t; + char *tmp; + + /* Try the filename as is */ + t = gdk_pixbuf_new_from_file_at_size(filename, w, -1, &error); + if ( t != NULL ) return t; + + if ( is->dname == NULL ) return NULL; + + /* Try the file prefixed with the directory the presentation + * is in */ + error = NULL; + tmp = malloc(strlen(filename) + strlen(is->dname) + 2); + if ( tmp == NULL ) return NULL; + strcpy(tmp, is->dname); + strcat(tmp, "/"); + strcat(tmp, filename); + printf("Trying '%s'\n", tmp); + t = gdk_pixbuf_new_from_file_at_size(tmp, w, -1, &error); + free(tmp); + if ( t != NULL ) return t; + + return NULL; +} + + +static GdkPixbuf *add_pixbuf(struct image_record *im, const char *filename, + int w, enum is_size isz, ImageStore *is) +{ + im->pixbuf[isz] = try_all_locations(filename, w, is); + + if ( im->pixbuf[isz] == NULL ) return NULL; im->w[isz] = w; @@ -137,7 +178,7 @@ static GdkPixbuf *add_new_image(ImageStore *is, const char *filename, int w, is->images[idx].w[j] = 0; } - return add_pixbuf(&is->images[idx], filename, w, isz); + return add_pixbuf(&is->images[idx], filename, w, isz, is); } @@ -191,6 +232,6 @@ GdkPixbuf *lookup_image(ImageStore *is, const char *filename, int w, } /* Slot is not filled in yet */ - pb = add_pixbuf(&is->images[i], filename, w, isz); + pb = add_pixbuf(&is->images[i], filename, w, isz, is); return pb; } diff --git a/src/imagestore.h b/src/imagestore.h index 349a409..f1d4ca3 100644 --- a/src/imagestore.h +++ b/src/imagestore.h @@ -43,6 +43,9 @@ extern ImageStore *imagestore_new(void); extern void imagestore_destroy(ImageStore *is); +extern void imagestore_set_presentation_file(ImageStore *is, + const char *filename); + extern GdkPixbuf *lookup_image(ImageStore *is, const char *filename, int w, enum is_size isz); diff --git a/src/presentation.c b/src/presentation.c index ee93691..ccd3c18 100644 --- a/src/presentation.c +++ b/src/presentation.c @@ -383,6 +383,7 @@ int save_presentation(struct presentation *p, const char *filename) /* Slightly fiddly because someone might * do save_presentation(p, p->filename) */ old_fn = p->filename; + imagestore_set_presentation_file(p->is, filename); p->filename = strdup(filename); if ( old_fn != NULL ) free(old_fn); update_titlebar(p); @@ -515,6 +516,7 @@ int load_presentation(struct presentation *p, const char *filename) assert(p->filename == NULL); p->filename = strdup(filename); update_titlebar(p); + imagestore_set_presentation_file(p->is, filename); p->cur_edit_slide = p->slides[0]; |