summaryrefslogtreecommitdiff
path: root/src/FbTk/Resource.cc
diff options
context:
space:
mode:
authorrathnor <rathnor>2003-07-18 15:40:55 (GMT)
committerrathnor <rathnor>2003-07-18 15:40:55 (GMT)
commit7e1733c4352befe577150bad63d362dd7c1a94b9 (patch)
tree1f339ae234f85aaa98529e9c511117dcb620a9f1 /src/FbTk/Resource.cc
parent06d723aefe7fb794f50b1e7a0ad8ad4473ae8ac6 (diff)
downloadfluxbox_lack-7e1733c4352befe577150bad63d362dd7c1a94b9.zip
fluxbox_lack-7e1733c4352befe577150bad63d362dd7c1a94b9.tar.bz2
reorganise operation of ResourceManager
plus fix initialisation of layer for slit and toolbar
Diffstat (limited to 'src/FbTk/Resource.cc')
-rw-r--r--src/FbTk/Resource.cc105
1 files changed, 82 insertions, 23 deletions
diff --git a/src/FbTk/Resource.cc b/src/FbTk/Resource.cc
index a222984..c21e55d 100644
--- a/src/FbTk/Resource.cc
+++ b/src/FbTk/Resource.cc
@@ -19,10 +19,10 @@
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE. 20// DEALINGS IN THE SOFTWARE.
21 21
22// $Id: Resource.cc,v 1.1 2003/05/18 22:06:59 fluxgen Exp $ 22// $Id: Resource.cc,v 1.2 2003/07/18 15:40:55 rathnor Exp $
23 23
24#include "Resource.hh"
25#include "XrmDatabaseHelper.hh" 24#include "XrmDatabaseHelper.hh"
25#include "Resource.hh"
26 26
27#include <iostream> 27#include <iostream>
28#include <cassert> 28#include <cassert>
@@ -31,22 +31,43 @@ using namespace std;
31 31
32namespace FbTk { 32namespace FbTk {
33 33
34ResourceManager::ResourceManager(const char *filename, bool lock_db) :
35 m_db_lock(0),
36 m_database(0),
37 m_filename(filename)
38{
39 ensureXrmIsInitialize();
40
41 if (lock_db)
42 lock();
43}
44
45ResourceManager::~ResourceManager() {
46 if (m_database)
47 delete m_database;
48}
49
34bool ResourceManager::m_init = false; 50bool ResourceManager::m_init = false;
35 51
36/** 52/**
37 loads a resourcefile 53 reloads all resources from resourcefile
38 @return true on success else false 54 @return true on success else false
39*/ 55*/
40bool ResourceManager::load(const char *filename) { 56bool ResourceManager::load(const char *filename) {
41 assert(filename); 57 m_filename = filename;
42 58
43 ensureXrmIsInitialize(); 59 // force reload (lock will ensure it exists)
44 60 if (m_database) {
45 XrmDatabaseHelper database; 61 delete m_database;
46 database = XrmGetFileDatabase(filename); 62 m_database = 0;
47 if (database==0) 63 }
64
65 lock();
66 if (!m_database) {
67 unlock();
48 return false; 68 return false;
49 69 }
70
50 XrmValue value; 71 XrmValue value;
51 char *value_type; 72 char *value_type;
52 73
@@ -56,7 +77,7 @@ bool ResourceManager::load(const char *filename) {
56 for (; i != i_end; ++i) { 77 for (; i != i_end; ++i) {
57 78
58 Resource_base *resource = *i; 79 Resource_base *resource = *i;
59 if (XrmGetResource(*database, resource->name().c_str(), 80 if (XrmGetResource(**m_database, resource->name().c_str(),
60 resource->altName().c_str(), &value_type, &value)) 81 resource->altName().c_str(), &value_type, &value))
61 resource->setFromString(value.addr); 82 resource->setFromString(value.addr);
62 else { 83 else {
@@ -66,6 +87,8 @@ bool ResourceManager::load(const char *filename) {
66 } 87 }
67 } 88 }
68 89
90 unlock();
91
69 return true; 92 return true;
70} 93}
71 94
@@ -75,9 +98,8 @@ bool ResourceManager::load(const char *filename) {
75*/ 98*/
76bool ResourceManager::save(const char *filename, const char *mergefilename) { 99bool ResourceManager::save(const char *filename, const char *mergefilename) {
77 assert(filename); 100 assert(filename);
78 101
79 ensureXrmIsInitialize(); 102 // empty database
80
81 XrmDatabaseHelper database; 103 XrmDatabaseHelper database;
82 104
83 string rc_string; 105 string rc_string;
@@ -92,20 +114,32 @@ bool ResourceManager::save(const char *filename, const char *mergefilename) {
92 114
93 if (database==0) 115 if (database==0)
94 return false; 116 return false;
95 117
96 //check if we want to merge a database 118 //check if we want to merge a database
97 if (mergefilename) { 119 if (mergefilename) {
98 XrmDatabaseHelper olddatabase(mergefilename); 120 // force reload of file
99 if (olddatabase == 0) // did we load the file? 121 m_filename = mergefilename;
122 if (m_database)
123 delete m_database;
124 m_database = 0;
125
126 lock();
127
128 if (!m_database) {
129 unlock();
100 return false; 130 return false;
101 131 }
102 XrmMergeDatabases(*database, &*olddatabase); // merge databases 132
103 XrmPutFileDatabase(*olddatabase, filename); // save database to file 133 XrmMergeDatabases(*database, &**m_database); // merge databases
104 134 XrmPutFileDatabase(**m_database, filename); // save database to file
105 *database = 0; // don't try to destroy the database 135
136 // don't try to destroy the database (XrmMergeDatabases destroys it)
137 *database = 0;
138 unlock();
106 } else // save database to file 139 } else // save database to file
107 XrmPutFileDatabase(*database, filename); 140 XrmPutFileDatabase(*database, filename);
108 141
142 m_filename = filename;
109 return true; 143 return true;
110} 144}
111 145
@@ -115,5 +149,30 @@ void ResourceManager::ensureXrmIsInitialize() {
115 m_init = true; 149 m_init = true;
116 } 150 }
117} 151}
118 152
153ResourceManager &ResourceManager::lock() {
154 ++m_db_lock;
155 // if the lock was zero, then load the database
156 if ((m_db_lock == 1 || !m_database) &&
157 m_filename != "") {
158 m_database = new XrmDatabaseHelper(m_filename.c_str());
159
160 // check that the database loaded ok
161 if (m_database && *m_database == 0) {
162 // didn't work
163 delete m_database;
164 m_database = 0;
165 }
166 }
167
168 return *this;
169}
170
171void ResourceManager::unlock() {
172 if (--m_db_lock == 0 && m_database) {
173 delete m_database;
174 m_database = 0;
175 }
176}
177
119}; // end namespace FbTk 178}; // end namespace FbTk