2 * \file GraphicsCacheItem.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS
15 #include "GraphicsCacheItem.h"
16 #include "GraphicsImage.h"
17 #include "GraphicsConverter.h"
19 #include "support/FileMonitor.h"
23 #include "support/LAssert.h"
24 #include "support/filetools.h"
26 #include <boost/shared_ptr.hpp>
27 #include <boost/bind.hpp>
28 #include <boost/signals/trackable.hpp>
34 struct CacheItem::Impl : public boost::signals::trackable {
37 Impl(string const & file);
39 /** Start the image conversion process, checking first that it is
40 * necessary. If it is necessary, then a conversion task is started.
41 * CacheItem asumes that the conversion is asynchronous and so
42 * passes a Signal to the converting routine. When the conversion
43 * is finished, this Signal is emitted, returning the converted
44 * file to this->imageConverted.
46 * If no file conversion is needed, then convertToDisplayFormat() calls
47 * loadImage() directly.
49 * convertToDisplayFormat() will set the loading status flag as
50 * approriate through calls to setStatus().
52 void convertToDisplayFormat();
54 /** Load the image into memory. This is called either from
55 * convertToDisplayFormat() direct or from imageConverted().
59 /** Get a notification when the image conversion is done.
60 * Connected to a signal on_finish_ which is passed to
63 void imageConverted(bool);
65 /** Get a notification when the image loading is done.
66 * Connected to a signal on_finish_ which is passed to
67 * grfx::Image::loadImage.
69 void imageLoaded(bool);
71 /** Sets the status of the loading process. Also notifies
72 * listeners that the status has changed.
74 void setStatus(ImageStatus new_status);
76 /** Can be invoked directly by the user, but is also connected to the
77 * FileMonitor and so is invoked when the file is changed
78 * (if monitoring is taking place).
82 /** If we are asked to load the file for a second or further time,
83 * (because the file has changed), then we'll have to first reset
84 * many of the variables below.
88 /// The filename we refer too.
89 string const filename_;
91 FileMonitor const monitor_;
93 /// Is the file compressed?
95 /// If so, store the uncompressed file in this temporary file.
96 string unzipped_filename_;
97 /// What file are we trying to load?
99 /** Should we delete the file after loading? True if the file is
100 * the result of a conversion process.
102 bool remove_loaded_file_;
104 /// The image and its loading status.
105 boost::shared_ptr<Image> image_;
109 /// This signal is emitted when the image loading status changes.
110 boost::signal0<void> statusChanged;
112 /// The connection to the signal Image::finishedLoading
113 boost::signals::connection cl_;
115 /// The connection of the signal ConvProcess::finishedConversion,
116 boost::signals::connection cc_;
119 boost::scoped_ptr<Converter> converter_;
123 CacheItem::CacheItem(string const & file)
124 : pimpl_(new Impl(file))
128 CacheItem::~CacheItem()
132 string const & CacheItem::filename() const
134 return pimpl_->filename_;
138 void CacheItem::startLoading() const
140 pimpl_->startLoading();
144 void CacheItem::startMonitoring() const
146 if (!pimpl_->monitor_.monitoring())
147 pimpl_->monitor_.start();
151 bool CacheItem::monitoring() const
153 return pimpl_->monitor_.monitoring();
157 unsigned long CacheItem::checksum() const
159 return pimpl_->monitor_.checksum();
163 Image const * CacheItem::image() const
165 return pimpl_->image_.get();
169 ImageStatus CacheItem::status() const
171 return pimpl_->status_;
175 boost::signals::connection CacheItem::connect(slot_type const & slot) const
177 return pimpl_->statusChanged.connect(slot);
181 //------------------------------
182 // Implementation details follow
183 //------------------------------
186 CacheItem::Impl::Impl(string const & file)
188 monitor_(file, 2000),
190 remove_loaded_file_(false),
191 status_(WaitingToLoad)
193 monitor_.connect(boost::bind(&Impl::startLoading, this));
197 void CacheItem::Impl::startLoading()
199 if (status_ != WaitingToLoad)
202 convertToDisplayFormat();
206 void CacheItem::Impl::reset()
209 if (!unzipped_filename_.empty())
210 lyx::unlink(unzipped_filename_);
211 unzipped_filename_.erase();
213 if (remove_loaded_file_ && !file_to_load_.empty())
214 lyx::unlink(file_to_load_);
215 remove_loaded_file_ = false;
216 file_to_load_.erase();
221 status_ = WaitingToLoad;
229 if (converter_.get())
234 void CacheItem::Impl::setStatus(ImageStatus new_status)
236 if (status_ == new_status)
239 status_ = new_status;
244 void CacheItem::Impl::imageConverted(bool success)
246 string const text = success ? "succeeded" : "failed";
247 lyxerr[Debug::GRAPHICS] << "Image conversion " << text << '.' << endl;
249 file_to_load_ = converter_.get() ?
250 converter_->convertedFile() : string();
254 success = !file_to_load_.empty() && IsFileReadable(file_to_load_);
255 lyxerr[Debug::GRAPHICS] << "Unable to find converted file!" << endl;
258 setStatus(ErrorConverting);
261 lyx::unlink(unzipped_filename_);
270 // This function gets called from the callback after the image has been
271 // converted successfully.
272 void CacheItem::Impl::loadImage()
275 lyxerr[Debug::GRAPHICS] << "Loading image." << endl;
277 image_ = Image::newImage();
280 cl_ = image_->finishedLoading.connect(
281 boost::bind(&Impl::imageLoaded, this, _1));
282 image_->load(file_to_load_);
286 void CacheItem::Impl::imageLoaded(bool success)
288 string const text = success ? "succeeded" : "failed";
289 lyxerr[Debug::GRAPHICS] << "Image loading " << text << '.' << endl;
291 // Clean up after loading.
293 lyx::unlink(unzipped_filename_);
295 if (remove_loaded_file_ && unzipped_filename_ != file_to_load_)
296 lyx::unlink(file_to_load_);
301 setStatus(ErrorLoading);
305 // Inform the outside world.
315 string const findTargetFormat(string const & from)
317 typedef grfx::Image::FormatList FormatList;
318 FormatList const formats = grfx::Image::loadableFormats();
320 // There must be a format to load from.
321 lyx::Assert(!formats.empty());
323 // First ascertain if we can load directly with no conversion
324 FormatList::const_iterator it = formats.begin();
325 FormatList::const_iterator end = formats.end();
326 for (; it != end; ++it) {
331 // So, we have to convert to a loadable format. Can we?
332 it = formats.begin();
333 for (; it != end; ++it) {
334 if (grfx::Converter::isReachable(from, *it))
337 lyxerr[Debug::GRAPHICS]
338 << "Unable to convert from " << from
339 << " to " << *it << std::endl;
342 // Failed! so we have to try to convert it to PPM format
343 // with the standard converter
344 return string("ppm");
352 void CacheItem::Impl::convertToDisplayFormat()
354 setStatus(Converting);
356 // First, check that the file exists!
357 if (!IsFileReadable(filename_)) {
358 if (status_ != ErrorNoFile) {
359 setStatus(ErrorNoFile);
360 lyxerr[Debug::GRAPHICS]
361 << "\tThe file is not readable" << endl;
366 // Make a local copy in case we unzip it
367 string const filename = zippedFile(filename_) ?
368 unzipFile(filename_) : filename_;
369 string const displayed_filename = MakeDisplayPath(filename_);
370 lyxerr[Debug::GRAPHICS] << "[GrahicsCacheItem::convertToDisplayFormat]\n"
371 << "\tAttempting to convert image file: " << filename
372 << "\n\twith displayed filename: " << displayed_filename
375 string from = getExtFromContents(filename);
376 lyxerr[Debug::GRAPHICS]
377 << "\n\tThe file contains " << from << " format data." << endl;
378 string const to = findTargetFormat(from);
381 // No conversion needed!
382 lyxerr[Debug::GRAPHICS] << "\tNo conversion needed (from == to)!" << endl;
383 file_to_load_ = filename;
388 lyxerr[Debug::GRAPHICS] << "\tConverting it to " << to << " format." << endl;
389 // Take only the filename part of the file, without path or extension.
390 string const temp = ChangeExtension(OnlyFilename(filename), string());
392 // Add some stuff to create a uniquely named temporary file.
393 // This file is deleted in loadImage after it is loaded into memory.
394 string const to_file_base = lyx::tempName(string(), temp);
395 remove_loaded_file_ = true;
397 // Remove the temp file, we only want the name...
398 lyx::unlink(to_file_base);
400 // Connect a signal to this->imageConverted and pass this signal to
401 // the graphics converter so that we can load the modified file
402 // on completion of the conversion process.
403 converter_.reset(new Converter(filename, to_file_base, from, to));
404 converter_->connect(boost::bind(&Impl::imageConverted, this, _1));
405 converter_->startConversion();