From d74766245facd48805c2576c711d20cafa33aa35 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 18 Sep 2021 18:07:10 +0200 Subject: Show option to open saucenao result urls ctrl+c for info should only copy the url --- src/plugins/Info.cpp | 59 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 14 deletions(-) (limited to 'src/plugins/Info.cpp') diff --git a/src/plugins/Info.cpp b/src/plugins/Info.cpp index b488eba..05efc44 100644 --- a/src/plugins/Info.cpp +++ b/src/plugins/Info.cpp @@ -5,39 +5,63 @@ #include "../../include/Program.hpp" #include "../../include/Notification.hpp" #include "../../include/Storage.hpp" +#include 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) { return url.find("youtube.com/") != std::string::npos || url.find("youtu.be/") != std::string::npos; } + 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 std::string&, const std::string &url, std::vector &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(program, image_url, false), nullptr}); return PluginResult::OK; + } else if(string_starts_with(url, GOOGLE_SEARCH_URL)) { + const std::string search_term = 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_url(url)) { result_tabs.push_back(Tab{nullptr, std::make_unique(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; + return open_with_browser(url); + } + } - const char *args[] = { launch_program, url_modified.c_str(), nullptr }; - return exec_program_async(args, nullptr) == 0 ? PluginResult::OK : PluginResult::ERR; + void InfoPage::copy_to_clipboard(const BodyItem *body_item) const { + 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()) + sf::Clipboard::setString(sf::String::fromUtf8(url.begin(), url.end())); } // static @@ -58,4 +82,11 @@ namespace QuickMedia { body_item->url = REVERSE_IMAGE_SEARCH_URL + image_url; return body_item; } + + // static + std::shared_ptr 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; + } } \ No newline at end of file -- cgit v1.2.3