Debian Codegraph @

plugins/dm.gui/gui/GuiManager.cpp

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