#include "acidlaunch.h"

/* convert a text boolean value into a numeric value */
static bool stringToBool(char* text)
{
	bool result = false;

	if(!strcmp("true", text)) result = true;
	if(!strcmp("yes", text)) result = true;
	if(!strcmp("1", text)) result = true;

	return result;
}

/* parse the node from the XML file */
void ConfigNode::parseNodes(xmlNodePtr node)
{
	node = node->xmlChildrenNode;

	while(node)
	{
		handleNode(node);
		node = node->next;
	}
}

void ConfigNode::handleNode(xmlNodePtr child)
{
	cerr << "WARNING: unhandled node:\n";
	cerr << "  name: " << child->name << " value: " << xmlNodeGetContent(child) << endl;
}

ConfigNode::ConfigNode(xmlNodePtr node)
{}

AppIconNode::AppIconNode(xmlNodePtr node)
: ConfigNode(node)
{}

void AppIconNode::handleNode(xmlNodePtr child)
{
	xmlChar* value = xmlNodeGetContent(child);
	if(!xmlStrcmp(child->name, (const xmlChar*)"name"))
	{
		setName(value);
	} else if(!xmlStrcmp(child->name, (const xmlChar*)"icon")) {
		setIcon(value);
	} else if(!xmlStrcmp(child->name, (const xmlChar*)"command")) {
		setCommand(value);
	}
	free(value);
}

LauncherTabNode::LauncherTabNode(xmlNodePtr node)
: ConfigNode(node)
{}

void LauncherTabNode::handleNode(xmlNodePtr child)
{
	xmlChar* value = xmlNodeGetContent(child);

	if(!xmlStrcmp(child->name, (const xmlChar*)"appicon"))
	{
		AppIconNode* app = new AppIconNode(child);
		app->parseNodes(child);
		append(app);
	} else if(!xmlStrcmp(child->name, (const xmlChar*)"title")) {
		setTitle(value);
	}

	free(value);
}

LauncherNode::LauncherNode(xmlNodePtr node)
: _vertical(false), _xsize(25), _ysize(25), _xpos(-1), _ypos(-1), _width(1), _withdrawn(false), 
	_bgcolor(0x6060a0), ConfigNode(node)
{}

void LauncherNode::handleNode(xmlNodePtr child)
{
	xmlChar* value = xmlNodeGetContent(child);
	
	if(!xmlStrcmp(child->name, (const xmlChar*)"tab"))
	{
		LauncherTabNode* tab = new LauncherTabNode(child);
		tab->parseNodes(child);
		append(tab);
	} else if(!xmlStrcmp(child->name, (const xmlChar*)"orientation")) {
		if(!xmlStrcmp(value, (const xmlChar*)"vertical"))
		{
			setIsVertical(true);
		} else {
			setIsVertical(false);
		}
	} else if(!xmlStrcmp(child->name, (const xmlChar*)"withdrawn")) {
		if(stringToBool((char*)value))
		{
			setIsWithdrawn(true);
		} else {
			setIsWithdrawn(false);
		}
	} else if(!xmlStrcmp(child->name, (const xmlChar*)"xsize")) {
		setxSize(atoi((char*)value));
	} else if(!xmlStrcmp(child->name, (const xmlChar*)"ysize")) {
		setySize(atoi((char*)value));
	} else if(!xmlStrcmp(child->name, (const xmlChar*)"width")) {
		setWidth(atoi((char*)value));
	} else if(!xmlStrcmp(child->name, (const xmlChar*)"bgcolor")
			|| !xmlStrcmp(child->name, (const xmlChar*)"bgcolour")) {
		setBgColor(strtoul((const char*)value, NULL, 0));
	}	else if(!xmlStrcmp(child->name, (const xmlChar*)"xpos")) {
		setxPos(atoi((char*)value));
	} else if(!xmlStrcmp(child->name, (const xmlChar*)"ypos")) {
		setyPos(atoi((char*)value));
	}

	free(value);
}

AcidLaunchNode::AcidLaunchNode(xmlNodePtr node)
: ConfigNode(node)
{}

void AcidLaunchNode::handleNode(xmlNodePtr child)
{
	if(!xmlStrcmp(child->name, (const xmlChar*)"launcher"))
	{
		LauncherNode* launcher = new LauncherNode(child);
		launcher->parseNodes(child);
		append(launcher);
	}
}

ConfigNode::~ConfigNode()
{
	list<ConfigNode*>::const_iterator iter;

	for(iter=children().begin(); iter!=children().end(); iter++)
	{
		delete (*iter);
	}
}

void ConfigNode::dumpChildren()
{
	list<ConfigNode*>::const_iterator iter;

	for(iter=children().begin(); iter!=children().end(); iter++)
	{
		(*iter)->dump();
		(*iter)->dumpChildren();
	}
}



syntax highlighted by Code2HTML, v. 0.9.1