| 1 | /* |
|---|
| 2 | * QuteCom, a voice over Internet phone |
|---|
| 3 | * Copyright (C) 2004-2010 Mbdsys |
|---|
| 4 | * |
|---|
| 5 | * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include <settings/SettingsXMLSerializer.h> |
|---|
| 21 | |
|---|
| 22 | #include <serializer/StringListXMLSerializer.h> |
|---|
| 23 | #include <settings/Settings.h> |
|---|
| 24 | |
|---|
| 25 | #include <util/String.h> |
|---|
| 26 | |
|---|
| 27 | #include <tinyxml.h> |
|---|
| 28 | |
|---|
| 29 | using namespace std; |
|---|
| 30 | |
|---|
| 31 | SettingsXMLSerializer::SettingsXMLSerializer(Settings & settings) |
|---|
| 32 | : _settings(settings) { |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | std::string SettingsXMLSerializer::serialize() { |
|---|
| 36 | string result; |
|---|
| 37 | |
|---|
| 38 | result += "<settings>\n"; |
|---|
| 39 | |
|---|
| 40 | for (Settings::Keys::const_iterator it = _settings._keyMap.begin(); it != _settings._keyMap.end(); ++it) { |
|---|
| 41 | string key = (*it).first; |
|---|
| 42 | const QVariant& keyValue = (*it).second; |
|---|
| 43 | string value; |
|---|
| 44 | |
|---|
| 45 | if (Settings::isString(keyValue)) { |
|---|
| 46 | value = "<string>" + keyValue.toString().toStdString() + "</string>"; |
|---|
| 47 | } else if (Settings::isStringList(keyValue)) { |
|---|
| 48 | StringList sl = Settings::toStringList(keyValue); |
|---|
| 49 | StringListXMLSerializer serializer(sl); |
|---|
| 50 | value = serializer.serialize(); |
|---|
| 51 | } else if (Settings::isBoolean(keyValue)) { |
|---|
| 52 | value = "<bool>" + String::fromBoolean(keyValue.toBool()) + "</bool>"; |
|---|
| 53 | } else if (Settings::isInteger(keyValue)) { |
|---|
| 54 | value = "<int>" + String::fromNumber(keyValue.toInt()) + "</int>"; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | result += ("<" + key + ">" + value + "</" + key + ">\n"); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | result += "</settings>\n"; |
|---|
| 61 | |
|---|
| 62 | return result; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | bool SettingsXMLSerializer::unserialize(const std::string & data) { |
|---|
| 66 | TiXmlBase::SetCondenseWhiteSpace(false); |
|---|
| 67 | |
|---|
| 68 | TiXmlDocument doc; |
|---|
| 69 | doc.Parse(data.c_str()); |
|---|
| 70 | |
|---|
| 71 | TiXmlHandle docHandle(& doc); |
|---|
| 72 | TiXmlNode * settings = docHandle.FirstChild("settings").Node(); |
|---|
| 73 | |
|---|
| 74 | if (settings == NULL) { |
|---|
| 75 | return false; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | TiXmlNode * lastChild = NULL; |
|---|
| 79 | while ((lastChild = settings->IterateChildren(lastChild))) { |
|---|
| 80 | string key = lastChild->Value(); |
|---|
| 81 | TiXmlNode * value = lastChild->FirstChild(); |
|---|
| 82 | if (value) { |
|---|
| 83 | string valueNode; |
|---|
| 84 | valueNode << *value; |
|---|
| 85 | string valueType = value->Value(); |
|---|
| 86 | TiXmlNode * valueDataNode = value->FirstChild(); |
|---|
| 87 | string valueData; |
|---|
| 88 | if (valueDataNode) { |
|---|
| 89 | valueData = valueDataNode->Value(); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | if (valueType == "stringlist") { |
|---|
| 93 | StringList list; |
|---|
| 94 | StringListXMLSerializer serializer(list); |
|---|
| 95 | serializer.unserialize(valueNode); |
|---|
| 96 | _settings.set(key, list); |
|---|
| 97 | } else if (valueType == "string") { |
|---|
| 98 | _settings.set(key, valueData); |
|---|
| 99 | } else if (valueType == "bool") { |
|---|
| 100 | _settings.set(key, String(valueData).toBoolean()); |
|---|
| 101 | } else if (valueType == "int") { |
|---|
| 102 | _settings.set(key, String(valueData).toInteger()); |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | return true; |
|---|
| 108 | } |
|---|