]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.cpp
d36de0fbe260257bf88627a8efffe74dd691bc69
[lyx.git] / src / graphics / GraphicsCache.cpp
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.
5  *
6  * \author Baruch Even
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GraphicsCache.h"
15 #include "GraphicsCacheItem.h"
16 #include "GraphicsImage.h"
17
18 #include "Format.h"
19
20 #include "support/debug.h"
21 #include "support/FileName.h"
22 #include "support/filetools.h"
23 #include "support/imagetools.h"
24
25 #include <map>
26
27 using namespace std;
28 using namespace lyx::support;
29
30 namespace lyx {
31
32 namespace graphics {
33
34 /** The cache contains one item per file, so use a map to find the
35  *  cache item quickly by filename.
36  */
37 typedef map<FileName, Cache::ItemPtr> CacheType;
38
39 class Cache::Impl {
40 public:
41         ///
42         CacheType cache;
43 };
44
45
46 Cache & Cache::get()
47 {
48         // Now return the cache
49         static Cache singleton;
50         return singleton;
51 }
52
53
54 Cache::Cache()
55         : pimpl_(new Impl)
56 {}
57
58
59 Cache::~Cache()
60 {
61         delete pimpl_;
62 }
63
64
65 vector<string> const & Cache::loadableFormats() const
66 {
67         static vector<string> fmts;
68
69         if (!fmts.empty())
70                 return fmts;
71
72         // The formats recognised by LyX
73         Formats::const_iterator begin = formats.begin();
74         Formats::const_iterator end   = formats.end();
75
76         // The formats natively loadable.
77         vector<string> nformat = loadableImageFormats();
78         
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());
84                                 break;
85                         }
86                 }
87         }
88
89         if (lyxerr.debugging()) {
90                 LYXERR(Debug::GRAPHICS, "LyX recognises the following image formats:");
91
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) {
95                         if (fit != fbegin)
96                                 LYXERR(Debug::GRAPHICS, ", ");
97                         LYXERR(Debug::GRAPHICS, *fit);
98                 }
99                 LYXERR(Debug::GRAPHICS, '\n');
100         }
101
102         return fmts;
103 }
104
105
106 void Cache::add(FileName const & file) const
107 {
108         // Is the file in the cache already?
109         if (inCache(file)) {
110                 LYXERR(Debug::GRAPHICS, "Cache::add(" << file << "):\n"
111                                         << "The file is already in the cache.");
112                 return;
113         }
114
115         pimpl_->cache[file] = ItemPtr(new CacheItem(file));
116 }
117
118
119 void Cache::remove(FileName const & file) const
120 {
121         CacheType::iterator it = pimpl_->cache.find(file);
122         if (it == pimpl_->cache.end())
123                 return;
124
125         ItemPtr & item = it->second;
126
127         if (item.use_count() == 1) {
128                 // The graphics file is in the cache, but nothing else
129                 // references it.
130                 pimpl_->cache.erase(it);
131         }
132 }
133
134
135 bool Cache::inCache(FileName const & file) const
136 {
137         return pimpl_->cache.find(file) != pimpl_->cache.end();
138 }
139
140
141 Cache::ItemPtr const Cache::item(FileName const & file) const
142 {
143         CacheType::const_iterator it = pimpl_->cache.find(file);
144         if (it == pimpl_->cache.end())
145                 return ItemPtr();
146
147         return it->second;
148 }
149
150 } // namespace graphics
151 } // namespace lyx