aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Page.cpp
blob: 5c6418313a062a2af6197fd00acbde1b8bda9938 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "../../plugins/Page.hpp"
#include "../../include/StringUtils.hpp"
#include "../../include/Theme.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);
    }

    PluginResult BookmarksPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) {
        return redirect_page->submit(title, url, result_tabs);
    }

    PluginResult BookmarksPage::lazy_fetch(BodyItems &result_items) {
        const char *bookmark_name = redirect_page->get_bookmark_name();
        if(!bookmark_name)
            return PluginResult::ERR;

        Path bookmark_path = get_storage_dir().join("bookmarks").join(bookmark_name);
        Json::Value json_root;
        if(!read_file_as_json(bookmark_path, json_root) || !json_root.isArray())
            return PluginResult::OK;

        const time_t time_now = time(nullptr);
        for(const Json::Value &item_json : json_root) {
            if(!item_json.isObject())
                continue;

            const Json::Value &title_json = item_json["title"];
            const Json::Value &author_json = item_json["author"];
            const Json::Value &url_json = item_json["url"];
            const Json::Value &timestamp_json = item_json["timestamp"];

            auto body_item = BodyItem::create(title_json.isString() ? title_json.asString() : "");
            if(author_json.isString())
                body_item->set_author(author_json.asString());
            if(url_json.isString())
                body_item->url = url_json.asString();
            if(timestamp_json.isInt64()) {
                body_item->set_description("Bookmarked " + seconds_to_relative_time_str(time_now - timestamp_json.asInt64()));
                body_item->set_description_color(get_current_theme().faded_text_color);
            }
            result_items.push_back(std::move(body_item));
        }

        std::reverse(result_items.begin(), result_items.end());
        return PluginResult::OK;
    }
}