nsxiv1d0f51f
Load thumbnails from cache
+30 −7
1 file changed
- M thumbs.c +30 −7
| @@ -31,6 +31,7 @@ extern Imlib_Image *im_invalid; | |||
| 31 | 31 | const int thumb_dim = THUMB_SIZE + 10; | |
| 32 | 32 | ||
| 33 | 33 | int tns_cache_enabled(); | |
| 34 | + | Imlib_Image* tns_cache_load(const char*); | |
| 34 | 35 | void tns_cache_write(thumb_t*, Bool); | |
| 35 | 36 | ||
| 36 | 37 | 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) { | |||
| 87 | 88 | imlib_free_image(); | |
| 88 | 89 | } | |
| 89 | 90 | ||
| 90 | − | if ((im = imlib_load_image(filename))) | |
| 91 | + | if ((tns_cache_enabled() && (im = tns_cache_load(filename))) || | |
| 92 | + | (im = imlib_load_image(filename))) | |
| 91 | 93 | imlib_context_set_image(im); | |
| 92 | 94 | else | |
| 93 | 95 | imlib_context_set_image(im_invalid); | |
| @@ -346,6 +348,29 @@ char* tns_cache_filename(const char *filename) { | |||
| 346 | 348 | return cfile; | |
| 347 | 349 | } | |
| 348 | 350 | ||
| 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 | + | ||
| 349 | 374 | void tns_cache_write(thumb_t *t, Bool force) { | |
| 350 | 375 | char *cfile; | |
| 351 | 376 | struct stat cstats, fstats; | |
| @@ -354,11 +379,10 @@ void tns_cache_write(thumb_t *t, Bool force) { | |||
| 354 | 379 | ||
| 355 | 380 | if (!t || !t->im || !t->filename) | |
| 356 | 381 | return; | |
| 382 | + | if (stat(t->filename, &fstats)) | |
| 383 | + | return; | |
| 357 | 384 | ||
| 358 | 385 | if ((cfile = tns_cache_filename(t->filename))) { | |
| 359 | − | if (stat(t->filename, &fstats)) | |
| 360 | − | goto end; | |
| 361 | − | ||
| 362 | 386 | if (force || stat(cfile, &cstats) || | |
| 363 | 387 | cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec || | |
| 364 | 388 | cstats.st_mtim.tv_nsec != fstats.st_mtim.tv_nsec) | |
| @@ -373,10 +397,9 @@ void tns_cache_write(thumb_t *t, Bool force) { | |||
| 373 | 397 | TIMESPEC_TO_TIMEVAL(×[0], &fstats.st_atim); | |
| 374 | 398 | TIMESPEC_TO_TIMEVAL(×[1], &fstats.st_mtim); | |
| 375 | 399 | utimes(cfile, times); | |
| 400 | + | printf("thumbnail cache file written: %s\n", t->filename); | |
| 376 | 401 | } | |
| 377 | 402 | } | |
| 403 | + | free(cfile); | |
| 378 | 404 | } | |
| 379 | − | ||
| 380 | − | end: | |
| 381 | − | free(cfile); | |
| 382 | 405 | } | |