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"
28 #include "main.h" // for g_settings
31 #include "httpfetch.h"
34 Json::Value fetchJsonValue(const std::string &url,
35 struct curl_slist *chunk) {
38 HTTPFetchRequest fetchrequest;
39 HTTPFetchResult fetchresult;
40 fetchrequest.url = url;
41 fetchrequest.caller = HTTPFETCH_SYNC;
43 struct curl_slist* runptr = chunk;
45 fetchrequest.extra_headers.push_back(runptr->data);
46 runptr = runptr->next;
48 httpfetch_sync(fetchrequest,fetchresult);
50 if (!fetchresult.succeeded) {
55 std::istringstream stream(fetchresult.data);
57 if (!reader.parse( stream, root ) )
59 errorstream << "URL: " << url << std::endl;
60 errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
61 errorstream << "data: \"" << fetchresult.data << "\"" << std::endl;
68 if ((root["list"].isArray())) {
78 std::vector<ModStoreMod> readModStoreList(Json::Value& modlist) {
79 std::vector<ModStoreMod> retval;
81 if (modlist.isArray()) {
82 for (unsigned int i = 0; i < modlist.size(); i++)
88 if (modlist[i]["id"].asString().size()) {
89 std::string id_raw = modlist[i]["id"].asString();
91 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
93 if ((id_raw != "") && (*endptr == 0)) {
94 toadd.id = numbervalue;
97 errorstream << "readModStoreList: missing id" << std::endl;
102 errorstream << "readModStoreList: missing id" << std::endl;
107 if (modlist[i]["title"].asString().size()) {
108 toadd.title = modlist[i]["title"].asString();
111 errorstream << "readModStoreList: missing title" << std::endl;
116 if (modlist[i]["basename"].asString().size()) {
117 toadd.basename = modlist[i]["basename"].asString();
120 errorstream << "readModStoreList: missing basename" << std::endl;
131 retval.push_back(toadd);
138 ModStoreModDetails readModStoreModDetails(Json::Value& details) {
140 ModStoreModDetails retval;
145 if (details["version_set"].isArray()) {
146 for (unsigned int i = 0; i < details["version_set"].size(); i++)
148 ModStoreVersionEntry toadd;
150 if (details["version_set"][i]["id"].asString().size()) {
151 std::string id_raw = details["version_set"][i]["id"].asString();
153 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
155 if ((id_raw != "") && (*endptr == 0)) {
156 toadd.id = numbervalue;
160 errorstream << "readModStoreModDetails: missing version_set id" << std::endl;
161 retval.valid = false;
165 if (details["version_set"][i]["date"].asString().size()) {
166 toadd.date = details["version_set"][i]["date"].asString();
170 if (details["version_set"][i]["file"].asString().size()) {
171 toadd.file = details["version_set"][i]["file"].asString();
174 errorstream << "readModStoreModDetails: missing version_set file" << std::endl;
175 retval.valid = false;
183 retval.versions.push_back(toadd);
191 if (retval.versions.size() < 1) {
192 infostream << "readModStoreModDetails: not a single version specified!" << std::endl;
193 retval.valid = false;
197 if (details["categories"].isObject()) {
198 for (unsigned int i = 0; i < details["categories"].size(); i++) {
199 ModStoreCategoryInfo toadd;
201 if (details["categories"][i]["id"].asString().size()) {
203 std::string id_raw = details["categories"][i]["id"].asString();
205 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
207 if ((id_raw != "") && (*endptr == 0)) {
208 toadd.id = numbervalue;
212 errorstream << "readModStoreModDetails: missing categories id" << std::endl;
213 retval.valid = false;
215 if (details["categories"][i]["title"].asString().size()) {
216 toadd.name = details["categories"][i]["title"].asString();
219 errorstream << "readModStoreModDetails: missing categories title" << std::endl;
220 retval.valid = false;
224 retval.categories.push_back(toadd);
233 if (details["author"].isObject()) {
234 if (details["author"]["id"].asString().size()) {
236 std::string id_raw = details["author"]["id"].asString();
238 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
240 if ((id_raw != "") && (*endptr == 0)) {
241 retval.author.id = numbervalue;
244 errorstream << "readModStoreModDetails: missing author id (convert)" << std::endl;
245 retval.valid = false;
249 errorstream << "readModStoreModDetails: missing author id" << std::endl;
250 retval.valid = false;
253 if (details["author"]["username"].asString().size()) {
254 retval.author.username = details["author"]["username"].asString();
257 errorstream << "readModStoreModDetails: missing author username" << std::endl;
258 retval.valid = false;
262 errorstream << "readModStoreModDetails: missing author" << std::endl;
263 retval.valid = false;
267 if (details["license"].isObject()) {
268 if (details["license"]["id"].asString().size()) {
270 std::string id_raw = details["license"]["id"].asString();
272 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
274 if ((id_raw != "") && (*endptr == 0)) {
275 retval.license.id = numbervalue;
279 errorstream << "readModStoreModDetails: missing license id" << std::endl;
280 retval.valid = false;
283 if (details["license"]["short"].asString().size()) {
284 retval.license.shortinfo = details["license"]["short"].asString();
287 errorstream << "readModStoreModDetails: missing license short" << std::endl;
288 retval.valid = false;
291 if (details["license"]["link"].asString().size()) {
292 retval.license.url = details["license"]["link"].asString();
298 if (details["titlepic"].isObject()) {
299 if (details["titlepic"]["id"].asString().size()) {
301 std::string id_raw = details["titlepic"]["id"].asString();
303 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
305 if ((id_raw != "") && (*endptr == 0)) {
306 retval.titlepic.id = numbervalue;
310 if (details["titlepic"]["file"].asString().size()) {
311 retval.titlepic.file = details["titlepic"]["file"].asString();
314 if (details["titlepic"]["desc"].asString().size()) {
315 retval.titlepic.description = details["titlepic"]["desc"].asString();
318 if (details["titlepic"]["mod"].asString().size()) {
320 std::string mod_raw = details["titlepic"]["mod"].asString();
322 int numbervalue = strtol(mod_raw.c_str(),&endptr,10);
324 if ((mod_raw != "") && (*endptr == 0)) {
325 retval.titlepic.mod = numbervalue;
331 if (details["id"].asString().size()) {
333 std::string id_raw = details["id"].asString();
335 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
337 if ((id_raw != "") && (*endptr == 0)) {
338 retval.id = numbervalue;
342 errorstream << "readModStoreModDetails: missing id" << std::endl;
343 retval.valid = false;
347 if (details["title"].asString().size()) {
348 retval.title = details["title"].asString();
351 errorstream << "readModStoreModDetails: missing title" << std::endl;
352 retval.valid = false;
356 if (details["basename"].asString().size()) {
357 retval.basename = details["basename"].asString();
360 errorstream << "readModStoreModDetails: missing basename" << std::endl;
361 retval.valid = false;
365 if (details["desc"].asString().size()) {
366 retval.description = details["desc"].asString();
370 if (details["replink"].asString().size()) {
371 retval.repository = details["replink"].asString();
375 if (details["rating"].asString().size()) {
377 std::string id_raw = details["rating"].asString();
379 float numbervalue = strtof(id_raw.c_str(),&endptr);
381 if ((id_raw != "") && (*endptr == 0)) {
382 retval.rating = numbervalue;
390 if (details["depends"].isArray()) {
395 if (details["softdep"].isArray()) {
400 if (details["screenshot_url"].asString().size()) {
401 retval.screenshot_url = details["screenshot_url"].asString();