| @@ -71,7 +71,10 @@ Imlib_Image* tns_cache_load(const char *filename) { |
| 71 | 71 | | struct stat cstats, fstats; |
| 72 | 72 | | Imlib_Image *im = NULL; |
| 73 | 73 | | |
| 74 | | − | if (!filename || stat(filename, &fstats)) |
| 74 | + | if (!filename) |
| 75 | + | return NULL; |
| 76 | + | |
| 77 | + | if (stat(filename, &fstats)) |
| 75 | 78 | | return NULL; |
| 76 | 79 | | |
| 77 | 80 | | if ((cfile = tns_cache_filename(filename))) { |
| @@ -97,6 +100,7 @@ void tns_cache_write(thumb_t *t, Bool force) { |
| 97 | 100 | | |
| 98 | 101 | | if (!t || !t->im || !t->filename) |
| 99 | 102 | | return; |
| 103 | + | |
| 100 | 104 | | if (stat(t->filename, &fstats)) |
| 101 | 105 | | return; |
| 102 | 106 | | |
| @@ -130,6 +134,40 @@ void tns_cache_write(thumb_t *t, Bool force) { |
| 130 | 134 | | } |
| 131 | 135 | | } |
| 132 | 136 | | |
| 137 | + | void tns_clear_cache(tns_t *tns) { |
| 138 | + | int dirlen, delete; |
| 139 | + | char *cfile, *filename, *tpos; |
| 140 | + | r_dir_t dir; |
| 141 | + | |
| 142 | + | if (!cache_dir) |
| 143 | + | return; |
| 144 | + | |
| 145 | + | if (r_opendir(&dir, cache_dir)) { |
| 146 | + | warn("could not open thumbnail cache directory: %s", cache_dir); |
| 147 | + | return; |
| 148 | + | } |
| 149 | + | |
| 150 | + | dirlen = strlen(cache_dir); |
| 151 | + | |
| 152 | + | while ((cfile = r_readdir(&dir))) { |
| 153 | + | filename = cfile + dirlen; |
| 154 | + | delete = 0; |
| 155 | + | |
| 156 | + | if ((tpos = strrchr(filename, '.'))) { |
| 157 | + | *tpos = '\0'; |
| 158 | + | delete = access(filename, F_OK); |
| 159 | + | *tpos = '.'; |
| 160 | + | } |
| 161 | + | |
| 162 | + | if (delete && unlink(cfile)) |
| 163 | + | warn("could not delete cache file: %s", cfile); |
| 164 | + | |
| 165 | + | free(cfile); |
| 166 | + | } |
| 167 | + | |
| 168 | + | r_closedir(&dir); |
| 169 | + | } |
| 170 | + | |
| 133 | 171 | | |
| 134 | 172 | | void tns_init(tns_t *tns, int cnt) { |
| 135 | 173 | | int len; |
| @@ -138,9 +176,14 @@ void tns_init(tns_t *tns, int cnt) { |
| 138 | 176 | | if (!tns) |
| 139 | 177 | | return; |
| 140 | 178 | | |
| 179 | + | if (cnt) { |
| 180 | + | tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t)); |
| 181 | + | memset(tns->thumbs, 0, cnt * sizeof(thumb_t)); |
| 182 | + | } else { |
| 183 | + | tns->thumbs = NULL; |
| 184 | + | } |
| 185 | + | |
| 141 | 186 | | tns->cnt = tns->first = tns->sel = 0; |
| 142 | | − | tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t)); |
| 143 | | − | memset(tns->thumbs, 0, cnt * sizeof(thumb_t)); |
| 144 | 187 | | tns->cap = cnt; |
| 145 | 188 | | tns->dirty = 0; |
| 146 | 189 | | |
| @@ -150,25 +193,28 @@ void tns_init(tns_t *tns, int cnt) { |
| 150 | 193 | | len = strlen(homedir) + 10; |
| 151 | 194 | | cache_dir = (char*) s_malloc(len * sizeof(char)); |
| 152 | 195 | | snprintf(cache_dir, len, "%s/.sxiv", homedir); |
| 196 | + | } else { |
| 197 | + | warn("could not locate thumbnail cache directory"); |
| 153 | 198 | | } |
| 154 | 199 | | } |
| 155 | 200 | | |
| 156 | 201 | | void tns_free(tns_t *tns, win_t *win) { |
| 157 | 202 | | int i; |
| 158 | 203 | | |
| 159 | | − | if (!tns || !tns->thumbs) |
| 204 | + | if (!tns) |
| 160 | 205 | | return; |
| 161 | 206 | | |
| 162 | | − | for (i = 0; i < tns->cnt; ++i) { |
| 163 | | − | if (tns->thumbs[i].im) { |
| 164 | | − | imlib_context_set_image(tns->thumbs[i].im); |
| 165 | | − | imlib_free_image(); |
| 207 | + | if (tns->thumbs) { |
| 208 | + | for (i = 0; i < tns->cnt; ++i) { |
| 209 | + | if (tns->thumbs[i].im) { |
| 210 | + | imlib_context_set_image(tns->thumbs[i].im); |
| 211 | + | imlib_free_image(); |
| 212 | + | } |
| 166 | 213 | | } |
| 214 | + | free(tns->thumbs); |
| 215 | + | tns->thumbs = NULL; |
| 167 | 216 | | } |
| 168 | 217 | | |
| 169 | | − | free(tns->thumbs); |
| 170 | | − | tns->thumbs = NULL; |
| 171 | | − | |
| 172 | 218 | | if (cache_dir) { |
| 173 | 219 | | free(cache_dir); |
| 174 | 220 | | cache_dir = NULL; |
| @@ -182,7 +228,7 @@ void tns_load(tns_t *tns, win_t *win, int n, const char *filename) { |
| 182 | 228 | | thumb_t *t; |
| 183 | 229 | | Imlib_Image *im; |
| 184 | 230 | | |
| 185 | | − | if (!tns || !win || !filename) |
| 231 | + | if (!tns || !tns->thumbs || !win || !filename) |
| 186 | 232 | | return; |
| 187 | 233 | | |
| 188 | 234 | | if (n >= tns->cap) |
| @@ -265,7 +311,10 @@ void tns_render(tns_t *tns, win_t *win) { |
| 265 | 311 | | int i, cnt, r, x, y; |
| 266 | 312 | | thumb_t *t; |
| 267 | 313 | | |
| 268 | | − | if (!tns || !tns->dirty || !win) |
| 314 | + | if (!tns || !tns->thumbs || !win) |
| 315 | + | return; |
| 316 | + | |
| 317 | + | if (!tns->dirty) |
| 269 | 318 | | return; |
| 270 | 319 | | |
| 271 | 320 | | win_clear(win); |
| @@ -313,7 +362,7 @@ void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) { |
| 313 | 362 | | thumb_t *t; |
| 314 | 363 | | unsigned long col; |
| 315 | 364 | | |
| 316 | | − | if (!tns || !win) |
| 365 | + | if (!tns || !tns->thumbs || !win) |
| 317 | 366 | | return; |
| 318 | 367 | | |
| 319 | 368 | | if (n >= 0 && n < tns->cnt) { |
| @@ -336,7 +385,7 @@ void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) { |
| 336 | 385 | | int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) { |
| 337 | 386 | | int old; |
| 338 | 387 | | |
| 339 | | − | if (!tns || !win) |
| 388 | + | if (!tns || !tns->thumbs || !win) |
| 340 | 389 | | return 0; |
| 341 | 390 | | |
| 342 | 391 | | old = tns->sel; |
| @@ -395,7 +444,10 @@ int tns_translate(tns_t *tns, int x, int y) { |
| 395 | 444 | | int n; |
| 396 | 445 | | thumb_t *t; |
| 397 | 446 | | |
| 398 | | − | if (!tns || x < tns->x || y < tns->y) |
| 447 | + | if (!tns || !tns->thumbs) |
| 448 | + | return -1; |
| 449 | + | |
| 450 | + | if (x < tns->x || y < tns->y) |
| 399 | 451 | | return -1; |
| 400 | 452 | | |
| 401 | 453 | | n = tns->first + (y - tns->y) / thumb_dim * tns->cols + |