Skip to content

Commit

Permalink
reload changed images
Browse files Browse the repository at this point in the history
  • Loading branch information
nadvornik committed Jul 17, 2008
1 parent 01e819f commit 6da5f50
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 52 deletions.
48 changes: 47 additions & 1 deletion src/filecache.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ struct _FileCacheEntry {
gulong size;
};

static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data);

FileCacheData *file_cache_new(FileCacheReleaseFunc release, gulong max_size)
{
FileCacheData *fc = g_new(FileCacheData, 1);
fc->release = release;
fc->list = NULL;
fc->max_size = max_size;
fc->size = 0;

file_data_register_notify_func(file_cache_notify_cb, fc, NOTIFY_PRIORITY_HIGH);

return fc;
}

Expand All @@ -60,6 +65,10 @@ gboolean file_cache_get(FileCacheData *fc, FileData *fd)
DEBUG_1("cache move to front: fc=%p %s", fc, fd->path);
fc->list = g_list_remove_link(fc->list, work);
fc->list = g_list_concat(work, fc->list);

// if (file_data_check_changed_files(fd)) /* this will eventually remove changed files from cache via file_cache_notify_cb */
// return FALSE;

if (debug_file_cache) file_cache_dump(fc);
return TRUE;
}
Expand All @@ -85,7 +94,7 @@ void file_cache_set_size(FileCacheData *fc, gulong size)
fc->list = g_list_delete_link(fc->list, work);
work = prev;

DEBUG_1("cache remove: fc=%p %s", fc, last_fe->fd->path);
DEBUG_1("file changed - cache remove: fc=%p %s", fc, last_fe->fd->path);
fc->size -= last_fe->size;
fc->release(last_fe->fd);
file_data_unref(last_fe->fd);
Expand Down Expand Up @@ -125,6 +134,33 @@ void file_cache_set_max_size(FileCacheData *fc, gulong size)
file_cache_set_size(fc, fc->max_size);
}

static void file_cache_remove_fd(FileCacheData *fc, FileData *fd)
{
GList *work;
FileCacheEntry *fe;

if (debug_file_cache) file_cache_dump(fc);

work = fc->list;
while (work)
{
GList *current = work;
fe = work->data;
work = work->next;

if (fe->fd == fd)
{
fc->list = g_list_delete_link(fc->list, current);

DEBUG_1("cache remove: fc=%p %s", fc, fe->fd->path);
fc->size -= fe->size;
fc->release(fe->fd);
file_data_unref(fe->fd);
g_free(fe);
}
}
}

void file_cache_dump(FileCacheData *fc)
{
GList *work;
Expand All @@ -139,3 +175,13 @@ void file_cache_dump(FileCacheData *fc)
DEBUG_1("cache entry: fc=%p [%lu] %s %ld", fc, ++n, fe->fd->path, fe->size);
}
}

static void file_cache_notify_cb(FileData *fd, NotifyType type, gpointer data)
{
FileCacheData *fc = data;

if (type == NOTIFY_TYPE_REREAD)
{
file_cache_remove_fd(fc, fd);
}
}
2 changes: 1 addition & 1 deletion src/filedata.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ static gboolean file_data_check_changed_files_recursive(FileData *fd, struct sta
}


