summaryrefslogtreecommitdiffstats
path: root/thumbs.c
diff options
context:
space:
mode:
authorBert <ber.t@gmx.com>2011-04-07 15:05:17 +0200
committerBert <ber.t@gmx.com>2011-04-07 15:05:17 +0200
commit1d0f51f95a5035f62b69bd85ef634e50f0e8c56e (patch)
tree6d3642a4613d0d96e88aa8023fd09b3466e5c625 /thumbs.c
parentc21a3e3f28a5c45497d09ab27d71538b983ca535 (diff)
downloadnsxiv-1d0f51f95a5035f62b69bd85ef634e50f0e8c56e.tar.zst
Load thumbnails from cache
Diffstat (limited to 'thumbs.c')
-rw-r--r--thumbs.c37
1 files changed, 30 insertions, 7 deletions
diff --git a/thumbs.c b/thumbs.c
index e3c3f53..80c2361 100644
--- a/thumbs.c
+++ b/thumbs.c
@@ -31,6 +31,7 @@ extern Imlib_Image *im_invalid;
const int thumb_dim = THUMB_SIZE + 10;
int tns_cache_enabled();
+Imlib_Image* tns_cache_load(const char*);
void tns_cache_write(thumb_t*, Bool);
void tns_init(tns_t *tns, int cnt) {
@@ -87,7 +88,8 @@ void tns_load(tns_t *tns, win_t *win, int n, const char *filename) {
imlib_free_image();
}
- if ((im = imlib_load_image(filename)))
+ if ((tns_cache_enabled() && (im = tns_cache_load(filename))) ||
+ (im = imlib_load_image(filename)))
imlib_context_set_image(im);
else
imlib_context_set_image(im_invalid);
@@ -346,6 +348,29 @@ char* tns_cache_filename(const char *filename) {
return cfile;
}
+Imlib_Image* tns_cache_load(const char *filename) {
+ char *cfile;
+ struct stat cstats, fstats;
+ Imlib_Image *im = NULL;
+
+ if (!filename || stat(filename, &fstats))
+ return NULL;
+
+ if ((cfile = tns_cache_filename(filename))) {
+ if (!stat(cfile, &cstats) &&
+ cstats.st_mtim.tv_sec == fstats.st_mtim.tv_sec &&
+ cstats.st_mtim.tv_nsec == fstats.st_mtim.tv_nsec)
+ {
+ printf("cache hit: %s\n", filename);
+ im = imlib_load_image(cfile);
+ } else
+ printf("cache MISS: %s\n", filename);
+ free(cfile);
+ }
+
+ return im;
+}
+
void tns_cache_write(thumb_t *t, Bool force) {
char *cfile;
struct stat cstats, fstats;
@@ -354,11 +379,10 @@ void tns_cache_write(thumb_t *t, Bool force) {
if (!t || !t->im || !t->filename)
return;
+ if (stat(t->filename, &fstats))
+ 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)
@@ -373,10 +397,9 @@ void tns_cache_write(thumb_t *t, Bool force) {
TIMESPEC_TO_TIMEVAL(&times[0], &fstats.st_atim);
TIMESPEC_TO_TIMEVAL(&times[1], &fstats.st_mtim);
utimes(cfile, times);
+ printf("thumbnail cache file written: %s\n", t->filename);
}
}
+ free(cfile);
}
-
-end:
- free(cfile);
}