aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-08-15 12:07:53 (GMT)
committerPavel Labath <pavelo@centrum.sk>2011-11-01 10:04:02 (GMT)
commit678a98de943963e993827540496741ae814af8bd (patch)
tree12ca6bb8baf02ce9a65e4cc7a473da9ba647333d /src
parent41e9c390274c2c84a4696fee0b5c63daae2ed6fe (diff)
downloadfluxbox_pavel-678a98de943963e993827540496741ae814af8bd.zip
fluxbox_pavel-678a98de943963e993827540496741ae814af8bd.tar.bz2
More preparation for automatic updating of config files
Diffstat (limited to 'src')
-rw-r--r--src/FbTk/LResource.cc34
-rw-r--r--src/FbTk/LResource.hh8
-rw-r--r--src/FbTk/Resource.hh41
-rw-r--r--src/Slit.cc2
-rw-r--r--src/Slit.hh14
5 files changed, 62 insertions, 37 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
diff --git a/src/Slit.cc b/src/Slit.cc
index f72c4ef..9fc4ef1 100644
--- a/src/Slit.cc
+++ b/src/Slit.cc
@@ -211,7 +211,7 @@ void Slit::SlitClientsRes::setFromLua(lua::state &l) {
211 211
212 l.pop(); 212 l.pop();
213 213
214 m_rm.resourceChanged(*this); 214 m_rm->resourceChanged(*this);
215} 215}
216 216
217void Slit::SlitClientsRes::pushToLua(lua::state &l) const { 217void Slit::SlitClientsRes::pushToLua(lua::state &l) const {
diff --git a/src/Slit.hh b/src/Slit.hh
index 77ce6f5..d6ba08f 100644
--- a/src/Slit.hh
+++ b/src/Slit.hh
@@ -180,12 +180,18 @@ private:
180 class SlitClientsRes: public FbTk::Resource_base, public SlitClients { 180 class SlitClientsRes: public FbTk::Resource_base, public SlitClients {
181 public: 181 public:
182 SlitClientsRes(FbTk::ResourceManager_base &rm, const std::string &name) 182 SlitClientsRes(FbTk::ResourceManager_base &rm, const std::string &name)
183 : FbTk::Resource_base(name, name), m_rm(rm) { 183 : FbTk::Resource_base(name, name), m_rm(&rm) {
184 m_rm.addResource(*this); 184 m_rm->addResource(*this);
185 } 185 }
186 186
187 ~SlitClientsRes() { 187 ~SlitClientsRes() {
188 m_rm.removeResource(*this); 188 m_rm->removeResource(*this);
189 }
190
191 virtual void setResourceManager(FbTk::ResourceManager_base &rm) {
192 m_rm->removeResource(*this);
193 m_rm = &rm;
194 m_rm->addResource(*this);
189 } 195 }
190 196
191 virtual void setDefaultValue() {} 197 virtual void setDefaultValue() {}
@@ -195,7 +201,7 @@ private:
195 virtual void pushToLua(lua::state &l) const; 201 virtual void pushToLua(lua::state &l) const;
196 202
197 private: 203 private:
198 FbTk::ResourceManager_base &m_rm; 204 FbTk::ResourceManager_base *m_rm;
199 }; 205 };
200 206
201 SlitClientsRes m_client_list; 207 SlitClientsRes m_client_list;