summaryrefslogtreecommitdiff
path: root/src/configuration.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/configuration.cpp')
-rw-r--r--src/configuration.cpp177
1 files changed, 144 insertions, 33 deletions
diff --git a/src/configuration.cpp b/src/configuration.cpp
index d8b11034..44fb6e2e 100644
--- a/src/configuration.cpp
+++ b/src/configuration.cpp
@@ -21,7 +21,7 @@
#include "configuration.h"
-#include "configlistener.h"
+#include "event.h"
#include "log.h"
#include "utils/stringutils.h"
@@ -38,15 +38,9 @@ void Configuration::setValue(const std::string &key, const std::string &value)
ConfigurationObject::setValue(key, value);
// Notify listeners
- ListenerMapIterator list = mListenerMap.find(key);
- if (list != mListenerMap.end())
- {
- Listeners listeners = list->second;
- for (ListenerIterator i = listeners.begin(); i != listeners.end(); i++)
- {
- (*i)->optionChanged(key);
- }
- }
+ Mana::Event event(EVENT_CONFIGOPTIONCHANGED);
+ event.setString("option", key);
+ event.trigger(CHANNEL_CONFIG);
}
std::string ConfigurationObject::getValue(const std::string &key,
@@ -79,8 +73,11 @@ double ConfigurationObject::getValue(const std::string &key,
void ConfigurationObject::deleteList(const std::string &name)
{
for (ConfigurationList::const_iterator
- it = mContainerOptions[name].begin(); it != mContainerOptions[name].end(); it++)
+ it = mContainerOptions[name].begin();
+ it != mContainerOptions[name].end(); it++)
+ {
delete *it;
+ }
mContainerOptions[name].clear();
}
@@ -88,8 +85,11 @@ void ConfigurationObject::deleteList(const std::string &name)
void ConfigurationObject::clear()
{
for (std::map<std::string, ConfigurationList>::const_iterator
- it = mContainerOptions.begin(); it != mContainerOptions.end(); it++)
+ it = mContainerOptions.begin(); it != mContainerOptions.end(); it++)
+ {
deleteList(it->first);
+ }
+
mOptions.clear();
}
@@ -98,6 +98,131 @@ ConfigurationObject::~ConfigurationObject()
clear();
}
+void Configuration::cleanDefaults()
+{
+ if (mDefaultsData)
+ {
+ for (DefaultsData::const_iterator iter = mDefaultsData->begin();
+ iter != mDefaultsData->end(); iter++)
+ {
+ if (iter->second)
+ delete(iter->second);
+ }
+ mDefaultsData->clear();
+ delete mDefaultsData;
+ mDefaultsData = 0;
+ }
+}
+
+Configuration::~Configuration()
+{
+ cleanDefaults();
+}
+
+void Configuration::setDefaultValues(DefaultsData *defaultsData)
+{
+ cleanDefaults();
+ mDefaultsData = defaultsData;
+}
+
+Mana::VariableData* Configuration::getDefault(const std::string &key,
+ Mana::VariableData::DataType type
+ ) const
+{
+ if (mDefaultsData)
+ {
+ DefaultsData::const_iterator itdef = mDefaultsData->find(key);
+
+ if (itdef != mDefaultsData->end() && itdef->second
+ && itdef->second->getType() == type)
+ {
+ return itdef->second;
+ }
+ else
+ {
+ logger->log("%s: No value in registry for key %s",
+ mConfigPath.c_str(), key.c_str());
+ }
+ }
+ return NULL;
+}
+
+int Configuration::getIntValue(const std::string &key) const
+{
+ int defaultValue = 0;
+ Options::const_iterator iter = mOptions.find(key);
+ if (iter == mOptions.end())
+ {
+ Mana::VariableData* vd = getDefault(key, Mana::VariableData::DATA_INT);
+ if (vd)
+ defaultValue = ((Mana::IntData*)vd)->getData();
+ }
+ else
+ {
+ defaultValue = atoi(iter->second.c_str());
+ }
+ return defaultValue;
+}
+
+std::string Configuration::getStringValue(const std::string &key) const
+{
+ std::string defaultValue = "";
+ Options::const_iterator iter = mOptions.find(key);
+ if (iter == mOptions.end())
+ {
+ Mana::VariableData* vd = getDefault(key,
+ Mana::VariableData::DATA_STRING);
+
+ if (vd)
+ defaultValue = ((Mana::StringData*)vd)->getData();
+ }
+ else
+ {
+ defaultValue = iter->second;
+ }
+ return defaultValue;
+}
+
+
+float Configuration::getFloatValue(const std::string &key) const
+{
+ float defaultValue = 0.0f;
+ Options::const_iterator iter = mOptions.find(key);
+ if (iter == mOptions.end())
+ {
+ Mana::VariableData* vd = getDefault(key,
+ Mana::VariableData::DATA_FLOAT);
+
+ if (vd)
+ defaultValue = ((Mana::FloatData*)vd)->getData();
+ }
+ else
+ {
+ defaultValue = atof(iter->second.c_str());
+ }
+ return defaultValue;
+}
+
+bool Configuration::getBoolValue(const std::string &key) const
+{
+ bool defaultValue = false;
+ Options::const_iterator iter = mOptions.find(key);
+ if (iter == mOptions.end())
+ {
+ Mana::VariableData* vd = getDefault(key,
+ Mana::VariableData::DATA_BOOL);
+
+ if (vd)
+ defaultValue = ((Mana::BoolData*)vd)->getData();
+ }
+ else
+ {
+ return getBoolFromString(iter->second, defaultValue);
+ }
+
+ return defaultValue;
+}
+
void ConfigurationObject::initFromXML(xmlNodePtr parent_node)
{
clear();
@@ -106,8 +231,7 @@ void ConfigurationObject::initFromXML(xmlNodePtr parent_node)
{
if (xmlStrEqual(node->name, BAD_CAST "list"))
{
- // list option handling
-
+ // List option handling.
std::string name = XML::getProperty(node, "name", std::string());
for_each_xml_child_node(subnode, node)
@@ -117,7 +241,7 @@ void ConfigurationObject::initFromXML(xmlNodePtr parent_node)
{
ConfigurationObject *cobj = new ConfigurationObject;
- cobj->initFromXML(subnode); // recurse
+ cobj->initFromXML(subnode); // Recurse
mContainerOptions[name].push_back(cobj);
}
@@ -126,14 +250,13 @@ void ConfigurationObject::initFromXML(xmlNodePtr parent_node)
}
else if (xmlStrEqual(node->name, BAD_CAST "option"))
{
- // single option handling
-
+ // Single option handling.
std::string name = XML::getProperty(node, "name", std::string());
std::string value = XML::getProperty(node, "value", std::string());
if (!name.empty())
mOptions[name] = value;
- } // otherwise ignore
+ } // Otherwise ignore
}
}
@@ -177,16 +300,16 @@ void ConfigurationObject::writeToXML(xmlTextWriterPtr writer)
}
for (std::map<std::string, ConfigurationList>::const_iterator
- it = mContainerOptions.begin(); it != mContainerOptions.end(); it++)
+ it = mContainerOptions.begin(); it != mContainerOptions.end(); it++)
{
const char *name = it->first.c_str();
xmlTextWriterStartElement(writer, BAD_CAST "list");
xmlTextWriterWriteAttribute(writer, BAD_CAST "name", BAD_CAST name);
- // recurse on all elements
+ // Recurse on all elements
for (ConfigurationList::const_iterator
- elt_it = it->second.begin(); elt_it != it->second.end(); elt_it++)
+ elt_it = it->second.begin(); elt_it != it->second.end(); elt_it++)
{
xmlTextWriterStartElement(writer, BAD_CAST name);
(*elt_it)->writeToXML(writer);
@@ -231,15 +354,3 @@ void Configuration::write()
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);
}
-
-void Configuration::addListener(
- const std::string &key, ConfigListener *listener)
-{
- mListenerMap[key].push_front(listener);
-}
-
-void Configuration::removeListener(
- const std::string &key, ConfigListener *listener)
-{
- mListenerMap[key].remove(listener);
-}