move LyXerr QString specialisation to support/qstring_helpers
[lyx.git] / src / ModuleList.cpp
blobe53af9b7e9f98d12dc315eefdcc17571cdbb5641
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 if (packageList.empty())
49 return true;
50 if (checked)
51 return available;
52 checked = true;
53 //check whether all of the required packages are available
54 vector<string>::const_iterator it = packageList.begin();
55 vector<string>::const_iterator end = packageList.end();
56 for (; it != end; ++it) {
57 if (!LaTeXFeatures::isAvailable(*it)) {
58 available = false;
59 return available;
62 available = true;
63 return available;
67 // used when sorting the module list.
68 class ModuleSorter
70 public:
71 int operator()(LyXModule const & lm1, LyXModule const & lm2) const
73 return lm1.getName() < lm2.getName();
78 //Much of this is borrowed from LayoutFileList::read()
79 bool ModuleList::load()
81 FileName const real_file = libFileSearch("", "lyxmodules.lst");
82 LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
84 if (real_file.empty()) {
85 LYXERR0("unable to find modules file `"
86 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
87 << "'.\nNo modules will be available.");
88 return false;
91 Lexer lex;
92 if (!lex.setFile(real_file)) {
93 LYXERR0("lyxlex was not able to set file: "
94 << real_file << ".\nNo modules will be available.");
95 return false;
98 if (!lex.isOK()) {
99 LYXERR0("unable to open modules file `"
100 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
101 << "'\nNo modules will be available.");
102 return false;
105 bool finished = false;
106 // Parse modules files
107 LYXERR(Debug::TCLASS, "Starting parsing of lyxmodules.lst");
108 while (lex.isOK() && !finished) {
109 LYXERR(Debug::TCLASS, "\tline by line");
110 switch (lex.lex()) {
111 case Lexer::LEX_FEOF:
112 finished = true;
113 break;
114 default:
115 string const modName = lex.getString();
116 LYXERR(Debug::TCLASS, "Module name: " << modName);
117 if (!lex.next())
118 break;
119 string const fname = lex.getString();
120 LYXERR(Debug::TCLASS, "Filename: " << fname);
121 if (!lex.next())
122 break;
123 string const desc = lex.getString();
124 LYXERR(Debug::TCLASS, "Description: " << desc);
125 //FIXME Add packages
126 if (!lex.next())
127 break;
128 string str = lex.getString();
129 LYXERR(Debug::TCLASS, "Packages: " << str);
130 vector<string> pkgs;
131 while (!str.empty()) {
132 string p;
133 str = split(str, p, ',');
134 pkgs.push_back(p);
136 if (!lex.next())
137 break;
138 str = lex.getString();
139 LYXERR(Debug::TCLASS, "Required: " << str);
140 vector<string> req;
141 while (!str.empty()) {
142 string p;
143 str = split(str, p, '|');
144 req.push_back(p);
146 if (!lex.next())
147 break;
148 str = lex.getString();
149 LYXERR(Debug::TCLASS, "Excluded: " << str);
150 vector<string> exc;
151 while (!str.empty()) {
152 string p;
153 str = split(str, p, '|');
154 exc.push_back(p);
156 // This code is run when we have
157 // modName, fname, desc, pkgs, req, and exc
158 addLayoutModule(modName, fname, desc, pkgs, req, exc);
159 } // end switch
160 } //end while
162 LYXERR(Debug::TCLASS, "End of parsing of lyxmodules.lst");
164 if (!moduleList.empty())
165 sort(moduleList.begin(), moduleList.end(), ModuleSorter());
166 return true;
170 void ModuleList::addLayoutModule(string const & moduleName,
171 string const & filename, string const & description,
172 vector<string> const & pkgs, vector<string> const & req,
173 vector<string> const & exc)
175 LyXModule lm(moduleName, filename, description, pkgs, req, exc);
176 modlist_.push_back(lm);
180 LyXModuleList::const_iterator ModuleList::begin() const
182 return modlist_.begin();
186 LyXModuleList::iterator ModuleList::begin()
188 return modlist_.begin();
192 LyXModuleList::const_iterator ModuleList::end() const
194 return modlist_.end();
198 LyXModuleList::iterator ModuleList::end()
200 return modlist_.end();
204 LyXModule * ModuleList::getModuleByName(string const & str)
206 LyXModuleList::iterator it = modlist_.begin();
207 for (; it != modlist_.end(); ++it)
208 if (it->getName() == str) {
209 LyXModule & mod = *it;
210 return &mod;
212 return 0;
215 LyXModule * ModuleList::operator[](string const & str)
217 LyXModuleList::iterator it = modlist_.begin();
218 for (; it != modlist_.end(); ++it)
219 if (it->getID() == str) {
220 LyXModule & mod = *it;
221 return &mod;
223 return 0;
226 } // namespace lyx