From c21a3e3f28a5c45497d09ab27d71538b983ca535 Mon Sep 17 00:00:00 2001 From: Bert Date: Thu, 7 Apr 2011 14:33:57 +0200 Subject: Write thumbnail cache files on exit --- thumbs.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'thumbs.c') diff --git a/thumbs.c b/thumbs.c index 8e5e67e..e3c3f53 100644 --- a/thumbs.c +++ b/thumbs.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -292,6 +293,9 @@ int tns_translate(tns_t *tns, int x, int y) { return -1; } + +/* thumbnail caching */ + int tns_cache_enabled() { int len, ret = 0; char *cpath, *homedir; @@ -309,5 +313,70 @@ int tns_cache_enabled() { return ret; } +char* tns_cache_filename(const char *filename) { + size_t len; + int i; + char *cfile, *abspath, *homedir; + + if (!filename) + return NULL; + if (!(homedir = getenv("HOME"))) + return NULL; + + if (*filename != '/') { + if (!(abspath = absolute_path(filename))) + return NULL; + } else { + abspath = (char*) s_malloc(strlen(filename) + 1); + strcpy(abspath, filename); + } + + len = strlen(abspath); + for (i = 1; i < len; ++i) { + if (abspath[i] == '/') + abspath[i] = '%'; + } + + len += strlen(homedir) + 15; + cfile = (char*) s_malloc(len); + snprintf(cfile, len, "%s/.sxiv/%s.png", homedir, abspath + 1); + + free(abspath); + + return cfile; +} + void tns_cache_write(thumb_t *t, Bool force) { + char *cfile; + struct stat cstats, fstats; + struct timeval times[2]; + Imlib_Load_Error err; + + if (!t || !t->im || !t->filename) + return; + + if ((cfile = tns_cache_filename(t->filename))) { + if (stat(t->filename, &fstats)) + goto end; + + if (force || stat(cfile, &cstats) || + cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec || + cstats.st_mtim.tv_nsec != fstats.st_mtim.tv_nsec) + { + imlib_context_set_image(t->im); + imlib_image_set_format("png"); + imlib_save_image_with_error_return(cfile, &err); + + if (err) { + warn("could not cache thumbnail:", t->filename); + } else { + TIMESPEC_TO_TIMEVAL(×[0], &fstats.st_atim); + TIMESPEC_TO_TIMEVAL(×[1], &fstats.st_mtim); + utimes(cfile, times); + } + } + } + +end: + free(cfile); } -- cgit v1.2.3-54-g00ecf