+#include "GraphicsCacheItem.h"
+#include "GraphicsConverter.h"
+#include "GraphicsImage.h"
+
+#include "debug.h"
+
+#include "support/filetools.h"
+#include "support/FileMonitor.h"
+#include "support/lyxlib.h"
+
+#include <boost/bind.hpp>
+
+
+namespace support = lyx::support;
+
+using support::ChangeExtension;
+using support::FileMonitor;
+using support::IsFileReadable;
+using support::MakeDisplayPath;
+using support::OnlyFilename;
+using support::getExtFromContents;
+using support::tempName;
+using support::unlink;
+using support::unzipFile;
+using support::unzippedFileName;
+using support::zippedFile;
+
+using std::endl;
+using std::string;
+
+
+namespace lyx {
+namespace graphics {
+
+struct CacheItem::Impl : public boost::signals::trackable {
+
+ ///
+ Impl(string const & file);
+
+ /** Start the image conversion process, checking first that it is
+ * necessary. If it is necessary, then a conversion task is started.
+ * CacheItem asumes that the conversion is asynchronous and so
+ * passes a Signal to the converting routine. When the conversion
+ * is finished, this Signal is emitted, returning the converted
+ * file to this->imageConverted.
+ *
+ * If no file conversion is needed, then convertToDisplayFormat() calls
+ * loadImage() directly.
+ *
+ * convertToDisplayFormat() will set the loading status flag as
+ * approriate through calls to setStatus().
+ */
+ void convertToDisplayFormat();
+
+ /** Load the image into memory. This is called either from
+ * convertToDisplayFormat() direct or from imageConverted().
+ */
+ void loadImage();
+
+ /** Get a notification when the image conversion is done.
+ * Connected to a signal on_finish_ which is passed to
+ * Converter::convert.
+ */
+ void imageConverted(bool);
+
+ /** Get a notification when the image loading is done.
+ * Connected to a signal on_finish_ which is passed to
+ * lyx::graphics::Image::loadImage.
+ */
+ void imageLoaded(bool);
+
+ /** Sets the status of the loading process. Also notifies
+ * listeners that the status has changed.
+ */
+ void setStatus(ImageStatus new_status);
+
+ /** Can be invoked directly by the user, but is also connected to the
+ * FileMonitor and so is invoked when the file is changed
+ * (if monitoring is taking place).
+ */
+ void startLoading();
+
+ /** If we are asked to load the file for a second or further time,
+ * (because the file has changed), then we'll have to first reset
+ * many of the variables below.
+ */
+ void reset();
+
+ /// The filename we refer too.
+ string const filename_;
+ ///
+ FileMonitor const monitor_;
+
+ /// Is the file compressed?
+ bool zipped_;
+ /// If so, store the uncompressed file in this temporary file.
+ string unzipped_filename_;
+ /// What file are we trying to load?
+ string file_to_load_;
+ /** Should we delete the file after loading? True if the file is
+ * the result of a conversion process.
+ */
+ bool remove_loaded_file_;
+
+ /// The image and its loading status.
+ boost::shared_ptr<Image> image_;
+ ///
+ ImageStatus status_;
+
+ /// This signal is emitted when the image loading status changes.
+ boost::signal0<void> statusChanged;
+
+ /// The connection to the signal Image::finishedLoading
+ boost::signals::connection cl_;
+
+ /// The connection of the signal ConvProcess::finishedConversion,
+ boost::signals::connection cc_;
+
+ ///
+ boost::scoped_ptr<Converter> converter_;
+};
+
+
+CacheItem::CacheItem(string const & file)
+ : pimpl_(new Impl(file))
+{}