From d6131b8ba482414be76f2478aea90bd7a4a2379b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 14 Sep 2020 18:37:00 +0200 Subject: Add support for 4chan pass --- src/QuickMedia.cpp | 130 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 108 insertions(+), 22 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 051fdc7..b464956 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -318,7 +318,7 @@ namespace QuickMedia { plugin_logo.setSmooth(true); } - search_bar = std::make_unique(font, plugin_logo, search_placeholder); + search_bar = std::make_unique(font, &plugin_logo, search_placeholder); search_bar->text_autosearch_delay = current_plugin->get_search_delay(); while(window.isOpen()) { @@ -421,8 +421,9 @@ namespace QuickMedia { } } else if(handle_searchbar && event.type == sf::Event::TextEntered) { search_bar->onTextEntered(event.text.unicode); + } else if(handle_searchbar) { + search_bar->on_event(event); } - search_bar->on_event(event); } static std::string base64_encode(const std::string &data) { @@ -471,7 +472,8 @@ namespace QuickMedia { enum class SearchSuggestionTab { ALL, HISTORY, - RECOMMENDED + RECOMMENDED, + LOGIN }; // Returns relative time as a string (approximation) @@ -734,10 +736,30 @@ namespace QuickMedia { body_size = sf::Vector2f(body_width, window_size.y - body_padding_vertical - related_videos_text_height); } + class LoginTab { + public: + LoginTab(sf::Font &font) : + username(std::make_unique(font, nullptr, "Token...")), + password(std::make_unique(font, nullptr, "PIN...", true)) + { + + } + std::unique_ptr username; + std::unique_ptr password; + }; + + struct Tab { + Body *body; + std::unique_ptr login_tab; + SearchSuggestionTab tab; + sf::Text *text; + }; + void Program::search_suggestion_page() { std::string update_search_text; bool search_running = false; bool typing = false; + bool is_fourchan = current_plugin->name == "4chan"; std::string autocomplete_text; bool autocomplete_running = false; @@ -747,6 +769,8 @@ namespace QuickMedia { sf::Text all_tab_text("All", font, tab_text_size); sf::Text history_tab_text("History", font, tab_text_size); sf::Text recommended_tab_text("Recommended", font, tab_text_size); + sf::Text login_tab_text("Login", font, tab_text_size); + SearchBar *focused_login_input = nullptr; if(current_plugin->name == "youtube") { recommended_body = std::make_unique(this, &font, &bold_font); @@ -754,17 +778,48 @@ namespace QuickMedia { fill_recommended_items_from_json(load_recommended_json(current_plugin), recommended_body->items); } - struct Tab { - Body *body; - SearchSuggestionTab tab; - sf::Text *text; + std::vector tabs; + int selected_tab = 0; + + auto login_submit_callback = [this, &tabs, &selected_tab](const std::string &text) -> bool { + if(!tabs[selected_tab].body) { + std::string username = tabs[selected_tab].login_tab->username->get_text(); + std::string password = tabs[selected_tab].login_tab->password->get_text(); + if(current_plugin->name == "4chan") { + std::string response_msg; + PluginResult result = static_cast(current_plugin)->login(username, password, response_msg); + if(result == PluginResult::NET_ERR) { + show_notification("4chan", "Login failed!", Urgency::CRITICAL); + } else if(result == PluginResult::ERR) { + std::string desc = "Login failed, reason: "; + if(response_msg.empty()) + desc += "Unknown"; + else + desc += response_msg; + show_notification("4chan", desc, Urgency::CRITICAL); + } else if(result == PluginResult::OK) { + show_notification("4chan", "Successfully logged in!", Urgency::LOW); + selected_tab = 0; + } + } + } + return false; }; - std::vector tabs = { Tab{body, SearchSuggestionTab::ALL, &all_tab_text}, Tab{&history_body, SearchSuggestionTab::HISTORY, &history_tab_text} }; + tabs.push_back(Tab{body, nullptr, SearchSuggestionTab::ALL, &all_tab_text}); + tabs.push_back(Tab{&history_body, nullptr, SearchSuggestionTab::HISTORY, &history_tab_text}); if(recommended_body) - tabs.push_back(Tab{recommended_body.get(), SearchSuggestionTab::RECOMMENDED, &recommended_tab_text}); + tabs.push_back(Tab{recommended_body.get(), nullptr, SearchSuggestionTab::RECOMMENDED, &recommended_tab_text}); + if(is_fourchan) { + tabs.push_back(Tab{nullptr, std::make_unique(font), SearchSuggestionTab::LOGIN, &login_tab_text}); + focused_login_input = tabs.back().login_tab->username.get(); - int selected_tab = 0; + tabs.back().login_tab->username->caret_visible = true; + tabs.back().login_tab->password->caret_visible = false; + + tabs.back().login_tab->username->onTextSubmitCallback = login_submit_callback; + tabs.back().login_tab->password->onTextSubmitCallback = login_submit_callback; + } plugin_get_watch_history(current_plugin, history_body.items); if(current_plugin->name == "youtube") @@ -878,14 +933,14 @@ namespace QuickMedia { //int fps = 0; while (current_page == Page::SEARCH_SUGGESTION) { while (window.pollEvent(event)) { - base_event_handler(event, Page::EXIT, false); + base_event_handler(event, Page::EXIT, false, true, tabs[selected_tab].body != nullptr); if(event.type == sf::Event::Resized || event.type == sf::Event::GainedFocus) redraw = true; else if(event.type == sf::Event::KeyPressed) { if(event.key.code == sf::Keyboard::Up) { - tabs[selected_tab].body->select_previous_item(); + if(tabs[selected_tab].body ) tabs[selected_tab].body->select_previous_item(); } else if(event.key.code == sf::Keyboard::Down) { - tabs[selected_tab].body->select_next_item(); + if(tabs[selected_tab].body) tabs[selected_tab].body->select_next_item(); } else if(event.key.code == sf::Keyboard::Escape) { current_page = Page::EXIT; exit_code = 1; @@ -896,7 +951,22 @@ namespace QuickMedia { selected_tab = std::min((int)tabs.size() - 1, selected_tab + 1); search_bar->clear(); } else if(event.key.code == sf::Keyboard::Tab) { - search_bar->set_to_autocomplete(); + if(tabs[selected_tab].body) search_bar->set_to_autocomplete(); + } + } + + if(!tabs[selected_tab].body) { + if(event.type == sf::Event::TextEntered) + focused_login_input->onTextEntered(event.text.unicode); + focused_login_input->on_event(event); + + if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Tab) { + focused_login_input->caret_visible = false; + if(focused_login_input == tabs[selected_tab].login_tab->username.get()) + focused_login_input = tabs[selected_tab].login_tab->password.get(); + else + focused_login_input = tabs[selected_tab].login_tab->username.get(); + focused_login_input->caret_visible = true; } } } @@ -907,7 +977,8 @@ namespace QuickMedia { get_body_dimensions(window_size, search_bar.get(), body_pos, body_size, true); } - search_bar->update(); + if(tabs[selected_tab].body) + search_bar->update(); if(!update_search_text.empty() && !search_running) { search_suggestion_future = std::async(std::launch::async, [this, update_search_text]() { @@ -959,10 +1030,19 @@ namespace QuickMedia { //window.draw(tab_spacing_rect); const float width_per_tab = window_size.x / tabs.size(); - const float tab_y = tab_spacer_height + std::floor(search_bar->getBottomWithoutShadow() + tab_height * 0.5f - (tab_text_size + 5.0f) * 0.5f); sf::RectangleShape tab_background(sf::Vector2f(std::floor(width_per_tab), tab_height)); - tabs[selected_tab].body->draw(window, body_pos, body_size); + float tab_vertical_offset = search_bar->getBottomWithoutShadow(); + if(tabs[selected_tab].body) { + tabs[selected_tab].body->draw(window, body_pos, body_size); + } else { + tabs[selected_tab].login_tab->username->draw(window, false); + tabs[selected_tab].login_tab->password->draw(window, false); + tabs[selected_tab].login_tab->password->set_vertical_position(tabs[selected_tab].login_tab->username->getBottomWithoutShadow()); + tab_vertical_offset = tabs[selected_tab].login_tab->username->getBottomWithoutShadow() + tabs[selected_tab].login_tab->password->getBottomWithoutShadow(); + } + const float tab_y = tab_spacer_height + std::floor(tab_vertical_offset + tab_height * 0.5f - (tab_text_size + 5.0f) * 0.5f); + int i = 0; for(Tab &tab : tabs) { if(i == selected_tab) { @@ -970,7 +1050,7 @@ namespace QuickMedia { } else { tab_background.setFillColor(tab_unselected_color); } - tab_background.setPosition(std::floor(i * width_per_tab), tab_spacer_height + std::floor(search_bar->getBottomWithoutShadow())); + tab_background.setPosition(std::floor(i * width_per_tab), tab_spacer_height + std::floor(tab_vertical_offset)); window.draw(tab_background); const float center = (i * width_per_tab) + (width_per_tab * 0.5f); tab.text->setPosition(std::floor(center - tab.text->getLocalBounds().width * 0.5f), tab_y); @@ -979,10 +1059,11 @@ namespace QuickMedia { } tab_drop_shadow.setSize(sf::Vector2f(window_size.x, 5.0f)); - tab_drop_shadow.setPosition(0.0f, std::floor(search_bar->getBottomWithoutShadow() + tab_height)); + tab_drop_shadow.setPosition(0.0f, std::floor(tab_vertical_offset + tab_height)); window.draw(tab_drop_shadow); } - search_bar->draw(window, false); + if(tabs[selected_tab].body) + search_bar->draw(window, false); // fps++; // if(tt.getElapsedTime().asMilliseconds() >= 1000) { @@ -2251,7 +2332,7 @@ namespace QuickMedia { // Instead of using search bar to searching, use it for commenting. // TODO: Have an option for the search bar to be multi-line. search_bar->onTextUpdateCallback = nullptr; - search_bar->onTextSubmitCallback = [&post_comment_future, &navigation_stage, &request_new_google_captcha_challenge, &comment_to_post, &captcha_post_id, &captcha_solved_time, &post_comment](const std::string &text) -> bool { + search_bar->onTextSubmitCallback = [&post_comment_future, &navigation_stage, &request_new_google_captcha_challenge, &comment_to_post, &captcha_post_id, &captcha_solved_time, &post_comment, &image_board](const std::string &text) -> bool { if(text.empty()) return false; @@ -2262,8 +2343,13 @@ namespace QuickMedia { post_comment(); return true; }); - } else { + } else if(image_board->get_pass_id().empty()) { request_new_google_captcha_challenge(); + } else if(!image_board->get_pass_id().empty()) { + post_comment_future = std::async(std::launch::async, [&post_comment]() -> bool { + post_comment(); + return true; + }); } return true; }; -- cgit v1.2.3