aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/MangaCombined.cpp
blob: 994bb4746ead4819433f7dd853e53952306900ee (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
#include "../../plugins/MangaCombined.hpp"

namespace QuickMedia {
    MangaCombinedSearchPage::MangaCombinedSearchPage(Program *program, std::vector<MangaPlugin> search_pages) :
        Page(program), search_pages(std::move(search_pages))
    {
        
    }

    static void result_items_add_thread_results(std::vector<std::pair<MangaPlugin*, std::future<BodyItems>>> &search_threads, BodyItems &result_items) {
        for(auto &search_thread : search_threads) {
            BodyItems search_page_body_items = search_thread.second.get();
            if(search_page_body_items.empty())
                continue;

            auto title_item = BodyItem::create("");
            title_item->set_author("======================== " + search_thread.first->title + " ========================");
            title_item->url = search_thread.first->service_name;

            result_items.push_back(std::move(title_item));
            for(auto &new_body_item : search_page_body_items) {
                new_body_item->userdata = search_thread.first->page.get();
            }
            result_items.insert(result_items.end(), std::move_iterator(search_page_body_items.begin()), std::move_iterator(search_page_body_items.end()));
        }
    }

    SearchResult MangaCombinedSearchPage::search(const std::string &str, BodyItems &result_items) {
        std::vector<std::pair<MangaPlugin*, std::future<BodyItems>>> search_threads;
        for(auto &search_page : search_pages) {
            search_threads.push_back(std::make_pair(&search_page, std::async(std::launch::async, [&str, &search_page]() {
                BodyItems search_page_body_items;
                search_page.page->search(str, search_page_body_items);
                return search_page_body_items;
            })));
        }

        result_items_add_thread_results(search_threads, result_items);
        return SearchResult::OK;
    }

    PluginResult MangaCombinedSearchPage::get_page(const std::string &str, int page, BodyItems &result_items) {
        std::vector<std::pair<MangaPlugin*, std::future<BodyItems>>> search_threads;
        for(auto &search_page : search_pages) {
            search_threads.push_back(std::make_pair(&search_page, std::async(std::launch::async, [&str, page, &search_page]() {
                BodyItems search_page_body_items;
                search_page.page->get_page(str, page, search_page_body_items);
                return search_page_body_items;
            })));
        }

        result_items_add_thread_results(search_threads, result_items);
        return PluginResult::OK;
    }

    PluginResult MangaCombinedSearchPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) {
        Page *page = (Page*)submit_body_item->userdata;
        if(!page) return PluginResult::OK;
        return page->submit(title, url, result_tabs);
    }
}