nsxivf52a99d
Refactored thumbnail cache_dir
+24 −18
1 file changed
- M thumbs.c +24 −18
| @@ -28,13 +28,18 @@ | |||
| 28 | 28 | #include "util.h" | |
| 29 | 29 | ||
| 30 | 30 | extern Imlib_Image *im_invalid; | |
| 31 | + | ||
| 31 | 32 | const int thumb_dim = THUMB_SIZE + 10; | |
| 33 | + | char *cache_dir = NULL; | |
| 32 | 34 | ||
| 33 | 35 | int tns_cache_enabled(); | |
| 34 | 36 | Imlib_Image* tns_cache_load(const char*); | |
| 35 | 37 | void tns_cache_write(thumb_t*, Bool); | |
| 36 | 38 | ||
| 37 | 39 | void tns_init(tns_t *tns, int cnt) { | |
| 40 | + | int len; | |
| 41 | + | char *homedir; | |
| 42 | + | ||
| 38 | 43 | if (!tns) | |
| 39 | 44 | return; | |
| 40 | 45 | ||
| @@ -43,6 +48,14 @@ void tns_init(tns_t *tns, int cnt) { | |||
| 43 | 48 | memset(tns->thumbs, 0, cnt * sizeof(thumb_t)); | |
| 44 | 49 | tns->cap = cnt; | |
| 45 | 50 | tns->dirty = 0; | |
| 51 | + | ||
| 52 | + | if ((homedir = getenv("HOME"))) { | |
| 53 | + | if (cache_dir) | |
| 54 | + | free(cache_dir); | |
| 55 | + | len = strlen(homedir) + 10; | |
| 56 | + | cache_dir = (char*) s_malloc(len * sizeof(char)); | |
| 57 | + | snprintf(cache_dir, len, "%s/.sxiv", homedir); | |
| 58 | + | } | |
| 46 | 59 | } | |
| 47 | 60 | ||
| 48 | 61 | void tns_free(tns_t *tns, win_t *win) { | |
| @@ -60,6 +73,11 @@ void tns_free(tns_t *tns, win_t *win) { | |||
| 60 | 73 | ||
| 61 | 74 | free(tns->thumbs); | |
| 62 | 75 | tns->thumbs = NULL; | |
| 76 | + | ||
| 77 | + | if (cache_dir) { | |
| 78 | + | free(cache_dir); | |
| 79 | + | cache_dir = NULL; | |
| 80 | + | } | |
| 63 | 81 | } | |
| 64 | 82 | ||
| 65 | 83 | void tns_load(tns_t *tns, win_t *win, int n, const char *filename) { | |
| @@ -301,30 +319,18 @@ int tns_translate(tns_t *tns, int x, int y) { | |||
| 301 | 319 | /* thumbnail caching */ | |
| 302 | 320 | ||
| 303 | 321 | int tns_cache_enabled() { | |
| 304 | − | int len, ret = 0; | |
| 305 | − | char *cpath, *homedir; | |
| 306 | 322 | struct stat stats; | |
| 307 | 323 | ||
| 308 | − | if ((homedir = getenv("HOME"))) { | |
| 309 | − | len = strlen(homedir) + 10; | |
| 310 | − | cpath = (char*) s_malloc(len * sizeof(char)); | |
| 311 | − | snprintf(cpath, len, "%s/.sxiv", homedir); | |
| 312 | − | ret = !stat(cpath, &stats) && S_ISDIR(stats.st_mode) && | |
| 313 | − | !access(cpath, W_OK); | |
| 314 | − | free(cpath); | |
| 315 | − | } | |
| 316 | − | ||
| 317 | − | return ret; | |
| 324 | + | return cache_dir && !stat(cache_dir, &stats) && S_ISDIR(stats.st_mode) && | |
| 325 | + | !access(cache_dir, W_OK); | |
| 318 | 326 | } | |
| 319 | 327 | ||
| 320 | 328 | char* tns_cache_filename(const char *filename) { | |
| 321 | 329 | size_t len; | |
| 322 | 330 | int i; | |
| 323 | − | char *cfile, *abspath, *homedir; | |
| 331 | + | char *cfile, *abspath; | |
| 324 | 332 | ||
| 325 | − | if (!filename) | |
| 326 | − | return NULL; | |
| 327 | − | if (!(homedir = getenv("HOME"))) | |
| 333 | + | if (!cache_dir || !filename) | |
| 328 | 334 | return NULL; | |
| 329 | 335 | ||
| 330 | 336 | if (*filename != '/') { | |
| @@ -341,9 +347,9 @@ char* tns_cache_filename(const char *filename) { | |||
| 341 | 347 | abspath[i] = '%'; | |
| 342 | 348 | } | |
| 343 | 349 | ||
| 344 | − | len += strlen(homedir) + 15; | |
| 350 | + | len += strlen(cache_dir) + 6; | |
| 345 | 351 | cfile = (char*) s_malloc(len); | |
| 346 | − | snprintf(cfile, len, "%s/.sxiv/%s.png", homedir, abspath + 1); | |
| 352 | + | snprintf(cfile, len, "%s/%s.png", cache_dir, abspath + 1); | |
| 347 | 353 | ||
| 348 | 354 | free(abspath); | |
| 349 | 355 | ||