]> git.lyx.org Git - features.git/blobdiff - src/insets/InsetBibtex.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[features.git] / src / insets / InsetBibtex.cpp
index 02fcde287595b98d1c7fe102fa4840e3d91d834b..5149087620024a4302bcba3d854d9e0cc6ec541f 100644 (file)
@@ -16,7 +16,6 @@
 #include "Buffer.h"
 #include "BufferParams.h"
 #include "DispatchResult.h"
-#include "EmbeddedFiles.h"
 #include "Encoding.h"
 #include "FuncRequest.h"
 #include "LaTeXFeatures.h"
@@ -48,37 +47,16 @@ namespace os = support::os;
 
 
 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
-       : InsetCommand(p, "bibtex"), bibfiles_()
+       : InsetCommand(p, "bibtex")
 {}
 
 
-void InsetBibtex::setBuffer(Buffer & buffer)
-{
-       // FIXME We ought to have a buffer.
-       if (buffer_) {
-               EmbeddedFileList::iterator it = bibfiles_.begin();
-               EmbeddedFileList::iterator it_end = bibfiles_.end();
-               for (; it != it_end; ++it) {
-                       try {
-                               *it = it->copyTo(&buffer);
-                       } catch (ExceptionMessage const & message) {
-                               Alert::error(message.title_, message.details_);
-                               // failed to embed
-                               it->setEmbed(false);
-                       }               
-               }
-       }
-       InsetCommand::setBuffer(buffer);
-}
-
-
 ParamInfo const & InsetBibtex::findInfo(string const & /* cmdName */)
 {
        static ParamInfo param_info_;
        if (param_info_.empty()) {
                param_info_.add("btprint", ParamInfo::LATEX_OPTIONAL);
                param_info_.add("bibfiles", ParamInfo::LATEX_REQUIRED);
-               param_info_.add("embed", ParamInfo::LYX_INTERNAL);
                param_info_.add("options", ParamInfo::LYX_INTERNAL);
        }
        return param_info_;
@@ -106,9 +84,7 @@ void InsetBibtex::doDispatch(Cursor & cur, FuncRequest & cmd)
                        break;
                }
                //
-               createBibFiles(p["bibfiles"], p["embed"]);
-               updateParam();
-               setParam("options", p["options"]);
+               setParams(p);
                buffer().updateBibfilesCache();
                break;
        }
@@ -163,11 +139,14 @@ int InsetBibtex::latex(odocstream & os, OutputParams const & runparams) const
        // use such filenames.)
        // Otherwise, store the (maybe absolute) path to the original,
        // unmangled database name.
-       EmbeddedFileList::const_iterator it = bibfiles_.begin();
-       EmbeddedFileList::const_iterator it_end = bibfiles_.end();
+       vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
+       vector<docstring>::const_iterator it = bibfilelist.begin();
+       vector<docstring>::const_iterator en = bibfilelist.end();
        odocstringstream dbs;
-       for (; it != it_end; ++it) {
-               string utf8input = removeExtension(it->availableFile().absFilename());
+       bool didone = false;
+
+       for (; it != en; ++it) {
+               string utf8input = to_utf8(*it);
                string database =
                        normalizeName(buffer(), runparams, utf8input, ".bib");
                FileName const try_in_file =
@@ -197,8 +176,10 @@ int InsetBibtex::latex(odocstream & os, OutputParams const & runparams) const
                                                            from_utf8(database));
                }
 
-               if (it != bibfiles_.begin())
+               if (didone)
                        dbs << ',';
+               else 
+                       didone =- true;
                // FIXME UNICODE
                dbs << from_utf8(latex_path(database));
        }
@@ -306,9 +287,27 @@ int InsetBibtex::latex(odocstream & os, OutputParams const & runparams) const
 }
 
 
