2007-08-30 20:46:42 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/**
|
|
|
|
* \file EmbeddedFiles.cpp
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Bo Peng
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "EmbeddedFiles.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
|
2007-08-30 20:46:42 +00:00
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferParams.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "ErrorList.h"
|
2007-08-30 20:46:42 +00:00
|
|
|
#include "Format.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "InsetIterator.h"
|
2007-09-10 03:54:02 +00:00
|
|
|
#include "Lexer.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "LyX.h"
|
|
|
|
#include "Paragraph.h"
|
|
|
|
#include "Session.h"
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
#include "frontends/alert.h"
|
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2007-08-30 20:46:42 +00:00
|
|
|
#include "support/filetools.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/gettext.h"
|
2007-08-30 20:46:42 +00:00
|
|
|
#include "support/convert.h"
|
2007-09-01 20:08:16 +00:00
|
|
|
#include "support/lstrings.h"
|
2008-01-03 16:41:06 +00:00
|
|
|
#include "support/ExceptionMessage.h"
|
2007-12-02 10:43:11 +00:00
|
|
|
#include "support/FileZipListDir.h"
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <utility>
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
2008-01-05 05:28:39 +00:00
|
|
|
/** Dir name used for ".." in the bundled file.
|
|
|
|
|
|
|
|
Under the lyx temp directory, content.lyx and its embedded files are usually
|
|
|
|
saved as
|
|
|
|
|
|
|
|
$temp/file.lyx
|
|
|
|
$temp/figure1.png for ./figure1.png)
|
|
|
|
$temp/sub/figure2.png for ./sub/figure2.png)
|
|
|
|
|
|
|
|
This works fine for embedded files that are in the current or deeper directory
|
|
|
|
of the document directory, but not for files such as ../figures/figure.png.
|
|
|
|
A unique name $upDirName is chosen to represent .. in such filenames so that
|
|
|
|
'up' directories can be stored 'down' the directory tree:
|
|
|
|
|
|
|
|
$temp/$upDirName/figures/figure.png for ../figures/figure.png
|
|
|
|
$temp/$upDirName/$upDirName/figure.png for ../../figure.png
|
|
|
|
|
|
|
|
This name has to be fixed because it is used in lyx bundled .zip file.
|
|
|
|
|
|
|
|
Note that absolute files are not embeddable because there is no easy
|
|
|
|
way to put them under $temp.
|
|
|
|
*/
|
|
|
|
const std::string upDirName = "LyX.Embed.Dir.Up";
|
|
|
|
|
2007-08-30 20:46:42 +00:00
|
|
|
namespace Alert = frontend::Alert;
|
|
|
|
|
2008-01-05 05:28:39 +00:00
|
|
|
EmbeddedFile::EmbeddedFile(string const & file, std::string const & buffer_path)
|
|
|
|
: DocFileName("", false), inzip_name_(""), embedded_(false), inset_list_()
|
|
|
|
{
|
|
|
|
set(file, buffer_path);
|
|
|
|
}
|
2007-08-30 20:46:42 +00:00
|
|
|
|
2008-01-05 05:28:39 +00:00
|
|
|
|
|
|
|
void EmbeddedFile::set(std::string const & filename, std::string const & buffer_path)
|
2007-09-08 04:10:43 +00:00
|
|
|
{
|
2008-01-05 05:28:39 +00:00
|
|
|
DocFileName::set(filename, buffer_path);
|
|
|
|
if (filename.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
inzip_name_ = to_utf8(makeRelPath(from_utf8(absFilename()),
|
|
|
|
from_utf8(buffer_path)));
|
|
|
|
// if inzip_name_ is an absolute path, this file is not embeddable
|
|
|
|
if (FileName(inzip_name_).isAbsolute())
|
|
|
|
inzip_name_ = "";
|
|
|
|
// replace .. by upDirName
|
|
|
|
if (prefixIs(inzip_name_, "."))
|
|
|
|
inzip_name_ = subst(inzip_name_, "..", upDirName);
|
|
|
|
LYXERR(Debug::FILES, "Create embedded file " << filename <<
|
|
|
|
" with in zip name " << inzip_name_ << endl);
|
2007-09-08 04:10:43 +00:00
|
|
|
}
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
string EmbeddedFile::embeddedFile(Buffer const * buf) const
|
|
|
|
{
|
2008-01-05 05:28:39 +00:00
|
|
|
BOOST_ASSERT(embeddable());
|
|
|
|
string temp = buf->temppath();
|
|
|
|
if (!suffixIs(temp, '/'))
|
|
|
|
temp += '/';
|
|
|
|
return temp + inzip_name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string EmbeddedFile::availableFile(Buffer const * buf) const
|
|
|
|
{
|
|
|
|
return embedded() ? embeddedFile(buf) : absFilename();
|
2007-08-30 20:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-09 07:07:13 +00:00
|
|
|
void EmbeddedFile::addInset(Inset const * inset)
|
2007-09-01 04:20:34 +00:00
|
|
|
{
|
2008-01-05 05:28:39 +00:00
|
|
|
if (inset != NULL)
|
|
|
|
inset_list_.push_back(inset);
|
2007-09-01 04:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-09 07:07:13 +00:00
|
|
|
Inset const * EmbeddedFile::inset(int idx) const
|
2007-08-30 20:46:42 +00:00
|
|
|
{
|
2007-09-08 04:10:43 +00:00
|
|
|
BOOST_ASSERT(idx < refCount());
|
|
|
|
// some embedded file do not have a valid par iterator
|
2007-09-09 07:07:13 +00:00
|
|
|
return inset_list_[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-05 05:28:39 +00:00
|
|
|
void EmbeddedFile::setEmbed(bool embed)
|
2007-09-01 20:08:16 +00:00
|
|
|
{
|
2008-01-05 05:28:39 +00:00
|
|
|
if (!embeddable() && embed) {
|
|
|
|
Alert::error(_("Embedding failed."), bformat(
|
|
|
|
_("Cannot embed file %1$s because its path is not relative to document path."),
|
|
|
|
from_utf8(absFilename())));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
embedded_ = embed;
|
2007-09-08 04:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-01 20:08:16 +00:00
|
|
|
bool EmbeddedFile::extract(Buffer const * buf) const
|
|
|
|
{
|
2008-01-05 05:28:39 +00:00
|
|
|
BOOST_ASSERT(embeddable());
|
|
|
|
|
2007-09-01 20:08:16 +00:00
|
|
|
string ext_file = absFilename();
|
|
|
|
string emb_file = embeddedFile(buf);
|
2007-09-08 04:10:43 +00:00
|
|
|
|
2007-11-08 00:09:58 +00:00
|
|
|
FileName emb(emb_file);
|
|
|
|
FileName ext(ext_file);
|
|
|
|
|
|
|
|
if (!emb.exists())
|
2008-01-05 05:28:39 +00:00
|
|
|
throw ExceptionMessage(ErrorException, _("Failed to extract file"),
|
|
|
|
bformat(_("Cannot extract file '%1$s'.\n"
|
|
|
|
"Source file %2$s does not exist"),
|
|
|
|
from_utf8(outputFilename()), from_utf8(emb_file)));
|
2007-09-08 04:10:43 +00:00
|
|
|
|
|
|
|
// if external file already exists ...
|
2007-11-08 00:09:58 +00:00
|
|
|
if (ext.exists()) {
|
2007-09-08 04:10:43 +00:00
|
|
|
// no need to copy if the files are the same
|
2007-11-30 09:23:44 +00:00
|
|
|
if (checksum() == FileName(emb_file).checksum())
|
2007-09-08 04:10:43 +00:00
|
|
|
return true;
|
|
|
|
// otherwise, ask if overwrite
|
|
|
|
int ret = Alert::prompt(
|
2007-09-01 20:08:16 +00:00
|
|
|
_("Overwrite external file?"),
|
|
|
|
bformat(_("External file %1$s already exists, do you want to overwrite it"),
|
|
|
|
from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
|
2007-09-08 04:10:43 +00:00
|
|
|
if (ret != 0)
|
|
|
|
// if the user does not want to overwrite, we still consider it
|
|
|
|
// a successful operation.
|
2007-09-01 20:08:16 +00:00
|
|
|
return true;
|
2007-09-08 04:10:43 +00:00
|
|
|
}
|
|
|
|
// copy file
|
2007-11-07 19:52:11 +00:00
|
|
|
|
|
|
|
// need to make directory?
|
2007-11-25 21:21:03 +00:00
|
|
|
FileName path = ext.onlyPath();
|
2007-12-14 22:43:39 +00:00
|
|
|
if (!path.createPath()) {
|
2008-01-03 16:41:06 +00:00
|
|
|
throw ExceptionMessage(ErrorException, _("Copy file failure"),
|
2007-12-14 22:43:39 +00:00
|
|
|
bformat(_("Cannot create file path '%1$s'.\n"
|
|
|
|
"Please check whether the path is writeable."),
|
|
|
|
from_utf8(path.absFilename())));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-01-05 05:28:39 +00:00
|
|
|
if (emb.copyTo(ext)) {
|
|
|
|
LYXERR(Debug::FILES, "Extract file " << emb_file << " to " << ext_file << endl);
|
2007-09-08 04:10:43 +00:00
|
|
|
return true;
|
2008-01-05 05:28:39 +00:00
|
|
|
}
|
2007-12-14 22:43:39 +00:00
|
|
|
|
2008-01-03 16:41:06 +00:00
|
|
|
throw ExceptionMessage(ErrorException, _("Copy file failure"),
|
2007-11-07 19:52:11 +00:00
|
|
|
bformat(_("Cannot copy file %1$s to %2$s.\n"
|
|
|
|
"Please check whether the directory exists and is writeable."),
|
|
|
|
from_utf8(emb_file), from_utf8(ext_file)));
|
2007-09-01 22:26:59 +00:00
|
|
|
return false;
|
2007-09-01 20:08:16 +00:00
|
|
|
}
|
|
|
|
|
2007-09-07 03:02:24 +00:00
|
|
|
|
2007-09-08 04:10:43 +00:00
|
|
|
bool EmbeddedFile::updateFromExternalFile(Buffer const * buf) const
|
2007-09-07 03:02:24 +00:00
|
|
|
{
|
2008-01-05 05:28:39 +00:00
|
|
|
BOOST_ASSERT(embeddable());
|
|
|
|
|
2007-09-07 03:02:24 +00:00
|
|
|
string ext_file = absFilename();
|
|
|
|
string emb_file = embeddedFile(buf);
|
2007-09-08 04:10:43 +00:00
|
|
|
|
2007-11-08 00:09:58 +00:00
|
|
|
FileName emb(emb_file);
|
|
|
|
FileName ext(ext_file);
|
|
|
|
|
2008-01-05 05:28:39 +00:00
|
|
|
if (!ext.exists()) {
|
|
|
|
// no need to update
|
|
|
|
if (emb.exists())
|
|
|
|
return true;
|
|
|
|
// no external and internal file
|
|
|
|
throw ExceptionMessage(ErrorException,
|
|
|
|
_("Failed to embed file"),
|
|
|
|
bformat(_("Failed to embed file %1$s.\n"
|
|
|
|
"Please check whether this file exists and is readable."),
|
|
|
|
from_utf8(ext_file)));
|
|
|
|
}
|
|
|
|
|
2007-09-08 04:10:43 +00:00
|
|
|
// if embedded file already exists ...
|
2007-11-08 00:09:58 +00:00
|
|
|
if (emb.exists()) {
|
2007-09-08 04:10:43 +00:00
|
|
|
// no need to copy if the files are the same
|
2007-11-30 09:23:44 +00:00
|
|
|
if (checksum() == FileName(emb_file).checksum())
|
2007-09-08 04:10:43 +00:00
|
|
|
return true;
|
|
|
|
// other wise, ask if overwrite
|
2007-09-07 03:02:24 +00:00
|
|
|
int const ret = Alert::prompt(
|
|
|
|
_("Update embedded file?"),
|
2007-10-24 22:02:48 +00:00
|
|
|
bformat(_("Embedded file %1$s already exists, do you want to overwrite it"),
|
2007-09-07 03:02:24 +00:00
|
|
|
from_utf8(ext_file)), 1, 1, _("&Overwrite"), _("&Cancel"));
|
2007-09-08 04:10:43 +00:00
|
|
|
if (ret != 0)
|
|
|
|
// if the user does not want to overwrite, we still consider it
|
|
|
|
// a successful operation.
|
|
|
|
return true;
|
2007-09-07 03:02:24 +00:00
|
|
|
}
|
2007-09-08 04:10:43 +00:00
|
|
|
// copy file
|
2007-11-07 19:52:11 +00:00
|
|
|
// need to make directory?
|
2007-11-25 21:21:03 +00:00
|
|
|
FileName path = emb.onlyPath();
|
|
|
|
if (!path.isDirectory())
|
2007-12-14 22:43:39 +00:00
|
|
|
path.createPath();
|
2007-12-15 08:25:53 +00:00
|
|
|
if (ext.copyTo(emb))
|
2007-09-08 04:10:43 +00:00
|
|
|
return true;
|
2008-01-05 05:28:39 +00:00
|
|
|
throw ExceptionMessage(ErrorException,
|
2008-01-03 16:41:06 +00:00
|
|
|
_("Copy file failure"),
|
|
|
|
bformat(_("Cannot copy file %1$s to %2$s.\n"
|
2007-11-07 19:52:11 +00:00
|
|
|
"Please check whether the directory exists and is writeable."),
|
|
|
|
from_utf8(ext_file), from_utf8(emb_file)));
|
2007-11-15 20:04:51 +00:00
|
|
|
//LYXERR(Debug::DEBUG, "Fs error: " << fe.what());
|
2007-09-08 04:10:43 +00:00
|
|
|
return false;
|
2007-09-07 03:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-12 14:27:47 +00:00
|
|
|
void EmbeddedFile::updateInsets(Buffer const * buf) const
|
|
|
|
{
|
|
|
|
vector<Inset const *>::const_iterator it = inset_list_.begin();
|
|
|
|
vector<Inset const *>::const_iterator it_end = inset_list_.end();
|
|
|
|
for (; it != it_end; ++it)
|
|
|
|
const_cast<Inset *>(*it)->updateEmbeddedFile(*buf, *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-05 05:28:39 +00:00
|
|
|
bool operator==(EmbeddedFile const & lhs, EmbeddedFile const & rhs)
|
|
|
|
{
|
|
|
|
return lhs.absFilename() == rhs.absFilename()
|
|
|
|
&& lhs.saveAbsPath() == rhs.saveAbsPath()
|
|
|
|
&& lhs.embedded() == rhs.embedded();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(EmbeddedFile const & lhs, EmbeddedFile const & rhs)
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-30 20:46:42 +00:00
|
|
|
bool EmbeddedFiles::enabled() const
|
|
|
|
{
|
|
|
|
return buffer_->params().embedded;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-11 21:27:57 +00:00
|
|
|
void EmbeddedFiles::enable(bool flag)
|
2007-08-30 20:46:42 +00:00
|
|
|
{
|
|
|
|
if (enabled() != flag) {
|
2008-01-03 16:41:06 +00:00
|
|
|
// update embedded file list
|
|
|
|
update();
|
|
|
|
// An exception may be thrown.
|
|
|
|
if (flag)
|
|
|
|
// if enable, copy all files to temppath()
|
|
|
|
updateFromExternalFile();
|
|
|
|
else
|
|
|
|
// if disable, extract all files
|
|
|
|
extractAll();
|
|
|
|
// if operation is successful (no exception is thrown)
|
2007-09-08 04:10:43 +00:00
|
|
|
buffer_->markDirty();
|
|
|
|
buffer_->params().embedded = flag;
|
2007-09-12 14:27:47 +00:00
|
|
|
if (flag)
|
|
|
|
updateInsets();
|
2007-08-30 20:46:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-05 05:28:39 +00:00
|
|
|
EmbeddedFile & EmbeddedFiles::registerFile(EmbeddedFile const & file, Inset const * inset)
|
2007-08-30 20:46:42 +00:00
|
|
|
{
|
|
|
|
// try to find this file from the list
|
|
|
|
EmbeddedFileList::iterator it = file_list_.begin();
|
|
|
|
EmbeddedFileList::iterator it_end = file_list_.end();
|
|
|
|
for (; it != it_end; ++it)
|
2008-01-05 05:28:39 +00:00
|
|
|
if (it->absFilename() == file.absFilename()) {
|
|
|
|
if (it->embedded() != file.embedded()) {
|
|
|
|
Alert::error(_("Wrong embedding status."),
|
|
|
|
bformat(_("File %1$s is included in more than one insets, "
|
|
|
|
"but with different embedding status. Assuming embedding status."),
|
|
|
|
from_utf8(it->outputFilename())));
|
|
|
|
it->setEmbed(true);
|
|
|
|
}
|
|
|
|
it->addInset(inset);
|
|
|
|
return *it;
|
|
|
|
}
|
2007-10-30 22:21:48 +00:00
|
|
|
//
|
2008-01-05 05:28:39 +00:00
|
|
|
file_list_.push_back(file);
|
|
|
|
file_list_.back().addInset(inset);
|
2007-09-13 05:30:44 +00:00
|
|
|
return file_list_.back();
|
2007-08-30 20:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EmbeddedFiles::update()
|
|
|
|
{
|
2007-10-30 22:21:48 +00:00
|
|
|
file_list_.clear();
|
2007-08-30 20:46:42 +00:00
|
|
|
|
2007-09-09 07:07:13 +00:00
|
|
|
for (InsetIterator it = inset_iterator_begin(buffer_->inset()); it; ++it)
|
|
|
|
it->registerEmbeddedFiles(*buffer_, *this);
|
2007-08-30 20:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-11 14:23:12 +00:00
|
|
|
bool EmbeddedFiles::writeFile(DocFileName const & filename)
|
2007-08-30 20:46:42 +00:00
|
|
|
{
|
|
|
|
// file in the temporary path has the content
|
|
|
|
string const content = FileName(addName(buffer_->temppath(),
|
2007-09-10 03:54:02 +00:00
|
|
|
"content.lyx")).toFilesystemEncoding();
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
vector<pair<string, string> > filenames;
|
2007-09-10 03:54:02 +00:00
|
|
|
// add content.lyx to filenames
|
|
|
|
filenames.push_back(make_pair(content, "content.lyx"));
|
2007-08-30 20:46:42 +00:00
|
|
|
// prepare list of embedded file
|
|
|
|
EmbeddedFileList::iterator it = file_list_.begin();
|
|
|
|
EmbeddedFileList::iterator it_end = file_list_.end();
|
|
|
|
for (; it != it_end; ++it) {
|
2007-10-30 22:21:48 +00:00
|
|
|
if (it->embedded()) {
|
2008-01-05 05:28:39 +00:00
|
|
|
string file = it->embeddedFile(buffer_);
|
|
|
|
if (!FileName(file).exists())
|
|
|
|
throw ExceptionMessage(ErrorException, _("Failed to write file"),
|
|
|
|
bformat(_("Embedded file %1$s does not exist. Did you tamper lyx temporary directory?"),
|
|
|
|
it->displayName()));
|
|
|
|
filenames.push_back(make_pair(file, it->inzipName()));
|
|
|
|
LYXERR(Debug::FILES, "Writing file " << it->outputFilename()
|
|
|
|
<< " as " << it->inzipName() << endl);
|
2007-08-30 20:46:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// write a zip file with all these files. Write to a temp file first, to
|
|
|
|
// avoid messing up the original file in case something goes terribly wrong.
|
|
|
|
DocFileName zipfile(addName(buffer_->temppath(),
|
|
|
|
onlyFilename(changeExtension(
|
|
|
|
filename.toFilesystemEncoding(), ".zip"))));
|
|
|
|
|
2007-09-03 21:09:11 +00:00
|
|
|
::zipFiles(zipfile.toFilesystemEncoding(), filenames);
|
2007-08-30 20:46:42 +00:00
|
|
|
// copy file back
|
2007-12-15 08:25:53 +00:00
|
|
|
if (!zipfile.copyTo(filename)) {
|
2007-08-30 20:46:42 +00:00
|
|
|
Alert::error(_("Save failure"),
|
|
|
|
bformat(_("Cannot create file %1$s.\n"
|
|
|
|
"Please check whether the directory exists and is writeable."),
|
|
|
|
from_utf8(filename.absFilename())));
|
2007-11-15 20:04:51 +00:00
|
|
|
//LYXERR(Debug::DEBUG, "Fs error: " << fe.what());
|
2007-08-30 20:46:42 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-07 19:52:11 +00:00
|
|
|
EmbeddedFiles::EmbeddedFileList::const_iterator
|
2007-12-12 19:28:07 +00:00
|
|
|
EmbeddedFiles::find(string filename) const
|
2007-09-05 17:50:07 +00:00
|
|
|
{
|
|
|
|
EmbeddedFileList::const_iterator it = file_list_.begin();
|
|
|
|
EmbeddedFileList::const_iterator it_end = file_list_.end();
|
|
|
|
for (; it != it_end; ++it)
|
2008-01-05 05:28:39 +00:00
|
|
|
if (it->absFilename() == filename || it->embeddedFile(buffer_) == filename)
|
2007-09-05 17:50:07 +00:00
|
|
|
return it;
|
|
|
|
return file_list_.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-03 16:41:06 +00:00
|
|
|
bool EmbeddedFiles::extractAll() const
|
2007-09-08 04:10:43 +00:00
|
|
|
{
|
|
|
|
EmbeddedFileList::const_iterator it = file_list_.begin();
|
|
|
|
EmbeddedFileList::const_iterator it_end = file_list_.end();
|
2008-01-03 16:41:06 +00:00
|
|
|
int count_extracted = 0;
|
|
|
|
int count_external = 0;
|
2007-09-08 04:10:43 +00:00
|
|
|
for (; it != it_end; ++it)
|
2008-01-03 16:41:06 +00:00
|
|
|
if (it->embedded()) {
|
|
|
|
if(!it->extract(buffer_)) {
|
|
|
|
throw ExceptionMessage(ErrorException,
|
|
|
|
_("Failed to extract file"),
|
|
|
|
bformat(_("Error: can not extract file %1$s.\n"), it->displayName()));
|
|
|
|
} else
|
|
|
|
count_extracted += 1;
|
|
|
|
} else
|
|
|
|
count_external += 1;
|
2008-01-05 05:28:39 +00:00
|
|
|
docstring const msg = bformat(_("%1$d external or non-embeddable files are ignored.\n"
|
2008-01-03 16:41:06 +00:00
|
|
|
"%2$d embedded files are extracted.\n"), count_external, count_extracted);
|
|
|
|
Alert::information(_("Unpacking all files"), msg);
|
2007-09-08 04:10:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool EmbeddedFiles::updateFromExternalFile() const
|
2007-09-01 20:08:16 +00:00
|
|
|
{
|
|
|
|
EmbeddedFileList::const_iterator it = file_list_.begin();
|
|
|
|
EmbeddedFileList::const_iterator it_end = file_list_.end();
|
2008-01-03 16:41:06 +00:00
|
|
|
int count_embedded = 0;
|
|
|
|
int count_external = 0;
|
2007-09-01 20:08:16 +00:00
|
|
|
for (; it != it_end; ++it)
|
2008-01-03 16:41:06 +00:00
|
|
|
if (it->embedded()) {
|
|
|
|
if (!it->updateFromExternalFile(buffer_)) {
|
|
|
|
throw ExceptionMessage(ErrorException,
|
|
|
|
_("Failed to embed file"),
|
|
|
|
bformat(_("Error: can not embed file %1$s.\n"), it->displayName()));
|
2007-09-08 04:10:43 +00:00
|
|
|
return false;
|
2008-01-03 16:41:06 +00:00
|
|
|
} else
|
2008-01-05 05:28:39 +00:00
|
|
|
count_embedded += 1;
|
2008-01-03 16:41:06 +00:00
|
|
|
} else
|
|
|
|
count_external += 1;
|
2008-01-05 05:28:39 +00:00
|
|
|
docstring const msg = bformat(_("%1$d external or non-embeddable files are ignored.\n"
|
2008-01-03 16:41:06 +00:00
|
|
|
"%2$d embeddable files are embedded.\n"), count_external, count_embedded);
|
|
|
|
Alert::information(_("Packing all files"), msg);
|
2007-09-01 20:08:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-12 14:27:47 +00:00
|
|
|
void EmbeddedFiles::updateInsets() const
|
|
|
|
{
|
|
|
|
EmbeddedFiles::EmbeddedFileList::const_iterator it = begin();
|
|
|
|
EmbeddedFiles::EmbeddedFileList::const_iterator it_end = end();
|
|
|
|
for (; it != it_end; ++it)
|
2007-10-30 22:21:48 +00:00
|
|
|
if (it->refCount() > 0)
|
2007-09-12 14:27:47 +00:00
|
|
|
it->updateInsets(buffer_);
|
|
|
|
}
|
|
|
|
|
2007-08-30 20:46:42 +00:00
|
|
|
}
|