Debian Codegraph @

plugins/dm.gui/gui/GuiManager.h

raw
1 #pragma once
2
3 #include "igui.h"
4 #include "util/Noncopyable.h"
5 #include <map>
6 #include "ifilesystem.h"
7 #include "string/string.h"
8 #include "parser/ThreadedDefLoader.h"
9
10 namespace gui
11 {
12
13 namespace
14 {
15 const std::string GUI_DIR("guis/readables/");
16 const std::string GUI_EXT("gui");
17 }
18
19 class Gui;
20 typedef std::shared_ptr<Gui> GuiPtr;
21
22 /**
23 * greebo: This manager keeps track of all the loaded GUIs,
24 * including parsing the .gui files on demand.
25 */
26 class GuiManager :
27 public IGuiManager,
28 public util::Noncopyable
29 {
30 private:
31 struct GuiInfo
32 {
33 // The type of this Gui (NOT_LOADED_YET by default)
34 GuiType type;
35
36 // the cached GUI pointer, can be NULL if load failed
37 GuiPtr gui;
38
39 GuiInfo() :
40 type(NOT_LOADED_YET)
41 {}
42
43 GuiInfo(const GuiPtr& gui_, GuiType type_) :
44 type(type_),
45 gui(gui_)
46 {}
47 };
48
49 // The table of all loaded Gui, sorted by VFS path
50 typedef std::map<std::string, GuiInfo> GuiInfoMap;
51 GuiInfoMap _guis;
52
53 parser::ThreadedDefLoader<void> _guiLoader;
54
55 // A List of all the errors occuring lastly.
56 StringList _errorList;
57
58 public:
59 GuiManager();
60
61 // Gets a GUI from the given VFS path, parsing it on demand
62 // Returns NULL if the GUI couldn't be found or loaded.
63 IGuiPtr getGui(const std::string& guiPath) override;
64
65 // Returns the number of known GUIs (or GUI paths)
66 std::size_t getNumGuis() override;
67
68 // Traverse all known GUIs using the given Visitor
69 void foreachGui(Visitor& visitor) override;
70
71 // Returns the GUI appearance type for the given GUI path
72 GuiType getGuiType(const std::string& guiPath) override;
73
74 // Reload the gui
75 void reloadGui(const std::string& guiPath) override;
76
77 // Returns the _errorList for use in a GUI.
78 const StringList& getErrorList() override { return _errorList; }
79
80 // Clears out the GUIs and reloads them
81 void reloadGuis() override;
82
83 // RegisterableModule
84 const std::string& getName() const override;
85 const StringSet& getDependencies() const override;
86 void initialiseModule(const IApplicationContext& ctx) override;
87 void shutdownModule() override;
88
89 private:
90 void init();
91 void clear();
92
93 // Searches the VFS for all available GUI definitions
94 void findGuis();
95
96 void ensureGuisLoaded();
97
98 GuiType determineGuiType(const GuiPtr& gui);
99
100 GuiPtr loadGui(const std::string& guiPath);
101
102 // Used by findGuis()
103 void registerGui(const std::string& guiPath);
104 };
105
106 } // namespace