]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
* Split the graphics loader into a frontend and a backend.
[lyx.git] / src / graphics / GraphicsCache.C
1 /*
2  * \file GraphicsCache.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Baruch Even <baruch.even@writeme.com>
7  * \author Angus Leeming <a.leeming@ic.ac.uk>
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "GraphicsCache.h"
17 #include "GraphicsCacheItem.h"
18 #include "GraphicsImage.h"
19
20 #include "debug.h"
21
22 #include "support/filetools.h"
23
24 #include "frontends/lyx_gui.h"
25
26 namespace grfx {
27
28 GCache & GCache::get()
29 {
30         static bool start = true;
31         if (start) {
32                 start = false;
33                 lyx_gui::init_graphics();
34         }
35
36         // Now return the cache
37         static GCache singleton;
38         return singleton;
39 }
40
41
42 GCache::GCache()
43 {
44         cache = new CacheType;
45 }
46
47
48 // all elements are destroyed by the shared_ptr's in the map.
49 GCache::~GCache()
50 {
51         delete cache;
52 }
53
54
55 std::vector<string> GCache::loadableFormats() const
56 {
57         return GImage::loadableFormats();
58 }
59
60
61 void GCache::add(string const & file)
62 {
63         if (!AbsolutePath(file)) {
64                 lyxerr << "GCacheItem::add(" << file << "):\n"
65                        << "The file must be have an absolute path."
66                        << std::endl;
67                 return;
68         }
69         
70         // Is the file in the cache already?
71         if (inCache(file)) {
72                 lyxerr[Debug::GRAPHICS] << "GCache::add(" << file << "):\n"
73                                         << "The file is already in the cache."
74                                         << std::endl;
75                 return;
76         }
77
78         
79         (*cache)[file] = GraphicPtr(new GCacheItem(file));
80 }
81
82
83 void GCache::remove(string const & file)
84 {
85         CacheType::iterator it = cache->find(file);
86         if (it == cache->end())
87                 return;
88
89         GraphicPtr 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                 cache->erase(it);
95         }
96 }
97
98
99 bool GCache::inCache(string const & file) const
100 {
101         return cache->find(file) != cache->end();
102 }
103
104
105 GraphicPtr const GCache::graphic(string const & file) const
106 {
107         CacheType::const_iterator it = cache->find(file);
108         if (it == cache->end())
109                 return GraphicPtr();
110
111         return it->second;
112 }
113
114
115 ImagePtr const GCache::image(string const & file) const
116 {
117         CacheType::const_iterator it = cache->find(file);
118         if (it == cache->end())
119                 return ImagePtr();
120
121         return it->second->image();
122 }
123
124
125 } // namespace grfx