2002-05-26 20:25:38 +00:00
|
|
|
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
|
|
|
|
// Configuration.hh for Blackbox - an X11 Window manager
|
|
|
|
// Copyright (c) 2002 - 2002 Ben Jansens (ben@orodu.net)
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2002-07-19 22:22:19 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2002-05-26 20:25:38 +00:00
|
|
|
#include "../config.h"
|
2002-07-19 22:22:19 +00:00
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#ifdef HAVE_STDLIB_H
|
|
|
|
# include <stdlib.h>
|
|
|
|
#endif // HAVE_STDLIB_H
|
|
|
|
}
|
2002-05-26 20:25:38 +00:00
|
|
|
|
|
|
|
#include "Configuration.hh"
|
|
|
|
#include "Util.hh"
|
|
|
|
|
2002-05-28 14:32:42 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2002-05-26 20:25:38 +00:00
|
|
|
using std::string;
|
|
|
|
|
2002-07-19 22:22:19 +00:00
|
|
|
bool Configuration::_initialized = False;
|
2002-05-26 20:25:38 +00:00
|
|
|
|
2002-07-07 21:16:35 +00:00
|
|
|
Configuration::Configuration(const string &file, bool autosave) {
|
2002-05-26 20:25:38 +00:00
|
|
|
setFile(file);
|
2002-07-19 22:22:19 +00:00
|
|
|
_modified = False;
|
|
|
|
_database = NULL;
|
|
|
|
_autosave = autosave;
|
|
|
|
if (! _initialized) {
|
2002-05-26 20:25:38 +00:00
|
|
|
XrmInitialize();
|
2002-07-19 22:22:19 +00:00
|
|
|
_initialized = True;
|
2002-05-26 20:25:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-07 21:16:35 +00:00
|
|
|
Configuration::Configuration(bool autosave) {
|
2002-07-19 22:22:19 +00:00
|
|
|
_modified = False;
|
|
|
|
_database = NULL;
|
|
|
|
_autosave = autosave;
|
|
|
|
if (! _initialized) {
|
2002-05-26 20:25:38 +00:00
|
|
|
XrmInitialize();
|
2002-07-19 22:22:19 +00:00
|
|
|
_initialized = True;
|
2002-05-26 20:25:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Configuration::~Configuration() {
|
2002-07-19 22:22:19 +00:00
|
|
|
if (_database != NULL)
|
|
|
|
XrmDestroyDatabase(_database);
|
2002-05-26 20:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Configuration::setFile(const string &file) {
|
2002-07-19 22:22:19 +00:00
|
|
|
_file = file;
|
2002-05-26 20:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Configuration::setAutoSave(bool autosave) {
|
2002-07-19 22:22:19 +00:00
|
|
|
_autosave = autosave;
|
2002-05-26 20:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Configuration::save() {
|
2002-07-19 22:22:19 +00:00
|
|
|
assert(_database != NULL);
|
|
|
|
XrmPutFileDatabase(_database, _file.c_str());
|
|
|
|
_modified = False;
|
2002-05-26 20:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Configuration::load() {
|
2002-07-19 22:22:19 +00:00
|
|
|
if (_database != NULL)
|
|
|
|
XrmDestroyDatabase(_database);
|
|
|
|
_modified = False;
|
|
|
|
if (NULL == (_database = XrmGetFileDatabase(_file.c_str())))
|
2002-07-05 22:09:40 +00:00
|
|
|
return False;
|
|
|
|
return True;
|
2002-05-26 20:25:38 +00:00
|
|
|
}
|
|
|
|
|
2002-07-07 21:16:35 +00:00
|
|
|
bool Configuration::merge(const string &file, bool overwrite) {
|
2002-07-19 22:22:19 +00:00
|
|
|
if (XrmCombineFileDatabase(file.c_str(), &_database, overwrite) == 0)
|
2002-07-07 21:16:35 +00:00
|
|
|
return False;
|
2002-07-19 22:22:19 +00:00
|
|
|
_modified = True;
|
|
|
|
if (_autosave)
|
2002-07-07 21:16:35 +00:00
|
|
|
save();
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
2002-05-26 20:25:38 +00:00
|
|
|
void Configuration::create() {
|
2002-07-19 22:22:19 +00:00
|
|
|
if (_database != NULL)
|
|
|
|
XrmDestroyDatabase(_database);
|
|
|
|
_modified = False;
|
|
|
|
assert(NULL != (_database = XrmGetStringDatabase("")));
|
2002-05-26 20:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Configuration::setValue(const string &rname, bool value) {
|
2002-07-19 22:22:19 +00:00
|
|
|
assert(_database != NULL);
|
2002-05-26 20:25:38 +00:00
|
|
|
|
|
|
|
const char *val = (value ? "True" : "False");
|
|
|
|
string rc_string = rname + ": " + val;
|
2002-07-19 22:22:19 +00:00
|
|
|
XrmPutLineResource(&_database, rc_string.c_str());
|
2002-05-26 20:25:38 +00:00
|
|
|
|
2002-07-19 22:22:19 +00:00
|
|
|
_modified = True;
|
|
|
|
if (_autosave)
|
2002-05-26 20:25:38 +00:00
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Configuration::setValue(const string &rname, unsigned long value) {
|
2002-07-19 22:22:19 +00:00
|
|
|
assert(_database != NULL);
|
2002-05-26 20:25:38 +00:00
|
|
|
|
|
|
|
string rc_string = rname + ": " + itostring(value);
|
2002-07-19 22:22:19 +00:00
|
|
|
XrmPutLineResource(&_database, rc_string.c_str());
|
2002-05-26 20:25:38 +00:00
|
|
|
|
2002-07-19 22:22:19 +00:00
|
|
|
_modified = True;
|
|
|
|
if (_autosave)
|
2002-05-26 20:25:38 +00:00
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Configuration::setValue(const string &rname, long value) {
|
2002-07-19 22:22:19 +00:00
|
|
|
assert(_database != NULL);
|
2002-05-26 20:25:38 +00:00
|
|
|
|
|
|
|
string rc_string = rname + ": " + itostring(value);
|
2002-07-19 22:22:19 +00:00
|
|
|
XrmPutLineResource(&_database, rc_string.c_str());
|
2002-05-26 20:25:38 +00:00
|
|
|
|
2002-07-19 22:22:19 +00:00
|
|
|
_modified = True;
|
|
|
|
if (_autosave)
|
2002-05-26 20:25:38 +00:00
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Configuration::setValue(const string &rname, const char *value) {
|
2002-07-19 22:22:19 +00:00
|
|
|
assert(_database != NULL);
|
2002-05-26 20:25:38 +00:00
|
|
|
assert(value != NULL);
|
|
|
|
|
|
|
|
string rc_string = rname + ": " + value;
|
2002-07-19 22:22:19 +00:00
|
|
|
XrmPutLineResource(&_database, rc_string.c_str());
|
2002-05-26 20:25:38 +00:00
|
|
|
|
2002-07-19 22:22:19 +00:00
|
|
|
_modified = True;
|
|
|
|
if (_autosave)
|
2002-05-26 20:25:38 +00:00
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Configuration::setValue(const string &rname, const string &value) {
|
2002-07-19 22:22:19 +00:00
|
|
|
assert(_database != NULL);
|
2002-05-26 20:25:38 +00:00
|
|
|
|
|
|
|
string rc_string = rname + ": " + value;
|
2002-07-19 22:22:19 +00:00
|
|
|
XrmPutLineResource(&_database, rc_string.c_str());
|
2002-05-26 20:25:38 +00:00
|
|
|
|
2002-07-19 22:22:19 +00:00
|
|
|
_modified = True;
|
|
|
|
if (_autosave)
|
2002-05-26 20:25:38 +00:00
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Configuration::getValue(const string &rname, bool &value) const {
|
2002-07-19 22:22:19 +00:00
|
|
|
assert(_database != NULL);
|
2002-05-26 20:25:38 +00:00
|
|
|
|
|
|
|
string rclass = createClassName(rname);
|
|
|
|
|
|
|
|
char *rettype;
|
|
|
|
XrmValue retvalue;
|
2002-07-19 22:22:19 +00:00
|
|
|
if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
|
2002-05-26 20:25:38 +00:00
|
|
|
&rettype, &retvalue) || retvalue.addr == NULL)
|
2002-07-05 22:09:40 +00:00
|
|
|
return False;
|
2002-05-26 20:25:38 +00:00
|
|
|
string val = retvalue.addr;
|
2002-07-05 22:09:40 +00:00
|
|
|
if (val == "True" || val == "True")
|
|
|
|
value = True;
|
2002-05-26 20:25:38 +00:00
|
|
|
else
|
2002-07-05 22:09:40 +00:00
|
|
|
value = False;
|
|
|
|
return True;
|
2002-05-26 20:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Configuration::getValue(const string &rname, long &value) const {
|
2002-07-19 22:22:19 +00:00
|
|
|
assert(_database != NULL);
|
2002-05-26 20:25:38 +00:00
|
|
|
|
|
|
|
string rclass = createClassName(rname);
|
|
|
|
|
|
|
|
char *rettype;
|
|
|
|
XrmValue retvalue;
|
2002-07-19 22:22:19 +00:00
|
|
|
if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
|
2002-05-26 20:25:38 +00:00
|
|
|
&rettype, &retvalue) || retvalue.addr == NULL)
|
2002-07-05 22:09:40 +00:00
|
|
|
return False;
|
2002-05-26 20:25:38 +00:00
|
|
|
char *end;
|
|
|
|
value = strtol(retvalue.addr, &end, 10);
|
|
|
|
if (end == retvalue.addr)
|
2002-07-05 22:09:40 +00:00
|
|
|
return False;
|
|
|
|
return True;
|
2002-05-26 20:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Configuration::getValue(const string &rname, unsigned long &value) const {
|
2002-07-19 22:22:19 +00:00
|
|
|
assert(_database != NULL);
|
2002-05-26 20:25:38 +00:00
|
|
|
|
|
|
|
string rclass = createClassName(rname);
|
|
|
|
|
|
|
|
char *rettype;
|
|
|
|
XrmValue retvalue;
|
2002-07-19 22:22:19 +00:00
|
|
|
if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
|
2002-05-26 20:25:38 +00:00
|
|
|
&rettype, &retvalue) || retvalue.addr == NULL)
|
2002-07-05 22:09:40 +00:00
|
|
|
return False;
|
2002-05-26 20:25:38 +00:00
|
|
|
char *end;
|
|
|
|
value = strtoul(retvalue.addr, &end, 10);
|
|
|
|
if (end == retvalue.addr)
|
2002-07-05 22:09:40 +00:00
|
|
|
return False;
|
|
|
|
return True;
|
2002-05-26 20:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Configuration::getValue(const string &rname,
|
|
|
|
string &value) const {
|
2002-07-19 22:22:19 +00:00
|
|
|
assert(_database != NULL);
|
2002-05-26 20:25:38 +00:00
|
|
|
|
|
|
|
string rclass = createClassName(rname);
|
|
|
|
|
|
|
|
char *rettype;
|
|
|
|
XrmValue retvalue;
|
2002-07-19 22:22:19 +00:00
|
|
|
if (0 == XrmGetResource(_database, rname.c_str(), rclass.c_str(),
|
2002-05-26 20:25:38 +00:00
|
|
|
&rettype, &retvalue) || retvalue.addr == NULL)
|
2002-07-05 22:09:40 +00:00
|
|
|
return False;
|
2002-05-26 20:25:38 +00:00
|
|
|
value = retvalue.addr;
|
2002-07-05 22:09:40 +00:00
|
|
|
return True;
|
2002-05-26 20:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string Configuration::createClassName(const string &rname) const {
|
|
|
|
string rclass(rname);
|
|
|
|
|
|
|
|
string::iterator it = rclass.begin(), end = rclass.end();
|
2002-07-05 22:09:40 +00:00
|
|
|
while (True) {
|
2002-05-26 20:25:38 +00:00
|
|
|
*it = toUpper(*it);
|
|
|
|
++it;
|
|
|
|
if (it == end) break;
|
|
|
|
it = std::find(it, rclass.end(), '.');
|
|
|
|
if (it == end) break;
|
|
|
|
++it;
|
|
|
|
if (it == end) break;
|
|
|
|
}
|
|
|
|
return rclass;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char Configuration::toUpper(char c) const {
|
|
|
|
if (c >= 'a' && c <= 'z')
|
|
|
|
return c - 'a' + 'A';
|
|
|
|
return c;
|
|
|
|
}
|