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 std::vector<std::string> *extra_headers) {
37 HTTPFetchRequest fetchrequest;
38 HTTPFetchResult fetchresult;
39 fetchrequest.url = url;
40 fetchrequest.caller = HTTPFETCH_SYNC;
42 if (extra_headers != NULL)
43 fetchrequest.extra_headers = *extra_headers;
45 httpfetch_sync(fetchrequest,fetchresult);
47 if (!fetchresult.succeeded) {
52 std::istringstream stream(fetchresult.data);
54 if (!reader.parse( stream, root ) )
56 errorstream << "URL: " << url << std::endl;
57 errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
58 errorstream << "data: \"" << fetchresult.data << "\"" << std::endl;
65 if ((root["list"].isArray())) {
75 std::vector<ModStoreMod> readModStoreList(Json::Value& modlist) {
76 std::vector<ModStoreMod> retval;
78 if (modlist.isArray()) {
79 for (unsigned int i = 0; i < modlist.size(); i++)
85 if (modlist[i]["id"].asString().size()) {
86 std::string id_raw = modlist[i]["id"].asString();
88 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
90 if ((id_raw != "") && (*endptr == 0)) {
91 toadd.id = numbervalue;
94 errorstream << "readModStoreList: missing id" << std::endl;
99 errorstream << "readModStoreList: missing id" << std::endl;
104 if (modlist[i]["title"].asString().size()) {
105 toadd.title = modlist[i]["title"].asString();
108 errorstream << "readModStoreList: missing title" << std::endl;
113 if (modlist[i]["basename"].asString().size()) {
114 toadd.basename = modlist[i]["basename"].asString();
117 errorstream << "readModStoreList: missing basename" << std::endl;
128 retval.push_back(toadd);
135 ModStoreModDetails readModStoreModDetails(Json::Value& details) {
137 ModStoreModDetails retval;
142 if (details["version_set"].isArray()) {
143 for (unsigned int i = 0; i < details["version_set"].size(); i++)
145 ModStoreVersionEntry toadd;
147 if (details["version_set"][i]["id"].asString().size()) {
148 std::string id_raw = details["version_set"][i]["id"].asString();
150 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
152 if ((id_raw != "") && (*endptr == 0)) {
153 toadd.id = numbervalue;
157 errorstream << "readModStoreModDetails: missing version_set id" << std::endl;
158 retval.valid = false;
162 if (details["version_set"][i]["date"].asString().size()) {
163 toadd.date = details["version_set"][i]["date"].asString();
167 if (details["version_set"][i]["file"].asString().size()) {
168 toadd.file = details["version_set"][i]["file"].asString();
171 errorstream << "readModStoreModDetails: missing version_set file" << std::endl;
172 retval.valid = false;
180 retval.versions.push_back(toadd);
188 if (retval.versions.size() < 1) {
189 infostream << "readModStoreModDetails: not a single version specified!" << std::endl;
190 retval.valid = false;
194 if (details["categories"].isObject()) {
195 for (unsigned int i = 0; i < details["categories"].size(); i++) {
196 ModStoreCategoryInfo toadd;
198 if (details["categories"][i]["id"].asString().size()) {
200 std::string id_raw = details["categories"][i]["id"].asString();
202 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
204 if ((id_raw != "") && (*endptr == 0)) {
205 toadd.id = numbervalue;
209 errorstream << "readModStoreModDetails: missing categories id" << std::endl;
210 retval.valid = false;
212 if (details["categories"][i]["title"].asString().size()) {
213 toadd.name = details["categories"][i]["title"].asString();
216 errorstream << "readModStoreModDetails: missing categories title" << std::endl;
217 retval.valid = false;
221 retval.categories.push_back(toadd);
230 if (details["author"].isObject()) {
231 if (details["author"]["id"].asString().size()) {
233 std::string id_raw = details["author"]["id"].asString();
235 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
237 if ((id_raw != "") && (*endptr == 0)) {
238 retval.author.id = numbervalue;
241 errorstream << "readModStoreModDetails: missing author id (convert)" << std::endl;
242 retval.valid = false;
246 errorstream << "readModStoreModDetails: missing author id" << std::endl;
247 retval.valid = false;
250 if (details["author"]["username"].asString().size()) {
251 retval.author.username = details["author"]["username"].asString();
254 errorstream << "readModStoreModDetails: missing author username" << std::endl;
255 retval.valid = false;
259 errorstream << "readModStoreModDetails: missing author" << std::endl;
260 retval.valid = false;
264 if (details["license"].isObject()) {
265 if (details["license"]["id"].asString().size()) {
267 std::string id_raw = details["license"]["id"].asString();
269 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
271 if ((id_raw != "") && (*endptr == 0)) {
272 retval.license.id = numbervalue;
276 errorstream << "readModStoreModDetails: missing license id" << std::endl;
277 retval.valid = false;
280 if (details["license"]["short"].asString().size()) {
281 retval.license.shortinfo = details["license"]["short"].asString();
284 errorstream << "readModStoreModDetails: missing license short" << std::endl;
285 retval.valid = false;
288 if (details["license"]["link"].asString().size()) {
289 retval.license.url = details["license"]["link"].asString();
295 if (details["titlepic"].isObject()) {
296 if (details["titlepic"]["id"].asString().size()) {
298 std::string id_raw = details["titlepic"]["id"].asString();
300 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
302 if ((id_raw != "") && (*endptr == 0)) {
303 retval.titlepic.id = numbervalue;
307 if (details["titlepic"]["file"].asString().size()) {
308 retval.titlepic.file = details["titlepic"]["file"].asString();
311 if (details["titlepic"]["desc"].asString().size()) {
312 retval.titlepic.description = details["titlepic"]["desc"].asString();
315 if (details["titlepic"]["mod"].asString().size()) {
317 std::string mod_raw = details["titlepic"]["mod"].asString();
319 int numbervalue = strtol(mod_raw.c_str(),&endptr,10);
321 if ((mod_raw != "") && (*endptr == 0)) {
322 retval.titlepic.mod = numbervalue;
328 if (details["id"].asString().size()) {
330 std::string id_raw = details["id"].asString();
332 int numbervalue = strtol(id_raw.c_str(),&endptr,10);
334 if ((id_raw != "") && (*endptr == 0)) {
335 retval.id = numbervalue;
339 errorstream << "readModStoreModDetails: missing id" << std::endl;
340 retval.valid = false;
344 if (details["title"].asString().size()) {
345 retval.title = details["title"].asString();
348 errorstream << "readModStoreModDetails: missing title" << std::endl;
349 retval.valid = false;
353 if (details["basename"].asString().size()) {
354 retval.basename = details["basename"].asString();
357 errorstream << "readModStoreModDetails: missing basename" << std::endl;
358 retval.valid = false;
362 if (details["desc"].asString().size()) {
363 retval.description = details["desc"].asString();
367 if (details["replink"].asString().size()) {
368 retval.repository = details["replink"].asString();
372 if (details["rating"].asString().size()) {
374 std::string id_raw = details["rating"].asString();
376 float numbervalue = strtof(id_raw.c_str(),&endptr);
378 if ((id_raw != "") && (*endptr == 0)) {
379 retval.rating = numbervalue;
387 if (details["depends"].isArray()) {
392 if (details["softdep"].isArray()) {
397 if (details["screenshot_url"].asString().size()) {
398 retval.screenshot_url = details["screenshot_url"].asString();