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"
25 #include "support/lyxlib.h"
27 #include <boost/shared_ptr.hpp>
28 #include <boost/bind.hpp>
29 #include <boost/signals/trackable.hpp>
31 using namespace lyx::support;
37 struct CacheItem::Impl : public boost::signals::trackable {
40 Impl(string const & file);
42 /** Start the image conversion process, checking first that it is
43 * necessary. If it is necessary, then a conversion task is started.
44 * CacheItem asumes that the conversion is asynchronous and so
45 * passes a Signal to the converting routine. When the conversion
46 * is finished, this Signal is emitted, returning the converted
47 * file to this->imageConverted.
49 * If no file conversion is needed, then convertToDisplayFormat() calls
50 * loadImage() directly.
52 * convertToDisplayFormat() will set the loading status flag as
53 * approriate through calls to setStatus().
55 void convertToDisplayFormat();
57 /** Load the image into memory. This is called either from
58 * convertToDisplayFormat() direct or from imageConverted().
62 /** Get a notification when the image conversion is done.
63 * Connected to a signal on_finish_ which is passed to
66 void imageConverted(bool);
68 /** Get a notification when the image loading is done.
69 * Connected to a signal on_finish_ which is passed to
70 * grfx::Image::loadImage.
72 void imageLoaded(bool);
74 /** Sets the status of the loading process. Also notifies
75 * listeners that the status has changed.
77 void setStatus(ImageStatus new_status);
79 /** Can be invoked directly by the user, but is also connected to the
80 * FileMonitor and so is invoked when the file is changed
81 * (if monitoring is taking place).
85 /** If we are asked to load the file for a second or further time,
86 * (because the file has changed), then we'll have to first reset
87 * many of the variables below.
91 /// The filename we refer too.
92 string const filename_;
94 FileMonitor const monitor_;
96 /// Is the file compressed?
98 /// If so, store the uncompressed file in this temporary file.
99 string unzipped_filename_;
100 /// What file are we trying to load?
101 string file_to_load_;
102 /** Should we delete the file after loading? True if the file is
103 * the result of a conversion process.
105 bool remove_loaded_file_;
107 /// The image and its loading status.
108 boost::shared_ptr<Image> image_;
112 /// This signal is emitted when the image loading status changes.
113 boost::signal0<void> statusChanged;
115 /// The connection to the signal Image::finishedLoading
116 boost::signals::connection cl_;
118 /// The connection of the signal ConvProcess::finishedConversion,
119 boost::signals::connection cc_;
122 boost::scoped_ptr<Converter> converter_;
126 CacheItem::CacheItem(string const & file)
127 : pimpl_(new Impl(file))
131 CacheItem::~CacheItem()
135 string const & CacheItem::filename() const
137 return pimpl_->filename_;
141 void CacheItem::startLoading() const
143 pimpl_->startLoading();
147 void CacheItem::startMonitoring() const
149 if (!pimpl_->monitor_.monitoring())
150 pimpl_->monitor_.start();
154 bool CacheItem::monitoring() const
156 return pimpl_->monitor_.monitoring();
160 unsigned long CacheItem::checksum() const
162 return pimpl_->monitor_.checksum();
166 Image const * CacheItem::image() const
168 return pimpl_->image_.get();
172 ImageStatus CacheItem::status() const
174 return pimpl_->status_;
178 boost::signals::connection CacheItem::connect(slot_type const & slot) const
180 return pimpl_->statusChanged.connect(slot);
184 //------------------------------
185 // Implementation details follow
186 //------------------------------
189 CacheItem::Impl::Impl(string const & file)
191 monitor_(file, 2000),
193 remove_loaded_file_(false),
194 status_(WaitingToLoad)
196 monitor_.connect(boost::bind(&Impl::startLoading, this));
200 void CacheItem::Impl::startLoading()
202 if (status_ != WaitingToLoad)
205 convertToDisplayFormat();
209 void CacheItem::Impl::reset()
212 if (!unzipped_filename_.empty())
213 unlink(unzipped_filename_);
214 unzipped_filename_.erase();
216 if (remove_loaded_file_ && !file_to_load_.empty())
217 unlink(file_to_load_);
218 remove_loaded_file_ = false;
219 file_to_load_.erase();
224 status_ = WaitingToLoad;
232 if (converter_.get())
237 void CacheItem::Impl::setStatus(ImageStatus new_status)
239 if (status_ == new_status)
242 status_ = new_status;
247 void CacheItem::Impl::imageConverted(bool success)
249 string const text = success ? "succeeded" : "failed";
250 lyxerr[Debug::GRAPHICS] << "Image conversion " << text << '.' << endl;
252 file_to_load_ = converter_.get() ?
253 converter_->convertedFile() : string();
257 success = !file_to_load_.empty() && IsFileReadable(file_to_load_);
260 lyxerr[Debug::GRAPHICS] << "Unable to find converted file!"
262 setStatus(ErrorConverting);
265 unlink(unzipped_filename_);
274 // This function gets called from the callback after the image has been
275 // converted successfully.
276 void CacheItem::Impl::loadImage()
279 lyxerr[Debug::GRAPHICS] << "Loading image." << endl;
281 image_ = Image::newImage();
284 cl_ = image_->finishedLoading.connect(
285 boost::bind(&Impl::imageLoaded, this, _1));
286 image_->load(file_to_load_);
290 void CacheItem::Impl::imageLoaded(bool success)
292 string const text = success ? "succeeded" : "failed";
293 lyxerr[Debug::GRAPHICS] << "Image loading " << text << '.' << endl;
295 // Clean up after loading.
297 unlink(unzipped_filename_);
299 if (remove_loaded_file_ && unzipped_filename_ != file_to_load_)
300 unlink(file_to_load_);
305 setStatus(ErrorLoading);
309 // Inform the outside world.
319 string const findTargetFormat(string const & from)
321 typedef grfx::Image::FormatList FormatList;
322 FormatList const formats = grfx::Image::loadableFormats();
324 // There must be a format to load from.
325 Assert(!formats.empty());
327 // First ascertain if we can load directly with no conversion
328 FormatList::const_iterator it = formats.begin();
329 FormatList::const_iterator end = formats.end();
330 for (; it != end; ++it) {
335 // So, we have to convert to a loadable format. Can we?
336 it = formats.begin();
337 for (; it != end; ++it) {
338 if (grfx::Converter::isReachable(from, *it))
341 lyxerr[Debug::GRAPHICS]
342 << "Unable to convert from " << from
343 << " to " << *it << std::endl;
346 // Failed! so we have to try to convert it to PPM format
347 // with the standard converter
348 return string("ppm");
356 void CacheItem::Impl::convertToDisplayFormat()
358 setStatus(Converting);
360 // First, check that the file exists!
361 if (!IsFileReadable(filename_)) {
362 if (status_ != ErrorNoFile) {
363 setStatus(ErrorNoFile);
364 lyxerr[Debug::GRAPHICS]
365 << "\tThe file is not readable" << endl;
370 // Make a local copy in case we unzip it
371 string const filename = zippedFile(filename_) ?
372 unzipFile(filename_) : filename_;
373 string const displayed_filename = MakeDisplayPath(filename_);
374 lyxerr[Debug::GRAPHICS] << "[GrahicsCacheItem::convertToDisplayFormat]\n"
375 << "\tAttempting to convert image file: " << filename
376 << "\n\twith displayed filename: " << displayed_filename
379 string from = getExtFromContents(filename);
380 lyxerr[Debug::GRAPHICS]
381 << "\n\tThe file contains " << from << " format data." << endl;
382 string const to = findTargetFormat(from);
385 // No conversion needed!
386 lyxerr[Debug::GRAPHICS] << "\tNo conversion needed (from == to)!" << endl;
387 file_to_load_ = filename;
392 lyxerr[Debug::GRAPHICS] << "\tConverting it to " << to << " format." << endl;
393 // Take only the filename part of the file, without path or extension.
394 string const temp = ChangeExtension(OnlyFilename(filename), string());
396 // Add some stuff to create a uniquely named temporary file.
397 // This file is deleted in loadImage after it is loaded into memory.
398 string const to_file_base = tempName(string(), temp);
399 remove_loaded_file_ = true;
401 // Remove the temp file, we only want the name...
402 unlink(to_file_base);
404 // Connect a signal to this->imageConverted and pass this signal to
405 // the graphics converter so that we can load the modified file
406 // on completion of the conversion process.
407 converter_.reset(new Converter(filename, to_file_base, from, to));
408 converter_->connect(boost::bind(&Impl::imageConverted, this, _1));
409 converter_->startConversion();