Krolyxon
Commit 1d0f51f

Branches

Refs →

nsxiv1d0f51f

Load thumbnails from cache

Author
Bert
Date
2011-04-07 15:05 +0200
Parent
c21a3e3
Commit
1d0f51f95a5035f62b69bd85ef634e50f0e8c56e

+30 −7

1 file changed

  1. M thumbs.c +30 −7
M thumbs.c View
@@ -31,6 +31,7 @@ extern Imlib_Image *im_invalid;
3131 const int thumb_dim = THUMB_SIZE + 10;
3232
3333 int tns_cache_enabled();
34+Imlib_Image* tns_cache_load(const char*);
3435 void tns_cache_write(thumb_t*, Bool);
3536
3637 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) {
8788 imlib_free_image();
8889 }
8990
90− if ((im = imlib_load_image(filename)))
91+ if ((tns_cache_enabled() && (im = tns_cache_load(filename))) ||
92+ (im = imlib_load_image(filename)))
9193 imlib_context_set_image(im);
9294 else
9395 imlib_context_set_image(im_invalid);
@@ -346,6 +348,29 @@ char* tns_cache_filename(const char *filename) {
346348 return cfile;
347349 }
348350
351+Imlib_Image* tns_cache_load(const char *filename) {
352+ char *cfile;
353+ struct stat cstats, fstats;
354+ Imlib_Image *im = NULL;
355+
356+ if (!filename || stat(filename, &fstats))
357+ return NULL;
358+
359+ if ((cfile = tns_cache_filename(filename))) {
360+ if (!stat(cfile, &cstats) &&
361+ cstats.st_mtim.tv_sec == fstats.st_mtim.tv_sec &&
362+ cstats.st_mtim.tv_nsec == fstats.st_mtim.tv_nsec)
363+ {
364+ printf("cache hit: %s\n", filename);
365+ im = imlib_load_image(cfile);
366+ } else
367+ printf("cache MISS: %s\n", filename);
368+ free(cfile);
369+ }
370+
371+ return im;
372+}
373+
349374 void tns_cache_write(thumb_t *t, Bool force) {
350375 char *cfile;
351376 struct stat cstats, fstats;
@@ -354,11 +379,10 @@ void tns_cache_write(thumb_t *t, Bool force) {
354379
355380 if (!t || !t->im || !t->filename)
356381 return;
382+ if (stat(t->filename, &fstats))
383+ return;
357384
358385 if ((cfile = tns_cache_filename(t->filename))) {
359− if (stat(t->filename, &fstats))
360− goto end;
361−
362386 if (force || stat(cfile, &cstats) ||
363387 cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec ||
364388 cstats.st_mtim.tv_nsec != fstats.st_mtim.tv_nsec)
@@ -373,10 +397,9 @@ void tns_cache_write(thumb_t *t, Bool force) {
373397 TIMESPEC_TO_TIMEVAL(&times[0], &fstats.st_atim);
374398 TIMESPEC_TO_TIMEVAL(&times[1], &fstats.st_mtim);
375399 utimes(cfile, times);
400+ printf("thumbnail cache file written: %s\n", t->filename);
376401 }
377402 }
403+ free(cfile);
378404 }
379−
380−end:
381− free(cfile);
382405 }