summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Smith <git@kismith.co.uk>2012-02-17 16:57:01 (GMT)
committerKevin Smith <git@kismith.co.uk>2012-02-21 14:34:55 (GMT)
commit3605b6622bc8b4abb810fac6b53f7f71be0fa7de (patch)
tree638a8fd6449dd235931fe307e50119adf9c9fe22 /Swift/Controllers/Settings/XMLSettingsProvider.cpp
parentb05f8fcb285d2d13d2be50a3eb1062048fbe30f5 (diff)
downloadswift-contrib-3605b6622bc8b4abb810fac6b53f7f71be0fa7de.zip
swift-contrib-3605b6622bc8b4abb810fac6b53f7f71be0fa7de.tar.bz2
Introduce system settings policies.
Release-Notes: It is now possible for sysadmins to deploy files with policies for configuration options, such as making it impossible for users to save passwords or to force sound notifications off, or to set defaults. Also allow changing an option so that Swift disconnects on idle timeout, instead of going away.
Diffstat (limited to 'Swift/Controllers/Settings/XMLSettingsProvider.cpp')
-rw-r--r--Swift/Controllers/Settings/XMLSettingsProvider.cpp124
1 files changed, 124 insertions, 0 deletions
diff --git a/Swift/Controllers/Settings/XMLSettingsProvider.cpp b/Swift/Controllers/Settings/XMLSettingsProvider.cpp
new file mode 100644
index 0000000..6ad3170
--- /dev/null
+++ b/Swift/Controllers/Settings/XMLSettingsProvider.cpp
@@ -0,0 +1,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;
+ }
+}
+
+
+}
+
+
+
+