Not so soon, I guess, since that FIXME was from r6305.
[lyx.git] / src / graphics / GraphicsCache.cpp
blobd50856389197f057675ebc910068d81ff7c34534
1 /**
2 * \file GraphicsCache.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Baruch Even
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
12 #include <config.h>
14 #include "GraphicsCache.h"
15 #include "GraphicsCacheItem.h"
16 #include "GraphicsImage.h"
18 #include "Format.h"
20 #include "frontends/Application.h"
22 #include "support/debug.h"
23 #include "support/FileName.h"
24 #include "support/filetools.h"
26 #include <map>
28 using namespace std;
29 using namespace lyx::support;
31 namespace lyx {
33 namespace graphics {
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;
40 class Cache::Impl {
41 public:
42 ///
43 CacheType cache;
47 Cache & Cache::get()
49 // Now return the cache
50 static Cache singleton;
51 return singleton;
55 Cache::Cache()
56 : pimpl_(new Impl)
60 Cache::~Cache()
62 delete pimpl_;
66 vector<string> const & Cache::loadableFormats() const
68 static vector<string> fmts;
70 if (!fmts.empty())
71 return fmts;
73 // The formats recognised by LyX
74 Formats::const_iterator begin = formats.begin();
75 Formats::const_iterator end = formats.end();
77 // The formats natively loadable.
78 vector<string> nformat = frontend::loadableImageFormats();
80 vector<string>::const_iterator it = nformat.begin();
81 for (; it != nformat.end(); ++it) {
82 for (Formats::const_iterator fit = begin; fit != end; ++fit) {
83 if (fit->extension() == *it) {
84 fmts.push_back(fit->name());
85 break;
90 if (lyxerr.debugging()) {
91 LYXERR(Debug::GRAPHICS, "LyX recognises the following image formats:");
93 vector<string>::const_iterator fbegin = fmts.begin();
94 vector<string>::const_iterator fend = fmts.end();
95 for (vector<string>::const_iterator fit = fbegin; fit != fend; ++fit) {
96 if (fit != fbegin)
97 LYXERR(Debug::GRAPHICS, ", ");
98 LYXERR(Debug::GRAPHICS, *fit);
100 LYXERR(Debug::GRAPHICS, '\n');
103 return fmts;
107 void Cache::add(FileName const & file) const
109 // Is the file in the cache already?
110 if (inCache(file)) {
111 LYXERR(Debug::GRAPHICS, "Cache::add(" << file << "):\n"
112 << "The file is already in the cache.");
113 return;
116 pimpl_->cache[file] = ItemPtr(new CacheItem(file));
120 void Cache::remove(FileName const & file) const
122 CacheType::iterator it = pimpl_->cache.find(file);
123 if (it == pimpl_->cache.end())
124 return;
126 ItemPtr & item = it->second;
128 if (item.use_count() == 1) {
129 // The graphics file is in the cache, but nothing else
130 // references it.
131 pimpl_->cache.erase(it);
136 bool Cache::inCache(FileName const & file) const
138 return pimpl_->cache.find(file) != pimpl_->cache.end();
142 Cache::ItemPtr const Cache::item(FileName const & file) const
144 CacheType::const_iterator it = pimpl_->cache.find(file);
145 if (it == pimpl_->cache.end())
146 return ItemPtr();
148 return it->second;
151 } // namespace graphics
152 } // namespace lyx