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;
50 // Now return the cache
51 static Cache singleton;
67 vector<string> const & Cache::loadableFormats() const
69 static vector<string> fmts;
74 // The formats recognised by LyX
75 Formats::const_iterator begin = formats.begin();
76 Formats::const_iterator end = formats.end();
78 // The formats natively loadable.
79 vector<string> nformat = frontend::loadableImageFormats();
81 vector<string>::const_iterator it = nformat.begin();
82 for (; it != nformat.end(); ++it) {
83 for (Formats::const_iterator fit = begin; fit != end; ++fit) {
84 if (fit->extension() == *it) {
85 fmts.push_back(fit->name());
91 if (lyxerr.debugging()) {
92 LYXERR(Debug::GRAPHICS, "LyX recognises the following image formats:");
94 vector<string>::const_iterator fbegin = fmts.begin();
95 vector<string>::const_iterator fend = fmts.end();
96 for (vector<string>::const_iterator fit = fbegin; fit != fend; ++fit) {
98 LYXERR(Debug::GRAPHICS, ", ");
99 LYXERR(Debug::GRAPHICS, *fit);
101 LYXERR(Debug::GRAPHICS, '\n');
108 void Cache::add(FileName const & file) const
110 // Is the file in the cache already?
112 LYXERR(Debug::GRAPHICS, "Cache::add(" << file << "):\n"
113 << "The file is already in the cache.");
117 pimpl_->cache[file] = ItemPtr(new CacheItem(file));
121 void Cache::remove(FileName const & file) const
123 CacheType::iterator it = pimpl_->cache.find(file);
124 if (it == pimpl_->cache.end())
127 ItemPtr & item = it->second;
129 if (item.use_count() == 1) {
130 // The graphics file is in the cache, but nothing else
132 pimpl_->cache.erase(it);
137 bool Cache::inCache(FileName const & file) const
139 return pimpl_->cache.find(file) != pimpl_->cache.end();
143 Cache::ItemPtr const Cache::item(FileName const & file) const
145 CacheType::const_iterator it = pimpl_->cache.find(file);
146 if (it == pimpl_->cache.end())
152 } // namespace graphics