static gboolean file_data_check_changed_files(FileData *fd)
gboolean file_data_check_changed_files(FileData *fd)
{
gboolean ret = FALSE;
struct stat st;
Expand Down
2 changes: 2 additions & 0 deletions src/filedata.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ FileData *file_data_ref(FileData *fd);
void file_data_unref(FileData *fd);
#endif

gboolean file_data_check_changed_files(FileData *fd);

void file_data_increment_version(FileData *fd);

gboolean file_data_add_change_info(FileData *fd, FileDataChangeType type, const gchar *src, const gchar *dest);
Expand Down
2 changes: 1 addition & 1 deletion src/fullscreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ FullScreenData *fullscreen_start(GtkWidget *window, ImageWindow *imd,
}

image_set_delay_flip(fs->imd, options->fullscreen.clean_flip);
image_auto_refresh(fs->imd, fs->normal_imd->auto_refresh_interval);
image_auto_refresh_enable(fs->imd, fs->normal_imd->auto_refresh);

if (options->fullscreen.clean_flip)
{
Expand Down
4 changes: 2 additions & 2 deletions src/image-overlay.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,8 @@ static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
osd_template_insert(vars, "number", g_strdup_printf("%d", n), OSDT_NO_DUP);
osd_template_insert(vars, "total", g_strdup_printf("%d", t), OSDT_NO_DUP);
osd_template_insert(vars, "name", (gchar *) name, OSDT_NONE);
osd_template_insert(vars, "date", (gchar *) text_from_time(imd->mtime), OSDT_NONE);
osd_template_insert(vars, "size", text_from_size_abrev(imd->size), OSDT_FREE);
osd_template_insert(vars, "date", imd->image_fd ? ((gchar *) text_from_time(imd->image_fd->date)) : "", OSDT_NONE);
osd_template_insert(vars, "size", imd->image_fd ? (text_from_size_abrev(imd->image_fd->size)) : g_strdup(""), OSDT_FREE);
osd_template_insert(vars, "zoom", image_zoom_get_as_text(imd), OSDT_FREE);

if (!imd->unknown)
Expand Down
68 changes: 30 additions & 38 deletions src/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@
/* the file size at which throttling take place */
#define IMAGE_THROTTLE_THRESHOLD 1048576

#define IMAGE_AUTO_REFRESH_TIME 3000


static GList *image_list = NULL;


Expand Down Expand Up @@ -773,8 +770,6 @@ static void image_change_complete(ImageWindow *imd, gdouble zoom, gint new)

imd->unknown = TRUE;
}
imd->size = filesize(imd->image_fd->path);
imd->mtime = filetime(imd->image_fd->path);
}
else
{
Expand All @@ -785,15 +780,12 @@ static void image_change_complete(ImageWindow *imd, gdouble zoom, gint new)
pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
image_change_pixbuf(imd, pixbuf, zoom);
g_object_unref(pixbuf);
imd->mtime = filetime(imd->image_fd->path);
}
else
{
image_change_pixbuf(imd, NULL, zoom);
imd->mtime = 0;
}
imd->unknown = TRUE;
imd->size = 0;
}

image_update_util(imd);
Expand All @@ -806,13 +798,20 @@ static void image_change_real(ImageWindow *imd, FileData *fd,
imd->collection = cd;
imd->collection_info = info;

if (imd->auto_refresh && imd->image_fd)
file_data_unregister_real_time_monitor(imd->image_fd);

file_data_unref(imd->image_fd);
imd->image_fd = file_data_ref(fd);


image_change_complete(imd, zoom, TRUE);

image_update_title(imd);
image_state_set(imd, IMAGE_STATE_IMAGE);

if (imd->auto_refresh && imd->image_fd)
file_data_register_real_time_monitor(imd->image_fd);
}

/*
Expand Down Expand Up @@ -988,11 +987,17 @@ FileData *image_get_fd(ImageWindow *imd)
/* merely changes path string, does not change the image! */
void image_set_fd(ImageWindow *imd, FileData *fd)
{
if (imd->auto_refresh && imd->image_fd)
file_data_unregister_real_time_monitor(imd->image_fd);

file_data_unref(imd->image_fd);
imd->image_fd = file_data_ref(fd);

image_update_title(imd);
image_state_set(imd, IMAGE_STATE_IMAGE);

if (imd->auto_refresh && imd->image_fd)
file_data_register_real_time_monitor(imd->image_fd);
}

/* load a new image */
Expand Down Expand Up @@ -1116,8 +1121,6 @@ void image_change_from_image(ImageWindow *imd, ImageWindow *source)

imd->collection = source->collection;
imd->collection_info = source->collection_info;
imd->size = source->size;
imd->mtime = source->mtime;

image_loader_free(imd->il);
imd->il = NULL;
Expand Down Expand Up @@ -1350,45 +1353,29 @@ void image_prebuffer_set(ImageWindow *imd, FileData *fd)
}
}

static gint image_auto_refresh_cb(gpointer data)
static void image_notify_cb(FileData *fd, NotifyType type, gpointer data)
{
ImageWindow *imd = data;
time_t newtime;

if (!imd || !image_get_pixbuf(imd) ||
imd->il || !imd->image_fd ||
!options->update_on_time_change) return TRUE;
!options->update_on_time_change) return;

newtime = filetime(imd->image_fd->path);
if (newtime > 0 && newtime != imd->mtime)
if (type == NOTIFY_TYPE_REREAD && fd == imd->image_fd)
{
imd->mtime = newtime;
image_reload(imd);
}

return TRUE;
}

/* image auto refresh on time stamp change, in 1/1000's second, -1 disables */

