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