2007-09-18 18:06:09 +04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Evoke, head honcho of everything
|
|
|
|
* Part of Equinox Desktop Environment (EDE).
|
|
|
|
* Copyright (c) 2000-2007 EDE Authors.
|
|
|
|
*
|
|
|
|
* This program is licensed under terms of the
|
|
|
|
* GNU General Public License version 2 or newer.
|
|
|
|
* See COPYING for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Xsm.h"
|
|
|
|
#include <edelib/Debug.h>
|
2007-10-26 16:01:01 +04:00
|
|
|
#include <edelib/TiXml.h>
|
|
|
|
#include <edelib/File.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
2007-09-18 18:06:09 +04:00
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
int ignore_xerrors(Display* display, XErrorEvent* xev) {
|
|
|
|
return True;
|
2007-09-18 18:06:09 +04:00
|
|
|
}
|
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
Xsm::Xsm() { }
|
2007-09-18 18:06:09 +04:00
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
Xsm::~Xsm() {
|
|
|
|
EDEBUG("Xsm::~Xsm()\n");
|
2007-09-18 18:06:09 +04:00
|
|
|
}
|
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
bool Xsm::load_serialized(const char* file) {
|
|
|
|
TiXmlDocument doc(file);
|
|
|
|
if(!doc.LoadFile())
|
|
|
|
return false;
|
2007-09-19 18:12:34 +04:00
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
const char* name = NULL, *type = NULL;
|
|
|
|
const char* v_string = NULL;
|
|
|
|
int v_int = 0;
|
|
|
|
int v_red = 0, v_green = 0, v_blue = 0, v_alpha = 0;
|
|
|
|
int cmp = 0;
|
2007-09-19 18:12:34 +04:00
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
TiXmlNode* elem = doc.FirstChild("ede-settings");
|
|
|
|
if(!elem)
|
2007-09-19 18:12:34 +04:00
|
|
|
return false;
|
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
for(elem = elem->FirstChildElement(); elem; elem = elem->NextSibling()) {
|
|
|
|
if(strcmp(elem->Value(), "setting") != 0) {
|
|
|
|
EWARNING(ESTRLOC ": Got unknown child in 'ede-setting' %s\n", elem->Value());
|
|
|
|
continue;
|
2007-09-19 18:12:34 +04:00
|
|
|
}
|
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
name = elem->ToElement()->Attribute("name");
|
|
|
|
if(!name) {
|
|
|
|
EWARNING(ESTRLOC ": Missing name key\n");
|
|
|
|
continue;
|
|
|
|
}
|
2007-09-19 18:12:34 +04:00
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
type = elem->ToElement()->Attribute("type");
|
|
|
|
if(!type) {
|
|
|
|
EWARNING(ESTRLOC ": Missing type key\n");
|
|
|
|
continue;
|
|
|
|
}
|
2007-09-19 18:12:34 +04:00
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
if(strcmp(type, "int") == 0)
|
|
|
|
cmp = 1;
|
|
|
|
else if(strcmp(type, "string") == 0)
|
|
|
|
cmp = 2;
|
|
|
|
else if(strcmp(type, "color") == 0)
|
|
|
|
cmp = 3;
|
|
|
|
else {
|
|
|
|
EWARNING(ESTRLOC ": Unknown type %s\n", type);
|
|
|
|
continue;
|
2007-09-19 18:12:34 +04:00
|
|
|
}
|
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
switch(cmp) {
|
|
|
|
case 1:
|
|
|
|
if(elem->ToElement()->QueryIntAttribute("value", &v_int) == TIXML_SUCCESS)
|
|
|
|
set(name, v_int);
|
|
|
|
else
|
|
|
|
EWARNING(ESTRLOC ": Unable to query integer value\n");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
v_string = elem->ToElement()->Attribute("value");
|
|
|
|
if(v_string)
|
|
|
|
set(name, v_string);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if((elem->ToElement()->QueryIntAttribute("red", &v_red) == TIXML_SUCCESS) &&
|
|
|
|
(elem->ToElement()->QueryIntAttribute("green", &v_green) == TIXML_SUCCESS) &&
|
|
|
|
(elem->ToElement()->QueryIntAttribute("blue", &v_blue) == TIXML_SUCCESS) &&
|
|
|
|
(elem->ToElement()->QueryIntAttribute("alpha", &v_alpha) == TIXML_SUCCESS)) {
|
|
|
|
set(name, v_red, v_green, v_blue, v_alpha);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2007-09-18 18:06:09 +04:00
|
|
|
}
|
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
return true;
|
2007-09-18 18:06:09 +04:00
|
|
|
}
|
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
bool Xsm::save_serialized(const char* file) {
|
|
|
|
// FIXME: a lot of this code could be in edelib
|
|
|
|
Atom type;
|
|
|
|
int format;
|
|
|
|
unsigned long n_items, bytes_after;
|
|
|
|
unsigned char* data;
|
|
|
|
int result;
|
|
|
|
edelib::XSettingsList* settings = NULL, *iter = NULL;
|
2007-09-18 18:06:09 +04:00
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
int (*old_handler)(Display*, XErrorEvent*);
|
2007-09-18 18:06:09 +04:00
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
// possible ?
|
|
|
|
if(!manager_data->manager_win)
|
|
|
|
return false;
|
2007-09-18 18:06:09 +04:00
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
old_handler = XSetErrorHandler(ignore_xerrors);
|
|
|
|
result = XGetWindowProperty(manager_data->display, manager_data->manager_win, manager_data->xsettings_atom,
|
|
|
|
0, LONG_MAX, False, manager_data->xsettings_atom,
|
|
|
|
&type, &format, &n_items, &bytes_after, (unsigned char**)&data);
|
|
|
|
|
|
|
|
XSetErrorHandler(old_handler);
|
|
|
|
if(result == Success && type != None) {
|
|
|
|
if(type != manager_data->xsettings_atom)
|
|
|
|
EWARNING(ESTRLOC ": Invalid type for XSETTINGS property\n");
|
|
|
|
else if(format != 8)
|
|
|
|
EWARNING(ESTRLOC ": Invalid format for XSETTINGS property\n");
|
|
|
|
else
|
|
|
|
settings = edelib::xsettings_decode(data, n_items, NULL);
|
|
|
|
XFree(data);
|
2007-09-18 18:06:09 +04:00
|
|
|
}
|
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
if(!settings)
|
|
|
|
return false;
|
2007-09-18 18:06:09 +04:00
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
edelib::File setting_file;
|
|
|
|
if(!setting_file.open(file, edelib::FIO_WRITE)) {
|
|
|
|
EWARNING(ESTRLOC ": Unable to write to %s\n", file);
|
|
|
|
edelib::xsettings_list_free(settings);
|
|
|
|
return false;
|
2007-09-18 18:06:09 +04:00
|
|
|
}
|
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
setting_file.printf("<? xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
|
|
|
|
setting_file.printf("<ede-settings>\n");
|
2007-09-19 18:12:34 +04:00
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
iter = settings;
|
|
|
|
while(iter) {
|
|
|
|
setting_file.printf("\t<setting name=\"%s\" ", iter->setting->name);
|
|
|
|
switch(iter->setting->type) {
|
|
|
|
case edelib::XSETTINGS_TYPE_INT:
|
|
|
|
setting_file.printf("type=\"int\" value=\"%i\" />\n", iter->setting->data.v_int);
|
|
|
|
break;
|
|
|
|
case edelib::XSETTINGS_TYPE_STRING:
|
|
|
|
setting_file.printf("type=\"string\" value=\"%s\" />\n", iter->setting->data.v_string);
|
|
|
|
break;
|
|
|
|
case edelib::XSETTINGS_TYPE_COLOR:
|
|
|
|
setting_file.printf("type=\"color\" red=\"%i\" green=\"%i\" blue=\"%i\" alpha=\"%i\" />\n",
|
|
|
|
iter->setting->data.v_color.red,
|
|
|
|
iter->setting->data.v_color.green,
|
|
|
|
iter->setting->data.v_color.blue,
|
|
|
|
iter->setting->data.v_color.alpha);
|
2007-09-19 18:12:34 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
iter = iter->next;
|
2007-09-19 18:12:34 +04:00
|
|
|
}
|
2007-10-26 16:01:01 +04:00
|
|
|
setting_file.printf("</ede-settings>\n");
|
2007-09-19 18:12:34 +04:00
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
setting_file.close();
|
|
|
|
edelib::xsettings_list_free(settings);
|
2007-12-03 16:39:22 +03:00
|
|
|
|
2007-10-26 16:01:01 +04:00
|
|
|
return true;
|
2007-09-19 18:12:34 +04:00
|
|
|
}
|