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 "support/debug.h"
21 #include "support/FileName.h"
22 #include "support/filetools.h"
23 #include "support/imagetools.h"
28 using namespace lyx::support;
34 /** The cache contains one item per file, so use a map to find the
35 * cache item quickly by filename.
37 typedef map<FileName, Cache::ItemPtr> CacheType;
48 // Now return the cache
49 static Cache singleton;
65 vector<string> const & Cache::loadableFormats() const
67 static vector<string> fmts;
72 // The formats recognised by LyX
73 Formats::const_iterator begin = formats.begin();
74 Formats::const_iterator end = formats.end();
76 // The formats natively loadable.
77 vector<string> nformat = loadableImageFormats();
79 vector<string>::const_iterator it = nformat.begin();
80 for (; it != nformat.end(); ++it) {
81 for (Formats::const_iterator fit = begin; fit != end; ++fit) {
82 if (fit->extension() == *it) {
83 fmts.push_back(fit->name());
89 if (lyxerr.debugging()) {
90 LYXERR(Debug::GRAPHICS, "LyX recognises the following image formats:");
92 vector<string>::const_iterator fbegin = fmts.begin();
93 vector<string>::const_iterator fend = fmts.end();
94 for (vector<string>::const_iterator fit = fbegin; fit != fend; ++fit) {
96 LYXERR(Debug::GRAPHICS, ", ");
97 LYXERR(Debug::GRAPHICS, *fit);
99 LYXERR(Debug::GRAPHICS, '\n');
106 void Cache::add(FileName const & file) const
108 // Is the file in the cache already?
110 LYXERR(Debug::GRAPHICS, "Cache::add(" << file << "):\n"
111 << "The file is already in the cache.");
115 pimpl_->cache[file] = ItemPtr(new CacheItem(file));
119 void Cache::remove(FileName const & file) const
121 CacheType::iterator it = pimpl_->cache.find(file);
122 if (it == pimpl_->cache.end())
125 ItemPtr & item = it->second;
127 if (item.use_count() == 1) {
128 // The graphics file is in the cache, but nothing else
130 pimpl_->cache.erase(it);
135 bool Cache::inCache(FileName const & file) const
137 return pimpl_->cache.find(file) != pimpl_->cache.end();
141 Cache::ItemPtr const Cache::item(FileName const & file) const
143 CacheType::const_iterator it = pimpl_->cache.find(file);
144 if (it == pimpl_->cache.end())
150 } // namespace graphics