/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                               DEMO PROGRAMS SET
                          registry.cpp  -  description
                             -------------------
    begin                : January 3, 2003
    copyright            : (C) 2003-2006 by Alexey S.Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef __BORLANDC__
#include <vcl.h>
#pragma hdrstop
#endif

#include <sptk3/CRegistry.h>
#include <sptk3/CException.h>

#include <iostream>

using namespace std;
using namespace sptk;

void printRegistry(CRegistryMode mode) {
   CStrings strings;
   // Open user settings, file is located in user home directory
	CRegistry   mySettings("mySettings.ini","sptk_test",mode);
	try {
		mySettings.load();
		cout << "---> Reading " << mySettings.fileName() << endl;

		CXmlNode* windowNode = mySettings.findFirst("window");
		if (windowNode) {
			CXmlNode::iterator itor = windowNode->begin();
			// Processing the subnodes of <window> node
			for (; itor != windowNode->end(); itor++) {
				CXmlNode*        node = *itor;
				CXmlAttributes&  attr = node->attributes;
				if (node->name() == "position")
					cout << "Window position: " << attr["x"] << ":" << attr["y"] << endl;
				else if (node->name() == "colors") {
					// Processing the subnodes of <colors>
					CXmlNode::iterator itor = node->begin();
					cout << "Window colors:" << endl;
					for (; itor != node->end(); itor++) {
						CXmlNode* colorNode = *itor;
						CXmlAttributes&  attr = colorNode->attributes;
						cout << "  " << attr["name"] 
						<< ": fg " << attr["foreground"] 
						<< ", bg " << attr["background"] << endl;
					}
				}
			}
		} else {
			cout << "The registry doesn't contain window information" << endl; 
		}
	}
	catch (exception& e) { puts(e.what()); }
}

void updateRegistry(CRegistryMode mode) {
   // Open user settings, file is located in user home directory
	CRegistry   mySettings("mySettings.ini","sptk_test",mode);
	try {
		cout << "<--- Updating " << mySettings.fileName() << endl;

		CXmlNode* windowNode = mySettings.findOrCreate("window");
		windowNode->clear();

		CXmlNode* positionNode = new CXmlElement(*windowNode,"position");
		positionNode->attributes["x"] = 100;
		positionNode->attributes["y"] = 150;

		CXmlNode* colorsNode = new CXmlElement(*windowNode,"colors");
		CXmlNode* colorNode;

		colorNode = new CXmlElement(*colorsNode,"color");
		colorNode->attributes["name"] = "Header";
		colorNode->attributes["foreground"] = "WHITE";
		colorNode->attributes["background"] = "BLUE";

		colorNode = new CXmlElement(*colorsNode,"color");
		colorNode->attributes["name"] = "Text";
		colorNode->attributes["foreground"] = "0x000000";
		colorNode->attributes["background"] = "0xFF80FF";

      mySettings.save();
	}
	catch (exception& e) {
		puts(e.what());
	}
}

int main() {
	cout << "-------- Test for the USER (stored in homedir)  registry. -----------" << endl;

   // Print the original registry
   // The registry file is located in the user home directory
	printRegistry(USER_REGISTRY);

   // Define the registry values and save the registry 
	updateRegistry(USER_REGISTRY);

   // Print the changed registry
	printRegistry(USER_REGISTRY);

	return 0;
}
//---------------------------------------------------------------------------
