]> git.lyx.org Git - lyx.git/blob - src/CiteEnginesList.cpp
Fix text direction issue for InsetInfo in RTL context
[lyx.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 & cfm,
42                              vector<string> const & dbs,
43                              string const & d, vector<string> const & p):
44         name_(n), id_(i), engine_types_(cet), cite_framework_(cfm), default_biblios_(dbs),
45         description_(d), package_list_(p), checked_(false), available_(false)
46 {
47         filename_ = id_ + ".citeengine";
48 }
49
50
51 vector<string> LyXCiteEngine::prerequisites() const
52 {
53         if (!checked_)
54                 isAvailable();
55         return prerequisites_;
56 }
57
58
59 bool LyXCiteEngine::isAvailable() const
60 {
61         if (package_list_.empty())
62                 return true;
63         if (checked_)
64                 return available_;
65         checked_ = true;
66         available_ = true;
67         //check whether all of the required packages are available
68         vector<string>::const_iterator it  = package_list_.begin();
69         vector<string>::const_iterator end = package_list_.end();
70         for (; it != end; ++it) {
71                 if (!LaTeXFeatures::isAvailable(*it)) {
72                         available_ = false;
73                         prerequisites_.push_back(*it);
74                 }
75         }
76         return available_;
77 }
78
79
80 bool LyXCiteEngine::hasEngineType(CiteEngineType const & et) const
81 {
82         return std::find(engine_types_.begin(), engine_types_.end(),
83                          theCiteEnginesList.getTypeAsString(et)) != engine_types_.end();
84 }
85
86
87 string LyXCiteEngine::getDefaultBiblio(CiteEngineType const & cet) const
88 {
89         string res;
90         string const etp = theCiteEnginesList.getTypeAsString(cet) + ":";
91         //check whether all of the required packages are available
92         for (string const &s: default_biblios_) {
93                 if (prefixIs(s, etp))
94                         res = split(s, ':');
95                 else if (!contains(s, ':') && res.empty())
96                         res = s;
97         }
98         return res;
99 }
100
101
102 bool LyXCiteEngine::isDefaultBiblio(string const & bf) const
103 {
104         string const bfs = ":" + bf;
105         for (string const &s: default_biblios_)
106                 if (suffixIs(s, bfs) || bf == s)
107                         return true;
108
109         return false;
110 }
111
112
113 bool LyXCiteEngine::requires(const string p) const
114 {
115         return find(package_list_.begin(), package_list_.end(), p) != package_list_.end();
116 }
117
118
119 // used when sorting the cite engine list.
120 class EngineSorter {
121 public:
122         int operator()(LyXCiteEngine const & ce1, LyXCiteEngine const & ce2) const
123         {
124                 return _(ce1.getName()) < _(ce2.getName());
125         }
126 };
127
128
129 // Local translators
130 namespace {
131
132 typedef Translator<string, CiteEngineType> CiteEngineTypeTranslator;
133
134
135 CiteEngineTypeTranslator const init_citeenginetypetranslator()
136 {
137         CiteEngineTypeTranslator translator("authoryear", ENGINE_TYPE_AUTHORYEAR);
138         translator.addPair("numerical", ENGINE_TYPE_NUMERICAL);
139         translator.addPair("default", ENGINE_TYPE_DEFAULT);
140         return translator;
141 }
142
143
144 CiteEngineTypeTranslator const & citeenginetypetranslator()
145 {
146         static CiteEngineTypeTranslator const translator =
147                 init_citeenginetypetranslator();
148         return translator;
149 }
150
151 } // namespace
152
153
154 string CiteEnginesList::getTypeAsString(CiteEngineType const & et) const
155 {
156         return citeenginetypetranslator().find(et);
157 }
158
159
160 CiteEngineType CiteEnginesList::getType(string const & et) const
161 {
162         return citeenginetypetranslator().find(et);
163 }
164
165
166 // Much of this is borrowed from LayoutFileList::read()
167 bool CiteEnginesList::read()
168 {
169         FileName const real_file = libFileSearch("", "lyxciteengines.lst");
170         LYXERR(Debug::TCLASS, "Reading cite engines from `" << real_file << '\'');
171
172         if (real_file.empty()) {
173                 LYXERR0("unable to find cite engines file `citeengines.lst'.\n"
174                         << "No cite engines will be available.");
175                 return false;
176         }
177
178         Lexer lex;
179         if (!lex.setFile(real_file)) {
180                 LYXERR0("lyxlex was not able to set file: "
181                         << real_file << ".\nNo cite engines will be available.");
182                 return false;
183         }
184
185         if (!lex.isOK()) {
186                 LYXERR0("unable to open cite engines file  `"
187                         << to_utf8(makeDisplayPath(real_file.absFileName(), 1000))
188                         << "'\nNo cite engines will be available.");
189                 return false;
190         }
191
192         bool finished = false;
193         // Parse cite engines files
194         LYXERR(Debug::TCLASS, "Starting parsing of lyxciteengines.lst");
195         while (lex.isOK() && !finished) {
196                 LYXERR(Debug::TCLASS, "\tline by line");
197                 switch (lex.lex()) {
198                 case Lexer::LEX_FEOF:
199                         finished = true;
200                         break;
201                 default:
202                         string const cename = lex.getString();
203                         LYXERR(Debug::TCLASS, "Engine name: " << cename);
204                         if (!lex.next())
205                                 break;
206                         string const fname = lex.getString();
207                         LYXERR(Debug::TCLASS, "Filename: " << fname);
208                         if (!lex.next(true))
209                                 break;
210                         string cet = lex.getString();
211                         LYXERR(Debug::TCLASS, "Engine Type: " << cet);
212                         vector<string> cets;
213                         while (!cet.empty()) {
214                                 string p;
215                                 cet = split(cet, p, '|');
216                                 cets.push_back(p);
217                         }
218                         if (!lex.next(true))
219                                 break;
220                         string const citeframework = lex.getString();
221                         LYXERR(Debug::TCLASS, "CiteFramework: " << citeframework);
222                         if (!lex.next(true))
223                                 break;
224                         string db = lex.getString();
225                         LYXERR(Debug::TCLASS, "Default Biblio: " << db);
226                         vector<string> dbs;
227                         while (!db.empty()) {
228                                 string p;
229                                 db = split(db, p, '|');
230                                 dbs.push_back(p);
231                         }
232                         if (!lex.next(true))
233                                 break;
234                         string const desc = lex.getString();
235                         LYXERR(Debug::TCLASS, "Description: " << desc);
236                         //FIXME Add packages
237                         if (!lex.next())
238                                 break;
239                         string str = lex.getString();
240                         LYXERR(Debug::TCLASS, "Packages: " << str);
241                         vector<string> pkgs;
242                         while (!str.empty()) {
243                                 string p;
244                                 str = split(str, p, ',');
245                                 pkgs.push_back(p);
246                         }
247                         // This code is run when we have
248                         // cename, fname, cets, citeframework, dbs, desc, pkgs
249                         addCiteEngine(cename, fname, cets, citeframework, dbs, desc, pkgs);
250                 } // end switch
251         } //end while
252
253         LYXERR(Debug::TCLASS, "End of parsing of lyxciteengines.lst");
254
255         if (!theCiteEnginesList.empty())
256                 sort(theCiteEnginesList.begin(), theCiteEnginesList.end(), EngineSorter());
257         return true;
258 }
259
260
261 void CiteEnginesList::addCiteEngine(string const & cename,
262         string const & filename, vector<string> const & cets,
263         string const & citeframework, vector<string> const & dbs,
264         string const & description, vector<string> const & pkgs)
265 {
266         LyXCiteEngine ce(cename, filename, cets, citeframework, dbs, description, pkgs);
267         englist_.push_back(ce);
268 }
269
270
271 LyXCiteEnginesList::const_iterator CiteEnginesList::begin() const
272 {
273         return englist_.begin();
274 }
275
276
277 LyXCiteEnginesList::iterator CiteEnginesList::begin()
278 {
279         return englist_.begin();
280 }
281
282
283 LyXCiteEnginesList::const_iterator CiteEnginesList::end() const
284 {
285         return englist_.end();
286 }
287
288
289 LyXCiteEnginesList::iterator CiteEnginesList::end()
290 {
291         return englist_.end();
292 }
293
294
295 LyXCiteEngine const * CiteEnginesList::operator[](string const & str) const
296 {
297         LyXCiteEnginesList::const_iterator it = englist_.begin();
298         for (; it != englist_.end(); ++it)
299                 if (it->getID() == str) {
300                         LyXCiteEngine const & eng = *it;
301                         return &eng;
302                 }
303         return 0;
304 }
305
306
307 LyXCiteEngine * CiteEnginesList::operator[](string const & str)
308 {
309         LyXCiteEnginesList::iterator it = englist_.begin();
310         for (; it != englist_.end(); ++it)
311                 if (it->getID() == str) {
312                         LyXCiteEngine & eng = *it;
313                         return &eng;
314                 }
315         return 0;
316 }
317
318 } // namespace lyx