aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Info.cpp
blob: b488eba432f53cfee75583cfa9734d749ec549e9 (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/Info.hpp"
#include "../../plugins/Saucenao.hpp"
#include "../../plugins/Youtube.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 bool is_youtube_url(const std::string &url) {
        return url.find("youtube.com/") != std::string::npos || url.find("youtu.be/") != std::string::npos;
    }

    PluginResult InfoPage::submit(const std::string&, const std::string &url, std::vector<Tab> &result_tabs) {
        if(string_starts_with(url, REVERSE_IMAGE_SEARCH_URL)) {
            std::string image_url = 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(is_youtube_url(url)) {
            result_tabs.push_back(Tab{nullptr, std::make_unique<YoutubeVideoPage>(program, url), nullptr});
            return PluginResult::OK;
        } else {
            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;
        }
    }

    // static
    std::shared_ptr<BodyItem> InfoPage::add_url(const std::string &url) {
        std::string title;
        if(is_youtube_url(url))
            title = "Play " + 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;
    }
}