From 54a7f725db703734a2e005876ded957387899389 Mon Sep 17 00:00:00 2001 From: Richard Heck Date: Sat, 29 Mar 2008 15:32:36 +0000 Subject: [PATCH] Improve createBibFiles so it does not recreate the whole list each time. Adapt other things to new findFile() method. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@24039 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/insets/InsetBibtex.cpp | 118 ++++++++++++++++++++++--------------- src/insets/InsetBibtex.h | 11 ++-- 2 files changed, 77 insertions(+), 52 deletions(-) diff --git a/src/insets/InsetBibtex.cpp b/src/insets/InsetBibtex.cpp index 4f6ae2b988..10e5a214e0 100644 --- a/src/insets/InsetBibtex.cpp +++ b/src/insets/InsetBibtex.cpp @@ -106,7 +106,7 @@ void InsetBibtex::doDispatch(Cursor & cur, FuncRequest & cmd) break; } // - createBibFiles(p["bibfiles"], p["embed"]); + createBibFiles(); updateParam(); setParam("options", p["options"]); buffer().updateBibfilesCache(); @@ -705,40 +705,46 @@ void InsetBibtex::fillWithBibKeys(BiblioInfo & keylist, } +FileName InsetBibtex::getBibTeXPath(docstring const & filename, Buffer const & buf) +{ + string texfile = changeExtension(to_utf8(filename), "bib"); + // note that, if the filename can be found directly from the path, + // findtexfile will just return a FileName object for that path. + FileName file(findtexfile(texfile, "bib")); + if (file.empty()) + file = FileName(makeAbsPath(texfile, buf.filePath())); + return file; +} + -bool InsetBibtex::addDatabase(docstring const & database) +bool InsetBibtex::addDatabase(docstring const & db) { - string db = to_utf8(database); - EmbeddedFile file(changeExtension(db, "bib"), buffer().filePath()); - - // only compare filename - EmbeddedFileList::iterator it = bibfiles_.begin(); - EmbeddedFileList::iterator it_end = bibfiles_.end(); - for (; it != it_end; ++it) - if (it->absFilename() == file.absFilename()) - return false; - + FileName const texPath = getBibTeXPath(db, buffer()); + EmbeddedFileList::const_iterator efp = + bibfiles_.findFile(texPath.absFilename()); + if (efp != bibfiles_.end()) + //already have it + return false; + + EmbeddedFile file(texPath.absFilename(), buffer().filePath()); bibfiles_.push_back(file); updateParam(); return true; } -bool InsetBibtex::delDatabase(docstring const & database) +bool InsetBibtex::delDatabase(docstring const & db) { - string db = to_utf8(database); - EmbeddedFile file(changeExtension(db, "bib"), buffer().filePath()); + FileName const texPath = getBibTeXPath(db, buffer()); + EmbeddedFileList::iterator efp = + bibfiles_.findFile(texPath.absFilename()); + if (efp == bibfiles_.end()) + // don't have it + return false; - // only compare filename - EmbeddedFileList::iterator it = bibfiles_.begin(); - EmbeddedFileList::iterator it_end = bibfiles_.end(); - for (; it != it_end; ++it) - if (it->absFilename() == file.absFilename()) { - bibfiles_.erase(it); - updateParam(); - return true; - } - return false; + bibfiles_.erase(efp); + updateParam(); + return true; } @@ -749,33 +755,53 @@ void InsetBibtex::validate(LaTeXFeatures & features) const } -void InsetBibtex::createBibFiles(docstring const & bibParam, - docstring const & embedParam) const +void InsetBibtex::createBibFiles() const { + // need to do this to keep old info while also not + // copying over any files that have been deleted. + EmbeddedFileList oldlist = bibfiles_; bibfiles_.clear(); - string tmp; - string emb; + docstring bibfile; + docstring embfile; - string bibfiles = to_utf8(bibParam); - string embedStatus = to_utf8(embedParam); + docstring bibfiles = getParam("bibfiles"); + docstring embfiles = getParam("embed"); - LYXERR(Debug::FILES, "Create bib files from parameters " - << bibfiles << " and " << embedStatus); - - bibfiles = split(bibfiles, tmp, ','); - embedStatus = split(embedStatus, emb, ','); + bibfiles = split(bibfiles, bibfile, ','); + embfiles = split(embfiles, embfile, ','); - while (!tmp.empty()) { - EmbeddedFile file(changeExtension(tmp, "bib"), buffer().filePath()); - - file.setInzipName(emb); - file.setEmbed(!emb.empty()); - file.enable(buffer().embedded(), &buffer(), false); - bibfiles_.push_back(file); + while (!bibfile.empty()) { + FileName bib = getBibTeXPath(bibfile, buffer()); + EmbeddedFileList::iterator efp = oldlist.findFile(bib.absFilename()); + if (efp != oldlist.end()) { + // already have this one + efp->setInzipName(to_utf8(embfile)); + efp->setEmbed(!embfile.empty()); + try { + efp->enable(buffer().embedded(), &buffer(), false); + } catch (ExceptionMessage const & message) { + Alert::error(message.title_, message.details_); + // failed to embed + efp->setEmbed(false); + } + bibfiles_.push_back(*efp); + } else { + EmbeddedFile file(bib.absFilename(), buffer().filePath()); + file.setInzipName(to_utf8(embfile)); + file.setEmbed(!embfile.empty()); + try { + file.enable(buffer().embedded(), &buffer(), true); + } catch (ExceptionMessage const & message) { + Alert::error(message.title_, message.details_); + // failed to embed + file.setEmbed(false); + } + bibfiles_.push_back(file); + } // Get next file name - bibfiles = split(bibfiles, tmp, ','); - embedStatus = split(embedStatus, emb, ','); + bibfiles = split(bibfiles, bibfile, ','); + embfiles = split(embfiles, embfile, ','); } } @@ -807,7 +833,7 @@ void InsetBibtex::updateParam() void InsetBibtex::registerEmbeddedFiles(EmbeddedFileList & files) const { if (bibfiles_.empty()) - createBibFiles(params()["bibfiles"], params()["embed"]); + createBibFiles(); EmbeddedFileList::const_iterator it = bibfiles_.begin(); EmbeddedFileList::const_iterator it_end = bibfiles_.end(); diff --git a/src/insets/InsetBibtex.h b/src/insets/InsetBibtex.h index 3711c39098..9fca7d25c0 100644 --- a/src/insets/InsetBibtex.h +++ b/src/insets/InsetBibtex.h @@ -56,15 +56,14 @@ public: /// static bool isCompatibleCommand(std::string const & s) { return s == "bibtex"; } - /// create bibfiles_ from params bibfiles and embed - /** - \param bibfiles comma separated bib files - \param embed comma separated embed status - */ - void createBibFiles(docstring const & bibfiles, docstring const & embed) const; /// update bibfiles and embed from bibfiles_ void updateParam(); + /// look up the path to the file using TeX + static support::FileName + getBibTeXPath(docstring const & filename, Buffer const & buf); private: + /// + void createBibFiles() const; /// void registerEmbeddedFiles(EmbeddedFileList &) const; /// -- 2.39.2