aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Page.cpp
blob: 8605d822a8fdeda92beb9d94900d974f3b73c0b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "../../plugins/Page.hpp"
#include "../../include/QuickMedia.hpp"
#include <json/reader.h>

namespace QuickMedia {
    DownloadResult Page::download_json(Json::Value &result, const std::string &url, std::vector<CommandArg> additional_args, bool use_browser_useragent, std::string *err_msg) {
        std::string server_response;
        if(download_to_string(url, server_response, std::move(additional_args), use_browser_useragent, err_msg == nullptr) != DownloadResult::OK) {
            if(err_msg)
                *err_msg = server_response;
            return DownloadResult::NET_ERR;
        }

        if(server_response.empty()) {
            result = Json::Value::nullSingleton();
            return DownloadResult::OK;
        }

        Json::CharReaderBuilder json_builder;
        std::unique_ptr<Json::CharReader> json_reader(json_builder.newCharReader());
        std::string json_errors;
        if(!json_reader->parse(&server_response[0], &server_response[server_response.size()], &result, &json_errors)) {
            fprintf(stderr, "download_json error: %s\n", json_errors.c_str());
            if(err_msg)
                *err_msg = std::move(json_errors);
            return DownloadResult::ERR;
        }

        return DownloadResult::OK;
    }

    std::unique_ptr<Body> Page::create_body(bool plain_text_list, bool prefer_card_view) {
        return program->create_body(plain_text_list, prefer_card_view);
    }

    std::unique_ptr<SearchBar> Page::create_search_bar(const std::string &placeholder_text, int search_delay) {
        return program->create_search_bar(placeholder_text, search_delay);
    }

    bool Page::load_manga_content_storage(const char *service_name, const std::string &manga_title, const std::string &manga_id) {
        return program->load_manga_content_storage(service_name, manga_title, manga_id);
    }
}