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
|
|
|
|
|
2008-01-07 03:34:21 +00:00
|
|
|
$temp/$embDirName/file.lyx
|
|
|
|
$temp/$embDirName/figure1.png for ./figure1.png)
|
|
|
|
$temp/$embDirName/sub/figure2.png for ./sub/figure2.png)
|
2008-01-05 05:28:39 +00:00
|
|
|
|
|
|
|
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:
|
|
|
|
|
2008-01-07 03:34:21 +00:00
|
|
|
$temp/$embDirName/$upDirName/figures/figure.png for ../figures/figure.png
|
|
|
|
$temp/$embDirName/$upDirName/$upDirName/figure.png for ../../figure.png
|
2008-01-05 05:28:39 +00:00
|
|
|
|
|
|
|
This name has to be fixed because it is used in lyx bundled .zip file.
|
|
|
|
|
2008-01-07 03:34:21 +00:00
|
|
|
Using a similar trick, we use $absDirName for absolute path so that
|
|
|
|
an absolute filename can be saved as
|
|
|
|
|
|
|
|
$temp/$embDirName/$absDirName/a/absolute/path for /a/absolute/path
|
|
|
|
|
2008-01-05 05:28:39 +00:00
|
|
|
*/
|
2008-01-07 03:34:21 +00:00
|
|
|
const std::string embDirName = "LyX.Embedded.Files";
|
2008-01-05 05:28:39 +00:00
|
|
|
const std::string upDirName = "LyX.Embed.Dir.Up";
|
2008-01-07 03:34:21 +00:00
|
|
|
const std::string absDirName = "LyX.Embed.Dir.Abs";
|
2008-01-05 05:28:39 +00:00
|
|
|
|
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)
|
2008-01-07 03:34:21 +00:00
|
|
|
: DocFileName("", false), inzip_name_(""), embedded_(false), inset_list_(),
|
|
|
|
temp_path_("")
|
2008-01-05 05:28:39 +00:00
|
|
|
{
|
|
|
|
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)));
|
2008-01-07 03:34:21 +00:00
|
|
|
|
2008-01-05 05:28:39 +00:00
|
|
|
if (FileName(inzip_name_).isAbsolute())
|
2008-01-07 03:34:21 +00:00
|
|
|
inzip_name_ = absDirName + '/' + inzip_name_;
|
|
|
|
|
2008-01-05 05:28:39 +00:00
|
|
|
// replace .. by upDirName
|
|
|
|
if (prefixIs(inzip_name_, "."))
|
|
|
|
inzip_name_ = subst(inzip_name_, "..", upDirName);
|
2008-01-07 03:34:21 +00:00
|
|
|
|
|
|
|
// to avoid name conflict between $docu_path/file and $temp_path/file
|
|
|
|
// embedded files are in a subdirectory of $temp_path.
|
|
|
|
inzip_name_ = embDirName + '/' + inzip_name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string EmbeddedFile::embeddedFile() const
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(enabled());
|
|
|
|
return temp_path_ + inzip_name_;
|
2007-09-08 04:10:43 +00:00
|
|
|
}
|
2007-08-30 20:46:42 +00:00
|
|
|
|
|
|
|
|
2008-01-07 03:34:21 +00:00
|
|
|
FileName EmbeddedFile::availableFile() const
|
2007-08-30 20:46:42 +00:00
|
|
|
{
|
2008-01-07 03:34:21 +00:00
|
|
|
if (enabled() && embedded())
|
|
|
|
return FileName(embeddedFile());
|
|
|
|
else
|
|
|
|
return *this;
|
2008-01-05 05:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-07 03:34:21 +00:00
|
|
|
string EmbeddedFile::latexFilename(std::string const & buffer_path) const
|
2008-01-05 05:28:39 +00:00
|
|
|
{
|
2008-01-07 03:34:21 +00:00
|
|
|
return (enabled() && embedded()) ? inzip_name_ : relFilename(buffer_path);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-07 03:34:21 +00:00
|
|
|
void EmbeddedFile::setEmbed(bool embed)
|
2007-08-30 20:46:42 +00:00
|
|
|
{
|
2008-01-07 03:34:21 +00:00
|
|
|
embedded_ = embed;
|
2007-09-09 07:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-07 03:34:21 +00:00
|
|
|
void EmbeddedFile::enable(bool flag, Buffer const * buf)
|
2007-09-01 20:08:16 +00:00
|
|
|
{
|
2008-01-07 03:34:21 +00:00
|
|
|
if (enabled() == flag)
|
2008-01-05 05:28:39 +00:00
|
|
|
return;
|
2008-01-07 03:34:21 +00:00
|
|
|
|
|
|
|
if (flag) {
|
|
|
|
temp_path_ = buf->temppath();
|
|
|
|
if (!suffixIs(temp_path_, '/'))
|
|
|
|
temp_path_ += '/';
|
|
|
|
if (embedded())
|
|
|
|
updateFromExternalFile();
|
|
|
|
} else {
|
|
|
|
extract();
|
|
|
|
temp_path_ = "";
|
2008-01-05 05:28:39 +00:00
|
|
|
}
|
2007-09-08 04:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-07 03:34:21 +00:00
|
|
|
bool EmbeddedFile::extract() const
|
2007-09-01 20:08:16 +00:00
|
|
|
{
|
2008-01-07 03:34:21 +00:00
|
|
|
BOOST_ASSERT(enabled());
|
2008-01-05 05:28:39 +00:00
|
|
|
|
2007-09-01 20:08:16 +00:00
|
|
|
string ext_file = absFilename();
|
2008-01-07 03:34:21 +00:00
|
|
|
string emb_file = embeddedFile();
|
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-07 03:34:21 +00:00
|
|
|
if (!emb.exists()) {
|
|
|
|
if (ext.exists())
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
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
|
|
|
|
2008-01-07 03:34:21 +00:00
|
|
|
bool EmbeddedFile::updateFromExternalFile() const
|
2007-09-07 03:02:24 +00:00
|
|
|
{
|
2008-01-07 03:34:21 +00:00
|
|
|
BOOST_ASSERT(enabled());
|
2008-01-05 05:28:39 +00:00
|
|
|
|
2007-09-07 03:02:24 +00:00
|
|
|
string ext_file = absFilename();
|
2008-01-07 03:34:21 +00:00
|
|
|
string emb_file = embeddedFile();
|
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-07 03:34:21 +00:00
|
|
|
bool EmbeddedFile::isReadableFile() const
|
|
|
|
{
|
|
|
|
return availableFile().isReadableFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2008-01-07 03:34:21 +00:00
|
|
|
if (enabled() == flag)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// update embedded file list
|
|
|
|
update();
|
|
|
|
|
|
|
|
int count_embedded = 0;
|
|
|
|
int count_external = 0;
|
|
|
|
EmbeddedFileList::iterator it = file_list_.begin();
|
|
|
|
EmbeddedFileList::iterator it_end = file_list_.end();
|
|
|
|
// an exception may be thrown
|
|
|
|
for (; it != it_end; ++it) {
|
|
|
|
it->enable(flag, buffer_);
|
|
|
|
if (it->embedded())
|
|
|
|
count_embedded ++;
|
2008-01-03 16:41:06 +00:00
|
|
|
else
|
2008-01-07 03:34:21 +00:00
|
|
|
count_external ++;
|
|
|
|
}
|
|
|
|
// if operation is successful (no exception is thrown)
|
|
|
|
buffer_->markDirty();
|
|
|
|
buffer_->params().embedded = flag;
|
|
|
|
|
|
|
|
// if the operation is successful, update insets
|
|
|
|
for (it = file_list_.begin(); it != it_end; ++it)
|
|
|
|
it->updateInsets(buffer_);
|
|
|
|
|
|
|
|
// show result
|
|
|
|
if (flag) {
|
|
|
|
docstring const msg = bformat(_("%1$d external files are ignored.\n"
|
|
|
|
"%2$d embeddable files are embedded.\n"), count_external, count_embedded);
|
|
|
|
Alert::information(_("Packing all files"), msg);
|
|
|
|
} else {
|
|
|
|
docstring const msg = bformat(_("%1$d external files are ignored.\n"
|
|
|
|
"%2$d embedded files are extracted.\n"), count_external, count_embedded);
|
|
|
|
Alert::information(_("Unpacking all files"), msg);
|
2007-08-30 20:46:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-07 03:34:21 +00:00
|
|
|
void EmbeddedFiles::registerFile(EmbeddedFile const & file, Inset const * inset)
|
2007-08-30 20:46:42 +00:00
|
|
|
{
|
2008-01-07 03:34:21 +00:00
|
|
|
BOOST_ASSERT(!enabled() || file.availableFile().exists());
|
|
|
|
BOOST_ASSERT(!enabled() || file.enabled());
|
|
|
|
|
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);
|
2008-01-07 03:34:21 +00:00
|
|
|
// update the inset with this embedding status.
|
|
|
|
const_cast<Inset*>(inset)->updateEmbeddedFile(*buffer_, *it);
|
2008-01-05 05:28:39 +00:00
|
|
|
}
|
|
|
|
it->addInset(inset);
|
2008-01-07 03:34:21 +00:00
|
|
|
return;
|
2008-01-05 05:28:39 +00:00
|
|
|
}
|
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-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
|
2008-01-07 03:34:21 +00:00
|
|
|
update();
|
2007-08-30 20:46:42 +00:00
|
|
|
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-07 03:34:21 +00:00
|
|
|
string file = it->embeddedFile();
|
2008-01-05 05:28:39 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|