]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
Next step of true unicode filenames: Use support::FileName instead of
[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/filename.h"
21 #include "support/filetools.h"
22
23 #include <map>
24
25 using std::string;
26
27
28 namespace lyx {
29
30 using support::FileName;
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 std::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
62
63 std::vector<string> Cache::loadableFormats() const
64 {
65         return Image::loadableFormats();
66 }
67
68
69 void Cache::add(FileName const & file) const
70 {
71         // Is the file in the cache already?
72         if (inCache(file)) {
73                 lyxerr[Debug::GRAPHICS] << "Cache::add(" << file << "):\n"
74                                         << "The file is already in the cache."
75                                         << std::endl;
76                 return;
77         }
78
79         pimpl_->cache[file] = ItemPtr(new CacheItem(file));
80 }
81
82
83 void Cache::remove(FileName const & file) const
84 {
85         CacheType::iterator it = pimpl_->cache.find(file);
86         if (it == pimpl_->cache.end())
87                 return;
88
89         ItemPtr & item = it->second;
90
91         if (item.use_count() == 1) {
92                 // The graphics file is in the cache, but nothing else
93                 // references it.
94                 pimpl_->cache.erase(it);
95         }
96 }
97
98
99 bool Cache::inCache(FileName const & file) const
100 {
101         return pimpl_->cache.find(file) != pimpl_->cache.end();
102 }
103
104
105 Cache::ItemPtr const Cache::item(FileName const & file) const
106 {
107         CacheType::const_iterator it = pimpl_->cache.find(file);
108         if (it == pimpl_->cache.end())
109                 return ItemPtr();
110
111         return it->second;
112 }
113
114 } // namespace graphics
115 } // namespace lyx