Code

gtk-tpdfv: Auto-reload the PDF on expose if the file has changed.
authorSebastian Harl <sh@tokkee.org>
Fri, 11 Nov 2011 13:17:30 +0000 (14:17 +0100)
committerSebastian Harl <sh@tokkee.org>
Fri, 11 Nov 2011 13:17:30 +0000 (14:17 +0100)
src/gtk-tpdfv.c

index ee68357d1253de739f77b7002a4785f2b3f4346b..cba2e1f2a1c13697e384cb8d1eeab7e62c0547e8 100644 (file)
@@ -43,6 +43,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <sys/stat.h>
+
 #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);