aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Info.cpp
blob: 29e3a637fffe861b629b4491521d343ee18c45ec (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "../../plugins/Info.hpp"
#include "../../plugins/Saucenao.hpp"
#include "../../plugins/Youtube.hpp"
#include "../../plugins/Fourchan.hpp"
#include "../../include/StringUtils.hpp"
#include "../../include/Program.hpp"
#include "../../include/Notification.hpp"
#include "../../include/Storage.hpp"

namespace QuickMedia {
    static const char *REVERSE_IMAGE_SEARCH_URL = "reverse-image-search://";
    static const char *GOOGLE_SEARCH_URL = "google-search://";

    static bool is_youtube_url(const std::string &url) {
        std::string video_id;
        return youtube_url_extract_id(url, video_id);
    }

    static bool is_youtube_channel_url(const std::string &url) {
        std::string channel_id;
        std::string channel_url;
        return youtube_url_extract_channel_id(url, channel_id, channel_url);
    }

    static PluginResult open_with_browser(const std::string &url) {
        const char *launch_program = "xdg-open";
        if(!is_program_executable_by_name("xdg-open")) {
            launch_program = getenv("BROWSER");
            if(!launch_program) {
                show_notification("QuickMedia", "xdg-utils which provides xdg-open needs to be installed to open urls. Alternatively set the $BROWSER environment variable to a browser", Urgency::CRITICAL);
                return PluginResult::ERR;
            }
        }

        std::string url_modified = url;
        if(strncmp(url.c_str(), "http://", 7) != 0 && strncmp(url.c_str(), "https://", 8) != 0)
            url_modified = "https://" + url;

        const char *args[] = { launch_program, url_modified.c_str(), nullptr };
        return exec_program_async(args, nullptr) == 0 ? PluginResult::OK : PluginResult::ERR;
    }

    PluginResult InfoPage::submit(const SubmitArgs &args, std::vector<Tab> &result_tabs) {
        std::string converted_url = invidious_url_to_youtube_url(args.url);

        std::string board_id, thread_id, post_id;
        if(string_starts_with(args.url, REVERSE_IMAGE_SEARCH_URL)) {
            std::string image_url = args.url.substr(strlen(REVERSE_IMAGE_SEARCH_URL));
            result_tabs.push_back(Tab{create_body(), std::make_unique<SaucenaoPage>(program, image_url, false), nullptr});
            return PluginResult::OK;
        } else if(string_starts_with(args.url, GOOGLE_SEARCH_URL)) {
            const std::string search_term = args.url.substr(strlen(GOOGLE_SEARCH_URL));
            const std::string search_url = "https://www.google.com/search?q=" + url_param_encode(search_term);
            return open_with_browser(search_url);
        } else if(is_youtube_channel_url(converted_url)) {
            YoutubeChannelPage::create_each_type(program, std::move(converted_url), "", "Channel", result_tabs);
            return PluginResult::OK;
        } else if(is_youtube_url(converted_url)) {
            result_tabs.push_back(Tab{nullptr, std::make_unique<YoutubeVideoPage>(program, std::move(converted_url), false), nullptr});
            return PluginResult::OK;
        } else if(fourchan_extract_url(args.url, board_id, thread_id, post_id)) {
            result_tabs.push_back(Tab{create_body(), std::make_unique<FourchanThreadPage>(program, std::move(board_id), std::move(thread_id), std::move(post_id), ""), nullptr});
            return PluginResult::OK;
        } else {
            return open_with_browser(args.url);
        }
    }

    void InfoPage::copy_to_clipboard(const BodyItem *body_item) {
        std::string url;
        if(string_starts_with(body_item->url, REVERSE_IMAGE_SEARCH_URL)) {
            url = body_item->url.substr(strlen(REVERSE_IMAGE_SEARCH_URL));
        } else if(string_starts_with(body_item->url, GOOGLE_SEARCH_URL)) {
            url = body_item->url.substr(strlen(GOOGLE_SEARCH_URL));
        } else {
            url = body_item->url;
        }

        if(!url.empty())
            set_clipboard(url);
    }

    // static
    std::shared_ptr<BodyItem> InfoPage::add_url(const std::string &url) {
        std::string board_id, thread_id, comment_id;
        std::string title;

        if(is_youtube_channel_url(url))
            title = "Open youtube channel " + url;
        else if(is_youtube_url(url))
            title = "Play " + url;
        else if(fourchan_extract_url(url, board_id, thread_id, comment_id))
            title = "Open " + url;
        else
            title = "Open " + url + " in a browser";

        auto body_item = BodyItem::create(std::move(title));
        body_item->url = url;
        return body_item;
    }

    // static
    std::shared_ptr<BodyItem> InfoPage::add_reverse_image_search(const std::string &image_url) {
        auto body_item = BodyItem::create("Reverse search the image with SauceNAO");
        body_item->url = REVERSE_IMAGE_SEARCH_URL + image_url;
        return body_item;
    }

    // static
    std::shared_ptr<BodyItem> InfoPage::add_google_search(const std::string &search_term) {
        auto body_item = BodyItem::create("Search for \"" + search_term + "\" with google search");
        body_item->url = GOOGLE_SEARCH_URL + search_term;
        return body_item;
    }
}