2 * \file GraphicsCacheItem.C
3 * Read the file COPYING
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS
15 #pragma implementation
18 #include "GraphicsCacheItem.h"
19 #include "GraphicsImage.h"
20 #include "GraphicsConverter.h"
22 #include "support/FileMonitor.h"
26 #include "support/LAssert.h"
27 #include "support/filetools.h"
29 #include <boost/shared_ptr.hpp>
30 #include <boost/bind.hpp>
31 #include <boost/signals/trackable.hpp>
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 lyx::unlink(unzipped_filename_);
214 unzipped_filename_.erase();
216 if (remove_loaded_file_ && !file_to_load_.empty())
217 lyx::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_);
258 lyxerr[Debug::GRAPHICS] << "Unable to find converted file!" << endl;
261 setStatus(ErrorConverting);
264 lyx::unlink(unzipped_filename_);
273 // This function gets called from the callback after the image has been
274 // converted successfully.
275 void CacheItem::Impl::loadImage()
278 lyxerr[Debug::GRAPHICS] << "Loading image." << endl;
280 image_ = Image::newImage();
283 cl_ = image_->finishedLoading.connect(
284 boost::bind(&Impl::imageLoaded, this, _1));
285 image_->load(file_to_load_);
289 void CacheItem::Impl::imageLoaded(bool success)
291 string const text = success ? "succeeded" : "failed";
292 lyxerr[Debug::GRAPHICS] << "Image loading " << text << "." << endl;
294 // Clean up after loading.
296 lyx::unlink(unzipped_filename_);
298 if (remove_loaded_file_ && unzipped_filename_ != file_to_load_)
299 lyx::unlink(file_to_load_);
304 setStatus(ErrorLoading);
308 // Inform the outside world.
318 string const findTargetFormat(string const & from)
320 typedef grfx::Image::FormatList FormatList;
321 FormatList const formats = grfx::Image::loadableFormats();
323 // There must be a format to load from.
324 lyx::Assert(!formats.empty());
326 // First ascertain if we can load directly with no conversion
327 FormatList::const_iterator it1 = formats.begin();
328 FormatList::const_iterator end = formats.end();
329 for (; it1 != end; ++it1) {
334 // So, we have to convert to a loadable format. Can we?
335 FormatList::const_iterator it2 = formats.begin();
336 for (; it2 != end; ++it2) {
337 if (grfx::Converter::isReachable(from, *it2))
341 // Failed! so we have to try to convert it to XPM format
342 // with the standard converter
343 return string("xpm");
351 void CacheItem::Impl::convertToDisplayFormat()
353 setStatus(Converting);
355 // First, check that the file exists!
356 if (!IsFileReadable(filename_)) {
357 if (status_ != ErrorNoFile) {
358 setStatus(ErrorNoFile);
359 lyxerr[Debug::GRAPHICS]
360 << "\tThe file is not readable" << endl;
365 // Make a local copy in case we unzip it
366 string const filename = zippedFile(filename_) ?
367 unzipFile(filename_) : filename_;
368 string const displayed_filename = MakeDisplayPath(filename_);
369 lyxerr[Debug::GRAPHICS] << "[GrahicsCacheItem::convertToDisplayFormat]\n"
370 << "\tAttempting to convert image file: " << filename
371 << "\n\twith displayed filename: " << displayed_filename
374 string from = getExtFromContents(filename);
375 lyxerr[Debug::GRAPHICS]
376 << "\n\tThe file contains " << from << " format data." << endl;
377 string const to = findTargetFormat(from);
380 // No conversion needed!
381 lyxerr[Debug::GRAPHICS] << "\tNo conversion needed (from == to)!" << endl;
382 file_to_load_ = filename;
387 lyxerr[Debug::GRAPHICS] << "\tConverting it to " << to << " format." << endl;
388 // Take only the filename part of the file, without path or extension.
389 string const temp = ChangeExtension(OnlyFilename(filename), string());
391 // Add some stuff to create a uniquely named temporary file.
392 // This file is deleted in loadImage after it is loaded into memory.
393 string const to_file_base = lyx::tempName(string(), temp);
394 remove_loaded_file_ = true;
396 // Remove the temp file, we only want the name...
397 lyx::unlink(to_file_base);
399 // Connect a signal to this->imageConverted and pass this signal to
400 // the graphics converter so that we can load the modified file
401 // on completion of the conversion process.
402 converter_.reset(new Converter(filename, to_file_base, from, to));
403 converter_->connect(boost::bind(&Impl::imageConverted, this, _1));
404 converter_->startConversion();