Embedding: simplify the interface of EmbeddedFiles, which will be later merged to EmbeddedFileList

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22444 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Bo Peng 2008-01-08 18:33:43 +00:00
parent 52cd35cd5f
commit c20f5a64d9
10 changed files with 63 additions and 84 deletions

View File

@ -233,7 +233,7 @@ Buffer::Impl::Impl(Buffer & parent, FileName const & file, bool readonly_)
: parent_buffer(0), lyx_clean(true), bak_clean(true), unnamed(false),
read_only(readonly_), filename(file), file_fully_loaded(false),
inset(params), toc_backend(&parent), macro_lock(false),
embedded_files(&parent), timestamp_(0), checksum_(0), wa_(0),
embedded_files(), timestamp_(0), checksum_(0), wa_(0),
undo_(parent)
{
temppath = createBufferTmpDir();
@ -378,6 +378,12 @@ EmbeddedFiles const & Buffer::embeddedFiles() const
}
bool Buffer::embedded() const
{
return params().embedded;
}
Undo & Buffer::undo()
{
return d->undo_;
@ -915,7 +921,7 @@ bool Buffer::writeFile(FileName const & fname) const
message(str + _(" writing embedded files!."));
// if embedding is enabled, write file.lyx and all the embedded files
// to the zip file fname.
if (!d->embedded_files.writeFile(fname)) {
if (!d->embedded_files.writeFile(fname, *this)) {
message(str + _(" could not write embedded files!."));
return false;
}

View File

@ -407,6 +407,7 @@ public:
//@{
EmbeddedFiles & embeddedFiles();
EmbeddedFiles const & embeddedFiles() const;
bool embedded() const;
//@}
Undo & undo();

View File

@ -1178,7 +1178,7 @@ bool BufferView::dispatch(FuncRequest const & cmd)
case LFUN_BUFFER_TOGGLE_EMBEDDING: {
// turn embedding on/off
try {
buffer_.embeddedFiles().enable(!buffer_.params().embedded);
buffer_.embeddedFiles().enable(!buffer_.params().embedded, buffer_);
} catch (ExceptionMessage const & message) {
Alert::error(message.title_, message.details_);
}

View File

@ -306,39 +306,33 @@ bool operator!=(EmbeddedFile const & lhs, EmbeddedFile const & rhs)
}
bool EmbeddedFiles::enabled() const
void EmbeddedFiles::enable(bool flag, Buffer & buffer)
{
return buffer_->params().embedded;
}
void EmbeddedFiles::enable(bool flag)
{
if (enabled() == flag)
if (buffer.embedded() == flag)
return;
// update embedded file list
update();
update(buffer);
int count_embedded = 0;
int count_external = 0;
EmbeddedFileList::iterator it = file_list_.begin();
EmbeddedFileList::iterator it_end = file_list_.end();
std::vector<EmbeddedFile>::iterator it = begin();
std::vector<EmbeddedFile>::iterator it_end = end();
// an exception may be thrown
for (; it != it_end; ++it) {
it->enable(flag, buffer_);
it->enable(flag, &buffer);
if (it->embedded())
count_embedded ++;
else
count_external ++;
}
// if operation is successful (no exception is thrown)
buffer_->markDirty();
buffer_->params().embedded = flag;
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_);
for (it = begin(); it != it_end; ++it)
it->updateInsets(&buffer);
// show result
if (flag) {
@ -353,14 +347,15 @@ void EmbeddedFiles::enable(bool flag)
}
void EmbeddedFiles::registerFile(EmbeddedFile const & file, Inset const * inset)
void EmbeddedFiles::registerFile(EmbeddedFile const & file,
Inset const * inset, Buffer const & buffer)
{
BOOST_ASSERT(!enabled() || file.availableFile().exists());
BOOST_ASSERT(!enabled() || file.enabled());
BOOST_ASSERT(!buffer.embedded() || file.availableFile().exists());
BOOST_ASSERT(!buffer.embedded() || file.enabled());
// try to find this file from the list
EmbeddedFileList::iterator it = file_list_.begin();
EmbeddedFileList::iterator it_end = file_list_.end();
std::vector<EmbeddedFile>::iterator it = begin();
std::vector<EmbeddedFile>::iterator it_end = end();
for (; it != it_end; ++it)
if (it->absFilename() == file.absFilename()) {
if (it->embedded() != file.embedded()) {
@ -370,39 +365,39 @@ void EmbeddedFiles::registerFile(EmbeddedFile const & file, Inset const * inset)
from_utf8(it->outputFilename())));
it->setEmbed(true);
// update the inset with this embedding status.
const_cast<Inset*>(inset)->updateEmbeddedFile(*buffer_, *it);
const_cast<Inset*>(inset)->updateEmbeddedFile(buffer, *it);
}
it->addInset(inset);
return;
}
//
file_list_.push_back(file);
file_list_.back().addInset(inset);
push_back(file);
back().addInset(inset);
}
void EmbeddedFiles::update()
void EmbeddedFiles::update(Buffer const & buffer)
{
file_list_.clear();
clear();
for (InsetIterator it = inset_iterator_begin(buffer_->inset()); it; ++it)
it->registerEmbeddedFiles(*buffer_, *this);
for (InsetIterator it = inset_iterator_begin(buffer.inset()); it; ++it)
it->registerEmbeddedFiles(buffer, *this);
}
bool EmbeddedFiles::writeFile(DocFileName const & filename)
bool EmbeddedFiles::writeFile(DocFileName const & filename, Buffer const & buffer)
{
// file in the temporary path has the content
string const content = FileName(addName(buffer_->temppath(),
string const content = FileName(addName(buffer.temppath(),
"content.lyx")).toFilesystemEncoding();
vector<pair<string, string> > filenames;
// add content.lyx to filenames
filenames.push_back(make_pair(content, "content.lyx"));
// prepare list of embedded file
update();
EmbeddedFileList::iterator it = file_list_.begin();
EmbeddedFileList::iterator it_end = file_list_.end();
update(buffer);
std::vector<EmbeddedFile>::iterator it = begin();
std::vector<EmbeddedFile>::iterator it_end = end();
for (; it != it_end; ++it) {
if (it->embedded()) {
string file = it->embeddedFile();
@ -417,7 +412,7 @@ bool EmbeddedFiles::writeFile(DocFileName const & filename)
}
// 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(),
DocFileName zipfile(addName(buffer.temppath(),
onlyFilename(changeExtension(
filename.toFilesystemEncoding(), ".zip"))));

View File

@ -167,46 +167,23 @@ bool operator==(EmbeddedFile const & lhs, EmbeddedFile const & rhs);
bool operator!=(EmbeddedFile const & lhs, EmbeddedFile const & rhs);
class EmbeddedFiles {
class EmbeddedFiles : public std::vector<EmbeddedFile> {
public:
typedef std::vector<EmbeddedFile> EmbeddedFileList;
public:
///
EmbeddedFiles(Buffer * buffer = 0) : file_list_(), buffer_(buffer) {}
///
~EmbeddedFiles() {}
/// return buffer params embedded flag
bool enabled() const;
/// set buffer params embedded flag. Files will be updated or extracted
/// if such an operation fails, enable will fail.
void enable(bool flag);
void enable(bool flag, Buffer & buffer);
/// add a file item.
/// add a file item.
/* \param file Embedded file to add
* \param inset Inset pointer
*/
void registerFile(EmbeddedFile const & file, Inset const * inset = 0);
void registerFile(EmbeddedFile const & file, Inset const * inset, Buffer const & buffer);
/// scan the buffer and get a list of EmbeddedFile
void update();
void update(Buffer const & buffer);
/// write a zip file
bool writeFile(support::DocFileName const & filename);
void clear() { file_list_.clear(); }
///
EmbeddedFileList::iterator begin() { return file_list_.begin(); }
EmbeddedFileList::iterator end() { return file_list_.end(); }
EmbeddedFileList::const_iterator begin() const { return file_list_.begin(); }
EmbeddedFileList::const_iterator end() const { return file_list_.end(); }
private:
/// list of embedded files
EmbeddedFileList file_list_;
///
Buffer * buffer_;
bool writeFile(support::DocFileName const & filename, Buffer const & buffer);
};
} // namespace lyx

View File

@ -971,9 +971,9 @@ void GuiDocument::updateEmbeddedFileList()
embeddedFilesModule->filesLW->clear();
// add current embedded files
EmbeddedFiles & files = buffer().embeddedFiles();
files.update();
EmbeddedFiles::EmbeddedFileList::iterator fit = files.begin();
EmbeddedFiles::EmbeddedFileList::iterator fit_end = files.end();
files.update(buffer());
EmbeddedFiles::iterator fit = files.begin();
EmbeddedFiles::iterator fit_end = files.end();
for (; fit != fit_end; ++fit) {
QString label = toqstr(fit->relFilename(buffer().filePath()));
if (fit->refCount() > 1)

View File

@ -341,7 +341,7 @@ EmbeddedFileList const InsetBibtex::getFiles(Buffer const & buffer) const
EmbeddedFile file(changeExtension(tmp, "bib"), buffer.filePath());
// If the file structure is correct, this should not fail.
file.setEmbed(true);
file.enable(buffer.embeddedFiles().enabled(), &buffer);
file.enable(buffer.embedded(), &buffer);
vec.push_back(file);
} else {
// this includes the cases when the embed parameter is empty
@ -351,7 +351,7 @@ EmbeddedFileList const InsetBibtex::getFiles(Buffer const & buffer) const
if (!file.empty()) {
EmbeddedFile efile = EmbeddedFile(file.absFilename(), buffer.filePath());
efile.setEmbed(false);
efile.enable(buffer.embeddedFiles().enabled(), &buffer);
efile.enable(buffer.embedded(), &buffer);
vec.push_back(efile);
}
}
@ -810,7 +810,7 @@ void InsetBibtex::registerEmbeddedFiles(Buffer const & buffer, EmbeddedFiles & f
EmbeddedFileList const dbs = getFiles(buffer);
for (vector<EmbeddedFile>::const_iterator it = dbs.begin();
it != dbs.end(); ++ it)
files.registerFile(*it, this);
files.registerFile(*it, this, buffer);
}

View File

@ -453,7 +453,7 @@ void InsetExternal::doDispatch(Cursor & cur, FuncRequest & cmd)
InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer, p);
if (!p.filename.empty()) {
try {
p.filename.enable(buffer.embeddedFiles().enabled(), &buffer);
p.filename.enable(buffer.embedded(), &buffer);
} catch (ExceptionMessage const & message) {
Alert::error(message.title_, message.details_);
// do not set parameter if an error happens
@ -496,10 +496,10 @@ bool InsetExternal::getStatus(Cursor & cur, FuncRequest const & cmd,
}
void InsetExternal::registerEmbeddedFiles(Buffer const &,
void InsetExternal::registerEmbeddedFiles(Buffer const & buffer,
EmbeddedFiles & files) const
{
files.registerFile(params_.filename, this);
files.registerFile(params_.filename, this, buffer);
}
@ -508,7 +508,7 @@ void InsetExternal::updateEmbeddedFile(Buffer const & buf,
{
// when embedding is enabled, change of embedding status leads to actions
EmbeddedFile temp = file;
temp.enable(buf.embeddedFiles().enabled(), &buf);
temp.enable(buf.embedded(), &buf);
// this will not be set if an exception is thorwn in enable()
params_.filename = temp;
}
@ -707,7 +707,7 @@ void InsetExternal::read(Buffer const & buffer, Lexer & lex)
InsetExternalParams params;
if (params.read(buffer, lex)) {
// exception handling is not needed as long as embedded files are in place.
params.filename.enable(buffer.embeddedFiles().enabled(), & buffer);
params.filename.enable(buffer.embedded(), & buffer);
setParams(params, buffer);
}
}

View File

@ -172,7 +172,7 @@ void InsetGraphics::doDispatch(Cursor & cur, FuncRequest & cmd)
InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer, p);
if (!p.filename.empty()) {
try {
p.filename.enable(buffer.embeddedFiles().enabled(), &buffer);
p.filename.enable(buffer.embedded(), &buffer);
} catch (ExceptionMessage const & message) {
Alert::error(message.title_, message.details_);
// do not set parameter if an error happens
@ -216,10 +216,10 @@ bool InsetGraphics::getStatus(Cursor & cur, FuncRequest const & cmd,
}
void InsetGraphics::registerEmbeddedFiles(Buffer const &,
void InsetGraphics::registerEmbeddedFiles(Buffer const & buffer,
EmbeddedFiles & files) const
{
files.registerFile(params().filename, this);
files.registerFile(params().filename, this, buffer);
}
@ -228,7 +228,7 @@ void InsetGraphics::updateEmbeddedFile(Buffer const & buf,
{
// when embedding is enabled, change of embedding status leads to actions
EmbeddedFile temp = file;
temp.enable(buf.embeddedFiles().enabled(), &buf);
temp.enable(buf.embedded(), &buf);
// this will not be set if an exception is thorwn in enable()
params_.filename = temp;
@ -279,7 +279,7 @@ void InsetGraphics::read(Buffer const & buf, Lexer & lex)
else
LYXERR(Debug::GRAPHICS, "Not a Graphics inset!");
params_.filename.enable(buf.embeddedFiles().enabled(), &buf);
params_.filename.enable(buf.embedded(), &buf);
graphic_->update(params().as_grfxParams());
}

View File

@ -141,7 +141,7 @@ EmbeddedFile const includedFilename(Buffer const & buffer,
EmbeddedFile file(to_utf8(params["filename"]),
onlyPath(parentFilename(buffer)));
file.setEmbed(params["embed"] == _("true") ? true : false);
file.enable(buffer.embeddedFiles().enabled(), &buffer);
file.enable(buffer.embedded(), &buffer);
return file;
}
@ -901,7 +901,7 @@ void InsetInclude::updateLabels(Buffer const & buffer, ParIterator const &)
void InsetInclude::registerEmbeddedFiles(Buffer const & buffer,
EmbeddedFiles & files) const
{
files.registerFile(includedFilename(buffer, params()), this);
files.registerFile(includedFilename(buffer, params()), this, buffer);
}