]> icculus.org git repositories - mikachu/openbox.git/blob - src/Resource.cc
need translations for Hide Toolbar
[mikachu/openbox.git] / src / Resource.cc
1 // Resource.cc for Openbox
2 // Copyright (c) 2002 - 2002 Ben Jansens (ben@orodu.net)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22 #include "Resource.h"
23 #include "Util.h"
24
25 #ifdef    HAVE_CONFIG_H
26 #  include "../config.h"
27 #endif // HAVE_CONFIG_H
28
29 #ifdef    HAVE_STDLIB_H
30 #  include <stdlib.h>
31 #endif // HAVE_STDLIB_H
32
33 #ifdef    HAVE_STDIO_H
34 #  include <stdio.h>
35 #endif // HAVE_STDIO_H
36
37 bool obResource::m_initialized = false;
38
39 obResource::obResource(const std::string &file) {
40   setFile(file);
41   m_modified = false;
42   m_database = NULL;
43   m_autosave = true;
44   if (!m_initialized) {
45     XrmInitialize();
46     m_initialized = true;
47   }
48 }
49
50 obResource::obResource() {
51   m_modified = false;
52   m_database = NULL;
53   m_autosave = true;
54   if (!m_initialized) {
55     XrmInitialize();
56     m_initialized = true;
57   }
58 }
59
60 obResource::~obResource() {
61   if (m_database != NULL)
62     XrmDestroyDatabase(m_database);
63 }
64
65 void obResource::setFile(const std::string &file) {
66   m_file = file;
67 }
68
69 void obResource::setAutoSave(bool autosave) {
70   m_autosave = autosave;
71 }
72
73 void obResource::save() {
74   ASSERT(m_database != NULL);
75   XrmPutFileDatabase(m_database, m_file.c_str());
76   m_modified = false;
77 }
78
79 bool obResource::load() {
80   if (m_database != NULL)
81     XrmDestroyDatabase(m_database);
82   m_modified = false;
83   if (NULL == (m_database = XrmGetFileDatabase(m_file.c_str())))
84     return false;
85   return true;
86 }
87
88 void obResource::setValue(const std::string &rname, bool value) {
89   ASSERT(m_database != NULL);
90
91   const char *val = (value ? "True" : "False");
92   std::string rc_string = rname + ": " + val;
93   XrmPutLineResource(&m_database, rc_string.c_str());
94
95   m_modified = true;
96   if (m_autosave)
97     save();
98 }
99
100 void obResource::setValue(const std::string &rname, int value) {
101   setValue(rname, (long)value);
102 }
103
104 void obResource::setValue(const std::string &rname, long value) {
105   ASSERT(m_database != NULL);
106   
107   char val[11];
108   sprintf(val, "%ld", value);
109   std::string rc_string = rname + ": " + val;
110   XrmPutLineResource(&m_database, rc_string.c_str());
111
112   m_modified = true;
113   if (m_autosave)
114     save();
115 }
116
117 void obResource::setValue(const std::string &rname, const char *value) {
118   ASSERT(m_database != NULL);
119   
120   std::string rc_string = rname + ": " + value;
121   XrmPutLineResource(&m_database, rc_string.c_str());
122
123   m_modified = true;
124   if (m_autosave)
125     save();
126 }
127
128 void obResource::setValue(const std::string &rname, const std::string &value) {
129   ASSERT(m_database != NULL);
130   
131   std::string rc_string = rname + ": " + value;
132   XrmPutLineResource(&m_database, rc_string.c_str());
133
134   m_modified = true;
135   if (m_autosave)
136     save();
137 }
138
139 bool obResource::getValue(const std::string &rname, const std::string &rclass,
140                           bool &value) const {
141   ASSERT(rclass.c_str() != NULL);
142   ASSERT(m_database != NULL);
143   
144   char *rettype;
145   XrmValue retvalue;
146   if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(), 
147                           &rettype, &retvalue) || retvalue.addr == NULL)
148     return false;
149   std::string val = retvalue.addr;
150   if (0 == strncasecmp(val.c_str(), "true", val.length()))
151     value = true;
152   else
153     value = false;
154   return true;
155 }
156
157 bool obResource::getValue(const std::string &rname, const std::string &rclass,
158                           long &value) const {
159   ASSERT(m_database != NULL);
160   
161   char *rettype;
162   XrmValue retvalue;
163   if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(), 
164                           &rettype, &retvalue) || retvalue.addr == NULL)
165     return false;
166   char *end;
167   value = strtol(retvalue.addr, &end, 10);
168   if (end == retvalue.addr)
169     return false;
170   return true;
171 }
172
173 bool obResource::getValue(const std::string &rname, const std::string &rclass,
174                           std::string &value) const {
175   ASSERT(m_database != NULL);
176   
177   char *rettype;
178   XrmValue retvalue;
179   if (0 == XrmGetResource(m_database, rname.c_str(), rclass.c_str(), 
180                           &rettype, &retvalue) || retvalue.addr == NULL)
181     return false;
182   value = retvalue.addr;
183   return true;
184 }