-EmbeddedFileList const & InsetBibtex::getBibFiles() const
+support::FileNameList InsetBibtex::getBibFiles() const
 {
-       return bibfiles_;
+       FileName path(buffer().filePath());
+       support::PathChanger p(path);
+       
+       support::FileNameList vec;
+       
+       vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
+       vector<docstring>::const_iterator it = bibfilelist.begin();
+       vector<docstring>::const_iterator en = bibfilelist.end();
+       for (; it != en; ++it) {
+               FileName const file = 
+                       findtexfile(changeExtension(to_utf8(*it), "bib"), "bib");
+               
+               // If we didn't find a matching file name just fail silently
+               if (!file.empty())
+                       vec.push_back(file);
+       }
+       
+       return vec;
+
 }
 
 namespace {
@@ -571,11 +570,11 @@ void InsetBibtex::fillWithBibKeys(BiblioInfo & keylist,
        // We don't restrict keys to ASCII in LyX, since our own
        // InsetBibitem can generate non-ASCII keys, and nonstandard
        // 8bit clean bibtex forks exist.
-       EmbeddedFileList const & files = getBibFiles();
-       EmbeddedFileList::const_iterator it = files.begin();
-       EmbeddedFileList::const_iterator en = files.end();
+       support::FileNameList const & files = getBibFiles();
+       support::FileNameList::const_iterator it = files.begin();
+       support::FileNameList::const_iterator en = files.end();
        for (; it != en; ++ it) {
-               idocfstream ifs(it->availableFile().toFilesystemEncoding().c_str(),
+               idocfstream ifs(it->toFilesystemEncoding().c_str(),
                        ios_base::in, buffer().params().encoding().iconvName());
 
                char_type ch;
@@ -705,124 +704,55 @@ void InsetBibtex::fillWithBibKeys(BiblioInfo & keylist,
 }
 
 
-
-bool InsetBibtex::addDatabase(string const & db)
-{
-       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;
-       
-       bibfiles_.push_back(file);
-       updateParam();
-       return true;
-}
-
-
-bool InsetBibtex::delDatabase(string const & db)
-{
-       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()) {
-                       bibfiles_.erase(it);
-                       updateParam();
-                       return true;
-               }
-       return false;
-}
-
-
-void InsetBibtex::validate(LaTeXFeatures & features) const
+FileName InsetBibtex::getBibTeXPath(docstring const & filename, Buffer const & buf)
 {
-       if (features.bufferParams().use_bibtopic)
-               features.require("bibtopic");
+       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;
 }
 
-
-void InsetBibtex::createBibFiles(docstring const & bibParam,
-       docstring const & embedParam) const
+bool InsetBibtex::addDatabase(docstring const & db)
 {
-       bibfiles_.clear();
-       
-       string tmp;
-       string emb;
-       
-       string bibfiles = to_utf8(bibParam);
-       string embedStatus = to_utf8(embedParam);
-       
-       LYXERR(Debug::FILES, "Create bib files from parameters "
-               << bibfiles << " and " << embedStatus);
-
-       bibfiles = split(bibfiles, tmp, ',');
-       embedStatus = split(embedStatus, emb, ',');
-       
-       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);
-               // Get next file name
-               bibfiles = split(bibfiles, tmp, ',');
-               embedStatus = split(embedStatus, emb, ',');
+       docstring bibfiles = getParam("bibfiles");
+       if (tokenPos(bibfiles, ',', db) == -1) {
+               if (!bibfiles.empty())
+               bibfiles += ',';
+               setParam("bibfiles", bibfiles + db);
+               return true;
        }
+       return false;
 }
 
 
-void InsetBibtex::updateParam()
+bool InsetBibtex::delDatabase(docstring const & db)
 {
-       docstring bibfiles;
-       docstring embed;
-
-       bool first = true;
-
-       EmbeddedFileList::iterator it = bibfiles_.begin();
-       EmbeddedFileList::iterator en = bibfiles_.end();
-       for (; it != en; ++it) {
-               if (!first) {
-                       bibfiles += ',';
-                       embed += ',';
-               } else
-                       first = false;
-               bibfiles += from_utf8(it->outputFilename(buffer().filePath()));
-               if (it->embedded())
-                       embed += from_utf8(it->inzipName());
+       docstring bibfiles = getParam("bibfiles");
+       if (contains(bibfiles, db)) {
+               int const n = tokenPos(bibfiles, ',', db);
+               docstring bd = db;
+               if (n > 0) {
+                       // this is not the first database
+                       docstring tmp = ',' + bd;
+                       setParam("bibfiles", subst(bibfiles, tmp, docstring()));
+               } else if (n == 0)
+                       // this is the first (or only) database
+                       setParam("bibfiles", split(bibfiles, bd, ','));
+               else
+                       return false;
        }
-       setParam("bibfiles", bibfiles);
-       setParam("embed", embed);
-}
-
-
-void InsetBibtex::registerEmbeddedFiles(EmbeddedFileList & files) const
-{
-       if (bibfiles_.empty())
-               createBibFiles(params()["bibfiles"], params()["embed"]);
-
-       EmbeddedFileList::const_iterator it = bibfiles_.begin();
-       EmbeddedFileList::const_iterator it_end = bibfiles_.end();
-       for (; it != it_end; ++it)
-               files.registerFile(*it, this, buffer());
+       return true;
 }
 
 
-void InsetBibtex::updateEmbeddedFile(EmbeddedFile const & file)
+void InsetBibtex::validate(LaTeXFeatures & features) const
 {
-       // look for the item and update status
-       for (EmbeddedFileList::iterator it = bibfiles_.begin();
-               it != bibfiles_.end(); ++it)
-               if (it->absFilename() == file.absFilename())
-                       *it = file;
-       
-       updateParam();
+       if (features.bufferParams().use_bibtopic)
+               features.require("bibtopic");
 }