diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-01-19 20:10:05 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-01-19 20:10:05 (GMT) |
| commit | c5e8b345219cd7d8ca4b0aa638f59a1fed2cd83b (patch) | |
| tree | 0d324f7b8d5bf6978f4543af415248b474676b97 /src/client | |
| parent | 6d3b447f8e32fdaccdb3727af4d72d341e191e94 (diff) | |
| download | powder-c5e8b345219cd7d8ca4b0aa638f59a1fed2cd83b.zip powder-c5e8b345219cd7d8ca4b0aa638f59a1fed2cd83b.tar.gz | |
Add "cajun" for JSON reading and writing, Save searching in client and some more stuff for searcg
Diffstat (limited to 'src/client')
| -rw-r--r-- | src/client/Client.cpp | 74 | ||||
| -rw-r--r-- | src/client/Client.h | 6 |
2 files changed, 79 insertions, 1 deletions
diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 2b742f4..1e2e76f 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -2,12 +2,21 @@ #include <iostream> #include <sstream> #include <string> +#include <vector> #include <time.h> + #include "Config.h" #include "Client.h" -#include "interface/Point.h" #include "Graphics.h" +#include "interface/Point.h" + +#include "search/Save.h" + +#include "cajun/reader.h" +#include "cajun/writer.h" +#include "cajun/elements.h" + Client::Client() { int i = 0; @@ -29,6 +38,69 @@ Client::~Client() http_done(); } +std::vector<Save> Client::SearchSaves(int start, int count, string query, string sort) +{ + lastError = ""; + std::vector<Save> saveArray; + std::stringstream urlStream; + char * data; + int dataStatus, dataLength; + urlStream << "http://" << SERVER << "/Browse.json?Start=" << start << "&Count=" << cout; + if(query.length() || sort.length()) + { + urlStream << "&Search_Query="; + if(query.length()) + urlStream << query; + if(sort.length()) + { + if(query.length()) + urlStream << " "; + urlStream << "sort:" << sort; + } + + } + data = http_simple_get((char *)urlStream.str().c_str(), &dataStatus, &dataLength); + if(dataStatus == 200 && data) + { + try + { + std::istringstream dataStream(data); // missing comma! + json::Object objDocument; + json::Reader::Read(objDocument, dataStream); + + json::Array savesArray = objDocument["Saves"]; + for(int j = 0; j < savesArray.Size(); j++) + { + json::Number tempID = savesArray[j]["ID"]; + json::Number tempScoreUp = savesArray[j]["ScoreUp"]; + json::Number tempScoreDown = savesArray[j]["ScoreDown"]; + json::String tempUsername = savesArray[j]["Username"]; + json::String tempName = savesArray[j]["Name"]; + saveArray.push_back( + Save( + tempID.Value(), + tempScoreUp.Value(), + tempScoreDown.Value(), + tempUsername.Value(), + tempName.Value() + ) + ); + } + } + catch (json::Exception &e) + { + lastError = "Could not read response"; + } + } + else + { + lastError = http_ret_text(dataStatus); + } + if(data) + free(data); + return saveArray; +} + void Client::ClearThumbnailRequests() { for(int i = 0; i < IMGCONNS; i++) diff --git a/src/client/Client.h b/src/client/Client.h index 3aa2333..9a86bfb 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -2,14 +2,18 @@ #define CLIENT_H #include <queue> +#include <vector> + #include "Config.h" #include "HTTP.h" #include "search/Thumbnail.h" +#include "search/Save.h" #include "Singleton.h" class Client: public Singleton<Client> { private: + std::string lastError; int thumbnailCacheNextID; Thumbnail * thumbnailCache[THUMB_CACHE_SIZE]; void * activeThumbRequests[IMGCONNS]; @@ -20,7 +24,9 @@ public: Client(); ~Client(); void ClearThumbnailRequests(); + std::vector<Save> SearchSaves(int start, int count, string query, string sort); Thumbnail * GetThumbnail(int saveID, int saveDate); + std::string GetLastError() { return lastError; } }; #endif // CLIENT_H |
