]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
Quote graphics conversion commands correctly.
[lyx.git] / src / graphics / GraphicsCache.C
1 /**
2  * \file GraphicsCache.C
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 "debug.h"
19
20 #include "support/filetools.h"
21
22 #include <map>
23
24 namespace support = lyx::support;
25
26 using std::string;
27
28
29 namespace lyx {
30 namespace graphics {
31
32 /** The cache contains one item per file, so use a map to find the
33  *  cache item quickly by filename.
34  */
35 typedef std::map<string, Cache::ItemPtr> CacheType;
36
37 class Cache::Impl {
38 public:
39         ///
40         CacheType cache;
41 };
42
43
44 Cache & Cache::get()
45 {
46         // Now return the cache
47         static Cache singleton;
48         return singleton;
49 }
50
51
52 Cache::Cache()
53         : pimpl_(new Impl)
54 {}
55
56
57 Cache::~Cache()
58 {}
59
60
61 std::vector<string> Cache::loadableFormats() const
62 {
63         return Image::loadableFormats();
64 }
65
66
67 void Cache::add(string const & file) const
68 {
69         if (!support::AbsolutePath(file)) {
70                 lyxerr << "Cache::add(" << file << "):\n"
71                        << "The file must be have an absolute path."
72                        << std::endl;
73                 return;
74         }
75
76         // Is the file in the cache already?
77         if (inCache(file)) {
78                 lyxerr[Debug::GRAPHICS] << "Cache::add(" << file << "):\n"
79                                         << "The file is already in the cache."
80                                         << std::endl;
81                 return;
82         }
83
84         pimpl_->cache[file] = ItemPtr(new CacheItem(file));
85 }
86
87
88 void Cache::remove(string const & file) const
89 {
90         CacheType::iterator it = pimpl_->cache.find(file);
91         if (it == pimpl_->cache.end())
92                 return;
93
94         ItemPtr & item = it->second;
95
96         if (item.use_count() == 1) {
97                 // The graphics file is in the cache, but nothing else
98                 // references it.
99                 pimpl_->cache.erase(it);
100         }
101 }
102
103
104 bool Cache::inCache(string const & file) const
105 {
106         return pimpl_->cache.find(file) != pimpl_->cache.end();
107 }
108
109
110 Cache::ItemPtr const Cache::item(string const & file) const
111 {
112         CacheType::const_iterator it = pimpl_->cache.find(file);
113         if (it == pimpl_->cache.end())
114                 return ItemPtr();
115
116         return it->second;
117 }
118
119 } // namespace graphics
120 } // namespace lyx