From: Sebastian Harl Date: Fri, 11 Nov 2011 13:17:30 +0000 (+0100) Subject: gtk-tpdfv: Auto-reload the PDF on expose if the file has changed. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=1e9e5005b44d65819a9969b613c784e50f90030f;p=tpdfview.git gtk-tpdfv: Auto-reload the PDF on expose if the file has changed. --- diff --git a/src/gtk-tpdfv.c b/src/gtk-tpdfv.c index ee68357..cba2e1f 100644 --- a/src/gtk-tpdfv.c +++ b/src/gtk-tpdfv.c @@ -43,6 +43,8 @@ #include #include +#include + #define TPDFV_MIN(a, b) ((a) <= (b) ? (a) : (b)) #define TPDFV_MAX(a, b) ((a) >= (b) ? (a) : (b)) @@ -56,6 +58,8 @@ typedef enum { typedef struct { char *filename; + time_t mtime; + PopplerDocument *doc; PopplerPage *current_page; @@ -84,9 +88,21 @@ tpdfv_open(gtk_tpdfv_t *pdf) { GError *err = NULL; + struct stat statbuf; + if ((! pdf) || (! pdf->filename)) return; + memset(&statbuf, 0, sizeof(statbuf)); + if (stat(pdf->filename + strlen("file://"), &statbuf) != 0) { + char errbuf[1024]; + strerror_r(errno, errbuf, sizeof(errbuf)); + fprintf(stderr, "Failed to access PDF: %s.\n", errbuf); + return; + } + + pdf->mtime = statbuf.st_mtime; + pdf->doc = poppler_document_new_from_file(pdf->filename, /* password = */ NULL, &err); if (! pdf->doc) { @@ -201,6 +217,8 @@ gtk_tpdfv_expose(GtkWidget *tpdfv, GdkEventExpose *event) gtk_tpdfv_t *pdf; cairo_t *cr; + struct stat statbuf; + gdouble width, height; gdouble page_width = 0.0; @@ -214,6 +232,17 @@ gtk_tpdfv_expose(GtkWidget *tpdfv, GdkEventExpose *event) if (! pdf) return FALSE; + memset(&statbuf, 0, sizeof(statbuf)); + if (stat(pdf->filename + strlen("file://"), &statbuf)) { + char errbuf[1024]; + strerror_r(errno, errbuf, sizeof(errbuf)); + fprintf(stderr, "Failed to access PDF: %s.\n", errbuf); + return FALSE; + } + + if (statbuf.st_mtime > pdf->mtime) + gtk_tpdfv_reload(tpdfv); + cr = gdk_cairo_create(tpdfv->window); cairo_rectangle(cr, event->area.x, event->area.y, event->area.width, event->area.height);