]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.cpp
5ed905252d71343d9571aea74b975eb2c4d88e3b
[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                         LYXERR(Debug::GRAPHICS, *fit << ',');
98                 }
99         }
100
101         return fmts;
102 }
103
104
105 void Cache::add(FileName const & file) const
106 {
107         // Is the file in the cache already?
108         if (inCache(file)) {
109                 LYXERR(Debug::GRAPHICS, "Cache::add(" << file << "):\n"
110                                         << "The file is already in the cache.");
111                 return;
112         }
113
114         pimpl_->cache[file] = ItemPtr(new CacheItem(file));
115 }
116
117
118 void Cache::remove(FileName const & file) const
119 {
120         CacheType::iterator it = pimpl_->cache.find(file);
121         if (it == pimpl_->cache.end())
122                 return;
123
124         ItemPtr & item = it->second;
125
126         if (item.use_count() == 1) {
127                 // The graphics file is in the cache, but nothing else
128                 // references it.
129                 pimpl_->cache.erase(it);
130         }
131 }
132
133
134 bool Cache::inCache(FileName const & file) const
135 {
136         return pimpl_->cache.find(file) != pimpl_->cache.end();
137 }
138
139
140 Cache::ItemPtr const Cache::item(FileName const & file) const
141 {
142         CacheType::const_iterator it = pimpl_->cache.find(file);
143         if (it == pimpl_->cache.end())
144                 return ItemPtr();
145
146         return it->second;
147 }
148
149 } // namespace graphics
150 } // namespace lyx