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

View File

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

View File

@ -1178,7 +1178,7 @@ bool BufferView::dispatch(FuncRequest const & cmd)
case LFUN_BUFFER_TOGGLE_EMBEDDING: { case LFUN_BUFFER_TOGGLE_EMBEDDING: {
// turn embedding on/off // turn embedding on/off
try { try {
buffer_.embeddedFiles().enable(!buffer_.params().embedded); buffer_.embeddedFiles().enable(!buffer_.params().embedded, buffer_);
} catch (ExceptionMessage const & message) { } catch (ExceptionMessage const & message) {
Alert::error(message.title_, message.details_); 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; if (buffer.embedded() == flag)
}
void EmbeddedFiles::enable(bool flag)
{
if (enabled() == flag)
return; return;
// update embedded file list // update embedded file list
update(); update(buffer);
int count_embedded = 0; int count_embedded = 0;
int count_external = 0; int count_external = 0;
EmbeddedFileList::iterator it = file_list_.begin(); std::vector<EmbeddedFile>::iterator it = begin();
EmbeddedFileList::iterator it_end = file_list_.end(); std::vector<EmbeddedFile>::iterator it_end = end();
// an exception may be thrown // an exception may be thrown
for (; it != it_end; ++it) { for (; it != it_end; ++it) {
it->enable(flag, buffer_); it->enable(flag, &buffer);
if (it->embedded()) if (it->embedded())
count_embedded ++; count_embedded ++;
else else
count_external ++; count_external ++;
} }
// if operation is successful (no exception is thrown) // if operation is successful (no exception is thrown)
buffer_->markDirty(); buffer.markDirty();
buffer_->params().embedded = flag; buffer.params().embedded = flag;
// if the operation is successful, update insets // if the operation is successful, update insets
for (it = file_list_.begin(); it != it_end; ++it) for (it = begin(); it != it_end; ++it)
it->updateInsets(buffer_); it->updateInsets(&buffer);
// show result // show result
if (flag) { 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(!buffer.embedded() || file.availableFile().exists());
BOOST_ASSERT(!enabled() || file.enabled()); BOOST_ASSERT(!buffer.embedded() || file.enabled());
// try to find this file from the list // try to find this file from the list
EmbeddedFileList::iterator it = file_list_.begin(); std::vector<EmbeddedFile>::iterator it = begin();
EmbeddedFileList::iterator it_end = file_list_.end(); std::vector<EmbeddedFile>::iterator it_end = end();
for (; it != it_end; ++it) for (; it != it_end; ++it)
if (it->absFilename() == file.absFilename()) { if (it->absFilename() == file.absFilename()) {
if (it->embedded() != file.embedded()) { if (it->embedded() != file.embedded()) {
@ -370,39 +365,39 @@ void EmbeddedFiles::registerFile(EmbeddedFile const & file, Inset const * inset)
from_utf8(it->outputFilename()))); from_utf8(it->outputFilename())));
it->setEmbed(true); it->setEmbed(true);
// update the inset with this embedding status. // update the inset with this embedding status.
const_cast<Inset*>(inset)->updateEmbeddedFile(*buffer_, *it); const_cast<Inset*>(inset)->updateEmbeddedFile(buffer, *it);
} }
it->addInset(inset); it->addInset(inset);
return; return;
} }
// //
file_list_.push_back(file); push_back(file);
file_list_.back().addInset(inset); 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) for (InsetIterator it = inset_iterator_begin(buffer.inset()); it; ++it)
it->registerEmbeddedFiles(*buffer_, *this); 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 // 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(); "content.lyx")).toFilesystemEncoding();
vector<pair<string, string> > filenames; vector<pair<string, string> > filenames;
// add content.lyx to filenames // add content.lyx to filenames
filenames.push_back(make_pair(content, "content.lyx")); filenames.push_back(make_pair(content, "content.lyx"));
// prepare list of embedded file // prepare list of embedded file
update(); update(buffer);
EmbeddedFileList::iterator it = file_list_.begin(); std::vector<EmbeddedFile>::iterator it = begin();
EmbeddedFileList::iterator it_end = file_list_.end(); std::vector<EmbeddedFile>::iterator it_end = end();
for (; it != it_end; ++it) { for (; it != it_end; ++it) {
if (it->embedded()) { if (it->embedded()) {
string file = it->embeddedFile(); 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 // 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. // avoid messing up the original file in case something goes terribly wrong.
DocFileName zipfile(addName(buffer_->temppath(), DocFileName zipfile(addName(buffer.temppath(),
onlyFilename(changeExtension( onlyFilename(changeExtension(
filename.toFilesystemEncoding(), ".zip")))); 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); bool operator!=(EmbeddedFile const & lhs, EmbeddedFile const & rhs);
class EmbeddedFiles { class EmbeddedFiles : public std::vector<EmbeddedFile> {
public: 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 /// set buffer params embedded flag. Files will be updated or extracted
/// if such an operation fails, enable will fail. /// 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 file Embedded file to add
* \param inset Inset pointer * \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 /// scan the buffer and get a list of EmbeddedFile
void update(); void update(Buffer const & buffer);
/// write a zip file /// write a zip file
bool writeFile(support::DocFileName const & filename); bool writeFile(support::DocFileName const & filename, Buffer const & buffer);
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_;
}; };
} // namespace lyx } // namespace lyx

View File

@ -971,9 +971,9 @@ void GuiDocument::updateEmbeddedFileList()
embeddedFilesModule->filesLW->clear(); embeddedFilesModule->filesLW->clear();
// add current embedded files // add current embedded files
EmbeddedFiles & files = buffer().embeddedFiles(); EmbeddedFiles & files = buffer().embeddedFiles();
files.update(); files.update(buffer());
EmbeddedFiles::EmbeddedFileList::iterator fit = files.begin(); EmbeddedFiles::iterator fit = files.begin();
EmbeddedFiles::EmbeddedFileList::iterator fit_end = files.end(); EmbeddedFiles::iterator fit_end = files.end();
for (; fit != fit_end; ++fit) { for (; fit != fit_end; ++fit) {
QString label = toqstr(fit->relFilename(buffer().filePath())); QString label = toqstr(fit->relFilename(buffer().filePath()));
if (fit->refCount() > 1) 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()); EmbeddedFile file(changeExtension(tmp, "bib"), buffer.filePath());
// If the file structure is correct, this should not fail. // If the file structure is correct, this should not fail.
file.setEmbed(true); file.setEmbed(true);
file.enable(buffer.embeddedFiles().enabled(), &buffer); file.enable(buffer.embedded(), &buffer);
vec.push_back(file); vec.push_back(file);
} else { } else {
// this includes the cases when the embed parameter is empty // 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()) { if (!file.empty()) {
EmbeddedFile efile = EmbeddedFile(file.absFilename(), buffer.filePath()); EmbeddedFile efile = EmbeddedFile(file.absFilename(), buffer.filePath());
efile.setEmbed(false); efile.setEmbed(false);
efile.enable(buffer.embeddedFiles().enabled(), &buffer); efile.enable(buffer.embedded(), &buffer);
vec.push_back(efile); vec.push_back(efile);
} }
} }
@ -810,7 +810,7 @@ void InsetBibtex::registerEmbeddedFiles(Buffer const & buffer, EmbeddedFiles & f
EmbeddedFileList const dbs = getFiles(buffer); EmbeddedFileList const dbs = getFiles(buffer);
for (vector<EmbeddedFile>::const_iterator it = dbs.begin(); for (vector<EmbeddedFile>::const_iterator it = dbs.begin();
it != dbs.end(); ++ it) 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); InsetExternalMailer::string2params(to_utf8(cmd.argument()), buffer, p);
if (!p.filename.empty()) { if (!p.filename.empty()) {
try { try {
p.filename.enable(buffer.embeddedFiles().enabled(), &buffer); p.filename.enable(buffer.embedded(), &buffer);
} catch (ExceptionMessage const & message) { } catch (ExceptionMessage const & message) {
Alert::error(message.title_, message.details_); Alert::error(message.title_, message.details_);
// do not set parameter if an error happens // 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 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 // when embedding is enabled, change of embedding status leads to actions
EmbeddedFile temp = file; 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() // this will not be set if an exception is thorwn in enable()
params_.filename = temp; params_.filename = temp;
} }
@ -707,7 +707,7 @@ void InsetExternal::read(Buffer const & buffer, Lexer & lex)
InsetExternalParams params; InsetExternalParams params;
if (params.read(buffer, lex)) { if (params.read(buffer, lex)) {
// exception handling is not needed as long as embedded files are in place. // 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); setParams(params, buffer);
} }
} }

View File

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

View File

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