Embedding: move file validation from InsetBibtex to GuiBibtex

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23710 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Bo Peng 2008-03-13 17:19:36 +00:00
parent dc177e2546
commit 3192307c86
3 changed files with 37 additions and 35 deletions

View File

@ -17,6 +17,7 @@
#include "Buffer.h" #include "Buffer.h"
#include "BufferParams.h" #include "BufferParams.h"
#include "EmbeddedFiles.h"
#include "ui_BibtexAddUi.h" #include "ui_BibtexAddUi.h"
#include "qt_helpers.h" #include "qt_helpers.h"
#include "Validator.h" #include "Validator.h"
@ -24,7 +25,10 @@
#include "ButtonPolicy.h" #include "ButtonPolicy.h"
#include "frontends/alert.h"
#include "support/debug.h" #include "support/debug.h"
#include "support/ExceptionMessage.h"
#include "support/FileFilterList.h" #include "support/FileFilterList.h"
#include "support/filetools.h" // changeExtension #include "support/filetools.h" // changeExtension
#include "support/gettext.h" #include "support/gettext.h"
@ -353,18 +357,34 @@ void GuiBibtex::updateContents()
void GuiBibtex::applyView() void GuiBibtex::applyView()
{ {
QString dbs = databaseLW->item(0)->text(); docstring dbs;
docstring emb = databaseLW->item(0)->checkState() == Qt::Checked ? _("true") : _("false"); docstring emb;
unsigned int maxCount = databaseLW->count(); unsigned int maxCount = databaseLW->count();
for (unsigned int i = 1; i < maxCount; i++) { Buffer & buf = buffer();
dbs += ','; for (unsigned int i = 0; i < maxCount; i++) {
dbs += databaseLW->item(i)->text(); if (i != 0) {
emb += ','; dbs += ',';
emb += databaseLW->item(i)->checkState() == Qt::Checked ? _("true") : _("false"); emb += ',';
}
QString filename = databaseLW->item(i)->text();
dbs += qstring_to_ucs4(filename);
try {
EmbeddedFile file(fromqstr(changeExtension(filename, "bib")),
buf.filePath());
file.setEmbed(databaseLW->item(i)->checkState() == Qt::Checked);
// move file around if needed, an exception may be raised.
file.enable(buf.embedded(), &buf, true);
// if things are OK...,
if (file.embedded())
emb += from_utf8(file.inzipName());
} catch (ExceptionMessage const & message) {
Alert::error(message.title_, message.details_);
// failed to embed
}
} }
params_["bibfiles"] = qstring_to_ucs4(dbs); params_["bibfiles"] = dbs;
params_["embed"] = emb; params_["embed"] = emb;
docstring const bibstyle = qstring_to_ucs4(styleCB->currentText()); docstring const bibstyle = qstring_to_ucs4(styleCB->currentText());

View File

@ -27,8 +27,8 @@
#include "frontends/alert.h" #include "frontends/alert.h"
#include "support/debug.h" #include "support/debug.h"
#include "support/ExceptionMessage.h"
#include "support/docstream.h" #include "support/docstream.h"
#include "support/ExceptionMessage.h"
#include "support/filetools.h" #include "support/filetools.h"
#include "support/gettext.h" #include "support/gettext.h"
#include "support/lstrings.h" #include "support/lstrings.h"
@ -86,17 +86,8 @@ void InsetBibtex::doDispatch(Cursor & cur, FuncRequest & cmd)
break; break;
} }
// //
InsetCommandParams orig = params(); createBibFiles(p["bibfiles"], p["embed"]);
try { updateParam();
// returned "embed" is composed of "true" or "false", which needs to be adjusted
createBibFiles(p["bibfiles"], p["embed"], true, true);
updateParam();
} catch (ExceptionMessage const & message) {
Alert::error(message.title_, message.details_);
// do not set parameter if an error happens
setParams(orig);
break;
}
buffer().updateBibfilesCache(); buffer().updateBibfilesCache();
break; break;
} }
@ -736,7 +727,7 @@ void InsetBibtex::validate(LaTeXFeatures & features) const
void InsetBibtex::createBibFiles(docstring const & bibParam, void InsetBibtex::createBibFiles(docstring const & bibParam,
docstring const & embedParam, bool boolStatus, bool updateFile) const docstring const & embedParam) const
{ {
bibfiles_.clear(); bibfiles_.clear();
@ -755,14 +746,9 @@ void InsetBibtex::createBibFiles(docstring const & bibParam,
while (!tmp.empty()) { while (!tmp.empty()) {
EmbeddedFile file(changeExtension(tmp, "bib"), buffer().filePath()); EmbeddedFile file(changeExtension(tmp, "bib"), buffer().filePath());
if (boolStatus) { file.setInzipName(emb);
BOOST_ASSERT(emb == "true" || emb == "false"); file.setEmbed(!emb.empty());
file.setEmbed(emb == "true"); file.enable(buffer().embedded(), &buffer(), false);
} else {
file.setInzipName(emb);
file.setEmbed(emb != "");
}
file.enable(buffer().embedded(), &buffer(), updateFile);
bibfiles_.push_back(file); bibfiles_.push_back(file);
// Get next file name // Get next file name
bibfiles = split(bibfiles, tmp, ','); bibfiles = split(bibfiles, tmp, ',');
@ -796,7 +782,7 @@ void InsetBibtex::updateParam()
void InsetBibtex::registerEmbeddedFiles(EmbeddedFileList & files) const void InsetBibtex::registerEmbeddedFiles(EmbeddedFileList & files) const
{ {
if (bibfiles_.empty()) if (bibfiles_.empty())
createBibFiles(params()["bibfiles"], params()["embed"], false, false); createBibFiles(params()["bibfiles"], params()["embed"]);
EmbeddedFileList::const_iterator it = bibfiles_.begin(); EmbeddedFileList::const_iterator it = bibfiles_.begin();
EmbeddedFileList::const_iterator it_end = bibfiles_.end(); EmbeddedFileList::const_iterator it_end = bibfiles_.end();

View File

@ -57,12 +57,8 @@ public:
/** /**
\param bibfiles comma separated bib files \param bibfiles comma separated bib files
\param embed comma separated embed status \param embed comma separated embed status
\param boolStatus if true, embed has values true/false, which
needs to be converted to inzip names.
\param updateFile whether or not try to update file.
*/ */
void createBibFiles(docstring const & bibfiles, docstring const & embed, void createBibFiles(docstring const & bibfiles, docstring const & embed) const;
bool boolStatus, bool updateFile) const;
/// update bibfiles and embed from bibfiles_ /// update bibfiles and embed from bibfiles_
void updateParam(); void updateParam();
private: private: