From 177eade2d2c09e8a9ef4c320a0f0426d2480e07b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 17 May 2021 19:28:51 +0200 Subject: Add ctrl+i to either open urls in browser or reverse image search. Also fix ctrl+c copy with missing title --- src/NetUtils.cpp | 2 ++ src/QuickMedia.cpp | 65 ++++++++++++++++++++++++++++++++++++++++------------ src/plugins/Info.cpp | 47 +++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 15 deletions(-) create mode 100644 src/plugins/Info.cpp (limited to 'src') diff --git a/src/NetUtils.cpp b/src/NetUtils.cpp index de908b1..f120cc0 100644 --- a/src/NetUtils.cpp +++ b/src/NetUtils.cpp @@ -1636,6 +1636,8 @@ namespace QuickMedia { // Also checks for balanced parentheses to allow text such as: (see: example.com/) that excludes the last parenthesis. std::vector extract_urls(const std::string &str) { std::vector ranges; + if(str.empty()) + return ranges; int parentheses_depth = 0; bool is_valid_url = false; diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index ea77bff..dfec05f 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -13,6 +13,7 @@ #include "../plugins/FileManager.hpp" #include "../plugins/Pipe.hpp" #include "../plugins/Saucenao.hpp" +#include "../plugins/Info.hpp" #include "../include/Scale.hpp" #include "../include/Program.hpp" #include "../include/VideoPlayer.hpp" @@ -1411,6 +1412,46 @@ namespace QuickMedia { pipe_selected_text = text; } + static bool is_url_video(const std::string &url) { + return string_ends_with(url, ".webm") || string_ends_with(url, ".mp4") || string_ends_with(url, ".mkv") || string_ends_with(url, ".gif"); + } + + bool Program::show_info_page(BodyItem *body_item, bool include_reverse_image_search) { + if(!body_item) + return false; + + std::string title = body_item->get_title(); + std::string description = body_item->get_description(); + std::string text = std::move(title); + if(!description.empty()) { + if(!text.empty()) + text += '\n'; + text += std::move(description); + } + + auto body = create_body(); + + if(include_reverse_image_search && !body_item->url.empty() && !body_item->thumbnail_url.empty()) { + std::string image_url = body_item->url; + if(is_url_video(body_item->url)) + image_url = body_item->thumbnail_url; + body->items.push_back(InfoPage::add_reverse_image_search(image_url)); + } + + std::vector urls = ranges_get_strings(text, extract_urls(text)); + for(const std::string &url : urls) { + body->items.push_back(InfoPage::add_url(url)); + } + + if(body->items.empty()) + return false; + + std::vector info_tabs; + info_tabs.push_back(Tab{std::move(body), std::make_unique(this), nullptr}); + page_loop(info_tabs); + return true; + } + void Program::page_loop_render(sf::RenderWindow &window, std::vector &tabs, int selected_tab, TabAssociatedData &tab_associated_data, const Json::Value *json_chapters, Tabs &ui_tabs) { if(tabs[selected_tab].search_bar) tabs[selected_tab].search_bar->draw(window, window_size, true); @@ -1770,14 +1811,19 @@ namespace QuickMedia { if(selected_item) { std::string title = selected_item->get_title(); std::string description = selected_item->get_description(); - std::string clipboard = title; - if(!clipboard.empty()) { - clipboard += '\n'; + std::string clipboard = std::move(title); + if(!description.empty()) { + if(!clipboard.empty()) + clipboard += '\n'; clipboard += std::move(description); } if(!clipboard.empty()) sf::Clipboard::setString(sf::String::fromUtf8(clipboard.begin(), clipboard.end())); } + } else if(event.key.code == sf::Keyboard::I && event.key.control) { + BodyItem *selected_item = tabs[selected_tab].body->get_selected(); + if(show_info_page(selected_item, false)) + redraw = true; } } } @@ -2995,10 +3041,6 @@ namespace QuickMedia { idle = true; } - static bool is_url_video(const std::string &url) { - return string_ends_with(url, ".webm") || string_ends_with(url, ".mp4") || string_ends_with(url, ".gif"); - } - void Program::image_board_thread_page(ImageBoardThreadPage *thread_page, Body *thread_body) { // TODO: Instead of using stage here, use different pages for each stage enum class NavigationStage { @@ -3294,14 +3336,7 @@ namespace QuickMedia { } } else if(event.key.code == sf::Keyboard::I && event.key.control) { BodyItem *selected_item = thread_body->get_selected(); - if(selected_item && !selected_item->url.empty() && !selected_item->thumbnail_url.empty()) { - std::string image_url = selected_item->url; - if(is_url_video(selected_item->url)) - image_url = selected_item->thumbnail_url; - - std::vector saucenao_tabs; - saucenao_tabs.push_back(Tab{create_body(), std::make_unique(this, image_url, false), nullptr}); - page_loop(saucenao_tabs); + if(show_info_page(selected_item, true)) { redraw = true; frame_skip_text_entry = true; } diff --git a/src/plugins/Info.cpp b/src/plugins/Info.cpp new file mode 100644 index 0000000..b1943b3 --- /dev/null +++ b/src/plugins/Info.cpp @@ -0,0 +1,47 @@ +#include "../../plugins/Info.hpp" +#include "../../plugins/Saucenao.hpp" +#include "../../include/StringUtils.hpp" +#include "../../include/Program.hpp" +#include "../../include/Notification.hpp" + +namespace QuickMedia { + static const char *REVERSE_IMAGE_SEARCH_URL = "reverse-image-search://"; + + 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 { + 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 InfoPage::add_url(const std::string &url) { + auto body_item = BodyItem::create("Open " + url + " in a browser"); + body_item->url = url; + return body_item; + } + + // static + std::shared_ptr 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; + } +} \ No newline at end of file -- cgit v1.2.3