]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCacheItem.C
remove more forms.h cruft
[lyx.git] / src / graphics / GraphicsCacheItem.C
1 /* This file is part of
2  * =================================================
3  * 
4  *          LyX, The Document Processor
5  *          Copyright 1995 Matthias Ettrich.
6  *          Copyright 1995-2001 The LyX Team.
7  *
8  *          This file Copyright 2000 Baruch Even
9  * ================================================= */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "graphics/GraphicsCache.h"
18 #include "graphics/GraphicsCacheItem.h"
19 #include "frontends/support/LyXImage.h"
20 #include "graphics/ImageLoaderXPM.h"
21 #include "converter.h"
22 #include "support/filetools.h"
23 #include "support/lyxlib.h"
24 #include "lyx_gui_misc.h"
25 #include "debug.h"
26 #include "support/LAssert.h"
27 #include "gettext.h"
28
29 using std::endl;
30
31 GraphicsCacheItem::GraphicsCacheItem(string const & filename)
32         : imageStatus_(GraphicsCacheItem::Loading)
33 {
34         filename_ = filename;
35         
36         bool success = convertImage(filename);
37         // For now we do it synchronously
38         if (success) 
39                 imageConverted(0);
40         else
41                 imageStatus_ = ErrorConverting;
42 }
43
44
45 GraphicsCacheItem::~GraphicsCacheItem()
46 {}
47
48
49 GraphicsCacheItem::ImageStatus 
50 GraphicsCacheItem::getImageStatus() const { return imageStatus_; }
51
52
53 LyXImage * 
54 GraphicsCacheItem::getImage() const { return image_.get(); }
55
56
57 void
58 GraphicsCacheItem::imageConverted(int retval)
59 {
60         lyxerr << "imageConverted, retval=" << retval << endl;
61
62         if (retval) {
63                 lyxerr << "(GraphicsCacheItem::imageConverter) "
64                         "Error converting image." << endl;
65                 imageStatus_ = GraphicsCacheItem::ErrorConverting;
66                 return;
67         }
68
69         // Do the actual image loading from file to memory.
70         loadImage();    
71 }
72
73
74 namespace {
75
76 string const findTargetFormat(string const & from)
77 {
78         typedef ImageLoader::FormatList FormatList;
79         FormatList formats = ImageLoaderXPM().loadableFormats();
80         lyx::Assert(formats.size() > 0); // There must be a format to load from.
81         
82         FormatList::const_iterator iter = formats.begin();
83         FormatList::const_iterator end  = formats.end();
84
85         for (; iter != end; ++iter) {
86                 if (converters.IsReachable(from, *iter))
87                         break;
88         }
89         if (iter == end) {
90                 // We do not know how to convert the image to something loadable.
91                 lyxerr << "ERROR: Do not know how to convert image." << std::endl;
92
93                 string const first(_("Cannot convert image to display format"));
94                 string const second1(_("Need converter from "));
95                 string const second2(_(" to "));
96                 string const second(second1 + from + second2 + formats[0]);
97
98                 WriteAlert(first, second);
99                 
100                 return string();
101         }
102
103         return (*iter);
104 }
105
106 } // anon namespace
107
108         
109 bool
110 GraphicsCacheItem::convertImage(string const & filename)
111 {
112         string const from = GetExtension(filename);
113         string const to = findTargetFormat(from);
114         if (to.empty()) 
115                 return false;
116         
117         // Take only the filename part of the file, without path or extension.
118         string temp = OnlyFilename(filename);
119         temp = ChangeExtension(filename, string());
120         
121         // Add some stuff to have it a unique temp file.
122         // This tempfile is deleted in loadImage after it is loaded to memory.
123         tempfile = lyx::tempName(string(), temp);
124         // Remove the temp file, we only want the name...
125         lyx::unlink(tempfile);
126
127         converters.Convert(0, filename, tempfile, from, to);
128
129         return true;
130 }
131
132
133 // This function gets called from the callback after the image has been
134 // converted successfully.
135 void
136 GraphicsCacheItem::loadImage()
137 {
138         lyxerr << "Loading XPM Image... ";
139
140         ImageLoaderXPM imageLoader;
141         if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
142                 lyxerr << "Success." << endl;
143                 image_.reset(imageLoader.getImage());
144                 imageStatus_ = GraphicsCacheItem::Loaded;
145         } else {
146                 lyxerr << "Loading " << tempfile << "Failed" << endl;
147                 imageStatus_ = GraphicsCacheItem::ErrorReading;
148         }
149
150         // remove the xpm file now.
151         lyx::unlink(tempfile);
152         // and remove the reference to the filename.
153         tempfile = string();
154 }