void image_auto_refresh(ImageWindow *imd, gint interval)
void image_auto_refresh_enable(ImageWindow *imd, gboolean enable)
{
if (!imd) return;
if (pixbuf_renderer_get_tiles((PixbufRenderer *)imd->pr)) return;

if (imd->auto_refresh_id > -1)
{
g_source_remove(imd->auto_refresh_id);
imd->auto_refresh_id = -1;
imd->auto_refresh_interval = -1;
}
if (!enable && imd->auto_refresh && imd->image_fd)
file_data_unregister_real_time_monitor(imd->image_fd);

if (interval < 0) return;
if (enable && !imd->auto_refresh && imd->image_fd)
file_data_register_real_time_monitor(imd->image_fd);

if (interval == 0) interval = IMAGE_AUTO_REFRESH_TIME;

imd->auto_refresh_id = g_timeout_add((guint32)interval, image_auto_refresh_cb, imd);
imd->auto_refresh_interval = interval;
imd->auto_refresh = enable;
}

void image_top_window_set_sync(ImageWindow *imd, gint allow_sync)
Expand Down Expand Up @@ -1600,10 +1587,14 @@ static void image_free(ImageWindow *imd)
{
image_list = g_list_remove(image_list, imd);

if (imd->auto_refresh && imd->image_fd)
file_data_unregister_real_time_monitor(imd->image_fd);

file_data_unregister_notify_func(image_notify_cb, imd);

image_reset(imd);

image_read_ahead_cancel(imd);
image_auto_refresh(imd, -1);

file_data_unref(imd->image_fd);
g_free(imd->title);
Expand Down Expand Up @@ -1713,8 +1704,7 @@ ImageWindow *image_new(gint frame)
imd->color_profile_use_image = FALSE;
imd->color_profile_from_image = COLOR_PROFILE_NONE;

imd->auto_refresh_id = -1;
imd->auto_refresh_interval = -1;
imd->auto_refresh = FALSE;

imd->delay_flip = FALSE;

Expand Down Expand Up @@ -1757,6 +1747,8 @@ ImageWindow *image_new(gint frame)
g_signal_connect(G_OBJECT(imd->pr), "drag",
G_CALLBACK(image_drag_cb), imd);

file_data_register_notify_func(image_notify_cb, imd, NOTIFY_PRIORITY_LOW);

image_list = g_list_append(image_list, imd);

return imd;
Expand Down
4 changes: 2 additions & 2 deletions src/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ gdouble image_zoom_get_default(ImageWindow *imd);
/* read ahead, pass NULL to cancel */
void image_prebuffer_set(ImageWindow *imd, FileData *fd);

/* auto refresh, interval is 1/1000 sec, 0 uses default, -1 disables */
void image_auto_refresh(ImageWindow *imd, gint interval);
/* auto refresh */
void image_auto_refresh_enable(ImageWindow *imd, gboolean enable);

/* allow top window to be resized ? */
void image_top_window_set_sync(ImageWindow *imd, gint allow_sync);
Expand Down
2 changes: 1 addition & 1 deletion src/img-view.c
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ static ViewWindow *real_view_window_new(FileData *fd, GList *list, CollectionDat

image_attach_window(vw->imd, vw->window, NULL, GQ_APPNAME, TRUE);

image_auto_refresh(vw->imd, 0);
image_auto_refresh_enable(vw->imd, TRUE);
image_top_window_set_sync(vw->imd, TRUE);

gtk_container_add(GTK_CONTAINER(vw->window), vw->imd->widget);
Expand Down
2 changes: 1 addition & 1 deletion src/layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ void layout_status_update_image(LayoutWindow *lw)
gtk_label_set_text(GTK_LABEL(lw->info_zoom), text);
g_free(text);

b = text_from_size(lw->image->size);
b = image_get_fd(lw->image) ? text_from_size(image_get_fd(lw->image)->size) : g_strdup("0");

if (lw->image->unknown)
{
Expand Down
2 changes: 1 addition & 1 deletion src/layout_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,7 @@ GtkWidget *layout_image_new(LayoutWindow *lw, gint i)

image_background_set_color(lw->split_images[i], options->image.use_custom_border_color ? &options->image.border_color : NULL);

image_auto_refresh(lw->split_images[i], 0);
image_auto_refresh_enable(lw->split_images[i], TRUE);

layout_image_dnd_init(lw, i);
image_color_profile_set(lw->split_images[i],
Expand Down
5 changes: 1 addition & 4 deletions src/typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,6 @@ struct _ImageWindow

FileData *image_fd;

gint64 size; /* file size (bytes) */
time_t mtime; /* file modified time stamp */
gint unknown; /* failed to load image */

ImageLoader *il; /* FIXME - image loader should probably go to FileData, but it must first support
Expand Down Expand Up @@ -414,8 +412,7 @@ struct _ImageWindow

gint prev_color_row;

gint auto_refresh_id;
gint auto_refresh_interval;
gint auto_refresh;

gint delay_flip;
gint orientation;
Expand Down

0 comments on commit 6da5f50

Please sign in to comment.