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"
33 Json::Value fetchJsonValue(const std::string url,
34 struct curl_slist *chunk) {
37 HTTPFetchRequest fetchrequest;
38 HTTPFetchResult fetchresult;
39 fetchrequest.url = url;
40 fetchrequest.caller = HTTPFETCH_SYNC;
42 struct curl_slist* runptr = chunk;
44 fetchrequest.extra_headers.push_back(runptr->data);
45 runptr = runptr->next;
47 httpfetch_sync(fetchrequest,fetchresult);
49 if (!fetchresult.succeeded) {
54 std::istringstream stream(fetchresult.data);
56 if (!reader.parse( stream, root ) )
58 errorstream << "URL: " << url << std::endl;
59 errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
60 errorstream << "data: \"" << fetchresult.data << "\"" << std::endl;
67 if ((root["list"].isArray())) {
77 std::vector<ModStoreMod> readModStoreList(Json::Value& modlist) {
78 std::vector<ModStoreMod> retval;
80 if (modlist.isArray()) {
81 for (unsigned int i = 0; i < modlist.size(); i++)
87 if (modlist[i]["id"].asString().size()) {
88 std::string id_raw = modlist[i]["id"].asString();
90 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
92 if ((id_raw != "") && (*endptr == 0)) {
93 toadd.id = numbervalue;
96 errorstream << "readModStoreList: missing id" << std::endl;
101 errorstream << "readModStoreList: missing id" << std::endl;
106 if (modlist[i]["title"].asString().size()) {
107 toadd.title = modlist[i]["title"].asString();
110 errorstream << "readModStoreList: missing title" << std::endl;
115 if (modlist[i]["basename"].asString().size()) {
116 toadd.basename = modlist[i]["basename"].asString();
119 errorstream << "readModStoreList: missing basename" << std::endl;
130 retval.push_back(toadd);
137 ModStoreModDetails readModStoreModDetails(Json::Value& details) {
139 ModStoreModDetails retval;
144 if (details["version_set"].isArray()) {
145 for (unsigned int i = 0; i < details["version_set"].size(); i++)
147 ModStoreVersionEntry toadd;
149 if (details["version_set"][i]["id"].asString().size()) {
150 std::string id_raw = details["version_set"][i]["id"].asString();
152 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
154 if ((id_raw != "") && (*endptr == 0)) {
155 toadd.id = numbervalue;
159 errorstream << "readModStoreModDetails: missing version_set id" << std::endl;
160 retval.valid = false;
164 if (details["version_set"][i]["date"].asString().size()) {
165 toadd.date = details["version_set"][i]["date"].asString();
169 if (details["version_set"][i]["file"].asString().size()) {
170 toadd.file = details["version_set"][i]["file"].asString();
173 errorstream << "readModStoreModDetails: missing version_set file" << std::endl;
174 retval.valid = false;
182 retval.versions.push_back(toadd);
190 if (retval.versions.size() < 1) {
191 infostream << "readModStoreModDetails: not a single version specified!" << std::endl;
192 retval.valid = false;
196 if (details["categories"].isObject()) {
197 for (unsigned int i = 0; i < details["categories"].size(); i++) {
198 ModStoreCategoryInfo toadd;
200 if (details["categories"][i]["id"].asString().size()) {
202 std::string id_raw = details["categories"][i]["id"].asString();
204 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
206 if ((id_raw != "") && (*endptr == 0)) {
207 toadd.id = numbervalue;
211 errorstream << "readModStoreModDetails: missing categories id" << std::endl;
212 retval.valid = false;
214 if (details["categories"][i]["title"].asString().size()) {
215 toadd.name = details["categories"][i]["title"].asString();
218 errorstream << "readModStoreModDetails: missing categories title" << std::endl;
219 retval.valid = false;
223 retval.categories.push_back(toadd);
232 if (details["author"].isObject()) {
233 if (details["author"]["id"].asString().size()) {
235 std::string id_raw = details["author"]["id"].asString();
237 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
239 if ((id_raw != "") && (*endptr == 0)) {
240 retval.author.id = numbervalue;
243 errorstream << "readModStoreModDetails: missing author id (convert)" << std::endl;
244 retval.valid = false;
248 errorstream << "readModStoreModDetails: missing author id" << std::endl;
249 retval.valid = false;
252 if (details["author"]["username"].asString().size()) {
253 retval.author.username = details["author"]["username"].asString();
256 errorstream << "readModStoreModDetails: missing author username" << std::endl;
257 retval.valid = false;
261 errorstream << "readModStoreModDetails: missing author" << std::endl;
262 retval.valid = false;
266 if (details["license"].isObject()) {
267 if (details["license"]["id"].asString().size()) {
269 std::string id_raw = details["license"]["id"].asString();
271 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
273 if ((id_raw != "") && (*endptr == 0)) {
274 retval.license.id = numbervalue;
278 errorstream << "readModStoreModDetails: missing license id" << std::endl;
279 retval.valid = false;
282 if (details["license"]["short"].asString().size()) {
283 retval.license.shortinfo = details["license"]["short"].asString();
286 errorstream << "readModStoreModDetails: missing license short" << std::endl;
287 retval.valid = false;
290 if (details["license"]["link"].asString().size()) {
291 retval.license.url = details["license"]["link"].asString();
297 if (details["titlepic"].isObject()) {
298 if (details["titlepic"]["id"].asString().size()) {
300 std::string id_raw = details["titlepic"]["id"].asString();
302 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
304 if ((id_raw != "") && (*endptr == 0)) {
305 retval.titlepic.id = numbervalue;
309 if (details["titlepic"]["file"].asString().size()) {
310 retval.titlepic.file = details["titlepic"]["file"].asString();
313 if (details["titlepic"]["desc"].asString().size()) {
314 retval.titlepic.description = details["titlepic"]["desc"].asString();
317 if (details["titlepic"]["mod"].asString().size()) {
319 std::string mod_raw = details["titlepic"]["mod"].asString();
321 int numbervalue = strtol(mod_raw.c_str(),&endptr,10);
323 if ((mod_raw != "") && (*endptr == 0)) {
324 retval.titlepic.mod = numbervalue;
330 if (details["id"].asString().size()) {
332 std::string id_raw = details["id"].asString();
334 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
336 if ((id_raw != "") && (*endptr == 0)) {
337 retval.id = numbervalue;
341 errorstream << "readModStoreModDetails: missing id" << std::endl;
342 retval.valid = false;
346 if (details["title"].asString().size()) {
347 retval.title = details["title"].asString();
350 errorstream << "readModStoreModDetails: missing title" << std::endl;
351 retval.valid = false;
355 if (details["basename"].asString().size()) {
356 retval.basename = details["basename"].asString();
359 errorstream << "readModStoreModDetails: missing basename" << std::endl;
360 retval.valid = false;
364 if (details["desc"].asString().size()) {
365 retval.description = details["desc"].asString();
369 if (details["replink"].asString().size()) {
370 retval.repository = details["replink"].asString();
374 if (details["rating"].asString().size()) {
376 std::string id_raw = details["rating"].asString();
378 float numbervalue = strtof(id_raw.c_str(),&endptr);
380 if ((id_raw != "") && (*endptr == 0)) {
381 retval.rating = numbervalue;
389 if (details["depends"].isArray()) {
394 if (details["softdep"].isArray()) {
399 if (details["screenshot_url"].asString().size()) {
400 retval.screenshot_url = details["screenshot_url"].asString();