]> git.lyx.org Git - features.git/blob - src/CiteEnginesList.cpp
Properly support the cite engines in the GUI
[features.git] / src / CiteEnginesList.cpp
1 // -*- C++ -*-
2 /**
3  * \file CiteEnginesList.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Richard Heck
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "CiteEnginesList.h"
16
17 #include "Citation.h"
18 #include "LaTeXFeatures.h"
19 #include "Lexer.h"
20
21 #include "support/debug.h"
22 #include "support/FileName.h"
23 #include "support/gettext.h"
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
26 #include "support/Translator.h"
27
28 #include <algorithm>
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34
35
36 //global variable: cite engine list
37 CiteEnginesList theCiteEnginesList;
38
39
40 LyXCiteEngine::LyXCiteEngine(string const & n, string const & i,
41                              vector<string> const & cet, string const & d,
42                              vector<string> const & p,
43                              vector<string> const & r, vector<string> const & e):
44         name_(n), id_(i), engine_types_(cet), description_(d), package_list_(p),
45         required_engines_(r), excluded_engines_(e),
46         checked_(false), available_(false)
47 {
48         filename_ = id_ + ".citeengine";
49 }
50
51
52 vector<string> LyXCiteEngine::prerequisites() const
53 {
54         if (!checked_)
55                 isAvailable();
56         return prerequisites_;
57 }
58
59
60 bool LyXCiteEngine::isAvailable() const
61 {
62         if (package_list_.empty())
63                 return true;
64         if (checked_)
65                 return available_;
66         checked_ = true;
67         available_ = true;
68         //check whether all of the required packages are available
69         vector<string>::const_iterator it  = package_list_.begin();
70         vector<string>::const_iterator end = package_list_.end();
71         for (; it != end; ++it) {
72                 if (!LaTeXFeatures::isAvailable(*it)) {
73                         available_ = false;
74                         prerequisites_.push_back(*it);
75                 }
76         }
77         return available_;
78 }
79
80
81 bool LyXCiteEngine::hasEngineType(CiteEngineType const & et) const
82 {
83         return std::find(engine_types_.begin(), engine_types_.end(),
84                          theCiteEnginesList.getTypeAsString(et)) != engine_types_.end();
85 }
86
87
88 bool LyXCiteEngine::isCompatible(string const & cename) const
89 {
90         // do we exclude it?
91         if (find(excluded_engines_.begin(), excluded_engines_.end(), cename) !=
92                         excluded_engines_.end())
93                 return false;
94
95         LyXCiteEngine const * const lm = theCiteEnginesList[cename];
96         if (!lm)
97                 return true;
98
99         // does it exclude us?
100         vector<string> const excengs = lm->getExcludedEngines();
101         if (find(excengs.begin(), excengs.end(), id_) != excengs.end())
102                 return false;
103
104         return true;
105 }
106
107
108 bool LyXCiteEngine::areCompatible(string const & eng1, string const & eng2)
109 {
110         LyXCiteEngine const * const lm1 = theCiteEnginesList[eng1];
111         if (lm1)
112                 return lm1->isCompatible(eng2);
113         LyXCiteEngine const * const lm2 = theCiteEnginesList[eng2];
114         if (lm2)
115                 return lm2->isCompatible(eng1);
116         // Can't check it either way.
117         return true;
118 }
119
120
121 // used when sorting the cite engine list.
122 class EngineSorter {
123 public:
124         int operator()(LyXCiteEngine const & ce1, LyXCiteEngine const & ce2) const
125         {
126                 return _(ce1.getName()) < _(ce2.getName());
127         }
128 };
129
130
131 // Local translators
132 namespace {
133
134 typedef Translator<string, CiteEngineType> CiteEngineTypeTranslator;
135
136
137 CiteEngineTypeTranslator const init_citeenginetypetranslator()
138 {
139         CiteEngineTypeTranslator translator("authoryear", ENGINE_TYPE_AUTHORYEAR);
140         translator.addPair("numerical", ENGINE_TYPE_NUMERICAL);
141         translator.addPair("default", ENGINE_TYPE_DEFAULT);
142         return translator;
143 }
144
145
146 CiteEngineTypeTranslator const & citeenginetypetranslator()
147 {
148         static CiteEngineTypeTranslator const translator =
149                 init_citeenginetypetranslator();
150         return translator;
151 }
152
153 } // namespace anon
154
155
156 string CiteEnginesList::getTypeAsString(CiteEngineType const & et) const
157 {
158         return citeenginetypetranslator().find(et);
159 }
160
161
162 CiteEngineType CiteEnginesList::getType(string const & et) const
163 {
164         return citeenginetypetranslator().find(et);
165 }
166
167
168 // Much of this is borrowed from LayoutFileList::read()
169 bool CiteEnginesList::read()
170 {
171         FileName const real_file = libFileSearch("", "lyxciteengines.lst");
172         LYXERR(Debug::TCLASS, "Reading cite engines from `" << real_file << '\'');
173
174         if (real_file.empty()) {
175                 LYXERR0("unable to find cite engines file `citeengines.lst'.\n"
176                         << "No cite engines will be available.");
177                 return false;
178         }
179
180         Lexer lex;
181         if (!lex.setFile(real_file)) {
182                 LYXERR0("lyxlex was not able to set file: "
183                         << real_file << ".\nNo cite engines will be available.");
184                 return false;
185         }
186
187         if (!lex.isOK()) {
188                 LYXERR0("unable to open cite engines file  `"
189                         << to_utf8(makeDisplayPath(real_file.absFileName(), 1000))
190                         << "'\nNo cite engines will be available.");
191                 return false;
192         }
193
194         bool finished = false;
195         // Parse cite engines files
196         LYXERR(Debug::TCLASS, "Starting parsing of lyxciteengines.lst");
197         while (lex.isOK() && !finished) {
198                 LYXERR(Debug::TCLASS, "\tline by line");
199                 switch (lex.lex()) {
200                 case Lexer::LEX_FEOF:
201                         finished = true;
202                         break;
203                 default:
204                         string const cename = lex.getString();
205                         LYXERR(Debug::TCLASS, "Engine name: " << cename);
206                         if (!lex.next())
207                                 break;
208                         string const fname = lex.getString();
209                         LYXERR(Debug::TCLASS, "Filename: " << fname);
210                         if (!lex.next(true))
211                                 break;
212                         string cet = lex.getString();
213                         LYXERR(Debug::TCLASS, "Engine Type: " << cet);
214                         vector<string> cets;
215                         while (!cet.empty()) {
216                                 string p;
217                                 cet = split(cet, p, '|');
218                                 cets.push_back(p);
219                         }
220                         if (!lex.next(true))
221                                 break;
222                         string const desc = lex.getString();
223                         LYXERR(Debug::TCLASS, "Description: " << desc);
224                         //FIXME Add packages
225                         if (!lex.next())
226                                 break;
227                         string str = lex.getString();
228                         LYXERR(Debug::TCLASS, "Packages: " << str);
229                         vector<string> pkgs;
230                         while (!str.empty()) {
231                                 string p;
232                                 str = split(str, p, ',');
233                                 pkgs.push_back(p);
234                         }
235                         if (!lex.next())
236                                 break;
237                         str = lex.getString();
238                         LYXERR(Debug::TCLASS, "Required: " << str);
239                         vector<string> req;
240                         while (!str.empty()) {
241                                 string p;
242                                 str = split(str, p, '|');
243                                 req.push_back(p);
244                         }
245                         if (!lex.next())
246                                 break;
247                         str = lex.getString();
248                         LYXERR(Debug::TCLASS, "Excluded: " << str);
249                         vector<string> exc;
250                         while (!str.empty()) {
251                                 string p;
252                                 str = split(str, p, '|');
253                                 exc.push_back(p);
254                         }
255                         // This code is run when we have
256                         // cename, fname, desc, pkgs, req and exc
257                         addCiteEngine(cename, fname, cets, desc, pkgs, req, exc);
258                 } // end switch
259         } //end while
260
261         LYXERR(Debug::TCLASS, "End of parsing of lyxciteengines.lst");
262
263         if (!theCiteEnginesList.empty())
264                 sort(theCiteEnginesList.begin(), theCiteEnginesList.end(), EngineSorter());
265         return true;
266 }
267
268
269 void CiteEnginesList::addCiteEngine(string const & cename,
270         string const & filename, vector<string> const & cets, string const & description,
271         vector<string> const & pkgs, vector<string> const & req,
272         vector<string> const & exc)
273 {
274         LyXCiteEngine ce(cename, filename, cets, description, pkgs, req, exc);
275         englist_.push_back(ce);
276 }
277
278
279 LyXCiteEnginesList::const_iterator CiteEnginesList::begin() const
280 {
281         return englist_.begin();
282 }
283
284
285 LyXCiteEnginesList::iterator CiteEnginesList::begin()
286 {
287         return englist_.begin();
288 }
289
290
291 LyXCiteEnginesList::const_iterator CiteEnginesList::end() const
292 {
293         return englist_.end();
294 }
295
296
297 LyXCiteEnginesList::iterator CiteEnginesList::end()
298 {
299         return englist_.end();
300 }
301
302
303 LyXCiteEngine const * CiteEnginesList::operator[](string const & str) const
304 {
305         LyXCiteEnginesList::const_iterator it = englist_.begin();
306         for (; it != englist_.end(); ++it)
307                 if (it->getID() == str) {
308                         LyXCiteEngine const & eng = *it;
309                         return &eng;
310                 }
311         return 0;
312 }
313
314
315 LyXCiteEngine * CiteEnginesList::operator[](string const & str)
316 {
317         LyXCiteEnginesList::iterator it = englist_.begin();
318         for (; it != englist_.end(); ++it)
319                 if (it->getID() == str) {
320                         LyXCiteEngine & eng = *it;
321                         return &eng;
322                 }
323         return 0;
324 }
325
326 } // namespace lyx