diff options
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/Info.cpp | 59 | ||||
-rw-r--r-- | src/plugins/Page.cpp | 24 | ||||
-rw-r--r-- | src/plugins/Saucenao.cpp | 73 |
3 files changed, 142 insertions, 14 deletions
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 <SFML/Window/Clipboard.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) { 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<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(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<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; + 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<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; + } }
\ No newline at end of file diff --git a/src/plugins/Page.cpp b/src/plugins/Page.cpp index 935d214..6cf3790 100644 --- a/src/plugins/Page.cpp +++ b/src/plugins/Page.cpp @@ -3,6 +3,7 @@ #include "../../include/Theme.hpp" #include "../../include/Storage.hpp" #include "../../include/QuickMedia.hpp" +#include <SFML/Window/Clipboard.hpp> #include <json/reader.h> namespace QuickMedia { @@ -44,6 +45,29 @@ namespace QuickMedia { return program->load_manga_content_storage(service_name, manga_title, manga_url, manga_id); } + void Page::copy_to_clipboard(const BodyItem *body_item) const { + std::string title = body_item->get_title(); + std::string author = body_item->get_author(); + std::string description = body_item->get_description(); + + std::string clipboard = std::move(title); + + if(!author.empty()) { + if(!clipboard.empty()) + clipboard += '\n'; + clipboard += std::move(author); + } + + 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())); + } + PluginResult BookmarksPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) { return redirect_page->submit(title, url, result_tabs); } diff --git a/src/plugins/Saucenao.cpp b/src/plugins/Saucenao.cpp index d81e233..ccb01f9 100644 --- a/src/plugins/Saucenao.cpp +++ b/src/plugins/Saucenao.cpp @@ -1,8 +1,78 @@ #include "../../plugins/Saucenao.hpp" +#include "../../plugins/Info.hpp" #include "../../include/StringUtils.hpp" #include <quickmedia/HtmlSearch.h> namespace QuickMedia { + class SaucenaoBodyItemUrls : public BodyItemExtra { + public: + std::vector<std::string> urls; + }; + + static int string_views_equal_case_insensitive(const QuickMediaStringView str1, const QuickMediaStringView str2) { + if(str2.size != str1.size) + return 1; + + for(size_t i = 0; i < str1.size; ++i) { + char c1 = str1.data[i]; + char c2 = str2.data[i]; + if(to_upper(c1) != to_upper(c2)) + return 1; + } + + return 0; + } + + static QuickMediaHtmlAttribute* get_attribute_by_name(QuickMediaHtmlNode *node, QuickMediaStringView name) { + for(QuickMediaHtmlAttribute *attr = node->first_attribute; attr; attr = attr->next) { + if(string_views_equal_case_insensitive(attr->key, name) == 0) + return attr; + } + return NULL; + } + + static void match_node_get_urls(QuickMediaHtmlNode *node, std::vector<std::string> &urls) { + if(node->is_tag && string_views_equal_case_insensitive(node->name, {"a", 1}) == 0) { + QuickMediaHtmlAttribute *attr = get_attribute_by_name(node, {"href", 4}); + if(attr) + urls.emplace_back(attr->value.data, attr->value.size); + } + + QuickMediaHtmlChildNode *child_node = node->first_child; + while(child_node) { + match_node_get_urls(&child_node->node, urls); + child_node = child_node->next; + } + } + + static std::string get_first_line(const std::string &str) { + const size_t line_end = str.find('\n'); + if(line_end == std::string::npos) + return str; + else + return str.substr(0, line_end); + } + + PluginResult SaucenaoPage::submit(const std::string &title, const std::string&, std::vector<Tab> &result_tabs) { + if(!submit_body_item) + return PluginResult::OK; + + SaucenaoBodyItemUrls *urls = static_cast<SaucenaoBodyItemUrls*>(submit_body_item->extra.get()); + BodyItems body_items; + + for(const std::string &url : urls->urls) { + // Ignore saucenao info pages + if(url.find("saucenao.com") == std::string::npos) + body_items.push_back(InfoPage::add_url(url)); + } + body_items.push_back(InfoPage::add_google_search(get_first_line(title))); + + auto body = create_body(); + body->set_items(std::move(body_items)); + result_tabs.push_back(Tab{ std::move(body), std::make_unique<InfoPage>(program), create_search_bar("Search...", SEARCH_DELAY_FILTER) }); + return PluginResult::OK; + } + PluginResult SaucenaoPage::lazy_fetch(BodyItems &result_items) { std::vector<CommandArg> additional_args; if(is_local) { @@ -31,6 +101,9 @@ namespace QuickMedia { title = title.erase(0, p_index + 1); title = strip(title); auto item = BodyItem::create(title); + auto urls = std::make_shared<SaucenaoBodyItemUrls>(); + match_node_get_urls(node->node, urls->urls); + item->extra = std::move(urls); item_data->push_back(std::move(item)); } return 0; |