-/*
+/**
* \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"
-
-namespace grfx {
+#include "debug.h"
-GCache & GCache::get()
-{
- static bool start = true;
- if (start) {
- start = false;
- GUIRunTime::initialiseGraphics();
- }
+#include "support/filetools.h"
- // Now return the cache
- static GCache singleton;
- return singleton;
-}
+#include "frontends/lyx_gui.h"
+#include <map>
-GCache::GCache()
-{
- cache = new CacheType;
-}
+namespace grfx {
+/** 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;
-// all elements are destroyed by the shared_ptr's in the map.
-GCache::~GCache()
-{
- delete cache;
-}
+struct Cache::Impl {
+ ///
+ CacheType cache;
+};
-void GCache::update(InsetGraphics const & inset, string const & filepath)
+Cache & Cache::get()
{
- // 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()) {
- CacheItemType item = it->second;
- if (item->filename() != params.filename) {
- 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;
+ // Now return the cache
+ static Cache singleton;
+ return singleton;
}
-void GCache::remove(InsetGraphics const & inset)
-{
- CacheType::iterator it = find(inset);
- if (it == cache->end())
- return;
-
- CacheItemType item = it->second;
- item->remove(inset);
- if (item->empty()) {
- cache->erase(it);
- }
-}
-
+Cache::Cache()
+ : pimpl_(new Impl())
+{}
-void GCache::startLoading(InsetGraphics const & inset)
-{
- CacheType::iterator it = find(inset);
- if (it == cache->end())
- return;
- it->second->startLoading(inset);
-}
+Cache::~Cache()
+{}
-ImagePtr const GCache::image(InsetGraphics const & inset) const
+std::vector<string> Cache::loadableFormats() const
{
- CacheType::const_iterator it = find(inset);
- if (it == cache->end())
- return ImagePtr();
-
- return it->second->image(inset);
+ return Image::loadableFormats();
}
-ImageStatus GCache::status(InsetGraphics const & inset) const
+void Cache::add(string const & file) const
{
- CacheType::const_iterator it = find(inset);
- if (it == cache->end())
- return ErrorUnknown;
-
- return it->second->status(inset);
-}
+ if (!AbsolutePath(file)) {
+ lyxerr << "Cache::add(" << file << "):\n"
+ << "The file must be have an absolute path."
+ << std::endl;
+ return;
+ }
+ // 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;
+ }
-void GCache::changeDisplay(bool changed_background)
-{
- CacheType::iterator it = cache->begin();
- CacheType::iterator end = cache->end();
- for(; it != end; ++it)
- it->second->changeDisplay(changed_background);
+ pimpl_->cache[file] = ItemPtr(new CacheItem(file));
}
-GCache::CacheType::iterator
-GCache::find(InsetGraphics const & inset)
+void Cache::remove(string const & file) const
{
- CacheType::iterator it = cache->begin();
- CacheType::iterator end = cache->end();
- for (; it != end; ++it) {
- if (it->second->referencedBy(inset))
- return it;
- }
-
- return cache->end();
-}
+ CacheType::iterator it = pimpl_->cache.find(file);
+ if (it == pimpl_->cache.end())
+ return;
+ ItemPtr & item = it->second;
-GCache::CacheType::const_iterator
-GCache::find(InsetGraphics const & inset) const
-{
- CacheType::const_iterator it = cache->begin();
- CacheType::const_iterator end = cache->end();
- for (; it != end; ++it) {
- if (it->second->referencedBy(inset))
- return it;
+ if (item.use_count() == 1) {
+ // The graphics file is in the cache, but nothing else
+ // references it.
+ pimpl_->cache.erase(it);
}
-
- return cache->end();
}
-unsigned int GCache::raw_width(string const & filename) const
+bool Cache::inCache(string const & file) const
{
- CacheType::const_iterator it = cache->find(filename);
- if (it == cache->end())
- return 0;
-
- return it->second->raw_width();
+ return pimpl_->cache.find(file) != pimpl_->cache.end();
}
-unsigned int GCache::raw_height(string const & filename) const
+Cache::ItemPtr const Cache::item(string const & file) const
{
- CacheType::const_iterator it = cache->find(filename);
- if (it == cache->end())
- return 0;
+ CacheType::const_iterator it = pimpl_->cache.find(file);
+ if (it == pimpl_->cache.end())
+ return ItemPtr();
- return it->second->raw_height();
+ return it->second;
}
} // namespace grfx
-