This draft patch should detect when a graphic file is changed,
and rebuild the cache.

A time_t variable (filemodtime_) is added to GraphicsCacheItem it is
set to the ModificationTime of the graphic file each time the
GraphicsCacheItem is modified. Each time insetgraphics performs a
setCache, it calls GraphicsCache::checkfile (a new method) if the
graphic file is already loaded.
GraphicsCache::checkfile compares GraphicsCacheItem::filemodtime_ and
the current modification time, and, if the graphic file is newer it
removes the cache item (cut and paste from GraphicsCache::update)

Seems to work, but it needs two setCache (or something similar), 
otherwise the graphics is only removed from the screen and not redrawn;
if I scroll the document the image is converted again.
But it does not regenerate the .ps when I update the compiled file
(dvi, ps, pdf etc...).
I'd really appreciate comments and suggestions to improve the patch
(I don't have clear the Cache behaviour, but perhaps you can
suggest me how to do it right)

Thanks in advance,

Marco Morandini

P.S. the diff is agains lyx 1.2.2 cvs
Index: src/graphics/GraphicsCache.C
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/graphics/GraphicsCache.C,v
retrieving revision 1.24
diff -u -r1.24 GraphicsCache.C
--- src/graphics/GraphicsCache.C        2002/04/19 09:17:05     1.24
+++ src/graphics/GraphicsCache.C        2002/08/30 13:50:43
@@ -19,6 +19,7 @@
 #include "GraphicsParams.h"
 #include "insets/insetgraphics.h"
 #include "frontends/GUIRunTime.h"
+#include "support/FileInfo.h"
 
 
 namespace grfx {
@@ -84,6 +85,42 @@
                (*cache)[params.filename] = item;
 }
 
+void GCache::checkfile(InsetGraphics const & inset, string const & filepath)
+{
+       // A subset only of InsetGraphicsParams is needed for display purposes.
+       // The GraphicsParams c-tor also interrogates lyxrc to ascertain whether
+       // to display or not.
+       GParams params(inset.params(), filepath);
+       
+       // Each inset can reference only one file, so check the cache for any
+       // graphics files referenced by inset. If the name of this file is
+       // different from that in params, then remove the reference.
+       CacheType::iterator it = find(inset);
+
+       if (it != cache->end()) {
+               FileInfo fi(params.filename);
+               CacheItemType item = it->second;
+               if (fi.isOK()) {
+                       if (item->filemodtime()<fi.getModificationTime()) {
+                               item->remove(inset);
+                               if (item->empty())
+                                       cache->erase(it);
+                       }
+               }
+       }
+
+       // Are we adding a new file or modifying the display of an existing one?
+       it = cache->find(params.filename);
+
+       if (it != cache->end()) {
+               it->second->modify(inset, params);
+               return;
+       }
+
+       CacheItemType item(new GCacheItem(inset, params));
+       if (item.get() != 0)
+               (*cache)[params.filename] = item;
+}
 
 void GCache::remove(InsetGraphics const & inset)
 {
Index: src/graphics/GraphicsCache.h
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/graphics/GraphicsCache.h,v
retrieving revision 1.18
diff -u -r1.18 GraphicsCache.h
--- src/graphics/GraphicsCache.h        2002/04/19 09:17:05     1.18
+++ src/graphics/GraphicsCache.h        2002/08/30 13:50:43
@@ -45,6 +45,9 @@
        /// Add a file to the cache (or modify an existing image).
        void update(InsetGraphics const &, string const & filepath);
 
+       /// Chech the cached file modification time.
+       void checkfile(InsetGraphics const &, string const & filepath);
+
        /** Remove the data associated with this inset.
         *  Called from the InsetGraphics d-tor.
         */
Index: src/graphics/GraphicsCacheItem.C
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/graphics/GraphicsCacheItem.C,v
retrieving revision 1.33.2.2
diff -u -r1.33.2.2 GraphicsCacheItem.C
--- src/graphics/GraphicsCacheItem.C    2002/07/17 21:21:38     1.33.2.2
+++ src/graphics/GraphicsCacheItem.C    2002/08/30 13:50:43
@@ -26,6 +26,7 @@
 #include "lyx_main.h" // for global dispatch method
 #include "support/LAssert.h"
 #include "support/filetools.h"
+#include "support/FileInfo.h"
 
 // Very, Very UGLY!
 extern BufferView * current_view;
@@ -36,7 +37,8 @@
 namespace grfx {
 
 GCacheItem::GCacheItem(InsetGraphics const & inset, GParams const & params)
-       : filename_(params.filename), zipped_(false),
+       : filename_(params.filename), 
+         filemodtime_(0), zipped_(false),
          remove_loaded_file_(false), status_(WaitingToLoad)
 {
        ModifiedItemPtr item(new ModifiedItem(inset, params, image_));
@@ -48,6 +50,7 @@
 
 typedef GCacheItem::ModifiedItemPtr ModifiedItemPtr;
 
+
 class Compare_Params {
 public:
        Compare_Params(GParams const & p) : p_(p) {}
@@ -81,6 +84,12 @@
 } // namespace anon
 
 
+time_t const & GCacheItem::filemodtime() const
+{
+       return filemodtime_;
+}
+
+
 void GCacheItem::modify(InsetGraphics const & inset, GParams const & params)
 {
        // Does this inset currently reference an existing ModifiedItem with
@@ -332,6 +341,14 @@
                        lyx::unlink(unzipped_filename_);
 
                return;
+       } else {
+               FileInfo fi(filename());
+               if (fi.isOK())
+                       filemodtime_ = fi.getModificationTime();
+               else {
+                       //should we raise an error?
+                       filemodtime_ = 0;
+               }
        }
 
        cc_.disconnect();
Index: src/graphics/GraphicsCacheItem.h
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/graphics/GraphicsCacheItem.h,v
retrieving revision 1.22
diff -u -r1.22 GraphicsCacheItem.h
--- src/graphics/GraphicsCacheItem.h    2002/04/11 17:40:44     1.22
+++ src/graphics/GraphicsCacheItem.h    2002/08/30 13:50:44
@@ -71,6 +71,9 @@
        /// The name of the original image file.
        string const & filename() const;
 
+       /// The modification time of the original image file.
+       time_t const & filemodtime() const;
+
        /// Is this image file referenced by this inset?
        bool referencedBy(InsetGraphics const &) const;
 
@@ -137,6 +140,8 @@
 
        /// The filename we refer too.
        string filename_;
+       /// the modification time of the converted file;
+       time_t filemodtime_;
        /// Is the file compressed?
        bool zipped_;
        /// If so, store the uncompressed file in this temporary file.
Index: src/insets/insetgraphics.C
===================================================================
RCS file: /cvs/lyx/lyx-devel/src/insets/insetgraphics.C,v
retrieving revision 1.109.2.5
diff -u -r1.109.2.5 insetgraphics.C
--- src/insets/insetgraphics.C  2002/07/16 22:11:17     1.109.2.5
+++ src/insets/insetgraphics.C  2002/08/30 13:50:45
@@ -197,6 +197,9 @@
                return;
 
        grfx::GCache & gc = grfx::GCache::get();
+       string filepath = OnlyPath(params().filename);
+       if (cached_status_ == grfx::Loaded)
+               gc.checkfile(*this,filepath);
        cached_status_ = gc.status(*this);
        cached_image_  = gc.image(*this);
 }

Reply via email to