summaryrefslogtreecommitdiffstats
blob: 6ad317095caf88d123b603647f73300760b7237c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Copyright (c) 2012 Kevin Smith
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <Swift/Controllers/Settings/XMLSettingsProvider.h>

#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>

#include <Swiften/Parser/PlatformXMLParserFactory.h>
#include <Swiften/Parser/XMLParser.h>
#include <Swiften/Base/Log.h>

namespace Swift {

XMLSettingsProvider::XMLSettingsProvider(const std::string& xmlConfig) : level_(0) {
	if (!xmlConfig.empty()) {
		PlatformXMLParserFactory factory;
		XMLParser* parser = factory.createXMLParser(this);
		if (parser->parse(xmlConfig)) {
			SWIFT_LOG(debug) << "Found and parsed system config" << std::endl;
		}
		else {
			SWIFT_LOG(debug) << "Found invalid system config" << std::endl;
		}
		delete parser;
	}
	else {
		SWIFT_LOG(debug) << "No system config found" << std::endl;
	}
}

XMLSettingsProvider::~XMLSettingsProvider() {

}

std::string XMLSettingsProvider::getSetting(const Setting<std::string>& setting) {
	if (values_.find(setting.getKey()) != values_.end()) {
		std::string value = values_[setting.getKey()];
		return value;
	}
	return setting.getDefaultValue();
}

void XMLSettingsProvider::storeSetting(const Setting<std::string>& /*settingPath*/, const std::string& /*settingValue*/) {
	assert(false);
}

bool XMLSettingsProvider::getSetting(const Setting<bool>& setting) {
	if (values_.find(setting.getKey()) != values_.end()) {
		std::string value = values_[setting.getKey()];
		return boost::iequals(value, "true") || value == "1";
	}
	return setting.getDefaultValue();
}

void XMLSettingsProvider::storeSetting(const Setting<bool>& /*settingPath*/, const bool& /*settingValue*/) {
	assert(false);
}

int XMLSettingsProvider::getSetting(const Setting<int>& setting) {
	if (values_.find(setting.getKey()) != values_.end()) {
		std::string value = values_[setting.getKey()];
		try {
			return value.empty() ? setting.getDefaultValue() : boost::lexical_cast<int>(value);;
		}
		catch(boost::bad_lexical_cast &) {}
	}
	return setting.getDefaultValue();
}

void XMLSettingsProvider::storeSetting(const Setting<int>& /*settingPath*/, const int& /*settingValue*/) {
	assert(false);
}

std::vector<std::string> XMLSettingsProvider::getAvailableProfiles() {
	assert(false);
}

void XMLSettingsProvider::createProfile(const std::string& /*profile*/) {
	assert(false);
}

void XMLSettingsProvider::removeProfile(const std::string& /*profile*/) {
	assert(false);
}

bool XMLSettingsProvider::getIsSettingFinal(const std::string& settingPath) {
	return finals_.count(settingPath);
}

void XMLSettingsProvider::handleStartElement(const std::string& element, const std::string& /*ns*/, const AttributeMap& attributes) {
	level_++;
	if (level_ == SettingLevel) {
		if (attributes.getBoolAttribute("final", false)) {
			finals_.insert(element);
		}
		currentElement_ = element;
		currentText_ = "";
	}
}

void XMLSettingsProvider::handleEndElement(const std::string& /*element*/, const std::string& /*ns*/) {
	if (level_ == SettingLevel) {
		values_[currentElement_] = currentText_;
		SWIFT_LOG(debug) << "Setting value of " << currentElement_ << " to " << currentText_ << std::endl;
	}
	level_--;
}

void XMLSettingsProvider::handleCharacterData(const std::string& data) {
	if (level_ >= SettingLevel) {
		currentText_ += data;
	}
}


}