* de.po: sync with branch.
[lyx.git] / src / ModuleList.cpp
blob85066a899e850d3596a2eabe86aacb97ab306d24
1 // -*- C++ -*-
2 /**
3 * \file ModuleList.cpp
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Richard Heck
9 * Full author contact details are available in file CREDITS.
12 #include <config.h>
14 #include "ModuleList.h"
16 #include "LaTeXFeatures.h"
17 #include "Lexer.h"
19 #include "support/debug.h"
20 #include "support/FileName.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
24 #include <algorithm>
26 using namespace std;
27 using namespace lyx::support;
29 namespace lyx {
32 //global variable: module list
33 ModuleList theModuleList;
36 LyXModule::LyXModule(string const & n, string const & i,
37 string const & d, vector<string> const & p,
38 vector<string> const & r, vector<string> const & e,
39 string const & c):
40 name_(n), id_(i), description_(d), package_list_(p),
41 required_modules_(r), excluded_modules_(e), category_(c),
42 checked_(false)
44 filename_ = id_ + ".module";
48 bool LyXModule::isAvailable() const {
49 #ifdef TEX2LYX
50 return true;
51 #else
52 if (package_list_.empty())
53 return true;
54 if (checked_)
55 return available_;
56 checked_ = true;
57 //check whether all of the required packages are available
58 vector<string>::const_iterator it = package_list_.begin();
59 vector<string>::const_iterator end = package_list_.end();
60 for (; it != end; ++it) {
61 if (!LaTeXFeatures::isAvailable(*it)) {
62 available_ = false;
63 return available_;
66 available_ = true;
67 return available_;
68 #endif
72 bool LyXModule::isCompatible(string const & modname) const
74 // do we exclude it?
75 if (find(excluded_modules_.begin(), excluded_modules_.end(), modname) !=
76 excluded_modules_.end())
77 return false;
79 LyXModule const * const lm = theModuleList[modname];
80 if (!lm)
81 return true;
83 // does it exclude us?
84 vector<string> const excmods = lm->getExcludedModules();
85 if (find(excmods.begin(), excmods.end(), id_) != excmods.end())
86 return false;
88 return true;
92 bool LyXModule::areCompatible(string const & mod1, string const & mod2)
94 LyXModule const * const lm1 = theModuleList[mod1];
95 if (lm1)
96 return lm1->isCompatible(mod2);
97 LyXModule const * const lm2 = theModuleList[mod2];
98 if (lm2)
99 return lm2->isCompatible(mod1);
100 // Can't check it either way.
101 return true;
104 // used when sorting the module list.
105 class ModuleSorter
107 public:
108 int operator()(LyXModule const & lm1, LyXModule const & lm2) const
110 return lm1.getName() < lm2.getName();
115 //Much of this is borrowed from LayoutFileList::read()
116 bool ModuleList::read()
118 FileName const real_file = libFileSearch("", "lyxmodules.lst");
119 LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
121 if (real_file.empty()) {
122 LYXERR0("unable to find modules file `"
123 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
124 << "'.\nNo modules will be available.");
125 return false;
128 Lexer lex;
129 if (!lex.setFile(real_file)) {
130 LYXERR0("lyxlex was not able to set file: "
131 << real_file << ".\nNo modules will be available.");
132 return false;
135 if (!lex.isOK()) {
136 LYXERR0("unable to open modules file `"
137 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
138 << "'\nNo modules will be available.");
139 return false;
142 bool finished = false;
143 // Parse modules files
144 LYXERR(Debug::TCLASS, "Starting parsing of lyxmodules.lst");
145 while (lex.isOK() && !finished) {
146 LYXERR(Debug::TCLASS, "\tline by line");
147 switch (lex.lex()) {
148 case Lexer::LEX_FEOF:
149 finished = true;
150 break;
151 default:
152 string const modname = lex.getString();
153 LYXERR(Debug::TCLASS, "Module name: " << modname);
154 if (!lex.next())
155 break;
156 string const fname = lex.getString();
157 LYXERR(Debug::TCLASS, "Filename: " << fname);
158 if (!lex.next(true))
159 break;
160 string const desc = lex.getString();
161 LYXERR(Debug::TCLASS, "Description: " << desc);
162 //FIXME Add packages
163 if (!lex.next())
164 break;
165 string str = lex.getString();
166 LYXERR(Debug::TCLASS, "Packages: " << str);
167 vector<string> pkgs;
168 while (!str.empty()) {
169 string p;
170 str = split(str, p, ',');
171 pkgs.push_back(p);
173 if (!lex.next())
174 break;
175 str = lex.getString();
176 LYXERR(Debug::TCLASS, "Required: " << str);
177 vector<string> req;
178 while (!str.empty()) {
179 string p;
180 str = split(str, p, '|');
181 req.push_back(p);
183 if (!lex.next())
184 break;
185 str = lex.getString();
186 LYXERR(Debug::TCLASS, "Excluded: " << str);
187 vector<string> exc;
188 while (!str.empty()) {
189 string p;
190 str = split(str, p, '|');
191 exc.push_back(p);
193 if (!lex.next())
194 break;
195 string const catgy = lex.getString();
196 LYXERR(Debug::TCLASS, "Category: " << catgy);
197 // This code is run when we have
198 // modName, fname, desc, pkgs, req, exc, and catgy
199 addLayoutModule(modname, fname, desc, pkgs, req, exc, catgy);
200 } // end switch
201 } //end while
203 LYXERR(Debug::TCLASS, "End of parsing of lyxmodules.lst");
205 if (!theModuleList.empty())
206 sort(theModuleList.begin(), theModuleList.end(), ModuleSorter());
207 return true;
211 void ModuleList::addLayoutModule(string const & modname,
212 string const & filename, string const & description,
213 vector<string> const & pkgs, vector<string> const & req,
214 vector<string> const & exc, string const & catgy)
216 LyXModule lm(modname, filename, description, pkgs, req, exc, catgy);
217 modlist_.push_back(lm);
221 LyXModuleList::const_iterator ModuleList::begin() const
223 return modlist_.begin();
227 LyXModuleList::iterator ModuleList::begin()
229 return modlist_.begin();
233 LyXModuleList::const_iterator ModuleList::end() const
235 return modlist_.end();
239 LyXModuleList::iterator ModuleList::end()
241 return modlist_.end();
245 LyXModule const * ModuleList::operator[](string const & str) const
247 LyXModuleList::const_iterator it = modlist_.begin();
248 for (; it != modlist_.end(); ++it)
249 if (it->getID() == str) {
250 LyXModule const & mod = *it;
251 return &mod;
253 return 0;
256 LyXModule * ModuleList::operator[](string const & str)
258 LyXModuleList::iterator it = modlist_.begin();
259 for (; it != modlist_.end(); ++it)
260 if (it->getID() == str) {
261 LyXModule & mod = *it;
262 return &mod;
264 return 0;
267 } // namespace lyx