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