From e49d38d6f947b11570627b7354641e03f9200cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bert=20M=C3=BCnnich?= Date: Sat, 16 Aug 2014 22:21:51 +0200 Subject: [PATCH] Fixed wrong thumbnail-to-file mappings caused by file deletions --- commands.c | 6 +++--- main.c | 8 ++++---- thumbs.c | 26 ++++++++++++++------------ thumbs.h | 6 +++--- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/commands.c b/commands.c index 7d5a986..f6f155c 100644 --- a/commands.c +++ b/commands.c @@ -77,7 +77,7 @@ bool cg_switch_mode(arg_t a) { if (mode == MODE_IMAGE) { if (tns.thumbs == NULL) - tns_init(&tns, filecnt, &win, &fileidx); + tns_init(&tns, files, filecnt, &fileidx, &win); img_close(&img, false); reset_timeout(reset_cursor); if (img.ss.on) { @@ -130,7 +130,7 @@ bool cg_reload_image(arg_t a) load_image(fileidx); } else { win_set_cursor(&win, CURSOR_WATCH); - if (!tns_load(&tns, fileidx, &files[fileidx], true)) { + if (!tns_load(&tns, fileidx, true)) { remove_file(fileidx, false); tns.dirty = true; } @@ -456,7 +456,7 @@ bool ct_move_sel(arg_t a) bool ct_reload_all(arg_t a) { tns_free(&tns); - tns_init(&tns, filecnt, &win, &fileidx); + tns_init(&tns, files, filecnt, &fileidx, &win); return false; } diff --git a/main.c b/main.c index 5e99c43..b3dba90 100644 --- a/main.c +++ b/main.c @@ -679,7 +679,7 @@ void run(void) /* load thumbnails */ reload = tns.loadnext != tns.cnt; set_timeout(redraw, TO_REDRAW_THUMBS, false); - if (tns_load(&tns, tns.loadnext, &files[tns.loadnext], reload)) { + if (tns_load(&tns, tns.loadnext, reload)) { if (!reload) tns.cnt++; } else { @@ -783,7 +783,7 @@ int main(int argc, char **argv) parse_options(argc, argv); if (options->clean_cache) { - tns_init(&tns, 0, NULL, NULL); + tns_init(&tns, NULL, 0, NULL, NULL); tns_clean_cache(&tns); exit(EXIT_SUCCESS); } @@ -876,8 +876,8 @@ int main(int argc, char **argv) if (options->thumb_mode) { mode = MODE_THUMB; - tns_init(&tns, filecnt, &win, &fileidx); - while (!tns_load(&tns, 0, &files[0], false)) + tns_init(&tns, files, filecnt, &fileidx, &win); + while (!tns_load(&tns, 0, false)) remove_file(0, false); tns.cnt = 1; } else { diff --git a/thumbs.c b/thumbs.c index 14ecf74..44e4b1b 100644 --- a/thumbs.c +++ b/thumbs.c @@ -75,7 +75,7 @@ Imlib_Image tns_cache_load(const char *filepath) return im; } -void tns_cache_write(thumb_t *t, bool force) +void tns_cache_write(thumb_t *t, const fileinfo_t *file, bool force) { char *cfile, *dirend; struct stat cstats, fstats; @@ -84,12 +84,12 @@ void tns_cache_write(thumb_t *t, bool force) if (t == NULL || t->im == NULL) return; - if (t->file == NULL || t->file->name == NULL || t->file->path == NULL) + if (file == NULL || file->name == NULL || file->path == NULL) return; - if (stat(t->file->path, &fstats) < 0) + if (stat(file->path, &fstats) < 0) return; - if ((cfile = tns_cache_filepath(t->file->path)) != NULL) { + if ((cfile = tns_cache_filepath(file->path)) != NULL) { if (force || stat(cfile, &cstats) < 0 || cstats.st_mtime != fstats.st_mtime) { @@ -150,7 +150,7 @@ void tns_clean_cache(tns_t *tns) } -void tns_init(tns_t *tns, int cnt, win_t *win, int *sel) +void tns_init(tns_t *tns, const fileinfo_t *files, int cnt, int *sel, win_t *win) { int len; const char *homedir, *dsuffix = ""; @@ -164,6 +164,7 @@ void tns_init(tns_t *tns, int cnt, win_t *win, int *sel) } else { tns->thumbs = NULL; } + tns->files = files; tns->cap = cnt; tns->cnt = tns->loadnext = tns->first = 0; tns->sel = sel; @@ -209,23 +210,24 @@ void tns_free(tns_t *tns) } } -bool tns_load(tns_t *tns, int n, const fileinfo_t *file, bool force) +bool tns_load(tns_t *tns, int n, bool force) { int w, h; bool cache_hit = false; float z, zw, zh; thumb_t *t; Imlib_Image im = NULL; + const fileinfo_t *file; if (tns == NULL || tns->thumbs == NULL) return false; - if (file == NULL || file->name == NULL || file->path == NULL) - return false; if (n < 0 || n >= tns->cap) return false; + file = &tns->files[n]; + if (file->name == NULL || file->path == NULL) + return false; t = &tns->thumbs[n]; - t->file = file; if (t->im != NULL) { imlib_context_set_image(t->im); @@ -322,7 +324,7 @@ bool tns_load(tns_t *tns, int n, const fileinfo_t *file, bool force) imlib_free_image_and_decache(); if (!cache_hit) - tns_cache_write(t, true); + tns_cache_write(t, file, true); t->loaded = true; tns->dirty = true; @@ -398,7 +400,7 @@ void tns_render(tns_t *tns) imlib_context_set_image(t->im); imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h, t->x, t->y, t->w, t->h); - if (t->file->marked) + if (tns->files[tns->first + i].marked) tns_mark(tns, tns->first + i, true); if ((i + 1) % tns->cols == 0) { x = tns->x; @@ -460,7 +462,7 @@ void tns_highlight(tns_t *tns, int n, bool hl) win_draw_rect(win, win->buf.pm, t->x - 3, t->y - 3, t->w + 6, t->h + 6, false, 2, col); - if (!hl && t->file->marked) + if (!hl && tns->files[n].marked) tns_mark(tns, n, true); } } diff --git a/thumbs.h b/thumbs.h index 1aeaccb..d2a6b83 100644 --- a/thumbs.h +++ b/thumbs.h @@ -26,7 +26,6 @@ #include "window.h" typedef struct { - const fileinfo_t *file; Imlib_Image im; int w; int h; @@ -36,6 +35,7 @@ typedef struct { } thumb_t; typedef struct { + const fileinfo_t *files; thumb_t *thumbs; int cap; int cnt; @@ -54,10 +54,10 @@ typedef struct { void tns_clean_cache(tns_t*); -void tns_init(tns_t*, int, win_t*, int*); +void tns_init(tns_t*, const fileinfo_t*, int, int*, win_t*); void tns_free(tns_t*); -bool tns_load(tns_t*, int, const fileinfo_t*, bool); +bool tns_load(tns_t*, int, bool); void tns_render(tns_t*); void tns_mark(tns_t*, int, bool);