1 /* This file is part of
2 * =================================================
4 * LyX, The Document Processor
5 * Copyright 1995 Matthias Ettrich.
6 * Copyright 1995-2001 The LyX Team.
9 * \author Herbert Voss <voss@lyx.org>
10 * ================================================= */
15 #pragma implementation
18 #include "graphics/GraphicsCache.h"
19 #include "graphics/GraphicsCacheItem.h"
20 #include "frontends/support/LyXImage.h"
21 #include "graphics/ImageLoaderXPM.h"
22 #include "converter.h"
23 #include "support/filetools.h"
24 #include "support/lyxlib.h"
25 #include "lyx_gui_misc.h"
27 #include "support/LAssert.h"
34 * The order of conversion:
36 * The c-tor calls convertImage()
38 * convertImage() verifies that we need to do conversion, if not it will just
39 * call the loadImage()
40 * if conversion is needed, it will initiate the conversion.
42 * When the conversion is completed imageConverted() is called, which in turn
45 * Since we currently do everything synchronously, convertImage() calls
46 * imageConverted() right after it does the call to the conversion process.
49 GraphicsCacheItem::GraphicsCacheItem(string const & filename)
50 : imageStatus_(GraphicsCacheItem::Loading)
54 bool success = convertImage(filename);
55 if (! success) // Conversion failed miserably (couldn't even start).
56 setStatus(ErrorConverting);
60 GraphicsCacheItem::~GraphicsCacheItem()
64 GraphicsCacheItem::ImageStatus
65 GraphicsCacheItem::getImageStatus() const { return imageStatus_; }
68 void GraphicsCacheItem::setStatus(ImageStatus new_status)
70 imageStatus_ = new_status;
75 GraphicsCacheItem::getImage() const { return image_.get(); }
78 void GraphicsCacheItem::imageConverted(bool success)
81 string text = "succeeded";
84 lyxerr << "imageConverted, conversion " << text << "." << endl;
87 lyxerr << "(GraphicsCacheItem::imageConverter) "
88 "Error converting image." << endl;
89 setStatus(GraphicsCacheItem::ErrorConverting);
93 // Do the actual image loading from file to memory.
100 string const findTargetFormat(string const & from)
102 typedef ImageLoader::FormatList FormatList;
103 FormatList formats = ImageLoaderXPM().loadableFormats();
104 lyx::Assert(formats.size() > 0); // There must be a format to load from.
106 FormatList::const_iterator iter = formats.begin();
107 FormatList::const_iterator end = formats.end();
109 for (; iter != end; ++iter) {
110 if (converters.isReachable(from, *iter))
114 // We do not know how to convert the image to something loadable.
115 lyxerr << "ERROR: Do not know how to convert image." << std::endl;
124 bool GraphicsCacheItem::convertImage(string const & filename)
126 setStatus(GraphicsCacheItem::Converting);
127 string filename_ = string(filename);
128 lyxerr << "try to convert image file: " << filename_ << endl;
129 // maybe that other zip extensions also be useful, especially the
130 // ones that may be declared in texmf/tex/latex/config/graphics.cfg.
132 /* -----------snip-------------
133 {\DeclareGraphicsRule{.pz}{eps}{.bb}{}%
134 \DeclareGraphicsRule{.eps.Z}{eps}{.eps.bb}{}%
135 \DeclareGraphicsRule{.ps.Z}{eps}{.ps.bb}{}%
136 \DeclareGraphicsRule{.ps.gz}{eps}{.ps.bb}{}%
137 \DeclareGraphicsRule{.eps.gz}{eps}{.eps.bb}{}}}%
138 -----------snip-------------*/
140 lyxerr << "GetExtension: " << GetExtension(filename_) << endl;
141 bool const zipped = zippedFile(filename_);
143 filename_ = unzipFile(filename_);
144 string const from = getExtFromContents(filename_); // get the type
145 lyxerr << "GetExtFromContents: " << from << endl;
146 string const to = findTargetFormat(from);
147 lyxerr << "from: " << from << " -> " << to << endl;
150 // manage zipped files. unzip them first into the tempdir
152 // No conversion needed!
153 // Saves more than just time: prevents the deletion of
154 // the "to" file after loading when it's the same as the "from"!
155 tempfile = filename_;
159 // Take only the filename part of the file, without path or extension.
160 string temp = OnlyFilename(filename_);
161 temp = ChangeExtension(filename_, string());
163 // Add some stuff to have it a unique temp file.
164 // This tempfile is deleted in loadImage after it is loaded to memory.
165 tempfile = lyx::tempName(string(), temp);
166 // Remove the temp file, we only want the name...
167 lyx::unlink(tempfile);
168 bool result = converters.convert(0, filename_, tempfile, from, to);
169 tempfile.append(".xpm");
170 // For now we are synchronous
171 imageConverted(result);
172 // Cleanup after the conversion.
173 lyx::unlink(tempfile);
175 lyx::unlink(filename_);
181 // This function gets called from the callback after the image has been
182 // converted successfully.
184 GraphicsCacheItem::loadImage()
186 lyxerr << "Loading XPM Image... ";
188 ImageLoaderXPM imageLoader;
189 if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
190 lyxerr << "Success." << endl;
191 image_.reset(imageLoader.getImage());
192 setStatus(GraphicsCacheItem::Loaded);
194 lyxerr << "Loading " << tempfile << "Failed" << endl;
195 setStatus(GraphicsCacheItem::ErrorReading);