Exemplo n.º 1
0
int Configure::load(const string& xmlpath)
{
    if (m_doc.LoadFile(xmlpath.c_str()) != XML_NO_ERROR) {
        g_sysLog.e("{Configure} can't load xml %s\n", xmlpath.c_str());
        return ERR_LDCFG;
    }

    XMLElement* rootElement = m_doc.RootElement();
    if (rootElement == NULL) {
        g_sysLog.e("{Configure} can't get root element %s\n", xmlpath.c_str());
        return ERR_LDCFG;    
    } else {
        bool reset = rootElement->IntAttribute("reset");
        if (reset)
            return ERR_LDCFG;

        int ver = rootElement->IntAttribute("version");
        if (ver != CONF_VERSION)
            return ERR_LDCFG; // Recreate configure.xml
    }

    XMLElement* tempElement = rootElement->FirstChildElement("srclan");
    if (tempElement && tempElement->GetText()) {
        m_srcLan = tempElement->GetText();
    }

    tempElement = rootElement->FirstChildElement("detlan");
    if (tempElement && tempElement->GetText()) {
        m_detLan = tempElement->GetText();
    }

    // Loading dictionary 
    vector<string> dictFiles;
    scanDictDir(dictFiles);

    XMLElement* dictsElement = rootElement->FirstChildElement("dict");
    XMLElement* dictElement = dictsElement->FirstChildElement();
    while (dictElement) {
        struct DictNode dict;
        const char* textE;
        int pos;

        if (dictElement->FirstChildElement("path")) {
            if ((textE = dictElement->FirstChildElement("path")->GetText()) != NULL)
                dict.path = textE;
        }

        if ((pos = findDict(dict.path, dictFiles)) == -1 ) {
            dictsElement->DeleteChild(dictElement);
            dictElement = dictElement->NextSiblingElement();
            continue;
        }
        dictFiles[pos] = "";

        dict.en = dictElement->BoolAttribute("en");
        if (dictElement->Attribute("open"))
            dict.open   = string(dictElement->Attribute("open"));

        if (dictElement->FirstChildElement("srclan")) {
            if ((textE = dictElement->FirstChildElement("srclan")->GetText()) != NULL)
                dict.srclan = textE;
        }
        if (dictElement->FirstChildElement("detlan")) {
	    if ((textE = dictElement->FirstChildElement("detlan")->GetText()) != NULL)
                dict.detlan = textE;
        }

        if (dictElement->FirstChildElement("name")) {
            if ((textE = dictElement->FirstChildElement("name")->GetText()) != NULL)
                dict.name = textE;
        }

        if (dictElement->FirstChildElement("summary")) {
            if ((textE = dictElement->FirstChildElement("summary")->GetText()) != NULL)
	        dict.summary = textE;
        }
        m_dictNodes.push_back(dict);
        dictElement = dictElement->NextSiblingElement();
    }

    vector<string>::iterator iter = dictFiles.begin();
    for (; iter != dictFiles.end(); iter++) {
        if (*iter != "") {
            struct DictNode d;
            d.en = true;
            d.path = *iter;
            d.name = boost::filesystem::path(d.path).filename().string();
            //printf("%s\n",d.name.c_str());
            m_dictNodes.push_back(d);
        }
    }
    // Loading dictionary -- end

    tempElement = rootElement->FirstChildElement(XML_TAG_CWS);
    if (tempElement) {
        m_cws.bselection  = tempElement->BoolAttribute("selection");
        m_cws.bclipboard  = tempElement->BoolAttribute("clipboard");
        m_cws.bmouse      = tempElement->BoolAttribute("mouse");
        m_cws.benable     = tempElement->BoolAttribute("enable"); 
        m_cws.shortcutKey = tempElement->IntAttribute("shortcutkey");
        m_cws.autoCloseEn = tempElement->BoolAttribute("AutoCloseEnable");
        m_cws.autoCloseInv= tempElement->IntAttribute("AutoCloseInterval");
    } else {
        m_cws.benable    = false;
        m_cws.bselection = true;
        m_cws.bmouse     = false;
        m_cws.bclipboard = false;
        m_cws.shortcutKey = 'g'-'a';
        m_cws.autoCloseEn  = true;
        m_cws.autoCloseInv = 1000*10;

        XMLElement* e = m_doc.NewElement(XML_TAG_CWS);
        e->SetAttribute("selection",        m_cws.bselection);
        e->SetAttribute("clipboard",        m_cws.bclipboard);
        e->SetAttribute("mouse",            m_cws.bmouse);
        e->SetAttribute("enable",           m_cws.benable);
        e->SetAttribute("shortcutkey",      m_cws.shortcutKey);
        e->SetAttribute("AutoCloseEnable",  m_cws.autoCloseEn);
        e->SetAttribute("AutoCloseInterval",m_cws.autoCloseInv);

        rootElement->InsertEndChild(e);
    }

    tempElement = rootElement->FirstChildElement(XML_TAG_SETTING);
    if (tempElement) {
        m_setting.uilanID     = tempElement->IntAttribute("uilan");
        m_setting.fontsize    = tempElement->IntAttribute("fontsize");
        m_setting.font        = util::XMLUtil::Attribute(tempElement, "font", "");
        m_setting.bsystemTray = tempElement->BoolAttribute("systemtray");
    } else {
        m_setting.uilanID = UILAN_NONE;
        m_setting.fontsize = 10;
        m_setting.font = "";
        m_setting.bsystemTray = true;

        XMLElement* e = m_doc.NewElement(XML_TAG_SETTING);
        e->SetAttribute("uilan", m_setting.uilanID);
        e->SetAttribute("fontsize", m_setting.fontsize);

        rootElement->InsertEndChild(e);
    }

    m_doc.SaveFile(xmlpath.c_str());
    return 0;
}