diff options
author | Pavel Labath <pavelo@centrum.sk> | 2011-08-04 09:02:04 (GMT) |
---|---|---|
committer | Paul Tagliamonte <paultag@fluxbox.org> | 2012-04-07 02:13:12 (GMT) |
commit | 76855f197979cb8b1fe59c208d65e080cf19081a (patch) | |
tree | 8f8c34edb1a551361c6524a61be8279c424db57a /src/FbTk | |
parent | 224ac66f4f932ea05bec55a7e4a1f1e6c47fea50 (diff) | |
download | fluxbox_paul-76855f197979cb8b1fe59c208d65e080cf19081a.zip fluxbox_paul-76855f197979cb8b1fe59c208d65e080cf19081a.tar.bz2 |
Move most of the resource loading code into ResourceManager_base
I mostly do this to avoid code duplication between fluxbox and fluxbox-update_configs.
Diffstat (limited to 'src/FbTk')
-rw-r--r-- | src/FbTk/LResource.cc | 30 | ||||
-rw-r--r-- | src/FbTk/LResource.hh | 3 | ||||
-rw-r--r-- | src/FbTk/Resource.cc | 39 | ||||
-rw-r--r-- | src/FbTk/Resource.hh | 15 |
4 files changed, 45 insertions, 42 deletions
diff --git a/src/FbTk/LResource.cc b/src/FbTk/LResource.cc index de90b95..18209cb 100644 --- a/src/FbTk/LResource.cc +++ b/src/FbTk/LResource.cc | |||
@@ -116,34 +116,12 @@ LResourceManager::LResourceManager(const std::string &root, Lua &l, unsigned int | |||
116 | setLua(l); | 116 | setLua(l); |
117 | } | 117 | } |
118 | 118 | ||
119 | void LResourceManager::load(const std::string &filename, const std::string &fallback) { | 119 | void LResourceManager::doLoad(const std::string &filename) { |
120 | _FB_USES_NLS; | ||
121 | m_l->checkstack(1); | 120 | m_l->checkstack(1); |
122 | lua::stack_sentry s(*m_l); | 121 | lua::stack_sentry s(*m_l); |
123 | 122 | ||
124 | m_filename = filename; | 123 | m_l->loadfile(filename.c_str()); |
125 | 124 | m_l->call(0, 0); | |
126 | try { | ||
127 | m_l->loadfile(filename.c_str()); | ||
128 | m_l->call(0, 0); | ||
129 | } | ||
130 | catch(lua::exception &e) { | ||
131 | std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFile, "Failed to load database", | ||
132 | "Failed trying to read rc file") << ":" << filename << std::endl; | ||
133 | std::cerr << "Fluxbox: " << e.what() << std::endl; | ||
134 | std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFileTrying, "Retrying with", | ||
135 | "Retrying rc file loading with (the following file)") | ||
136 | << ": " << fallback << std::endl; | ||
137 | try { | ||
138 | m_l->loadfile(fallback.c_str()); | ||
139 | m_l->call(0, 0); | ||
140 | } | ||
141 | catch(lua::exception &e) { | ||
142 | std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFile, "Failed to load database", "") | ||
143 | << ": " << fallback << std::endl; | ||
144 | std::cerr << "Fluxbox: " << e.what() << std::endl; | ||
145 | } | ||
146 | } | ||
147 | } | 125 | } |
148 | 126 | ||
149 | bool LResourceManager::save(const char *filename, const char *) { | 127 | bool LResourceManager::save(const char *filename, const char *) { |
@@ -153,8 +131,6 @@ bool LResourceManager::save(const char *filename, const char *) { | |||
153 | if(filename == NULL) | 131 | if(filename == NULL) |
154 | filename = m_filename.c_str(); | 132 | filename = m_filename.c_str(); |
155 | 133 | ||
156 | std::cerr << "XXX SAVING " << filename << std::endl; | ||
157 | |||
158 | m_l->getfield(lua::REGISTRYINDEX, dump_resources); | 134 | m_l->getfield(lua::REGISTRYINDEX, dump_resources); |
159 | m_l->getfield(lua::GLOBALSINDEX, m_root.c_str()); | 135 | m_l->getfield(lua::GLOBALSINDEX, m_root.c_str()); |
160 | m_l->pushstring(filename); | 136 | m_l->pushstring(filename); |
diff --git a/src/FbTk/LResource.hh b/src/FbTk/LResource.hh index 30f0e02..a198197 100644 --- a/src/FbTk/LResource.hh +++ b/src/FbTk/LResource.hh | |||
@@ -45,7 +45,6 @@ public: | |||
45 | * starts a timer. If another resource is modified, the timer is restarted. 0 = disabled | 45 | * starts a timer. If another resource is modified, the timer is restarted. 0 = disabled |
46 | */ | 46 | */ |
47 | LResourceManager(const std::string &root, Lua &l, unsigned int autosave = 0); | 47 | LResourceManager(const std::string &root, Lua &l, unsigned int autosave = 0); |
48 | void load(const std::string &filename, const std::string &fallback); | ||
49 | virtual bool save(const char *filename, const char *); | 48 | virtual bool save(const char *filename, const char *); |
50 | virtual void addResource(Resource_base &r); | 49 | virtual void addResource(Resource_base &r); |
51 | virtual void removeResource(Resource_base &r); | 50 | virtual void removeResource(Resource_base &r); |
@@ -55,9 +54,9 @@ public: | |||
55 | private: | 54 | private: |
56 | void doAddResource(Resource_base &r); | 55 | void doAddResource(Resource_base &r); |
57 | void doRemoveResource(Resource_base &r); | 56 | void doRemoveResource(Resource_base &r); |
57 | virtual void doLoad(const std::string &filename); | ||
58 | 58 | ||
59 | Lua *m_l; | 59 | Lua *m_l; |
60 | std::string m_filename; | ||
61 | Timer m_savetimer; | 60 | Timer m_savetimer; |
62 | }; | 61 | }; |
63 | 62 | ||
diff --git a/src/FbTk/Resource.cc b/src/FbTk/Resource.cc index 37555b3..8f63322 100644 --- a/src/FbTk/Resource.cc +++ b/src/FbTk/Resource.cc | |||
@@ -38,6 +38,33 @@ using std::string; | |||
38 | namespace FbTk { | 38 | namespace FbTk { |
39 | 39 | ||
40 | 40 | ||
41 | void ResourceManager_base::load(const std::string &filename, const std::string &fallback) { | ||
42 | _FB_USES_NLS; | ||
43 | |||
44 | m_filename = filename; | ||
45 | |||
46 | try { | ||
47 | doLoad(filename); | ||
48 | } | ||
49 | catch(std::runtime_error &e) { | ||
50 | std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFile, "Failed to load database", | ||
51 | "Failed trying to read rc file") << ":" << filename << std::endl; | ||
52 | std::cerr << "Fluxbox: " << e.what() << std::endl; | ||
53 | std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFileTrying, "Retrying with", | ||
54 | "Retrying rc file loading with (the following file)") | ||
55 | << ": " << fallback << std::endl; | ||
56 | try { | ||
57 | doLoad(fallback); | ||
58 | } | ||
59 | catch(std::runtime_error &e) { | ||
60 | std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFile, "Failed to load database", "") | ||
61 | << ": " << fallback << std::endl; | ||
62 | std::cerr << "Fluxbox: " << e.what() << std::endl; | ||
63 | throw; | ||
64 | } | ||
65 | } | ||
66 | } | ||
67 | |||
41 | void ResourceManager_base::addResource(Resource_base &r) { | 68 | void ResourceManager_base::addResource(Resource_base &r) { |
42 | m_resourcelist.push_back(&r); | 69 | m_resourcelist.push_back(&r); |
43 | m_resourcelist.unique(); | 70 | m_resourcelist.unique(); |
@@ -87,7 +114,6 @@ ResourceManager::ResourceManager(const std::string &root, const std::string &alt | |||
87 | ResourceManager_base(root), | 114 | ResourceManager_base(root), |
88 | m_db_lock(0), | 115 | m_db_lock(0), |
89 | m_database(0), | 116 | m_database(0), |
90 | m_filename(filename ? filename : ""), | ||
91 | m_alt_root(alt_root) | 117 | m_alt_root(alt_root) |
92 | { | 118 | { |
93 | static bool xrm_initialized = false; | 119 | static bool xrm_initialized = false; |
@@ -96,6 +122,7 @@ ResourceManager::ResourceManager(const std::string &root, const std::string &alt | |||
96 | xrm_initialized = true; | 122 | xrm_initialized = true; |
97 | } | 123 | } |
98 | 124 | ||
125 | m_filename = filename ? filename : ""; | ||
99 | if (lock_db) | 126 | if (lock_db) |
100 | lock(); | 127 | lock(); |
101 | } | 128 | } |
@@ -108,11 +135,9 @@ ResourceManager::~ResourceManager() { | |||
108 | 135 | ||
109 | /** | 136 | /** |
110 | reloads all resources from resourcefile | 137 | reloads all resources from resourcefile |
111 | @return true on success else false | 138 | throws an exception in case of failure |
112 | */ | 139 | */ |
113 | bool ResourceManager::load(const char *filename) { | 140 | void ResourceManager::doLoad(const std::string &filename) { |
114 | m_filename = StringUtil::expandFilename(filename).c_str(); | ||
115 | |||
116 | // force reload (lock will ensure it exists) | 141 | // force reload (lock will ensure it exists) |
117 | if (m_database) { | 142 | if (m_database) { |
118 | delete m_database; | 143 | delete m_database; |
@@ -122,7 +147,7 @@ bool ResourceManager::load(const char *filename) { | |||
122 | lock(); | 147 | lock(); |
123 | if (!m_database) { | 148 | if (!m_database) { |
124 | unlock(); | 149 | unlock(); |
125 | return false; | 150 | throw std::runtime_error(""); |
126 | } | 151 | } |
127 | 152 | ||
128 | XrmValue value; | 153 | XrmValue value; |
@@ -146,8 +171,6 @@ bool ResourceManager::load(const char *filename) { | |||
146 | } | 171 | } |
147 | 172 | ||
148 | unlock(); | 173 | unlock(); |
149 | |||
150 | return true; | ||
151 | } | 174 | } |
152 | 175 | ||
153 | /** | 176 | /** |
diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh index c4a7951..700fe70 100644 --- a/src/FbTk/Resource.hh +++ b/src/FbTk/Resource.hh | |||
@@ -92,6 +92,11 @@ public: | |||
92 | 92 | ||
93 | virtual ~ResourceManager_base() {} | 93 | virtual ~ResourceManager_base() {} |
94 | 94 | ||
95 | /// Load all resources registered to this class | ||
96 | /// if loading of filename fails, it tries to load fallback | ||
97 | /// if that fails, it throws an exception | ||
98 | void load(const std::string &filename, const std::string &fallback); | ||
99 | |||
95 | /// Save all resouces registered to this class | 100 | /// Save all resouces registered to this class |
96 | /// @return true on success | 101 | /// @return true on success |
97 | virtual bool save(const char *filename, const char *mergefilename=0) = 0; | 102 | virtual bool save(const char *filename, const char *mergefilename=0) = 0; |
@@ -130,8 +135,12 @@ public: | |||
130 | ResourceList::const_iterator end() { return m_resourcelist.end(); } | 135 | ResourceList::const_iterator end() { return m_resourcelist.end(); } |
131 | 136 | ||
132 | protected: | 137 | protected: |
138 | /// does the actual loading | ||
139 | virtual void doLoad(const std::string &filename) = 0; | ||
140 | |||
133 | ResourceList m_resourcelist; | 141 | ResourceList m_resourcelist; |
134 | const std::string m_root; | 142 | const std::string m_root; |
143 | std::string m_filename; | ||
135 | }; | 144 | }; |
136 | 145 | ||
137 | class ResourceManager: public ResourceManager_base | 146 | class ResourceManager: public ResourceManager_base |
@@ -143,10 +152,6 @@ public: | |||
143 | const char *filename, bool lock_db); | 152 | const char *filename, bool lock_db); |
144 | virtual ~ResourceManager(); | 153 | virtual ~ResourceManager(); |
145 | 154 | ||
146 | /// Load all resources registered to this class | ||
147 | /// @return true on success | ||
148 | virtual bool load(const char *filename); | ||
149 | |||
150 | /// Save all resouces registered to this class | 155 | /// Save all resouces registered to this class |
151 | /// @return true on success | 156 | /// @return true on success |
152 | virtual bool save(const char *filename, const char *mergefilename=0); | 157 | virtual bool save(const char *filename, const char *mergefilename=0); |
@@ -173,6 +178,7 @@ public: | |||
173 | } | 178 | } |
174 | } | 179 | } |
175 | protected: | 180 | protected: |
181 | virtual void doLoad(const std::string &filename); | ||
176 | 182 | ||
177 | int m_db_lock; | 183 | int m_db_lock; |
178 | 184 | ||
@@ -180,7 +186,6 @@ private: | |||
180 | 186 | ||
181 | XrmDatabaseHelper *m_database; | 187 | XrmDatabaseHelper *m_database; |
182 | 188 | ||
183 | std::string m_filename; | ||
184 | std::string m_alt_root; | 189 | std::string m_alt_root; |
185 | }; | 190 | }; |
186 | 191 | ||