aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-08-02 11:56:31 (GMT)
committerPavel Labath <pavelo@centrum.sk>2013-02-18 21:04:25 (GMT)
commitd9be60362f364145daeaa21dcea99d0fff4c1203 (patch)
treeba967b97366967c49db552bf9af26061e70a6813 /src/FbTk
parentff9a9752654e8958ca66c8f8c7f3d1621295ca03 (diff)
downloadfluxbox_pavel-d9be60362f364145daeaa21dcea99d0fff4c1203.zip
fluxbox_pavel-d9be60362f364145daeaa21dcea99d0fff4c1203.tar.bz2
Automatically save init file when a resource is modified
previously, init was autosaved only when a resource was modified using the menu. I have also included modifications through lua code. To avoid wasting resources, the file is not saved immediately, but with a 5 second delay (to enable saving a bunch of changes in one go)
Diffstat (limited to 'src/FbTk')
-rw-r--r--src/FbTk/LResource.cc21
-rw-r--r--src/FbTk/LResource.hh11
-rw-r--r--src/FbTk/Resource.hh11
3 files changed, 38 insertions, 5 deletions
diff --git a/src/FbTk/LResource.cc b/src/FbTk/LResource.cc
index 31362bd..de90b95 100644
--- a/src/FbTk/LResource.cc
+++ b/src/FbTk/LResource.cc
@@ -26,8 +26,11 @@
26 26
27#include "I18n.hh" 27#include "I18n.hh"
28#include "LuaUtil.hh" 28#include "LuaUtil.hh"
29#include "MemFun.hh"
29#include "Resource.hh" 30#include "Resource.hh"
30 31
32// defined in LResourceHelper-lua.cc
33// contains the compiled code of LResourceHelper.lua
31extern const char LResourceHelper[]; 34extern const char LResourceHelper[];
32extern const unsigned int LResourceHelper_size; 35extern const unsigned int LResourceHelper_size;
33 36
@@ -101,8 +104,15 @@ void LResourceManager::convert(ResourceManager &old, const std::string &new_file
101 new_rm.save(new_file.c_str(), NULL); 104 new_rm.save(new_file.c_str(), NULL);
102} 105}
103 106
104LResourceManager::LResourceManager(const std::string &root, Lua &l) 107LResourceManager::LResourceManager(const std::string &root, Lua &l, unsigned int autosave)
105 : ResourceManager_base(root), m_l(&l) { 108 : ResourceManager_base(root), m_l(&l) {
109
110 m_savetimer.setInterval(autosave);
111 m_savetimer.fireOnce(true);
112 m_savetimer.setFunctor( MemFunBind(*this, &LResourceManager::save,
113 static_cast<const char *>(NULL), static_cast<const char *>(NULL)
114 ) );
115
106 setLua(l); 116 setLua(l);
107} 117}
108 118
@@ -143,6 +153,8 @@ bool LResourceManager::save(const char *filename, const char *) {
143 if(filename == NULL) 153 if(filename == NULL)
144 filename = m_filename.c_str(); 154 filename = m_filename.c_str();
145 155
156 std::cerr << "XXX SAVING " << filename << std::endl;
157
146 m_l->getfield(lua::REGISTRYINDEX, dump_resources); 158 m_l->getfield(lua::REGISTRYINDEX, dump_resources);
147 m_l->getfield(lua::GLOBALSINDEX, m_root.c_str()); 159 m_l->getfield(lua::GLOBALSINDEX, m_root.c_str());
148 m_l->pushstring(filename); 160 m_l->pushstring(filename);
@@ -194,6 +206,11 @@ void LResourceManager::doRemoveResource(Resource_base &r) {
194 m_l->pop(); 206 m_l->pop();
195} 207}
196 208
209void LResourceManager::resourceChanged(Resource_base &r) {
210 if(! m_savetimer.isTiming())
211 m_savetimer.start();
212}
213
197void LResourceManager::setLua(Lua &l) { 214void LResourceManager::setLua(Lua &l) {
198 l.checkstack(2); 215 l.checkstack(2);
199 lua::stack_sentry s(l); 216 lua::stack_sentry s(l);
diff --git a/src/FbTk/LResource.hh b/src/FbTk/LResource.hh
index f0f8050..30f0e02 100644
--- a/src/FbTk/LResource.hh
+++ b/src/FbTk/LResource.hh
@@ -28,6 +28,7 @@
28#include <string> 28#include <string>
29 29
30#include "Resource.hh" 30#include "Resource.hh"
31#include "Timer.hh"
31 32
32namespace FbTk { 33namespace FbTk {
33 34
@@ -37,11 +38,18 @@ class LResourceManager: public ResourceManager_base {
37public: 38public:
38 static void convert(ResourceManager &old, const std::string &new_file); 39 static void convert(ResourceManager &old, const std::string &new_file);
39 40
40 LResourceManager(const std::string &root, Lua &l); 41 /**
42 * @param root the name of the table where settings will reside
43 * @param l lua context
44 * @param autosave delay (in seconds) for automatic saving of resources. Modifying a resource
45 * starts a timer. If another resource is modified, the timer is restarted. 0 = disabled
46 */
47 LResourceManager(const std::string &root, Lua &l, unsigned int autosave = 0);
41 void load(const std::string &filename, const std::string &fallback); 48 void load(const std::string &filename, const std::string &fallback);
42 virtual bool save(const char *filename, const char *); 49 virtual bool save(const char *filename, const char *);
43 virtual void addResource(Resource_base &r); 50 virtual void addResource(Resource_base &r);
44 virtual void removeResource(Resource_base &r); 51 virtual void removeResource(Resource_base &r);
52 virtual void resourceChanged(Resource_base &r);
45 void setLua(Lua &l); 53 void setLua(Lua &l);
46 54
47private: 55private:
@@ -50,6 +58,7 @@ private:
50 58
51 Lua *m_l; 59 Lua *m_l;
52 std::string m_filename; 60 std::string m_filename;
61 Timer m_savetimer;
53}; 62};
54 63
55} // end namespace FbTk 64} // end namespace FbTk
diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh
index 7a797a1..c4a7951 100644
--- a/src/FbTk/Resource.hh
+++ b/src/FbTk/Resource.hh
@@ -96,8 +96,6 @@ public:
96 /// @return true on success 96 /// @return true on success
97 virtual bool save(const char *filename, const char *mergefilename=0) = 0; 97 virtual bool save(const char *filename, const char *mergefilename=0) = 0;
98 98
99
100
101 /// Add resource to list, only used in Resource<T> 99 /// Add resource to list, only used in Resource<T>
102 virtual void addResource(Resource_base &r); 100 virtual void addResource(Resource_base &r);
103 101
@@ -106,6 +104,9 @@ public:
106 m_resourcelist.remove(&r); 104 m_resourcelist.remove(&r);
107 } 105 }
108 106
107 /// Called by Resources when their value changes
108 virtual void resourceChanged(Resource_base &r) = 0;
109
109 /// searches for the resource with the resourcename 110 /// searches for the resource with the resourcename
110 /// @return pointer to resource base on success, else 0. 111 /// @return pointer to resource base on success, else 0.
111 Resource_base *findResource(const std::string &resourcename); 112 Resource_base *findResource(const std::string &resourcename);
@@ -154,6 +155,8 @@ public:
154 /// Add resource to list, only used in Resource<T> 155 /// Add resource to list, only used in Resource<T>
155 virtual void addResource(Resource_base &r); 156 virtual void addResource(Resource_base &r);
156 157
158 virtual void resourceChanged(Resource_base &r) {};
159
157 // this marks the database as "in use" and will avoid reloading 160 // this marks the database as "in use" and will avoid reloading
158 // resources unless it is zero. 161 // resources unless it is zero.
159 // It returns this resource manager. Useful for passing to 162 // It returns this resource manager. Useful for passing to
@@ -211,12 +214,14 @@ public:
211 214
212 void setDefaultValue() { 215 void setDefaultValue() {
213 m_value = m_defaultval; 216 m_value = m_defaultval;
217 m_rm.resourceChanged(*this);
214 m_modified_sig.emit(m_value); 218 m_modified_sig.emit(m_value);
215 } 219 }
216 /// sets resource from string, specialized, must be implemented 220 /// sets resource from string, specialized, must be implemented
217 void setFromString(const char *strval) { 221 void setFromString(const char *strval) {
218 try { 222 try {
219 m_value = Traits::fromString(strval); 223 m_value = Traits::fromString(strval);
224 m_rm.resourceChanged(*this);
220 m_modified_sig.emit(m_value); 225 m_modified_sig.emit(m_value);
221 } 226 }
222 catch(ConversionError &e) { 227 catch(ConversionError &e) {
@@ -226,6 +231,7 @@ public:
226 } 231 }
227 Accessor<T> &operator =(const T& newvalue) { 232 Accessor<T> &operator =(const T& newvalue) {
228 m_value = newvalue; 233 m_value = newvalue;
234 m_rm.resourceChanged(*this);
229 m_modified_sig.emit(m_value); 235 m_modified_sig.emit(m_value);
230 return *this; 236 return *this;
231 } 237 }
@@ -236,6 +242,7 @@ public:
236 virtual void setFromLua(lua::state &l) { 242 virtual void setFromLua(lua::state &l) {
237 try { 243 try {
238 m_value = Traits::fromLua(l); 244 m_value = Traits::fromLua(l);
245 m_rm.resourceChanged(*this);
239 m_modified_sig.emit(m_value); 246 m_modified_sig.emit(m_value);
240 } 247 }
241 catch(ConversionError &e) { 248 catch(ConversionError &e) {