aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-08-15 12:07:53 (GMT)
committerPavel Labath <pavelo@centrum.sk>2013-02-18 21:04:26 (GMT)
commita897844ea1cfa6303112fd3f0d472d3e135bdc6d (patch)
treec65582488fa42930d7e44c0f585e6eba4f1f3650 /src/FbTk
parenta5792b747d5c0318f66360092dafbd4852e2c68c (diff)
downloadfluxbox_pavel-a897844ea1cfa6303112fd3f0d472d3e135bdc6d.zip
fluxbox_pavel-a897844ea1cfa6303112fd3f0d472d3e135bdc6d.tar.bz2
More preparation for automatic updating of config files
Diffstat (limited to 'src/FbTk')
-rw-r--r--src/FbTk/LResource.cc34
-rw-r--r--src/FbTk/LResource.hh8
-rw-r--r--src/FbTk/Resource.hh41
3 files changed, 51 insertions, 32 deletions
diff --git a/src/FbTk/LResource.cc b/src/FbTk/LResource.cc
index 18209cb..589e52f 100644
--- a/src/FbTk/LResource.cc
+++ b/src/FbTk/LResource.cc
@@ -89,21 +89,6 @@ namespace {
89 89
90} // anonymous namespace 90} // anonymous namespace
91 91
92void LResourceManager::convert(ResourceManager &old, const std::string &new_file) {
93 Lua l;
94
95 LResourceManager new_rm(old.root(), l);
96 for(ResourceList::const_iterator i = old.begin(); i != old.end(); ++i) {
97 // adding the resource to new_rm will set it to default value
98 // we save the value to a temp variable so we can restore it later
99 const std::string &t = (*i)->getString();
100 new_rm.addResource(**i);
101 (*i)->setFromString(t.c_str());
102 }
103
104 new_rm.save(new_file.c_str(), NULL);
105}
106
107LResourceManager::LResourceManager(const std::string &root, Lua &l, unsigned int autosave) 92LResourceManager::LResourceManager(const std::string &root, Lua &l, unsigned int autosave)
108 : ResourceManager_base(root), m_l(&l) { 93 : ResourceManager_base(root), m_l(&l) {
109 94
@@ -116,6 +101,25 @@ LResourceManager::LResourceManager(const std::string &root, Lua &l, unsigned int
116 setLua(l); 101 setLua(l);
117} 102}
118 103
104LResourceManager::LResourceManager(ResourceManager &old, Lua &l)
105 : ResourceManager_base(old.root()), m_l(&l) {
106
107 // We create a copy of the list so we can safely traverse it while the resources disassociate
108 // themselves from the old resource manager
109 ResourceList list;
110 for(ResourceList::const_iterator i = old.begin(); i != old.end(); ++i) {
111 list.push_back(*i);
112 }
113
114 for(ResourceList::const_iterator i = list.begin(); i != list.end(); ++i) {
115 // adding the resource to this resource manager will set it to default value
116 // we save the value to a temp variable so we can restore it later
117 const std::string &t = (*i)->getString();
118 (*i)->setResourceManager(*this);
119 (*i)->setFromString(t.c_str());
120 }
121}
122
119void LResourceManager::doLoad(const std::string &filename) { 123void LResourceManager::doLoad(const std::string &filename) {
120 m_l->checkstack(1); 124 m_l->checkstack(1);
121 lua::stack_sentry s(*m_l); 125 lua::stack_sentry s(*m_l);
diff --git a/src/FbTk/LResource.hh b/src/FbTk/LResource.hh
index a198197..022e6f5 100644
--- a/src/FbTk/LResource.hh
+++ b/src/FbTk/LResource.hh
@@ -36,7 +36,6 @@ class Lua;
36 36
37class LResourceManager: public ResourceManager_base { 37class LResourceManager: public ResourceManager_base {
38public: 38public:
39 static void convert(ResourceManager &old, const std::string &new_file);
40 39
41 /** 40 /**
42 * @param root the name of the table where settings will reside 41 * @param root the name of the table where settings will reside
@@ -45,16 +44,21 @@ public:
45 * starts a timer. If another resource is modified, the timer is restarted. 0 = disabled 44 * starts a timer. If another resource is modified, the timer is restarted. 0 = disabled
46 */ 45 */
47 LResourceManager(const std::string &root, Lua &l, unsigned int autosave = 0); 46 LResourceManager(const std::string &root, Lua &l, unsigned int autosave = 0);
47 /**
48 * Take over resources from the old resource manager. Used in fluxbox-update_configs.
49 * Resource managers created via this constructor don't support automatic saving.
50 */
51 explicit LResourceManager(ResourceManager &old, Lua &l);
48 virtual bool save(const char *filename, const char *); 52 virtual bool save(const char *filename, const char *);
49 virtual void addResource(Resource_base &r); 53 virtual void addResource(Resource_base &r);
50 virtual void removeResource(Resource_base &r); 54 virtual void removeResource(Resource_base &r);
51 virtual void resourceChanged(Resource_base &r); 55 virtual void resourceChanged(Resource_base &r);
56 virtual void doLoad(const std::string &filename);
52 void setLua(Lua &l); 57 void setLua(Lua &l);
53 58
54private: 59private:
55 void doAddResource(Resource_base &r); 60 void doAddResource(Resource_base &r);
56 void doRemoveResource(Resource_base &r); 61 void doRemoveResource(Resource_base &r);
57 virtual void doLoad(const std::string &filename);
58 62
59 Lua *m_l; 63 Lua *m_l;
60 Timer m_savetimer; 64 Timer m_savetimer;
diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh
index 700fe70..b729ad4 100644
--- a/src/FbTk/Resource.hh
+++ b/src/FbTk/Resource.hh
@@ -47,12 +47,19 @@ private:
47 std::string m_str; 47 std::string m_str;
48}; 48};
49 49
50// forward declarations
51template <typename T, typename Traits>
52class Resource;
53class ResourceManager_base;
54
50/// Base class for resources, this is only used in ResourceManager 55/// Base class for resources, this is only used in ResourceManager
51class Resource_base:private FbTk::NotCopyable 56class Resource_base:private FbTk::NotCopyable
52{ 57{
53public: 58public:
54 virtual ~Resource_base() { }; 59 virtual ~Resource_base() { };
55 60
61 virtual void setResourceManager(ResourceManager_base &rm) = 0;
62
56 /// set from string value 63 /// set from string value
57 virtual void setFromString(char const *strval) = 0; 64 virtual void setFromString(char const *strval) = 0;
58 /// set default value 65 /// set default value
@@ -80,9 +87,6 @@ private:
80 std::string m_altname; ///< alternative name 87 std::string m_altname; ///< alternative name
81}; 88};
82 89
83template <typename T, typename Traits>
84class Resource;
85
86class ResourceManager_base 90class ResourceManager_base
87{ 91{
88public: 92public:
@@ -134,10 +138,10 @@ public:
134 ResourceList::const_iterator begin() { return m_resourcelist.begin(); } 138 ResourceList::const_iterator begin() { return m_resourcelist.begin(); }
135 ResourceList::const_iterator end() { return m_resourcelist.end(); } 139 ResourceList::const_iterator end() { return m_resourcelist.end(); }
136 140
137protected:
138 /// does the actual loading 141 /// does the actual loading
139 virtual void doLoad(const std::string &filename) = 0; 142 virtual void doLoad(const std::string &filename) = 0;
140 143
144protected:
141 ResourceList m_resourcelist; 145 ResourceList m_resourcelist;
142 const std::string m_root; 146 const std::string m_root;
143 std::string m_filename; 147 std::string m_filename;
@@ -177,9 +181,10 @@ public:
177 std::cerr<<(*it)->name()<<std::endl; 181 std::cerr<<(*it)->name()<<std::endl;
178 } 182 }
179 } 183 }
180protected: 184
181 virtual void doLoad(const std::string &filename); 185 virtual void doLoad(const std::string &filename);
182 186
187protected:
183 int m_db_lock; 188 int m_db_lock;
184 189
185private: 190private:
@@ -202,31 +207,37 @@ public:
202 207
203 Resource(ResourceManager_base &rm, T val, const std::string &name, 208 Resource(ResourceManager_base &rm, T val, const std::string &name,
204 const std::string &altname, const Traits &traits = Traits() ): 209 const std::string &altname, const Traits &traits = Traits() ):
205 Resource_base(name, altname), Traits(traits), m_value(val), m_defaultval(val), m_rm(rm) { 210 Resource_base(name, altname), Traits(traits), m_value(val), m_defaultval(val), m_rm(&rm) {
206 m_rm.addResource(*this); // add this to resource handler 211 m_rm->addResource(*this); // add this to resource handler
207 } 212 }
208 213
209 // LResourceManager does not use altname, so we provide a constructor which initializes 214 // LResourceManager does not use altname, so we provide a constructor which initializes
210 // altname to name 215 // altname to name
211 Resource(ResourceManager_base &rm, T val, const std::string &name, 216 Resource(ResourceManager_base &rm, T val, const std::string &name,
212 const Traits &traits = Traits() ): 217 const Traits &traits = Traits() ):
213 Resource_base(name, name), Traits(traits), m_value(val), m_defaultval(val), m_rm(rm) { 218 Resource_base(name, name), Traits(traits), m_value(val), m_defaultval(val), m_rm(&rm) {
214 m_rm.addResource(*this); // add this to resource handler 219 m_rm->addResource(*this); // add this to resource handler
215 } 220 }
216 virtual ~Resource() { 221 virtual ~Resource() {
217 m_rm.removeResource(*this); // remove this from resource handler 222 m_rm->removeResource(*this); // remove this from resource handler
223 }
224
225 virtual void setResourceManager(ResourceManager_base &rm) {
226 m_rm->removeResource(*this);
227 m_rm = &rm;
228 m_rm->addResource(*this);
218 } 229 }
219 230
220 void setDefaultValue() { 231 void setDefaultValue() {
221 m_value = m_defaultval; 232 m_value = m_defaultval;
222 m_rm.resourceChanged(*this); 233 m_rm->resourceChanged(*this);
223 m_modified_sig.emit(m_value); 234 m_modified_sig.emit(m_value);
224 } 235 }
225 /// sets resource from string, specialized, must be implemented 236 /// sets resource from string, specialized, must be implemented
226 void setFromString(const char *strval) { 237 void setFromString(const char *strval) {
227 try { 238 try {
228 m_value = Traits::fromString(strval); 239 m_value = Traits::fromString(strval);
229 m_rm.resourceChanged(*this); 240 m_rm->resourceChanged(*this);
230 m_modified_sig.emit(m_value); 241 m_modified_sig.emit(m_value);
231 } 242 }
232 catch(ConversionError &e) { 243 catch(ConversionError &e) {
@@ -236,7 +247,7 @@ public:
236 } 247 }
237 Accessor<T> &operator =(const T& newvalue) { 248 Accessor<T> &operator =(const T& newvalue) {
238 m_value = newvalue; 249 m_value = newvalue;
239 m_rm.resourceChanged(*this); 250 m_rm->resourceChanged(*this);
240 m_modified_sig.emit(m_value); 251 m_modified_sig.emit(m_value);
241 return *this; 252 return *this;
242 } 253 }
@@ -247,7 +258,7 @@ public:
247 virtual void setFromLua(lua::state &l) { 258 virtual void setFromLua(lua::state &l) {
248 try { 259 try {
249 m_value = Traits::fromLua(l); 260 m_value = Traits::fromLua(l);
250 m_rm.resourceChanged(*this); 261 m_rm->resourceChanged(*this);
251 m_modified_sig.emit(m_value); 262 m_modified_sig.emit(m_value);
252 } 263 }
253 catch(ConversionError &e) { 264 catch(ConversionError &e) {
@@ -268,7 +279,7 @@ public:
268 279
269private: 280private:
270 T m_value, m_defaultval; 281 T m_value, m_defaultval;
271 ResourceManager_base &m_rm; 282 ResourceManager_base *m_rm;
272 Signal<const T &> m_modified_sig; 283 Signal<const T &> m_modified_sig;
273}; 284};
274 285