summaryrefslogtreecommitdiffstats
path: root/src/util/pvsSettingsManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/pvsSettingsManager.cpp')
-rw-r--r--src/util/pvsSettingsManager.cpp185
1 files changed, 185 insertions, 0 deletions
diff --git a/src/util/pvsSettingsManager.cpp b/src/util/pvsSettingsManager.cpp
new file mode 100644
index 0000000..a6a742b
--- /dev/null
+++ b/src/util/pvsSettingsManager.cpp
@@ -0,0 +1,185 @@
+#include "pvsSettingsManager.h"
+#include "TextFile.h"
+
+PVSSettingsManager* PVSSettingsManager::getManager()
+{
+ if (myself)
+ return myself;
+ else
+ return myself = new PVSSettingsManager;
+}
+
+void PVSSettingsManager::setConfigFile(QString path)
+{
+ if (path.size() && fileExists(path))
+ {
+ _path = path;
+ _parseFile(_path);
+
+ }
+ else
+ ConsoleLog writeError(QString("Can't open config file \"").append(QString(path).append("\"")));
+}
+
+bool PVSSettingsManager::hasEntry(QString name)
+{
+ for (SettingsIter it = settingsList.begin(); it != settingsList.end(); it++)
+ {
+ if ((*it).first.compare(name) == 0)
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+QString PVSSettingsManager::getEntryString(QString name)
+{
+ for (SettingsIter it = settingsList.begin(); it != settingsList.end(); it++)
+ {
+ if ((*it).first.compare(name) == 0)
+ {
+ return (*it).second;
+ }
+ }
+ return QString();
+}
+
+void PVSSettingsManager::writeEntry(QString name, QString value)
+{
+ if (name.size() && value.size())
+ return;
+ bool unique = true;
+ for (SettingsIter it = settingsList.begin(); it != settingsList.end(); it++)
+ {
+ if ((*it).first.compare(name) == 0)
+ {
+ unique = false;
+ (*it).second = value;
+ break;
+ }
+ }
+ if (unique)
+ {
+ SettingsEntry tmp(name, value);
+ settingsList.push_back(tmp);
+ }
+}
+
+
+PVSSettingsManager* PVSSettingsManager::myself = NULL;
+
+PVSSettingsManager::PVSSettingsManager()
+{
+
+}
+
+void PVSSettingsManager::setConfigs()
+{
+ //default settings
+ _configs.setValue("Chat/chatstate", "on");
+ _configs.setValue("Chat/chatmode", "bossmode");
+ _configs.setValue("Room/roomId", "0");
+ _configs.setValue("VNC/permit", "off");
+ _configs.setValue("VNC/quality", "high");
+ _configs.sync();
+}
+void PVSSettingsManager::reWriteConfigs(QString set, QString val)
+{
+ _configs.setValue(set, val);
+ _configs.sync();
+}
+
+void PVSSettingsManager::readConfigs(QString sett, QString vall)
+{
+ //TODO: read the config file..
+ _configs.value("Chat/chatstate").toBool();
+ _configs.value("Chat/chatmode").toString();
+ _configs.value("Room/room").toInt();
+ _configs.value("VNC/permit").toBool();
+ _configs.value("VNC/quality").toString();
+}
+
+void PVSSettingsManager::_parseFile(QString path)
+{
+ QString line;
+ TextFile file(path);
+
+ SettingsList tmpList;
+
+ if (file.good())
+ {
+ while (!file.eof())
+ {
+ line = file.readLine();
+ if (!(line.length() <=1)) // ignore blank
+ {
+ if (!(line[0] == '#' || line[0] == '/' || line[0] == '[')) // ignore comments and section headers
+ {
+ SettingsEntry tmp = _parseLine(line);
+ if (tmp.first.size() && tmp.second.size())
+ {
+ bool unique = true;
+ for (SettingsIter it = tmpList.begin(); it != tmpList.end(); it++)
+ {
+ if ((*it).first.compare(tmp.first) == 0)
+ {
+ unique = false;
+ break;
+ }
+ }
+ if (unique)
+ tmpList.push_back(tmp);
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ ConsoleLog writeError(QString("No configfile \"").append(QString(path).append("\" found or file corrupt.")));
+ }
+
+ if (tmpList.size())
+ settingsList = tmpList;
+}
+#ifdef verbose
+ConsoleLog writeLine(QString("Dumping Config Content of ").append(QString(path).append(" : ")));
+for (SettingsIter it = settingsList.begin(); it != settingsList.end(); it++)
+{
+ ConsoleLog writeLine(QString("Option: ").append(QString((*it).first).append(QString(" | Value: ").append((*it).second))));
+}
+ConsoleLog writeLine(QString("End of ").append(QString(path).append(".")));
+#endif
+
+SettingsEntry PVSSettingsManager::_parseLine(QString line)
+{
+ QString name;
+ QString value;
+
+ name = lineSplitter(line, "=\n\t", true);
+ value = lineSplitter(line, "=\n\t", false);
+
+ if (!(name.size() && value.size()))
+ return SettingsEntry("","");
+
+
+ // remove whitespaces in front of option name
+ for (int i = 0; i < name.size(); i++)
+ {
+ if (name[i] == '\t' || name[i] == ' ')
+ {
+ name.remove(i, 1);
+ i--;
+ }
+ else
+ break;
+ }
+ // whitespaces after the value are trimmed by the lineSplitter
+
+ SettingsEntry tmp(name, value);
+ return tmp;
+}
+
+
+