Krolyxon
Commit dc54981

Branches

Refs →

nsxivdc54981

New option: -C, clean thumbnail cache

Author
Bert
Date
2011-04-08 14:44 +0200
Parent
eee921e
Commit
dc54981a68589c719937c82c36eeb9247d915a81

+83 −18

5 files changed

  1. M main.c +6 −0
  2. M options.c +6 −2
  3. M options.h +1 −0
  4. M thumbs.c +68 −16
  5. M thumbs.h +2 −0
M main.c View
@@ -104,6 +104,12 @@ int main(int argc, char **argv) {
104104
105105 parse_options(argc, argv);
106106
107+ if (options->clean_cache) {
108+ tns_init(&tns, 0);
109+ tns_clear_cache(&tns);
110+ exit(0);
111+ }
112+
107113 if (!options->filecnt) {
108114 print_usage();
109115 exit(1);
M options.c View
@@ -31,7 +31,7 @@ options_t _options;
3131 const options_t *options = (const options_t*) &_options;
3232
3333 void print_usage() {
34− printf("usage: sxiv [-adFfhpqrstvZ] [-g GEOMETRY] [-z ZOOM] FILES...\n");
34+ printf("usage: sxiv [-aCdFfhpqrstvZ] [-g GEOMETRY] [-z ZOOM] FILES...\n");
3535 }
3636
3737 void print_version() {
@@ -53,9 +53,10 @@ void parse_options(int argc, char **argv) {
5353
5454 _options.all = 0;
5555 _options.quiet = 0;
56+ _options.clean_cache = 0;
5657 _options.recursive = 0;
5758
58− while ((opt = getopt(argc, argv, "adFfg:hpqrstvZz:")) != -1) {
59+ while ((opt = getopt(argc, argv, "aCdFfg:hpqrstvZz:")) != -1) {
5960 switch (opt) {
6061 case '?':
6162 print_usage();
@@ -63,6 +64,9 @@ void parse_options(int argc, char **argv) {
6364 case 'a':
6465 _options.all = 1;
6566 break;
67+ case 'C':
68+ _options.clean_cache = 1;
69+ break;
6670 case 'd':
6771 _options.scalemode = SCALE_DOWN;
6872 break;
M options.h View
@@ -37,6 +37,7 @@ typedef struct {
3737
3838 unsigned char all;
3939 unsigned char quiet;
40+ unsigned char clean_cache;
4041 unsigned char recursive;
4142 } options_t;
4243
M thumbs.c View
@@ -71,7 +71,10 @@ Imlib_Image* tns_cache_load(const char *filename) {
7171 struct stat cstats, fstats;
7272 Imlib_Image *im = NULL;
7373
74− if (!filename || stat(filename, &fstats))
74+ if (!filename)
75+ return NULL;
76+
77+ if (stat(filename, &fstats))
7578 return NULL;
7679
7780 if ((cfile = tns_cache_filename(filename))) {
@@ -97,6 +100,7 @@ void tns_cache_write(thumb_t *t, Bool force) {
97100
98101 if (!t || !t->im || !t->filename)
99102 return;
103+
100104 if (stat(t->filename, &fstats))
101105 return;
102106
@@ -130,6 +134,40 @@ void tns_cache_write(thumb_t *t, Bool force) {
130134 }
131135 }
132136
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+
133171
134172 void tns_init(tns_t *tns, int cnt) {
135173 int len;
@@ -138,9 +176,14 @@ void tns_init(tns_t *tns, int cnt) {
138176 if (!tns)
139177 return;
140178
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+
141186 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));
144187 tns->cap = cnt;
145188 tns->dirty = 0;
146189
@@ -150,25 +193,28 @@ void tns_init(tns_t *tns, int cnt) {
150193 len = strlen(homedir) + 10;
151194 cache_dir = (char*) s_malloc(len * sizeof(char));
152195 snprintf(cache_dir, len, "%s/.sxiv", homedir);
196+ } else {
197+ warn("could not locate thumbnail cache directory");
153198 }
154199 }
155200
156201 void tns_free(tns_t *tns, win_t *win) {
157202 int i;
158203
159− if (!tns || !tns->thumbs)
204+ if (!tns)
160205 return;
161206
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+ }
166213 }
214+ free(tns->thumbs);
215+ tns->thumbs = NULL;
167216 }
168217
169− free(tns->thumbs);
170− tns->thumbs = NULL;
171−
172218 if (cache_dir) {
173219 free(cache_dir);
174220 cache_dir = NULL;
@@ -182,7 +228,7 @@ void tns_load(tns_t *tns, win_t *win, int n, const char *filename) {
182228 thumb_t *t;
183229 Imlib_Image *im;
184230
185− if (!tns || !win || !filename)
231+ if (!tns || !tns->thumbs || !win || !filename)
186232 return;
187233
188234 if (n >= tns->cap)
@@ -265,7 +311,10 @@ void tns_render(tns_t *tns, win_t *win) {
265311 int i, cnt, r, x, y;
266312 thumb_t *t;
267313
268− if (!tns || !tns->dirty || !win)
314+ if (!tns || !tns->thumbs || !win)
315+ return;
316+
317+ if (!tns->dirty)
269318 return;
270319
271320 win_clear(win);
@@ -313,7 +362,7 @@ void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
313362 thumb_t *t;
314363 unsigned long col;
315364
316− if (!tns || !win)
365+ if (!tns || !tns->thumbs || !win)
317366 return;
318367
319368 if (n >= 0 && n < tns->cnt) {
@@ -336,7 +385,7 @@ void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
336385 int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
337386 int old;
338387
339− if (!tns || !win)
388+ if (!tns || !tns->thumbs || !win)
340389 return 0;
341390
342391 old = tns->sel;
@@ -395,7 +444,10 @@ int tns_translate(tns_t *tns, int x, int y) {
395444 int n;
396445 thumb_t *t;
397446
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)
399451 return -1;
400452
401453 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
M thumbs.h View
@@ -52,6 +52,8 @@ typedef struct {
5252 unsigned char dirty;
5353 } tns_t;
5454
55+void tns_clear_cache(tns_t*);
56+
5557 void tns_init(tns_t*, int);
5658 void tns_free(tns_t*, win_t*);
5759