2002-02-27 09:59:52 +00:00
|
|
|
/*
|
|
|
|
* \file GraphicsCacheItem.C
|
|
|
|
* Copyright 2002 the LyX Team
|
|
|
|
* Read the file COPYING
|
2000-07-31 12:30:10 +00:00
|
|
|
*
|
2002-02-27 09:59:52 +00:00
|
|
|
* \author Baruch Even <baruch.even@writeme.com>
|
2002-01-31 14:20:09 +00:00
|
|
|
* \author Herbert Voss <voss@lyx.org>
|
2002-02-27 09:59:52 +00:00
|
|
|
* \author Angus Leeming <a.leeming@ic.ac.uk>
|
|
|
|
*/
|
2000-07-31 12:30:10 +00:00
|
|
|
|
2000-07-31 16:39:50 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2000-07-31 12:30:10 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
#include "graphics/GraphicsCacheItem.h"
|
|
|
|
#include "graphics/GraphicsImage.h"
|
|
|
|
#include "graphics/GraphicsConverter.h"
|
2002-05-29 16:21:03 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
#include "debug.h"
|
2002-05-29 16:21:03 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
#include "support/LAssert.h"
|
2002-02-16 15:59:55 +00:00
|
|
|
#include "support/filetools.h"
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-05-29 16:21:03 +00:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
using std::endl;
|
|
|
|
|
2002-03-11 22:58:05 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
namespace grfx {
|
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
GCacheItem::GCacheItem(string const & file)
|
|
|
|
: filename_(file), zipped_(false),
|
2002-02-27 09:59:52 +00:00
|
|
|
remove_loaded_file_(false), status_(WaitingToLoad)
|
2002-06-25 15:59:10 +00:00
|
|
|
{}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
void GCacheItem::startLoading()
|
2002-02-16 15:59:55 +00:00
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
if (status() != WaitingToLoad)
|
|
|
|
return;
|
|
|
|
|
|
|
|
convertToDisplayFormat();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GCacheItem::setStatus(ImageStatus new_status)
|
|
|
|
{
|
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;
|
|
|
|
statusChanged();
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GCacheItem::imageConverted(string const & file_to_load)
|
|
|
|
{
|
|
|
|
bool const success =
|
|
|
|
(!file_to_load.empty() && IsFileReadable(file_to_load));
|
|
|
|
|
|
|
|
string const text = success ? "succeeded" : "failed";
|
|
|
|
lyxerr[Debug::GRAPHICS] << "Image conversion " << text << "." << endl;
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
setStatus(ErrorConverting);
|
|
|
|
|
|
|
|
if (zipped_)
|
|
|
|
lyx::unlink(unzipped_filename_);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cc_.disconnect();
|
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
// Do the actual image loading from file to memory.
|
2002-02-27 09:59:52 +00:00
|
|
|
file_to_load_ = file_to_load;
|
|
|
|
|
|
|
|
loadImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This function gets called from the callback after the image has been
|
|
|
|
// converted successfully.
|
|
|
|
void GCacheItem::loadImage()
|
|
|
|
{
|
|
|
|
setStatus(Loading);
|
|
|
|
lyxerr[Debug::GRAPHICS] << "Loading image." << endl;
|
|
|
|
|
|
|
|
// Connect a signal to this->imageLoaded and pass this signal to
|
|
|
|
// GImage::loadImage.
|
|
|
|
SignalLoadTypePtr on_finish;
|
|
|
|
on_finish.reset(new SignalLoadType);
|
2002-05-29 16:21:03 +00:00
|
|
|
cl_ = on_finish->connect(boost::bind(&GCacheItem::imageLoaded, this, _1));
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
image_ = GImage::newImage();
|
|
|
|
image_->load(file_to_load_, on_finish);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GCacheItem::imageLoaded(bool success)
|
|
|
|
{
|
|
|
|
string const text = success ? "succeeded" : "failed";
|
|
|
|
lyxerr[Debug::GRAPHICS] << "Image loading " << text << "." << endl;
|
|
|
|
|
|
|
|
// Clean up after loading.
|
|
|
|
if (zipped_)
|
|
|
|
lyx::unlink(unzipped_filename_);
|
|
|
|
|
|
|
|
if (remove_loaded_file_ && unzipped_filename_ != file_to_load_)
|
|
|
|
lyx::unlink(file_to_load_);
|
|
|
|
|
|
|
|
cl_.disconnect();
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
setStatus(ErrorLoading);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setStatus(Loaded);
|
2002-04-11 17:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
namespace {
|
2001-04-24 15:25:26 +00:00
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
string const findTargetFormat(string const & from)
|
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
typedef GImage::FormatList FormatList;
|
2002-04-12 14:08:00 +00:00
|
|
|
FormatList const formats = GImage::loadableFormats();
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
// There must be a format to load from.
|
2002-02-27 09:59:52 +00:00
|
|
|
lyx::Assert(!formats.empty());
|
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
|
|
|
|
FormatList::const_iterator it1 = formats.begin();
|
2002-02-27 09:59:52 +00:00
|
|
|
FormatList::const_iterator end = formats.end();
|
2002-02-28 19:55:26 +00:00
|
|
|
for (; it1 != end; ++it1) {
|
|
|
|
if (from == *it1)
|
|
|
|
return *it1;
|
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?
|
|
|
|
grfx::GConverter const & graphics_converter = grfx::GConverter::get();
|
|
|
|
|
|
|
|
FormatList::const_iterator it2 = formats.begin();
|
|
|
|
for (; it2 != end; ++it2) {
|
|
|
|
if (graphics_converter.isReachable(from, *it2))
|
|
|
|
return *it2;
|
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-06-18 20:47:49 +00:00
|
|
|
// Failed! so we have to try to convert it to XPM format
|
|
|
|
// with the standard converter
|
|
|
|
return string("xpm");
|
2000-10-12 10:46:06 +00:00
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
} // anon namespace
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
void GCacheItem::convertToDisplayFormat()
|
2000-08-08 09:18:39 +00:00
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
setStatus(Converting);
|
2002-06-18 20:47:49 +00:00
|
|
|
// Make a local copy in case we unzip it
|
|
|
|
string const filename = zippedFile(filename_) ?
|
|
|
|
unzipFile(filename_) : filename_;
|
2002-02-27 09:59:52 +00:00
|
|
|
string const displayed_filename = MakeDisplayPath(filename_);
|
2002-06-18 20:47:49 +00:00
|
|
|
lyxerr[Debug::GRAPHICS] << "[GrahicsCacheItem::convertToDisplayFormat]\n"
|
|
|
|
<< "\tAttempting to convert image file: " << filename
|
|
|
|
<< "\n\twith displayed filename: " << displayed_filename
|
|
|
|
<< endl;
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
// First, check that the file exists!
|
|
|
|
if (!IsFileReadable(filename)) {
|
|
|
|
setStatus(ErrorNoFile);
|
2002-06-18 20:47:49 +00:00
|
|
|
lyxerr[Debug::GRAPHICS] << "\tThe file is not readable" << endl;
|
2002-02-27 09:59:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-06-18 20:47:49 +00:00
|
|
|
string from = getExtFromContents(filename);
|
|
|
|
// Some old ps-files make problems, so we do not need direct
|
|
|
|
// loading of an ps-file
|
|
|
|
if (from == "ps") {
|
|
|
|
lyxerr[Debug::GRAPHICS]
|
|
|
|
<< "\n\tThe file contains PostScript format data.\n"
|
|
|
|
<< "\tchanging it to eps-format to get it converted to xpm\n";
|
|
|
|
from = "eps";
|
|
|
|
} else
|
|
|
|
lyxerr[Debug::GRAPHICS]
|
|
|
|
<< "\n\tThe file contains " << from << " format data." << endl;
|
|
|
|
string const to = grfx::findTargetFormat(from);
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2001-09-20 13:16:17 +00:00
|
|
|
if (from == to) {
|
|
|
|
// No conversion needed!
|
2002-06-18 20:47:49 +00:00
|
|
|
lyxerr[Debug::GRAPHICS] << "\tNo conversion needed (from == to)!" << endl;
|
2002-02-27 09:59:52 +00:00
|
|
|
file_to_load_ = filename;
|
|
|
|
loadImage();
|
|
|
|
return;
|
2001-09-20 13:16:17 +00:00
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-06-18 20:47:49 +00:00
|
|
|
lyxerr[Debug::GRAPHICS] << "\tConverting it to " << to << " format." << endl;
|
2001-02-22 16:53:59 +00:00
|
|
|
// Take only the filename part of the file, without path or extension.
|
2002-02-27 09:59:52 +00:00
|
|
|
string const temp = ChangeExtension(OnlyFilename(filename), string());
|
2002-03-21 17:27:08 +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.
|
|
|
|
string const to_file_base = lyx::tempName(string(), temp);
|
|
|
|
remove_loaded_file_ = true;
|
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
// Remove the temp file, we only want the name...
|
2002-02-27 09:59:52 +00:00
|
|
|
lyx::unlink(to_file_base);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
SignalConvertTypePtr on_finish;
|
|
|
|
on_finish.reset(new SignalConvertType);
|
2002-05-29 16:21:03 +00:00
|
|
|
cc_ = on_finish->connect(boost::bind(&GCacheItem::imageConverted, this, _1));
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
GConverter & graphics_converter = GConverter::get();
|
|
|
|
graphics_converter.convert(filename, to_file_base, from, to, on_finish);
|
2000-08-08 09:18:39 +00:00
|
|
|
}
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
} // namespace grfx
|