2 * \file GraphicsCache.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
14 #include "GraphicsCache.h"
15 #include "GraphicsCacheItem.h"
16 #include "GraphicsImage.h"
20 #include "frontends/Application.h"
22 #include "support/debug.h"
23 #include "support/FileName.h"
24 #include "support/filetools.h"
29 using namespace lyx::support;
35 /** The cache contains one item per file, so use a map to find the
36 * cache item quickly by filename.
38 typedef map<FileName, Cache::ItemPtr> CacheType;
49 // Now return the cache
50 static Cache singleton;
66 vector<string> const & Cache::loadableFormats() const
68 static vector<string> fmts;
73 for (string const & native_extension : frontend::loadableImageFormats()) {
74 for (Format const & format : theFormats()) {
75 if (format.extension() == native_extension) {
76 fmts.push_back(format.name());
82 if (lyxerr.debugging()) {
83 LYXERR(Debug::GRAPHICS, "LyX recognises the following image formats:");
84 for (string const & format : fmts) {
85 LYXERR(Debug::GRAPHICS, format << ',');
93 void Cache::add(FileName const & file, FileName const & doc_file) const
95 // Is the file in the cache already?
97 LYXERR(Debug::GRAPHICS, "Cache::add(" << file << "):\n"
98 << "The file is already in the cache.");
102 pimpl_->cache[file] = ItemPtr(new CacheItem(file, doc_file));
106 void Cache::remove(FileName const & file) const
108 CacheType::iterator it = pimpl_->cache.find(file);
109 if (it == pimpl_->cache.end())
112 ItemPtr & item = it->second;
114 if (item.use_count() == 1) {
115 // The graphics file is in the cache, but nothing else
117 pimpl_->cache.erase(it);
122 bool Cache::inCache(FileName const & file) const
124 return pimpl_->cache.find(file) != pimpl_->cache.end();
128 Cache::ItemPtr const Cache::item(FileName const & file) const
130 CacheType::const_iterator it = pimpl_->cache.find(file);
131 if (it == pimpl_->cache.end())
137 } // namespace graphics