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 "GraphicsConverter.h"
17 #include "GraphicsImage.h"
21 #include "support/filetools.h"
22 #include "support/FileMonitor.h"
23 #include "support/LAssert.h"
24 #include "support/lyxlib.h"
26 #include <boost/bind.hpp>
28 namespace support = lyx::support;
30 using support::Assert;
31 using support::ChangeExtension;
32 using support::FileMonitor;
33 using support::IsFileReadable;
34 using support::MakeDisplayPath;
35 using support::OnlyFilename;
36 using support::getExtFromContents;
37 using support::tempName;
38 using support::unlink;
39 using support::unzipFile;
40 using support::zippedFile;
47 struct CacheItem::Impl : public boost::signals::trackable {
50 Impl(string const & file);
52 /** Start the image conversion process, checking first that it is
53 * necessary. If it is necessary, then a conversion task is started.
54 * CacheItem asumes that the conversion is asynchronous and so
55 * passes a Signal to the converting routine. When the conversion
56 * is finished, this Signal is emitted, returning the converted
57 * file to this->imageConverted.
59 * If no file conversion is needed, then convertToDisplayFormat() calls
60 * loadImage() directly.
62 * convertToDisplayFormat() will set the loading status flag as
63 * approriate through calls to setStatus().
65 void convertToDisplayFormat();
67 /** Load the image into memory. This is called either from
68 * convertToDisplayFormat() direct or from imageConverted().
72 /** Get a notification when the image conversion is done.
73 * Connected to a signal on_finish_ which is passed to
76 void imageConverted(bool);
78 /** Get a notification when the image loading is done.
79 * Connected to a signal on_finish_ which is passed to
80 * lyx::graphics::Image::loadImage.
82 void imageLoaded(bool);
84 /** Sets the status of the loading process. Also notifies
85 * listeners that the status has changed.
87 void setStatus(ImageStatus new_status);
89 /** Can be invoked directly by the user, but is also connected to the
90 * FileMonitor and so is invoked when the file is changed
91 * (if monitoring is taking place).
95 /** If we are asked to load the file for a second or further time,
96 * (because the file has changed), then we'll have to first reset
97 * many of the variables below.
101 /// The filename we refer too.
102 string const filename_;
104 FileMonitor const monitor_;
106 /// Is the file compressed?
108 /// If so, store the uncompressed file in this temporary file.
109 string unzipped_filename_;
110 /// What file are we trying to load?
111 string file_to_load_;
112 /** Should we delete the file after loading? True if the file is
113 * the result of a conversion process.
115 bool remove_loaded_file_;
117 /// The image and its loading status.
118 boost::shared_ptr<Image> image_;
122 /// This signal is emitted when the image loading status changes.
123 boost::signal0<void> statusChanged;
125 /// The connection to the signal Image::finishedLoading
126 boost::signals::connection cl_;
128 /// The connection of the signal ConvProcess::finishedConversion,
129 boost::signals::connection cc_;
132 boost::scoped_ptr<Converter> converter_;
136 CacheItem::CacheItem(string const & file)
137 : pimpl_(new Impl(file))
141 CacheItem::~CacheItem()
145 string const & CacheItem::filename() const
147 return pimpl_->filename_;
151 void CacheItem::startLoading() const
153 pimpl_->startLoading();
157 void CacheItem::startMonitoring() const
159 if (!pimpl_->monitor_.monitoring())
160 pimpl_->monitor_.start();
164 bool CacheItem::monitoring() const
166 return pimpl_->monitor_.monitoring();
170 unsigned long CacheItem::checksum() const
172 return pimpl_->monitor_.checksum();
176 Image const * CacheItem::image() const
178 return pimpl_->image_.get();
182 ImageStatus CacheItem::status() const
184 return pimpl_->status_;
188 boost::signals::connection CacheItem::connect(slot_type const & slot) const
190 return pimpl_->statusChanged.connect(slot);
194 //------------------------------
195 // Implementation details follow
196 //------------------------------
199 CacheItem::Impl::Impl(string const & file)
201 monitor_(file, 2000),
203 remove_loaded_file_(false),
204 status_(WaitingToLoad)
206 monitor_.connect(boost::bind(&Impl::startLoading, this));
210 void CacheItem::Impl::startLoading()
212 if (status_ != WaitingToLoad)
215 convertToDisplayFormat();
219 void CacheItem::Impl::reset()
222 if (!unzipped_filename_.empty())
223 unlink(unzipped_filename_);
224 unzipped_filename_.erase();
226 if (remove_loaded_file_ && !file_to_load_.empty())
227 unlink(file_to_load_);
228 remove_loaded_file_ = false;
229 file_to_load_.erase();
234 status_ = WaitingToLoad;
242 if (converter_.get())
247 void CacheItem::Impl::setStatus(ImageStatus new_status)
249 if (status_ == new_status)
252 status_ = new_status;
257 void CacheItem::Impl::imageConverted(bool success)
259 string const text = success ? "succeeded" : "failed";
260 lyxerr[Debug::GRAPHICS] << "Image conversion " << text << '.' << endl;
262 file_to_load_ = converter_.get() ?
263 converter_->convertedFile() : string();
267 success = !file_to_load_.empty() && IsFileReadable(file_to_load_);
270 lyxerr[Debug::GRAPHICS] << "Unable to find converted file!"
272 setStatus(ErrorConverting);
275 unlink(unzipped_filename_);
284 // This function gets called from the callback after the image has been
285 // converted successfully.
286 void CacheItem::Impl::loadImage()
289 lyxerr[Debug::GRAPHICS] << "Loading image." << endl;
291 image_ = Image::newImage();
294 cl_ = image_->finishedLoading.connect(
295 boost::bind(&Impl::imageLoaded, this, _1));
296 image_->load(file_to_load_);
300 void CacheItem::Impl::imageLoaded(bool success)
302 string const text = success ? "succeeded" : "failed";
303 lyxerr[Debug::GRAPHICS] << "Image loading " << text << '.' << endl;
305 // Clean up after loading.
307 unlink(unzipped_filename_);
309 if (remove_loaded_file_ && unzipped_filename_ != file_to_load_)
310 unlink(file_to_load_);
315 setStatus(ErrorLoading);
319 // Inform the outside world.
324 } // namespace graphics
330 string const findTargetFormat(string const & from)
332 typedef lyx::graphics::Image::FormatList FormatList;
333 FormatList const formats = lyx::graphics::Image::loadableFormats();
335 // There must be a format to load from.
336 Assert(!formats.empty());
338 // First ascertain if we can load directly with no conversion
339 FormatList::const_iterator it = formats.begin();
340 FormatList::const_iterator end = formats.end();
341 for (; it != end; ++it) {
346 // So, we have to convert to a loadable format. Can we?
347 it = formats.begin();
348 for (; it != end; ++it) {
349 if (lyx::graphics::Converter::isReachable(from, *it))
352 lyxerr[Debug::GRAPHICS]
353 << "Unable to convert from " << from
354 << " to " << *it << std::endl;
357 // Failed! so we have to try to convert it to PPM format
358 // with the standard converter
359 return string("ppm");
368 void CacheItem::Impl::convertToDisplayFormat()
370 setStatus(Converting);
372 // First, check that the file exists!
373 if (!IsFileReadable(filename_)) {
374 if (status_ != ErrorNoFile) {
375 setStatus(ErrorNoFile);
376 lyxerr[Debug::GRAPHICS]
377 << "\tThe file is not readable" << endl;
382 // Make a local copy in case we unzip it
383 string const filename = zippedFile(filename_) ?
384 unzipFile(filename_) : filename_;
385 string const displayed_filename = MakeDisplayPath(filename_);
386 lyxerr[Debug::GRAPHICS] << "[GrahicsCacheItem::convertToDisplayFormat]\n"
387 << "\tAttempting to convert image file: " << filename
388 << "\n\twith displayed filename: " << displayed_filename
391 string from = getExtFromContents(filename);
392 lyxerr[Debug::GRAPHICS]
393 << "\n\tThe file contains " << from << " format data." << endl;
394 string const to = findTargetFormat(from);
397 // No conversion needed!
398 lyxerr[Debug::GRAPHICS] << "\tNo conversion needed (from == to)!" << endl;
399 file_to_load_ = filename;
404 lyxerr[Debug::GRAPHICS] << "\tConverting it to " << to << " format." << endl;
405 // Take only the filename part of the file, without path or extension.
406 string const temp = ChangeExtension(OnlyFilename(filename), string());
408 // Add some stuff to create a uniquely named temporary file.
409 // This file is deleted in loadImage after it is loaded into memory.
410 string const to_file_base = tempName(string(), temp);
411 remove_loaded_file_ = true;
413 // Remove the temp file, we only want the name...
414 unlink(to_file_base);
416 // Connect a signal to this->imageConverted and pass this signal to
417 // the graphics converter so that we can load the modified file
418 // on completion of the conversion process.
419 converter_.reset(new Converter(filename, to_file_base, from, to));
420 converter_->connect(boost::bind(&Impl::imageConverted, this, _1));
421 converter_->startConversion();
424 } // namespace graphics