]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
remove noload/don't typeset
[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 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "GraphicsCache.h"
19 #include "GraphicsCacheItem.h"
20 #include "GraphicsImage.h"
21
22 #include "debug.h"
23
24 #include "support/filetools.h"
25
26 #include "frontends/lyx_gui.h"
27
28 #include <map>
29
30 namespace grfx {
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 struct Cache::Impl {
38         ///
39         CacheType cache;
40 };
41
42
43 Cache & Cache::get()
44 {
45         // Now return the cache
46         static Cache singleton;
47         return singleton;
48 }
49
50
51 Cache::Cache()
52         : pimpl_(new Impl())
53 {}
54
55
56 Cache::~Cache()
57 {}
58
59
60 std::vector<string> Cache::loadableFormats() const
61 {
62         return Image::loadableFormats();
63 }
64
65
66 void Cache::add(string const & file) const
67 {
68         if (!AbsolutePath(file)) {
69                 lyxerr << "Cache::add(" << file << "):\n"
70                        << "The file must be have an absolute path."
71                        << std::endl;
72                 return;
73         }
74
75         // Is the file in the cache already?
76         if (inCache(file)) {
77                 lyxerr[Debug::GRAPHICS] << "Cache::add(" << file << "):\n"
78                                         << "The file is already in the cache."
79                                         << std::endl;
80                 return;
81         }
82
83         pimpl_->cache[file] = ItemPtr(new CacheItem(file));
84 }
85
86
87 void Cache::remove(string const & file) const
88 {
89         CacheType::iterator it = pimpl_->cache.find(file);
90         if (it == pimpl_->cache.end())
91                 return;
92
93         ItemPtr & item = it->second;
94
95         if (item.use_count() == 1) {
96                 // The graphics file is in the cache, but nothing else
97                 // references it.
98                 pimpl_->cache.erase(it);
99         }
100 }
101
102
103 bool Cache::inCache(string const & file) const
104 {
105         return pimpl_->cache.find(file) != pimpl_->cache.end();
106 }
107
108
109 Cache::ItemPtr const Cache::item(string const & file) const
110 {
111         CacheType::const_iterator it = pimpl_->cache.find(file);
112         if (it == pimpl_->cache.end())
113                 return ItemPtr();
114
115         return it->second;
116 }
117
118 } // namespace grfx