/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "IMXMLUtil.hh"

bool
IMXMLModifier::dumpconf(string &file)
{
    xmlDocPtr doc = NULL;
//    xmlDtdPtr dtd;
    xmlNodePtr root, les, le, key, node;
    xmlAttrPtr attr;
    FILE *fp;
    bool retval = false;
    IMModuleInfoVec *levec = get_leinfo();
    IMLEHotkeyInfoVec *hotkeyvec = get_hotkeyinfo();
    IMKeyParser keyparser("");
    string s;

    doc = xmlNewDoc((xmlChar *)"1.0");
    doc->encoding = xmlStrdup((xmlChar *)"UTF-8");
//    dtd = xmlCreateIntSubset(doc, (xmlChar *)"iiimf", NULL, (xmlChar *)"le.dtd");
    root = xmlNewDocNode(doc, NULL, (xmlChar *)"iiimf", NULL);
    xmlDocSetRootElement(doc, root);
    les = xmlNewTextChild(root, NULL, (xmlChar *)"LanguageEngines", NULL);

    for (IMModuleInfoVec::iterator i = levec->begin(); i != levec->end(); i++) {
	if (!i->modules.empty()) {
	    le = xmlNewTextChild(les, NULL, (xmlChar *)"LanguageEngine", NULL);
	    attr = xmlNewProp(le, (xmlChar *)"lang", (xmlChar *)i->lang.c_str());
	    for (ModuleInfoVec::iterator j = i->modules.begin(); j != i->modules.end(); j++) {
		node = xmlNewTextChild(le, NULL, (xmlChar *)"module", NULL);
		attr = xmlNewProp(node, (xmlChar *)"path", (xmlChar *)j->c_str());
		for (IMLEHotkeyInfoVec::iterator k = hotkeyvec->begin(); k != hotkeyvec->end(); k++) {
		    if (k->modname == *j) {
			for (IMKeySpecList::iterator l = k->hotkeys.begin(); l != k->hotkeys.end(); l++) {
			    int pos;
			    string mtoken, mods, hk;

			    s = keyparser.generate_key(*l);
			    for (int m = 0; m < s.size(); m++) {
				if (s[m] == '<') {
				    pos = s.find('>', m + 1);
				    if (pos > m) {
					mtoken = s.substr(m + 1, pos - m - 1);
					if (!mods.empty())
					    mods += string(",");
					mods += mtoken;
					m = pos;
				    }
				} else {
				    hk.assign(&s[m], s.size() - m);
				    break;
				}
			    }
			    key = xmlNewTextChild(node, NULL, (xmlChar *)"key", (xmlChar *)hk.c_str());
			    if (!mods.empty())
				attr = xmlNewProp(key, (xmlChar *)"modifier", (xmlChar *)mods.c_str());
			}
		    }
		}
	    }
	}
    }
    if (xmlSaveFile(file.c_str(), doc) > 0)
	retval = true;

    if (doc != NULL)
	xmlFreeDoc(doc);

    return true;
}

#if 0
int
main (int argc, char **argv)
{
    IMXMLModifier xml((const char *)argv[1]);
    IMModuleInfoVec *lev;
    string mod("/usr/lib/im/leif/freewnnle.so");
    string lang("ja");
    string out("foo.conf");

    xml.parse();
    lev = xml.get_leinfo();
    for (IMModuleInfoVec::iterator i = lev->begin(); i != lev->end(); i++) {
	printf ("%s:\n", i->lang.c_str());
	for (ModuleInfoVec::iterator j = i->modules.begin(); j != i->modules.end(); j++) {
	    printf ("- %s\n", j->c_str());
	}
    }
    printf("\n\n*** prepend\n");
    xml.prependmodule(mod, lang);
    /*
      for (IMModuleInfoVec::iterator i = lev->begin (); i != lev->end (); i++) {
      printf ("%s:\n", i->lang.c_str ());
      for (ModuleInfoVec::iterator j = i->modules.begin (); j != i->modules.end (); j++) {
      printf ("- %s\n", j->c_str ());
      }
      }
    */
    printf("\n\n*** remove\n");
    mod = "/usr/lib/im/leif/CannaLE.so";
    xml.removemodule(mod, lang);
    /*
      for (IMModuleInfoVec::iterator i = lev->begin (); i != lev->end (); i++) {
      printf ("%s:\n", i->lang.c_str ());
      for (ModuleInfoVec::iterator j = i->modules.begin (); j != i->modules.end (); j++) {
      printf ("- %s\n", j->c_str ());
      }
      }
    */
    printf("\n");

    xml.dumpconf(out);

    return 0;
}
#endif
