]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.cpp
Embedding: saving inzip name to .lyx file so that embedded files can always be found...
[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 "support/debug.h"
19 #include "support/FileName.h"
20 #include "support/filetools.h"
21
22 #include <map>
23
24 using namespace std;
25 using namespace lyx::support;
26
27 namespace lyx {
28
29 namespace graphics {
30
31 /** The cache contains one item per file, so use a map to find the
32  *  cache item quickly by filename.
33  */
34 typedef map<FileName, Cache::ItemPtr> CacheType;
35
36 class Cache::Impl {
37 public:
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         delete pimpl_;
59 }
60
61
62 vector<string> Cache::loadableFormats() const
63 {
64         return Image::loadableFormats();
65 }
66
67
68 void Cache::add(FileName const & file) const
69 {
70         // Is the file in the cache already?
71         if (inCache(file)) {
72                 LYXERR(Debug::GRAPHICS, "Cache::add(" << file << "):\n"
73                                         << "The file is already in the cache.");
74                 return;
75         }
76
77         pimpl_->cache[file] = ItemPtr(new CacheItem(file));
78 }
79
80
81 void Cache::remove(FileName const & file) const
82 {
83         CacheType::iterator it = pimpl_->cache.find(file);
84         if (it == pimpl_->cache.end())
85                 return;
86
87         ItemPtr & item = it->second;
88
89         if (item.use_count() == 1) {
90                 // The graphics file is in the cache, but nothing else
91                 // references it.
92                 pimpl_->cache.erase(it);
93         }
94 }
95
96
97 bool Cache::inCache(FileName const & file) const
98 {
99         return pimpl_->cache.find(file) != pimpl_->cache.end();
100 }
101
102
103 Cache::ItemPtr const Cache::item(FileName const & file) const
104 {
105         CacheType::const_iterator it = pimpl_->cache.find(file);
106         if (it == pimpl_->cache.end())
107                 return ItemPtr();
108
109         return it->second;
110 }
111
112 } // namespace graphics
113 } // namespace lyx