changeset 888:d8e1e820cee7

reload changed images
author nadvornik
date Thu, 17 Jul 2008 21:51:21 +0000
parents e418c33a49c8
children cb3b6238782a
files src/filecache.c src/filedata.c src/filedata.h src/fullscreen.c src/image-overlay.c src/image.c src/image.h src/img-view.c src/layout.c src/layout_image.c src/typedefs.h
diffstat 11 files changed, 89 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/src/filecache.c	Tue Jul 15 15:37:14 2008 +0000
+++ b/src/filecache.c	Thu Jul 17 21:51:21 2008 +0000
@@ -31,6 +31,8 @@
 	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);
@@ -38,6 +40,9 @@
 	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;
 }
 
@@ -60,6 +65,10 @@
 			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;
 			}
@@ -85,7 +94,7 @@
 		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);
@@ -125,6 +134,33 @@
 	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;
@@ -139,3 +175,13 @@
 		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);
+		}
+}
\ No newline at end of file
--- a/src/filedata.c	Tue Jul 15 15:37:14 2008 +0000
+++ b/src/filedata.c	Thu Jul 17 21:51:21 2008 +0000
@@ -259,7 +259,7 @@
 }
 
 
-static gboolean file_data_check_changed_files(FileData *fd)
+gboolean file_data_check_changed_files(FileData *fd)
 {
 	gboolean ret = FALSE;
 	struct stat st;
--- a/src/filedata.h	Tue Jul 15 15:37:14 2008 +0000
+++ b/src/filedata.h	Thu Jul 17 21:51:21 2008 +0000
@@ -35,6 +35,8 @@
 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);
--- a/src/fullscreen.c	Tue Jul 15 15:37:14 2008 +0000
+++ b/src/fullscreen.c	Thu Jul 17 21:51:21 2008 +0000
@@ -296,7 +296,7 @@
 		}
 
 	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)
 		{
--- a/src/image-overlay.c	Tue Jul 15 15:37:14 2008 +0000
+++ b/src/image-overlay.c	Thu Jul 17 21:51:21 2008 +0000
@@ -520,8 +520,8 @@
 		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)
--- a/src/image.c	Tue Jul 15 15:37:14 2008 +0000
+++ b/src/image.c	Thu Jul 17 21:51:21 2008 +0000
@@ -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;
 
 
@@ -773,8 +770,6 @@
 
 			imd->unknown = TRUE;
 			}
-		imd->size = filesize(imd->image_fd->path);
-		imd->mtime = filetime(imd->image_fd->path);
 		}
 	else
 		{
@@ -785,15 +780,12 @@
 			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);
@@ -806,13 +798,20 @@
 	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);
 }
 
 /*
@@ -988,11 +987,17 @@
 /* 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 */
@@ -1116,8 +1121,6 @@
 
 	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;
@@ -1350,45 +1353,29 @@
 		}
 }
 
-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 (!enable && imd->auto_refresh && imd->image_fd)
+		file_data_unregister_real_time_monitor(imd->image_fd);
 
-	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_register_real_time_monitor(imd->image_fd);
 
-	if (interval < 0) return;
-
-	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)
@@ -1600,10 +1587,14 @@
 {
 	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);
@@ -1713,8 +1704,7 @@
 	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;
 
@@ -1757,6 +1747,8 @@
 	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;
--- a/src/image.h	Tue Jul 15 15:37:14 2008 +0000
+++ b/src/image.h	Thu Jul 17 21:51:21 2008 +0000
@@ -88,8 +88,8 @@
 /* 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);
--- a/src/img-view.c	Tue Jul 15 15:37:14 2008 +0000
+++ b/src/img-view.c	Thu Jul 17 21:51:21 2008 +0000
@@ -854,7 +854,7 @@
 
 	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);
--- a/src/layout.c	Tue Jul 15 15:37:14 2008 +0000
+++ b/src/layout.c	Thu Jul 17 21:51:21 2008 +0000
@@ -585,7 +585,7 @@
 	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)
 		{
--- a/src/layout_image.c	Tue Jul 15 15:37:14 2008 +0000
+++ b/src/layout_image.c	Thu Jul 17 21:51:21 2008 +0000
@@ -1776,7 +1776,7 @@
 
 		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],
--- a/src/typedefs.h	Tue Jul 15 15:37:14 2008 +0000
+++ b/src/typedefs.h	Thu Jul 17 21:51:21 2008 +0000
@@ -351,8 +351,6 @@
 
 	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
@@ -414,8 +412,7 @@
 
 	gint prev_color_row;
 
-	gint auto_refresh_id;
-	gint auto_refresh_interval;
+	gint auto_refresh;
 
 	gint delay_flip;
 	gint orientation;