Fixed wrong thumbnail-to-file mappings caused by file deletions

This commit is contained in:
Bert Münnich 2014-08-16 22:21:51 +02:00
parent e267dc7793
commit e49d38d6f9
4 changed files with 24 additions and 22 deletions

View File

@ -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;
}

8
main.c
View File

@ -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 {

View File

@ -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);
}
}

View File

@ -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);