* qt_helpers.cpp:
[lyx.git] / src / ModuleList.cpp
blob4127c4d39c27390806b7d0d66ce351f4a1c70531
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 moduleList;
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 name(n), id(i), description(d),
40 packageList(p), requiredModules(r), excludedModules(e),
41 checked(false)
43 filename = id + ".module";
47 bool LyXModule::isAvailable() {
48 #ifdef TEX2LYX
49 return true;
50 #else
51 if (packageList.empty())
52 return true;
53 if (checked)
54 return available;
55 checked = true;
56 //check whether all of the required packages are available
57 vector<string>::const_iterator it = packageList.begin();
58 vector<string>::const_iterator end = packageList.end();
59 for (; it != end; ++it) {
60 if (!LaTeXFeatures::isAvailable(*it)) {
61 available = false;
62 return available;
65 available = true;
66 return available;
67 #endif
71 bool LyXModule::isCompatible(string const & modName) const
73 // do we exclude it?
74 if (find(excludedModules.begin(), excludedModules.end(), modName) !=
75 excludedModules.end())
76 return false;
78 LyXModule const * const lm = moduleList[modName];
79 if (!lm)
80 return true;
82 // does it exclude us?
83 vector<string> const excMods = lm->getExcludedModules();
84 if (find(excMods.begin(), excMods.end(), id) != excMods.end())
85 return false;
87 return true;
91 bool LyXModule::areCompatible(string const & mod1, string const & mod2)
93 LyXModule const * const lm1 = moduleList[mod1];
94 if (lm1)
95 return lm1->isCompatible(mod2);
96 LyXModule const * const lm2 = moduleList[mod2];
97 if (lm2)
98 return lm2->isCompatible(mod1);
99 // Can't check it either way.
100 return true;
103 // used when sorting the module list.
104 class ModuleSorter
106 public:
107 int operator()(LyXModule const & lm1, LyXModule const & lm2) const
109 return lm1.getName() < lm2.getName();
114 //Much of this is borrowed from LayoutFileList::read()
115 bool ModuleList::read()
117 FileName const real_file = libFileSearch("", "lyxmodules.lst");
118 LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
120 if (real_file.empty()) {
121 LYXERR0("unable to find modules file `"
122 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
123 << "'.\nNo modules will be available.");
124 return false;
127 Lexer lex;
128 if (!lex.setFile(real_file)) {
129 LYXERR0("lyxlex was not able to set file: "
130 << real_file << ".\nNo modules will be available.");
131 return false;
134 if (!lex.isOK()) {
135 LYXERR0("unable to open modules file `"
136 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
137 << "'\nNo modules will be available.");
138 return false;
141 bool finished = false;
142 // Parse modules files
143 LYXERR(Debug::TCLASS, "Starting parsing of lyxmodules.lst");
144 while (lex.isOK() && !finished) {
145 LYXERR(Debug::TCLASS, "\tline by line");
146 switch (lex.lex()) {
147 case Lexer::LEX_FEOF:
148 finished = true;
149 break;
150 default:
151 string const modName = lex.getString();
152 LYXERR(Debug::TCLASS, "Module name: " << modName);
153 if (!lex.next())
154 break;
155 string const fname = lex.getString();
156 LYXERR(Debug::TCLASS, "Filename: " << fname);
157 if (!lex.next(true))
158 break;
159 string const desc = lex.getString();
160 LYXERR(Debug::TCLASS, "Description: " << desc);
161 //FIXME Add packages
162 if (!lex.next())
163 break;
164 string str = lex.getString();
165 LYXERR(Debug::TCLASS, "Packages: " << str);
166 vector<string> pkgs;
167 while (!str.empty()) {
168 string p;
169 str = split(str, p, ',');
170 pkgs.push_back(p);
172 if (!lex.next())
173 break;
174 str = lex.getString();
175 LYXERR(Debug::TCLASS, "Required: " << str);
176 vector<string> req;
177 while (!str.empty()) {
178 string p;
179 str = split(str, p, '|');
180 req.push_back(p);
182 if (!lex.next())
183 break;
184 str = lex.getString();
185 LYXERR(Debug::TCLASS, "Excluded: " << str);
186 vector<string> exc;
187 while (!str.empty()) {
188 string p;
189 str = split(str, p, '|');
190 exc.push_back(p);
192 // This code is run when we have
193 // modName, fname, desc, pkgs, req, and exc
194 addLayoutModule(modName, fname, desc, pkgs, req, exc);
195 } // end switch
196 } //end while
198 LYXERR(Debug::TCLASS, "End of parsing of lyxmodules.lst");
200 if (!moduleList.empty())
201 sort(moduleList.begin(), moduleList.end(), ModuleSorter());
202 return true;
206 void ModuleList::addLayoutModule(string const & moduleName,
207 string const & filename, string const & description,
208 vector<string> const & pkgs, vector<string> const & req,
209 vector<string> const & exc)
211 LyXModule lm(moduleName, filename, description, pkgs, req, exc);
212 modlist_.push_back(lm);
216 LyXModuleList::const_iterator ModuleList::begin() const
218 return modlist_.begin();
222 LyXModuleList::iterator ModuleList::begin()
224 return modlist_.begin();
228 LyXModuleList::const_iterator ModuleList::end() const
230 return modlist_.end();
234 LyXModuleList::iterator ModuleList::end()
236 return modlist_.end();
240 LyXModule * ModuleList::operator[](string const & str)
242 LyXModuleList::iterator it = modlist_.begin();
243 for (; it != modlist_.end(); ++it)
244 if (it->getID() == str) {
245 LyXModule & mod = *it;
246 return &mod;
248 return 0;
251 } // namespace lyx