]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/GraphicsCache.C
The std::string mammoth path.
[lyx.git] / src / graphics / GraphicsCache.C
index b478331033086f5b48e82136686ce20d0e76b293..124141604e3fa115fb0f642d0e9b0ef48d892735 100644 (file)
-/*
+/**
  * \file GraphicsCache.C
- * Copyright 2002 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * \author Baruch Even <baruch.even@writeme.com>
- * \author Angus Leeming <a.leeming@ic.ac.uk>
+ * \author Baruch Even
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "GraphicsCache.h"
 #include "GraphicsCacheItem.h"
 #include "GraphicsImage.h"
-#include "GraphicsParams.h"
-#include "insets/insetgraphics.h"
-#include "frontends/GUIRunTime.h"
 
+#include "debug.h"
 
-namespace grfx {
+#include "support/filetools.h"
 
-GCache & GCache::get()
-{
-       static bool start = true;
-       if (start) {
-               start = false;
-               GUIRunTime::initialiseGraphics();
-       }
+namespace support = lyx::support;
 
-       // Now return the cache
-       static GCache singleton;
-       return singleton;
-}
+using std::string;
 
 
-GCache::GCache()
-{
-       cache = new CacheType;
-}
+namespace lyx {
+namespace graphics {
+
+/** The cache contains one item per file, so use a map to find the
+ *  cache item quickly by filename.
+ */
+typedef std::map<string, Cache::ItemPtr> CacheType;
+
+struct Cache::Impl {
+       ///
+       CacheType cache;
+};
 
 
-// all elements are destroyed by the shared_ptr's in the map.
-GCache::~GCache()
+Cache & Cache::get()
 {
-       delete cache;
+       // Now return the cache
+       static Cache singleton;
+       return singleton;
 }
 
 
-void GCache::update(InsetGraphics const & inset)
-{
-       // 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());
-
-       // 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()) {
-               CacheItemType item = it->second;
-               if (item->filename() != params.filename) {
-                       item->remove(inset);
-                       if (item->empty())
-                               cache->erase(it);
-               }
-       }
+Cache::Cache()
+       : pimpl_(new Impl)
+{}
 
-       // 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;
-       }
+Cache::~Cache()
+{}
 
-       CacheItemType item(new GCacheItem(inset, params));
-       if (item.get() != 0)
-               (*cache)[params.filename] = item;
+
+std::vector<string> Cache::loadableFormats() const
+{
+       return Image::loadableFormats();
 }
 
 
-void GCache::remove(InsetGraphics const & inset)
+void Cache::add(string const & file) const
 {
-       CacheType::iterator it = find(inset);
-       if (it == cache->end())
+       if (!support::AbsolutePath(file)) {
+               lyxerr << "Cache::add(" << file << "):\n"
+                      << "The file must be have an absolute path."
+                      << std::endl;
                return;
-
-       CacheItemType item = it->second;
-       item->remove(inset);
-       if (item->empty()) {
-               cache->erase(it);
        }
-}
-
 
-void GCache::startLoading(InsetGraphics const & inset)
-{
-       CacheType::iterator it = find(inset);
-       if (it == cache->end())
+       // Is the file in the cache already?
+       if (inCache(file)) {
+               lyxerr[Debug::GRAPHICS] << "Cache::add(" << file << "):\n"
+                                       << "The file is already in the cache."
+                                       << std::endl;
                return;
+       }
 
-       it->second->startLoading(inset);
+       pimpl_->cache[file] = ItemPtr(new CacheItem(file));
 }
 
 
-ImagePtr const GCache::image(InsetGraphics const & inset) const
+void Cache::remove(string const & file) const
 {
-       CacheType::const_iterator it = find(inset);
-       if (it == cache->end())
-               return ImagePtr();
-
-       return it->second->image(inset);
-}
-
+       CacheType::iterator it = pimpl_->cache.find(file);
+       if (it == pimpl_->cache.end())
+               return;
 
-ImageStatus GCache::status(InsetGraphics const & inset) const
-{
-       CacheType::const_iterator it = find(inset);
-       if (it == cache->end())
-               return ErrorUnknown;
+       ItemPtr & item = it->second;
 
-       return it->second->status(inset);
+       if (item.use_count() == 1) {
+               // The graphics file is in the cache, but nothing else
+               // references it.
+               pimpl_->cache.erase(it);
+       }
 }
 
 
-void GCache::changeDisplay(bool changed_background)
+bool Cache::inCache(string const & file) const
 {
-       CacheType::iterator it = cache->begin();
-       CacheType::iterator end = cache->end();
-       for(; it != end; ++it)
-               it->second->changeDisplay(changed_background);
+       return pimpl_->cache.find(file) != pimpl_->cache.end();
 }
 
 
-GCache::CacheType::iterator
-GCache::find(InsetGraphics const & inset)
+Cache::ItemPtr const Cache::item(string const & file) const
 {
-       CacheType::iterator it = cache->begin();
-       for (; it != cache->end(); ++it) {
-               if (it->second->referencedBy(inset))
-                       return it;
-       }
-       
-       return cache->end();
-}
-
+       CacheType::const_iterator it = pimpl_->cache.find(file);
+       if (it == pimpl_->cache.end())
+               return ItemPtr();
 
-GCache::CacheType::const_iterator
-GCache::find(InsetGraphics const & inset) const
-{
-       CacheType::const_iterator it = cache->begin();
-       for (; it != cache->end(); ++it) {
-               if (it->second->referencedBy(inset))
-                       return it;
-       }
-       
-       return cache->end();
+       return it->second;
 }
 
-} // namespace grfx
+} // namespace graphics
+} // namespace lyx