/
GuiManager.cpp
226 lines (177 loc) · 4.23 KB
/
GuiManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "GuiManager.h"
#include "iarchive.h"
#include "ifilesystem.h"
#include "itextstream.h"
#include "parser/CodeTokeniser.h"
#include "Gui.h"
namespace gui
{
GuiManager::GuiManager() :
_guiLoader(GUI_DIR, GUI_EXT, std::bind(&GuiManager::findGuis, this))
{}
void GuiManager::registerGui(const std::string& guiPath)
{
// Just store the path in the map, for later reference
_guis.insert(GuiInfoMap::value_type(GUI_DIR + guiPath, GuiInfo()));
}
std::size_t GuiManager::getNumGuis()
{
ensureGuisLoaded();
return _guis.size();
}
void GuiManager::foreachGui(Visitor& visitor)
{
ensureGuisLoaded();
for (GuiInfoMap::iterator i = _guis.begin(); i != _guis.end(); ++i)
{
visitor.visit(i->first, i->second.type);
}
}
void GuiManager::reloadGui(const std::string& guiPath)
{
GuiPtr gui = loadGui(guiPath);
determineGuiType(gui);
}
GuiType GuiManager::getGuiType(const std::string& guiPath)
{
// Get the GUI (will load the file if necessary)
GuiPtr gui = std::static_pointer_cast<Gui>(getGui(guiPath));
GuiInfoMap::iterator found = _guis.find(guiPath);
if (found == _guis.end())
{
return FILE_NOT_FOUND;
}
// Gui Info found, determine readable type if necessary
if (found->second.type == UNDETERMINED)
{
found->second.type = determineGuiType(found->second.gui);
}
return found->second.type;
}
GuiType GuiManager::determineGuiType(const GuiPtr& gui)
{
if (gui)
{
// TODO: Find a better way of distinguishing GUIs
if (gui->findWindowDef("body") != NULL)
{
return ONE_SIDED_READABLE;
}
else if (gui->findWindowDef("leftBody") != NULL)
{
return TWO_SIDED_READABLE;
}
}
else
{
return IMPORT_FAILURE;
}
return NO_READABLE;
}
void GuiManager::init()
{
_guiLoader.start();
}
void GuiManager::reloadGuis()
{
clear();
init();
}
void GuiManager::findGuis()
{
_errorList.clear();
_guis.clear();
// Traverse the file system, using this class as callback
GlobalFileSystem().forEachFile(
GUI_DIR, GUI_EXT,
[&](const vfs::FileInfo& fileInfo) { registerGui(fileInfo.name); },
99
);
rMessage() << "[GuiManager]: Found " << _guis.size() << " guis." << std::endl;
}
void GuiManager::clear()
{
_guiLoader.reset();
_guis.clear();
_errorList.clear();
}
IGuiPtr GuiManager::getGui(const std::string& guiPath)
{
ensureGuisLoaded();
GuiInfoMap::iterator i = _guis.find(guiPath);
// Path existent?
if (i != _guis.end())
{
// Found in the map, load if not yet attempted
if (i->second.type == NOT_LOADED_YET)
{
loadGui(guiPath);
}
return i->second.gui;
}
// GUI not buffered, try to load afresh
return loadGui(guiPath);
}
void GuiManager::ensureGuisLoaded()
{
_guiLoader.ensureFinished();
}
GuiPtr GuiManager::loadGui(const std::string& guiPath)
{
ensureGuisLoaded();
// Insert a new entry in the map, if necessary
std::pair<GuiInfoMap::iterator, bool> result = _guis.insert(
GuiInfoMap::value_type(guiPath, GuiInfo())
);
GuiInfo& info = result.first->second;
ArchiveTextFilePtr file = GlobalFileSystem().openTextFile(guiPath);
if (file == NULL)
{
std::string errMSG = "Could not open file: " + guiPath + "\n";
_errorList.push_back(errMSG);
rError() << errMSG;
info.type = FILE_NOT_FOUND;
return GuiPtr();
}
// Construct a Code Tokeniser, which is able to handle #includes
try
{
parser::CodeTokeniser tokeniser(file, parser::WHITESPACE, "{}(),;");
info.gui = Gui::createFromTokens(tokeniser);
info.type = UNDETERMINED;
return info.gui;
}
catch (parser::ParseException& p)
{
std::string errMSG = "Error while parsing " + guiPath + ": " + p.what() + "\n";
_errorList.push_back(errMSG);
rError() << errMSG;
info.type = IMPORT_FAILURE;
return GuiPtr();
}
}
const std::string& GuiManager::getName() const
{
static std::string _name(MODULE_GUIMANAGER);
return _name;
}
const StringSet& GuiManager::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_VIRTUALFILESYSTEM);
}
return _dependencies;
}
void GuiManager::initialiseModule(const IApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called." << std::endl;
// Search the VFS for GUIs
init();
}
void GuiManager::shutdownModule()
{
clear();
}
} // namespace gui