summaryrefslogtreecommitdiff
path: root/src/client/Client.cpp
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-06-22 00:04:55 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-06-22 00:04:55 (GMT)
commit3c91e526bb5576727e53b98fb850570074f9d036 (patch)
treede60725cad767bdb60df2bd5a9f7d5f2d430fbc4 /src/client/Client.cpp
parent550f6e28e012ceba8df9274fc71c7fbddfd90530 (diff)
downloadpowder-3c91e526bb5576727e53b98fb850570074f9d036.zip
powder-3c91e526bb5576727e53b98fb850570074f9d036.tar.gz
Client now provides methods for reading and saving preferences - less powerful than raw access to Cajun, (no mixed type arrays, for example) but allows other save formats to be used, such as property lists on OS X
Diffstat (limited to 'src/client/Client.cpp')
-rw-r--r--src/client/Client.cpp212
1 files changed, 211 insertions, 1 deletions
diff --git a/src/client/Client.cpp b/src/client/Client.cpp
index 285a9ad..41c1faf 100644
--- a/src/client/Client.cpp
+++ b/src/client/Client.cpp
@@ -6,6 +6,7 @@
#include <iomanip>
#include <time.h>
#include <stdio.h>
+#include <deque>
#ifdef WIN32
#include <direct.h>
@@ -194,7 +195,7 @@ void Client::AddListener(ClientListener * listener)
listeners.push_back(listener);
}
-Client::~Client()
+void Client::Shutdown()
{
ClearThumbnailRequests();
http_done();
@@ -226,6 +227,10 @@ Client::~Client()
}
}
+Client::~Client()
+{
+}
+
void Client::SetAuthUser(User user)
{
@@ -1277,3 +1282,208 @@ std::vector<string> * Client::AddTag(int saveID, string tag)
free(data);
return tags;
}
+
+vector<std::string> Client::explodePropertyString(std::string property)
+{
+ vector<string> stringArray;
+ string current = "";
+ for (string::iterator iter = property.begin(); iter != property.end(); ++iter) {
+ if (*iter == '.') {
+ if (current.length() > 0) {
+ stringArray.push_back(current);
+ current = "";
+ }
+ } else {
+ current += *iter;
+ }
+ }
+ if(current.length() > 0)
+ stringArray.push_back(current);
+ return stringArray;
+}
+
+std::string Client::GetPrefString(std::string property, std::string defaultValue)
+{
+ try
+ {
+ json::String value = GetPref(property);
+ return value.Value();
+ }
+ catch (json::Exception & e)
+ {
+
+ }
+ return defaultValue;
+}
+
+double Client::GetPrefNumber(std::string property, double defaultValue)
+{
+ try
+ {
+ json::Number value = GetPref(property);
+ return value.Value();
+ }
+ catch (json::Exception & e)
+ {
+
+ }
+ return defaultValue;
+}
+
+vector<string> Client::GetPrefStringArray(std::string property)
+{
+ try
+ {
+ json::Array value = GetPref(property);
+ vector<string> strArray;
+ for(json::Array::iterator iter = value.Begin(); iter != value.End(); ++iter)
+ {
+ json::String cValue = *iter;
+ strArray.push_back(cValue.Value());
+ }
+ return strArray;
+ }
+ catch (json::Exception & e)
+ {
+
+ }
+ return vector<string>();
+}
+
+vector<double> Client::GetPrefNumberArray(std::string property)
+{
+ try
+ {
+ json::Array value = GetPref(property);
+ vector<double> strArray;
+ for(json::Array::iterator iter = value.Begin(); iter != value.End(); ++iter)
+ {
+ json::Number cValue = *iter;
+ strArray.push_back(cValue.Value());
+ }
+ return strArray;
+ }
+ catch (json::Exception & e)
+ {
+
+ }
+ return vector<double>();
+}
+
+vector<bool> Client::GetPrefBoolArray(std::string property)
+{
+ try
+ {
+ json::Array value = GetPref(property);
+ vector<bool> strArray;
+ for(json::Array::iterator iter = value.Begin(); iter != value.End(); ++iter)
+ {
+ json::Boolean cValue = *iter;
+ strArray.push_back(cValue.Value());
+ }
+ return strArray;
+ }
+ catch (json::Exception & e)
+ {
+
+ }
+ return vector<bool>();
+}
+
+bool Client::GetPrefBool(std::string property, bool defaultValue)
+{
+ try
+ {
+ json::Boolean value = GetPref(property);
+ return value.Value();
+ }
+ catch (json::Exception & e)
+ {
+
+ }
+ return defaultValue;
+}
+
+void Client::SetPref(std::string property, std::string value)
+{
+ json::UnknownElement stringValue = json::String(value);
+ SetPref(property, stringValue);
+}
+
+void Client::SetPref(std::string property, double value)
+{
+ json::UnknownElement numberValue = json::Number(value);
+ SetPref(property, numberValue);
+}
+
+void Client::SetPref(std::string property, vector<string> value)
+{
+ json::Array newArray;
+ for(vector<string>::iterator iter = value.begin(); iter != value.end(); ++iter)
+ {
+ newArray.Insert(json::String(*iter));
+ }
+ json::UnknownElement newArrayValue = newArray;
+ SetPref(property, newArrayValue);
+}
+
+void Client::SetPref(std::string property, vector<double> value)
+{
+ json::Array newArray;
+ for(vector<double>::iterator iter = value.begin(); iter != value.end(); ++iter)
+ {
+ newArray.Insert(json::Number(*iter));
+ }
+ json::UnknownElement newArrayValue = newArray;
+ SetPref(property, newArrayValue);
+}
+
+void Client::SetPref(std::string property, vector<bool> value)
+{
+ json::Array newArray;
+ for(vector<bool>::iterator iter = value.begin(); iter != value.end(); ++iter)
+ {
+ newArray.Insert(json::Boolean(*iter));
+ }
+ json::UnknownElement newArrayValue = newArray;
+ SetPref(property, newArrayValue);
+}
+
+void Client::SetPref(std::string property, bool value)
+{
+ json::UnknownElement boolValue = json::Boolean(value);
+ SetPref(property, boolValue);
+}
+
+json::UnknownElement Client::GetPref(std::string property)
+{
+ vector<string> pTokens = Client::explodePropertyString(property);
+ const json::UnknownElement & configDocumentCopy = configDocument;
+ json::UnknownElement currentRef = configDocumentCopy;
+ for(vector<string>::iterator iter = pTokens.begin(); iter != pTokens.end(); ++iter)
+ {
+ currentRef = currentRef[*iter];
+ }
+ return currentRef;
+}
+
+void Client::setPrefR(std::deque<string> tokens, json::UnknownElement & element, json::UnknownElement & value)
+{
+ if(tokens.size())
+ {
+ std::string token = tokens.front();
+ tokens.pop_front();
+ setPrefR(tokens, element[token], value);
+ }
+ else
+ element = value;
+}
+
+void Client::SetPref(std::string property, json::UnknownElement & value)
+{
+ vector<string> pTokens = Client::explodePropertyString(property);
+ deque<string> dTokens(pTokens.begin(), pTokens.end());
+ string token = dTokens.front();
+ dTokens.pop_front();
+ setPrefR(dTokens, configDocument[token], value);
+}