]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.cpp
Always use utf-8 in scripts with Python3
[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
17 #include "Format.h"
18
19 #include "frontends/Application.h"
20
21 #include "support/debug.h"
22 #include "support/FileName.h"
23
24 #include <map>
25
26 using namespace std;
27 using namespace lyx::support;
28
29 namespace lyx {
30
31 namespace graphics {
32
33 /** The cache contains one item per file, so use a map to find the
34  *  cache item quickly by filename.
35  */
36 typedef map<FileName, Cache::ItemPtr> CacheType;
37
38 class Cache::Impl {
39 public:
40         ///
41         CacheType cache;
42 };
43
44
45 Cache & Cache::get()
46 {
47         // Now return the cache
48         static Cache singleton;
49         return singleton;
50 }
51
52
53 Cache::Cache()
54         : pimpl_(new Impl)
55 {}
56
57
58 Cache::~Cache()
59 {
60         delete pimpl_;
61 }
62
63
64 vector<string> const & Cache::loadableFormats() const
65 {
66         static vector<string> fmts;
67
68         if (!fmts.empty())
69                 return fmts;
70
71         for (string const & native_extension : frontend::loadableImageFormats()) {
72                 for (Format const & format : theFormats()) {
73                         if (format.extension() == native_extension) {
74                                 fmts.push_back(format.name());
75                                 break;
76                         }
77                 }
78         }
79
80         if (lyxerr.debugging()) {
81                 LYXERR(Debug::GRAPHICS, "LyX recognises the following image formats:");
82                 for (string const & format : fmts) {
83                         LYXERR(Debug::GRAPHICS, format << ',');
84                 }
85         }
86
87         return fmts;
88 }
89
90
91 void Cache::add(FileName const & file, FileName const & doc_file) const
92 {
93         // Is the file in the cache already?
94         if (inCache(file)) {
95                 LYXERR(Debug::GRAPHICS, "Cache::add(" << file << "):\n"
96                                         << "The file is already in the cache.");
97                 return;
98         }
99
100         pimpl_->cache[file] = ItemPtr(new CacheItem(file, doc_file));
101 }
102
103
104 void Cache::remove(FileName const & file) const
105 {
106         CacheType::iterator it = pimpl_->cache.find(file);
107         if (it == pimpl_->cache.end())
108                 return;
109
110         ItemPtr & item = it->second;
111
112         if (item.use_count() == 1) {
113                 // The graphics file is in the cache, but nothing else
114                 // references it.
115                 pimpl_->cache.erase(it);
116         }
117 }
118
119
120 bool Cache::inCache(FileName const & file) const
121 {
122         return pimpl_->cache.find(file) != pimpl_->cache.end();
123 }
124
125
126 Cache::ItemPtr const Cache::item(FileName const & file) const
127 {
128         CacheType::const_iterator it = pimpl_->cache.find(file);
129         if (it == pimpl_->cache.end())
130                 return ItemPtr();
131
132         return it->second;
133 }
134
135 } // namespace graphics
136 } // namespace lyx