2002-09-05 11:31:30 +00:00
|
|
|
/**
|
2007-04-25 03:01:35 +00:00
|
|
|
* \file GraphicsCacheItem.cpp
|
2002-09-05 15:14:23 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2000-07-31 12:30:10 +00:00
|
|
|
*
|
2002-11-27 10:30:28 +00:00
|
|
|
* \author Baruch Even
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Herbert Voß
|
2002-11-27 10:30:28 +00:00
|
|
|
* \author Angus Leeming
|
2002-09-05 11:31:30 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-02-27 09:59:52 +00:00
|
|
|
*/
|
2000-07-31 12:30:10 +00:00
|
|
|
|
2000-07-31 16:39:50 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
#include "GraphicsCacheItem.h"
|
2008-06-06 11:34:49 +00:00
|
|
|
|
|
|
|
#include "GraphicsCache.h"
|
2002-07-17 16:56:42 +00:00
|
|
|
#include "GraphicsConverter.h"
|
2003-09-05 03:10:30 +00:00
|
|
|
#include "GraphicsImage.h"
|
2002-07-18 14:01:42 +00:00
|
|
|
|
2006-11-13 10:27:57 +00:00
|
|
|
#include "ConverterCache.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Format.h"
|
2002-05-29 16:21:03 +00:00
|
|
|
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/debug.h"
|
2007-12-17 16:04:46 +00:00
|
|
|
#include "support/FileName.h"
|
2002-02-16 15:59:55 +00:00
|
|
|
#include "support/filetools.h"
|
2003-09-05 03:10:30 +00:00
|
|
|
#include "support/FileMonitor.h"
|
2011-07-14 17:00:35 +00:00
|
|
|
#include "support/lassert.h"
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2010-04-22 11:16:58 +00:00
|
|
|
#include "support/bind.h"
|
2002-05-29 16:21:03 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
2003-09-04 00:29:22 +00:00
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
namespace graphics {
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2005-01-19 15:03:31 +00:00
|
|
|
class CacheItem::Impl : public boost::signals::trackable {
|
|
|
|
public:
|
2002-06-28 11:22:56 +00:00
|
|
|
|
|
|
|
///
|
2006-11-26 21:30:39 +00:00
|
|
|
Impl(FileName const & file);
|
2002-06-28 11:22:56 +00:00
|
|
|
|
2008-06-06 13:30:38 +00:00
|
|
|
/**
|
|
|
|
* If no file conversion is needed, then tryDisplayFormat() calls
|
|
|
|
* loadImage() directly.
|
2009-03-12 19:25:07 +00:00
|
|
|
* \return true if a conversion is necessary and no error occurred.
|
2008-06-06 13:30:38 +00:00
|
|
|
*/
|
|
|
|
bool tryDisplayFormat(FileName & filename, string & from);
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
/** 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.
|
|
|
|
*
|
|
|
|
* 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().
|
|
|
|
*/
|
2008-06-20 16:12:48 +00:00
|
|
|
bool loadImage();
|
2002-06-28 11:22:56 +00:00
|
|
|
|
|
|
|
/** Get a notification when the image conversion is done.
|
|
|
|
* Connected to a signal on_finish_ which is passed to
|
|
|
|
* Converter::convert.
|
|
|
|
*/
|
|
|
|
void imageConverted(bool);
|
|
|
|
|
|
|
|
/** Sets the status of the loading process. Also notifies
|
2002-07-17 16:56:42 +00:00
|
|
|
* listeners that the status has changed.
|
2002-06-28 11:22:56 +00:00
|
|
|
*/
|
|
|
|
void setStatus(ImageStatus new_status);
|
|
|
|
|
2002-07-18 14:01:42 +00:00
|
|
|
/** 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();
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
/// The filename we refer too.
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const filename_;
|
2002-07-18 14:01:42 +00:00
|
|
|
///
|
|
|
|
FileMonitor const monitor_;
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
/// Is the file compressed?
|
|
|
|
bool zipped_;
|
|
|
|
/// If so, store the uncompressed file in this temporary file.
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName unzipped_filename_;
|
2006-11-13 10:27:57 +00:00
|
|
|
/// The target format
|
|
|
|
string to_;
|
2002-06-28 11:22:56 +00:00
|
|
|
/// What file are we trying to load?
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName file_to_load_;
|
2002-06-28 11:22:56 +00:00
|
|
|
/** 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.
|
2010-04-22 11:37:32 +00:00
|
|
|
shared_ptr<Image> image_;
|
2002-06-28 11:22:56 +00:00
|
|
|
///
|
|
|
|
ImageStatus status_;
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
/// This signal is emitted when the image loading status changes.
|
2004-09-26 14:19:47 +00:00
|
|
|
boost::signal<void()> statusChanged;
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
/// The connection of the signal ConvProcess::finishedConversion,
|
|
|
|
boost::signals::connection cc_;
|
|
|
|
|
|
|
|
///
|
|
|
|
boost::scoped_ptr<Converter> converter_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
CacheItem::CacheItem(FileName const & file)
|
2002-07-17 16:56:42 +00:00
|
|
|
: pimpl_(new Impl(file))
|
2002-06-25 15:59:10 +00:00
|
|
|
{}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
CacheItem::~CacheItem()
|
2007-11-21 23:47:47 +00:00
|
|
|
{
|
|
|
|
delete pimpl_;
|
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const & CacheItem::filename() const
|
2002-02-16 15:59:55 +00:00
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
return pimpl_->filename_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-06 13:30:38 +00:00
|
|
|
bool CacheItem::tryDisplayFormat() const
|
|
|
|
{
|
|
|
|
if (pimpl_->status_ != WaitingToLoad)
|
|
|
|
pimpl_->reset();
|
2009-03-12 19:44:40 +00:00
|
|
|
FileName filename;
|
|
|
|
string from;
|
|
|
|
bool const conversion_needed = pimpl_->tryDisplayFormat(filename, from);
|
2009-03-12 19:25:07 +00:00
|
|
|
bool const success = status() == Loaded && !conversion_needed;
|
2008-06-07 09:06:18 +00:00
|
|
|
if (!success)
|
|
|
|
pimpl_->reset();
|
|
|
|
return success;
|
2008-06-06 13:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
void CacheItem::startLoading() const
|
2002-06-28 11:22:56 +00:00
|
|
|
{
|
2002-07-18 14:01:42 +00:00
|
|
|
pimpl_->startLoading();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CacheItem::startMonitoring() const
|
|
|
|
{
|
|
|
|
if (!pimpl_->monitor_.monitoring())
|
|
|
|
pimpl_->monitor_.start();
|
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-07-18 14:01:42 +00:00
|
|
|
|
|
|
|
bool CacheItem::monitoring() const
|
|
|
|
{
|
|
|
|
return pimpl_->monitor_.monitoring();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long CacheItem::checksum() const
|
|
|
|
{
|
|
|
|
return pimpl_->monitor_.checksum();
|
2002-06-28 11:22:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Image const * CacheItem::image() const
|
|
|
|
{
|
|
|
|
return pimpl_->image_.get();
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
ImageStatus CacheItem::status() const
|
|
|
|
{
|
|
|
|
return pimpl_->status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
boost::signals::connection CacheItem::connect(slot_type const & slot) const
|
|
|
|
{
|
|
|
|
return pimpl_->statusChanged.connect(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
//------------------------------
|
|
|
|
// Implementation details follow
|
|
|
|
//------------------------------
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
CacheItem::Impl::Impl(FileName const & file)
|
2002-07-18 14:01:42 +00:00
|
|
|
: filename_(file),
|
|
|
|
monitor_(file, 2000),
|
|
|
|
zipped_(false),
|
|
|
|
remove_loaded_file_(false),
|
|
|
|
status_(WaitingToLoad)
|
|
|
|
{
|
2010-04-22 11:16:58 +00:00
|
|
|
monitor_.connect(bind(&Impl::startLoading, this));
|
2002-07-18 14:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CacheItem::Impl::startLoading()
|
|
|
|
{
|
|
|
|
if (status_ != WaitingToLoad)
|
|
|
|
reset();
|
|
|
|
|
|
|
|
convertToDisplayFormat();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CacheItem::Impl::reset()
|
|
|
|
{
|
|
|
|
zipped_ = false;
|
|
|
|
if (!unzipped_filename_.empty())
|
2007-11-28 09:01:49 +00:00
|
|
|
unzipped_filename_.removeFile();
|
2002-07-18 21:24:47 +00:00
|
|
|
unzipped_filename_.erase();
|
2002-07-18 14:01:42 +00:00
|
|
|
|
|
|
|
if (remove_loaded_file_ && !file_to_load_.empty())
|
2007-11-28 09:01:49 +00:00
|
|
|
file_to_load_.removeFile();
|
2002-07-18 14:01:42 +00:00
|
|
|
remove_loaded_file_ = false;
|
2002-07-18 21:24:47 +00:00
|
|
|
file_to_load_.erase();
|
2006-11-13 10:27:57 +00:00
|
|
|
to_.erase();
|
2002-07-18 14:01:42 +00:00
|
|
|
|
|
|
|
if (image_.get())
|
|
|
|
image_.reset();
|
|
|
|
|
|
|
|
status_ = WaitingToLoad;
|
|
|
|
|
|
|
|
if (cc_.connected())
|
|
|
|
cc_.disconnect();
|
|
|
|
|
|
|
|
if (converter_.get())
|
|
|
|
converter_.reset();
|
|
|
|
}
|
2002-06-28 11:22:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
void CacheItem::Impl::setStatus(ImageStatus new_status)
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2002-06-25 15:59:10 +00:00
|
|
|
if (status_ == new_status)
|
2002-02-27 09:59:52 +00:00
|
|
|
return;
|
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
status_ = new_status;
|
2002-07-17 16:56:42 +00:00
|
|
|
statusChanged();
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
void CacheItem::Impl::imageConverted(bool success)
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
|
|
|
string const text = success ? "succeeded" : "failed";
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GRAPHICS, "Image conversion " << text << '.');
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
file_to_load_ = converter_.get() ?
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName(converter_->convertedFile()) : FileName();
|
2002-06-28 11:22:56 +00:00
|
|
|
converter_.reset();
|
|
|
|
cc_.disconnect();
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2007-11-25 11:18:16 +00:00
|
|
|
success = !file_to_load_.empty() && file_to_load_.isReadableFile();
|
2002-06-28 11:22:56 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
if (!success) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GRAPHICS, "Unable to find converted file!");
|
2002-02-27 09:59:52 +00:00
|
|
|
setStatus(ErrorConverting);
|
|
|
|
|
|
|
|
if (zipped_)
|
2007-11-28 09:01:49 +00:00
|
|
|
unzipped_filename_.removeFile();
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-11-13 10:27:57 +00:00
|
|
|
// Add the converted file to the file cache
|
|
|
|
ConverterCache::get().add(filename_, to_, file_to_load_);
|
|
|
|
|
2008-06-20 16:12:48 +00:00
|
|
|
setStatus(loadImage() ? Loaded : ErrorLoading);
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This function gets called from the callback after the image has been
|
|
|
|
// converted successfully.
|
2008-06-20 16:12:48 +00:00
|
|
|
bool CacheItem::Impl::loadImage()
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GRAPHICS, "Loading image.");
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2008-09-19 12:01:01 +00:00
|
|
|
image_.reset(newImage());
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2008-06-06 11:52:02 +00:00
|
|
|
bool success = image_->load(file_to_load_);
|
2002-02-27 09:59:52 +00:00
|
|
|
string const text = success ? "succeeded" : "failed";
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GRAPHICS, "Image loading " << text << '.');
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
// Clean up after loading.
|
|
|
|
if (zipped_)
|
2007-11-28 09:01:49 +00:00
|
|
|
unzipped_filename_.removeFile();
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
if (remove_loaded_file_ && unzipped_filename_ != file_to_load_)
|
2007-11-28 09:01:49 +00:00
|
|
|
file_to_load_.removeFile();
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2008-06-20 16:12:48 +00:00
|
|
|
return success;
|
2002-04-11 17:40:44 +00:00
|
|
|
}
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
static string const findTargetFormat(string const & from)
|
2001-04-02 19:56:48 +00:00
|
|
|
{
|
2008-06-06 11:34:49 +00:00
|
|
|
typedef vector<string> FormatList;
|
|
|
|
FormatList const & formats = Cache::get().loadableFormats();
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2006-03-10 12:38:21 +00:00
|
|
|
// There must be a format to load from.
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(!formats.empty(), /**/);
|
2006-03-10 12:38:21 +00:00
|
|
|
|
2006-01-19 21:18:25 +00:00
|
|
|
// Use the standard converter if we don't know the format to load
|
|
|
|
// from.
|
2006-03-10 12:38:21 +00:00
|
|
|
if (from.empty())
|
2008-07-11 11:40:47 +00:00
|
|
|
return string("ppm");
|
2001-07-23 16:07:29 +00:00
|
|
|
|
2002-02-28 19:55:26 +00:00
|
|
|
// First ascertain if we can load directly with no conversion
|
2002-10-18 14:35:42 +00:00
|
|
|
FormatList::const_iterator it = formats.begin();
|
2002-02-27 09:59:52 +00:00
|
|
|
FormatList::const_iterator end = formats.end();
|
2002-10-18 14:35:42 +00:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
if (from == *it)
|
|
|
|
return *it;
|
2001-04-02 19:56:48 +00:00
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-02-28 19:55:26 +00:00
|
|
|
// So, we have to convert to a loadable format. Can we?
|
2002-10-18 14:35:42 +00:00
|
|
|
it = formats.begin();
|
|
|
|
for (; it != end; ++it) {
|
2003-07-21 21:30:57 +00:00
|
|
|
if (lyx::graphics::Converter::isReachable(from, *it))
|
2002-10-18 14:35:42 +00:00
|
|
|
return *it;
|
|
|
|
else
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GRAPHICS, "Unable to convert from " << from
|
|
|
|
<< " to " << *it);
|
2002-02-28 19:55:26 +00:00
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2008-07-11 11:40:47 +00:00
|
|
|
// Failed! so we have to try to convert it to PPM format
|
2002-06-18 20:47:49 +00:00
|
|
|
// with the standard converter
|
2008-07-11 11:40:47 +00:00
|
|
|
return string("ppm");
|
2000-10-12 10:46:06 +00:00
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
|
2008-06-06 13:30:38 +00:00
|
|
|
bool CacheItem::Impl::tryDisplayFormat(FileName & filename, string & from)
|
2000-08-08 09:18:39 +00:00
|
|
|
{
|
2002-07-18 14:01:42 +00:00
|
|
|
// First, check that the file exists!
|
2009-12-05 02:50:34 +00:00
|
|
|
filename_.refresh();
|
2007-11-25 11:18:16 +00:00
|
|
|
if (!filename_.isReadableFile()) {
|
2002-07-18 14:01:42 +00:00
|
|
|
if (status_ != ErrorNoFile) {
|
2008-06-20 16:12:48 +00:00
|
|
|
status_ = ErrorNoFile;
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GRAPHICS, "\tThe file is not readable");
|
2002-07-18 14:01:42 +00:00
|
|
|
}
|
2009-03-12 19:25:07 +00:00
|
|
|
return false;
|
2002-07-18 14:01:42 +00:00
|
|
|
}
|
|
|
|
|
2011-09-19 00:12:09 +00:00
|
|
|
zipped_ = formats.isZippedFile(filename);
|
2005-11-02 20:11:36 +00:00
|
|
|
if (zipped_) {
|
2007-12-16 09:52:36 +00:00
|
|
|
unzipped_filename_ = FileName::tempName(
|
2007-12-16 07:52:38 +00:00
|
|
|
filename_.toFilesystemEncoding());
|
2004-02-25 12:00:53 +00:00
|
|
|
if (unzipped_filename_.empty()) {
|
2008-06-20 16:12:48 +00:00
|
|
|
status_ = ErrorConverting;
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GRAPHICS, "\tCould not create temporary file.");
|
2009-03-12 19:25:07 +00:00
|
|
|
return false;
|
2004-02-25 12:00:53 +00:00
|
|
|
}
|
2006-11-26 21:30:39 +00:00
|
|
|
filename = unzipFile(filename_, unzipped_filename_.toFilesystemEncoding());
|
2007-10-20 17:35:27 +00:00
|
|
|
} else {
|
2004-02-25 12:00:53 +00:00
|
|
|
filename = filename_;
|
2007-10-20 17:35:27 +00:00
|
|
|
}
|
2004-02-25 12:00:53 +00:00
|
|
|
|
2010-04-21 01:19:09 +00:00
|
|
|
docstring const displayed_filename = makeDisplayPath(filename_.absFileName());
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GRAPHICS, "[CacheItem::Impl::convertToDisplayFormat]\n"
|
2002-06-18 20:47:49 +00:00
|
|
|
<< "\tAttempting to convert image file: " << filename
|
2007-11-15 20:04:51 +00:00
|
|
|
<< "\n\twith displayed filename: " << to_utf8(displayed_filename));
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2008-06-06 13:30:38 +00:00
|
|
|
from = formats.getFormatFromFile(filename);
|
2004-11-09 19:08:34 +00:00
|
|
|
if (from.empty()) {
|
2008-06-20 16:12:48 +00:00
|
|
|
status_ = ErrorConverting;
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GRAPHICS, "\tCould not determine file format.");
|
2004-11-09 19:08:34 +00:00
|
|
|
}
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GRAPHICS, "\n\tThe file contains " << from << " format data.");
|
2006-11-13 10:27:57 +00:00
|
|
|
to_ = findTargetFormat(from);
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2006-11-13 10:27:57 +00:00
|
|
|
if (from == to_) {
|
2001-09-20 13:16:17 +00:00
|
|
|
// No conversion needed!
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GRAPHICS, "\tNo conversion needed (from == to)!");
|
2002-02-27 09:59:52 +00:00
|
|
|
file_to_load_ = filename;
|
2008-06-20 16:12:48 +00:00
|
|
|
status_ = loadImage() ? Loaded : ErrorLoading;
|
2009-03-12 19:25:07 +00:00
|
|
|
return false;
|
2001-09-20 13:16:17 +00:00
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2006-11-13 10:27:57 +00:00
|
|
|
if (ConverterCache::get().inCache(filename, to_)) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GRAPHICS, "\tNo conversion needed (file in file cache)!");
|
2006-11-13 10:27:57 +00:00
|
|
|
file_to_load_ = ConverterCache::get().cacheName(filename, to_);
|
2008-06-20 16:12:48 +00:00
|
|
|
status_ = loadImage() ? Loaded : ErrorLoading;
|
2009-03-12 19:25:07 +00:00
|
|
|
return false;
|
2006-11-13 10:27:57 +00:00
|
|
|
}
|
2009-03-12 19:25:07 +00:00
|
|
|
return true;
|
2008-06-06 13:30:38 +00:00
|
|
|
}
|
|
|
|
|
2006-11-13 10:27:57 +00:00
|
|
|
|
2008-06-06 13:30:38 +00:00
|
|
|
void CacheItem::Impl::convertToDisplayFormat()
|
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GRAPHICS, "\tConverting it to " << to_ << " format.");
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2008-06-06 13:30:38 +00:00
|
|
|
// Make a local copy in case we unzip it
|
|
|
|
FileName filename;
|
|
|
|
string from;
|
2009-03-12 19:25:07 +00:00
|
|
|
if (!tryDisplayFormat(filename, from)) {
|
2008-06-20 16:12:48 +00:00
|
|
|
// The image status has changed, tell it to the outside world.
|
|
|
|
statusChanged();
|
2008-06-06 13:30:38 +00:00
|
|
|
return;
|
2008-06-20 16:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We will need a conversion, tell it to the outside world.
|
|
|
|
setStatus(Converting);
|
2008-06-06 13:30:38 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
// Add some stuff to create a uniquely named temporary file.
|
|
|
|
// This file is deleted in loadImage after it is loaded into memory.
|
2007-12-16 09:52:36 +00:00
|
|
|
FileName const to_file_base = FileName::tempName("CacheItem");
|
2002-02-27 09:59:52 +00:00
|
|
|
remove_loaded_file_ = true;
|
|
|
|
|
|
|
|
// Connect a signal to this->imageConverted and pass this signal to
|
|
|
|
// the graphics converter so that we can load the modified file
|
|
|
|
// on completion of the conversion process.
|
2010-04-21 01:19:09 +00:00
|
|
|
converter_.reset(new Converter(filename, to_file_base.absFileName(), from, to_));
|
2010-04-22 11:16:58 +00:00
|
|
|
converter_->connect(bind(&Impl::imageConverted, this, _1));
|
2002-06-28 11:22:56 +00:00
|
|
|
converter_->startConversion();
|
2000-08-08 09:18:39 +00:00
|
|
|
}
|
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
} // namespace graphics
|
|
|
|
} // namespace lyx
|