3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "convert_json.h"
30 #include <curl/curl.h>
32 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
34 ((std::string*)userp)->append((char*)contents, size * nmemb);
40 Json::Value fetchJsonValue(const std::string url,
41 struct curl_slist *chunk) {
43 std::string liststring;
46 curl = curl_easy_init();
51 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
52 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
53 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
54 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &liststring);
57 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
60 res = curl_easy_perform(curl);
62 errorstream<<"Jsonreader: "<< url <<" not found (internet connection?)"<<std::endl;
63 curl_easy_cleanup(curl);
68 std::istringstream stream(liststring);
69 if (!liststring.size()) {
73 if (!reader.parse( stream, root ) )
75 errorstream << "URL: " << url << std::endl;
76 errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
77 errorstream << "data: \"" << liststring << "\"" << std::endl;
84 if ((root["list"].isArray())) {
94 std::vector<ModStoreMod> readModStoreList(Json::Value& modlist) {
95 std::vector<ModStoreMod> retval;
97 if (modlist.isArray()) {
98 for (unsigned int i = 0; i < modlist.size(); i++)
104 if (modlist[i]["id"].asString().size()) {
105 const char* id_raw = modlist[i]["id"].asString().c_str();
107 int numbervalue = strtol(id_raw,&endptr,10);
109 if ((*id_raw != 0) && (*endptr == 0)) {
110 toadd.id = numbervalue;
118 if (modlist[i]["title"].asString().size()) {
119 toadd.title = modlist[i]["title"].asString();
126 if (modlist[i]["basename"].asString().size()) {
127 toadd.basename = modlist[i]["basename"].asString();
140 retval.push_back(toadd);
147 ModStoreModDetails readModStoreModDetails(Json::Value& details) {
149 ModStoreModDetails retval;
154 if (details["version_set"].isArray()) {
155 for (unsigned int i = 0; i < details["version_set"].size(); i++)
157 ModStoreVersionEntry toadd;
159 if (details["version_set"][i]["id"].asString().size()) {
160 const char* id_raw = details["version_set"][i]["id"].asString().c_str();
162 int numbervalue = strtol(id_raw,&endptr,10);
164 if ((*id_raw != 0) && (*endptr == 0)) {
165 toadd.id = numbervalue;
169 retval.valid = false;
173 if (details["version_set"][i]["date"].asString().size()) {
174 toadd.date = details["version_set"][i]["date"].asString();
178 if (details["version_set"][i]["file"].asString().size()) {
179 toadd.file = details["version_set"][i]["file"].asString();
182 retval.valid = false;
190 retval.versions.push_back(toadd);
198 if (retval.versions.size() < 1) {
199 retval.valid = false;
203 if (details["categories"].isObject()) {
204 for (unsigned int i = 0; i < details["categories"].size(); i++) {
205 ModStoreCategoryInfo toadd;
207 if (details["categories"][i]["id"].asString().size()) {
209 const char* id_raw = details["categories"][i]["id"].asString().c_str();
211 int numbervalue = strtol(id_raw,&endptr,10);
213 if ((*id_raw != 0) && (*endptr == 0)) {
214 toadd.id = numbervalue;
218 retval.valid = false;
220 if (details["categories"][i]["title"].asString().size()) {
221 toadd.name = details["categories"][i]["title"].asString();
224 retval.valid = false;
228 retval.categories.push_back(toadd);
237 if (details["author"].isObject()) {
238 if (details["author"]["id"].asString().size()) {
240 const char* id_raw = details["author"]["id"].asString().c_str();
242 int numbervalue = strtol(id_raw,&endptr,10);
244 if ((*id_raw != 0) && (*endptr == 0)) {
245 retval.author.id = numbervalue;
248 retval.valid = false;
252 retval.valid = false;
255 if (details["author"]["username"].asString().size()) {
256 retval.author.username = details["author"]["username"].asString();
259 retval.valid = false;
263 retval.valid = false;
267 if (details["license"].isObject()) {
268 if (details["license"]["id"].asString().size()) {
270 const char* id_raw = details["license"]["id"].asString().c_str();
272 int numbervalue = strtol(id_raw,&endptr,10);
274 if ((*id_raw != 0) && (*endptr == 0)) {
275 retval.license.id = numbervalue;
279 retval.valid = false;
282 if (details["license"]["short"].asString().size()) {
283 retval.license.shortinfo = details["license"]["short"].asString();
286 retval.valid = false;
289 if (details["license"]["link"].asString().size()) {
290 retval.license.url = details["license"]["link"].asString();
296 if (details["id"].asString().size()) {
298 const char* id_raw = details["id"].asString().c_str();
300 int numbervalue = strtol(id_raw,&endptr,10);
302 if ((*id_raw != 0) && (*endptr == 0)) {
303 retval.id = numbervalue;
307 retval.valid = false;
311 if (details["title"].asString().size()) {
312 retval.title = details["title"].asString();
315 retval.valid = false;
319 if (details["basename"].asString().size()) {
320 retval.basename = details["basename"].asString();
323 retval.valid = false;
327 if (details["desc"].asString().size()) {
328 retval.description = details["desc"].asString();
332 if (details["replink"].asString().size()) {
333 retval.repository = details["replink"].asString();
337 if (details["rating"].asString().size()) {
339 const char* id_raw = details["rating"].asString().c_str();
341 float numbervalue = strtof(id_raw,&endptr);
343 if ((*id_raw != 0) && (*endptr == 0)) {
344 retval.rating = numbervalue;
352 if (details["depends"].isArray()) {
357 if (details["softdep"].isArray()) {
362 if (details["screenshot_url"].asString().size()) {
363 retval.screenshot_url = details["screenshot_url"].asString();