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) {
37 HTTPFetchRequest fetchrequest;
38 HTTPFetchResult fetchresult;
39 fetchrequest.url = url;
40 fetchrequest.caller = HTTPFETCH_SYNC;
43 struct curl_slist* runptr = chunk;
45 fetchrequest.extra_headers.push_back(runptr->data);
46 runptr = runptr->next;
49 httpfetch_sync(fetchrequest,fetchresult);
51 if (!fetchresult.succeeded) {
56 std::istringstream stream(fetchresult.data);
58 if (!reader.parse( stream, root ) )
60 errorstream << "URL: " << url << std::endl;
61 errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
62 errorstream << "data: \"" << fetchresult.data << "\"" << std::endl;
69 if ((root["list"].isArray())) {
79 std::vector<ModStoreMod> readModStoreList(Json::Value& modlist) {
80 std::vector<ModStoreMod> retval;
82 if (modlist.isArray()) {
83 for (unsigned int i = 0; i < modlist.size(); i++)
89 if (modlist[i]["id"].asString().size()) {
90 std::string id_raw = modlist[i]["id"].asString();
92 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
94 if ((id_raw != "") && (*endptr == 0)) {
95 toadd.id = numbervalue;
98 errorstream << "readModStoreList: missing id" << std::endl;
103 errorstream << "readModStoreList: missing id" << std::endl;
108 if (modlist[i]["title"].asString().size()) {
109 toadd.title = modlist[i]["title"].asString();
112 errorstream << "readModStoreList: missing title" << std::endl;
117 if (modlist[i]["basename"].asString().size()) {
118 toadd.basename = modlist[i]["basename"].asString();
121 errorstream << "readModStoreList: missing basename" << std::endl;
132 retval.push_back(toadd);
139 ModStoreModDetails readModStoreModDetails(Json::Value& details) {
141 ModStoreModDetails retval;
146 if (details["version_set"].isArray()) {
147 for (unsigned int i = 0; i < details["version_set"].size(); i++)
149 ModStoreVersionEntry toadd;
151 if (details["version_set"][i]["id"].asString().size()) {
152 std::string id_raw = details["version_set"][i]["id"].asString();
154 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
156 if ((id_raw != "") && (*endptr == 0)) {
157 toadd.id = numbervalue;
161 errorstream << "readModStoreModDetails: missing version_set id" << std::endl;
162 retval.valid = false;
166 if (details["version_set"][i]["date"].asString().size()) {
167 toadd.date = details["version_set"][i]["date"].asString();
171 if (details["version_set"][i]["file"].asString().size()) {
172 toadd.file = details["version_set"][i]["file"].asString();
175 errorstream << "readModStoreModDetails: missing version_set file" << std::endl;
176 retval.valid = false;
184 retval.versions.push_back(toadd);
192 if (retval.versions.size() < 1) {
193 infostream << "readModStoreModDetails: not a single version specified!" << std::endl;
194 retval.valid = false;
198 if (details["categories"].isObject()) {
199 for (unsigned int i = 0; i < details["categories"].size(); i++) {
200 ModStoreCategoryInfo toadd;
202 if (details["categories"][i]["id"].asString().size()) {
204 std::string id_raw = details["categories"][i]["id"].asString();
206 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
208 if ((id_raw != "") && (*endptr == 0)) {
209 toadd.id = numbervalue;
213 errorstream << "readModStoreModDetails: missing categories id" << std::endl;
214 retval.valid = false;
216 if (details["categories"][i]["title"].asString().size()) {
217 toadd.name = details["categories"][i]["title"].asString();
220 errorstream << "readModStoreModDetails: missing categories title" << std::endl;
221 retval.valid = false;
225 retval.categories.push_back(toadd);
234 if (details["author"].isObject()) {
235 if (details["author"]["id"].asString().size()) {
237 std::string id_raw = details["author"]["id"].asString();
239 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
241 if ((id_raw != "") && (*endptr == 0)) {
242 retval.author.id = numbervalue;
245 errorstream << "readModStoreModDetails: missing author id (convert)" << std::endl;
246 retval.valid = false;
250 errorstream << "readModStoreModDetails: missing author id" << std::endl;
251 retval.valid = false;
254 if (details["author"]["username"].asString().size()) {
255 retval.author.username = details["author"]["username"].asString();
258 errorstream << "readModStoreModDetails: missing author username" << std::endl;
259 retval.valid = false;
263 errorstream << "readModStoreModDetails: missing author" << std::endl;
264 retval.valid = false;
268 if (details["license"].isObject()) {
269 if (details["license"]["id"].asString().size()) {
271 std::string id_raw = details["license"]["id"].asString();
273 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
275 if ((id_raw != "") && (*endptr == 0)) {
276 retval.license.id = numbervalue;
280 errorstream << "readModStoreModDetails: missing license id" << std::endl;
281 retval.valid = false;
284 if (details["license"]["short"].asString().size()) {
285 retval.license.shortinfo = details["license"]["short"].asString();
288 errorstream << "readModStoreModDetails: missing license short" << std::endl;
289 retval.valid = false;
292 if (details["license"]["link"].asString().size()) {
293 retval.license.url = details["license"]["link"].asString();
299 if (details["titlepic"].isObject()) {
300 if (details["titlepic"]["id"].asString().size()) {
302 std::string id_raw = details["titlepic"]["id"].asString();
304 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
306 if ((id_raw != "") && (*endptr == 0)) {
307 retval.titlepic.id = numbervalue;
311 if (details["titlepic"]["file"].asString().size()) {
312 retval.titlepic.file = details["titlepic"]["file"].asString();
315 if (details["titlepic"]["desc"].asString().size()) {
316 retval.titlepic.description = details["titlepic"]["desc"].asString();
319 if (details["titlepic"]["mod"].asString().size()) {
321 std::string mod_raw = details["titlepic"]["mod"].asString();
323 int numbervalue = strtol(mod_raw.c_str(),&endptr,10);
325 if ((mod_raw != "") && (*endptr == 0)) {
326 retval.titlepic.mod = numbervalue;
332 if (details["id"].asString().size()) {
334 std::string id_raw = details["id"].asString();
336 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
338 if ((id_raw != "") && (*endptr == 0)) {
339 retval.id = numbervalue;
343 errorstream << "readModStoreModDetails: missing id" << std::endl;
344 retval.valid = false;
348 if (details["title"].asString().size()) {
349 retval.title = details["title"].asString();
352 errorstream << "readModStoreModDetails: missing title" << std::endl;
353 retval.valid = false;
357 if (details["basename"].asString().size()) {
358 retval.basename = details["basename"].asString();
361 errorstream << "readModStoreModDetails: missing basename" << std::endl;
362 retval.valid = false;
366 if (details["desc"].asString().size()) {
367 retval.description = details["desc"].asString();
371 if (details["replink"].asString().size()) {
372 retval.repository = details["replink"].asString();
376 if (details["rating"].asString().size()) {
378 std::string id_raw = details["rating"].asString();
380 float numbervalue = strtof(id_raw.c_str(),&endptr);
382 if ((id_raw != "") && (*endptr == 0)) {
383 retval.rating = numbervalue;
391 if (details["depends"].isArray()) {
396 if (details["softdep"].isArray()) {
401 if (details["screenshot_url"].asString().size()) {
402 retval.screenshot_url = details["screenshot_url"].asString();