diff options
Diffstat (limited to 'src/QuickMedia.cpp')
-rw-r--r-- | src/QuickMedia.cpp | 148 |
1 files changed, 108 insertions, 40 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 4ed11b2..b9f0bc8 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -2151,7 +2151,7 @@ namespace QuickMedia { std::unique_ptr<VideoPlayer> video_player; BodyItems related_videos; - if(video_page->autoplay_next_item()) + if(video_page->autoplay_next_item() && play_index + 1 >= 0 && play_index + 1 < (int)next_play_items.size()) related_videos.insert(related_videos.end(), next_play_items.begin() + play_index + 1, next_play_items.end()); std::string channel_url; @@ -2380,14 +2380,17 @@ namespace QuickMedia { std::string new_video_title; // Find video that hasn't been played before in this video session - for(auto it = related_videos.begin(), end = related_videos.end(); it != end; ++it) { - if(watched_videos.find((*it)->url) == watched_videos.end() && !video_page->video_should_be_skipped((*it)->url)) { - new_video_url = (*it)->url; - new_video_title = (*it)->get_title(); - related_videos.erase(it); - break; + auto find_next_video = [this, &related_videos, &video_page, &new_video_url, &new_video_title]() { + for(auto it = related_videos.begin(), end = related_videos.end(); it != end; ++it) { + if(!(*it)->url.empty() && watched_videos.find((*it)->url) == watched_videos.end() && !video_page->video_should_be_skipped((*it)->url)) { + new_video_url = (*it)->url; + new_video_title = (*it)->get_title(); + related_videos.erase(it); + break; + } } - } + }; + find_next_video(); if(new_video_url.empty() && parent_page && parent_body_page) { BodyItems new_body_items; @@ -2406,16 +2409,7 @@ namespace QuickMedia { next_play_items.insert(next_play_items.end(), new_body_items.begin(), new_body_items.end()); (*parent_body_page)++; related_videos = std::move(new_body_items); - - // Find video that hasn't been played before in this video session - for(auto it = related_videos.begin(), end = related_videos.end(); it != end; ++it) { - if(watched_videos.find((*it)->url) == watched_videos.end() && !video_page->video_should_be_skipped((*it)->url)) { - new_video_url = (*it)->url; - new_video_title = (*it)->get_title(); - related_videos.erase(it); - break; - } - } + find_next_video(); } } @@ -3073,15 +3067,18 @@ namespace QuickMedia { comment_input.draw_background = false; comment_input.set_editable(false); - auto post_comment = [this, &comment_input, &navigation_stage, &thread_page, &captcha_post_id, &comment_to_post, &request_new_google_captcha_challenge]() { + std::string selected_file_for_upload; + + auto post_comment = [&comment_input, &selected_file_for_upload, &navigation_stage, &thread_page, &captcha_post_id, &request_new_google_captcha_challenge](std::string comment_to_post, std::string file_to_upload) { comment_input.set_editable(false); navigation_stage = NavigationStage::POSTING_COMMENT; - PostResult post_result = thread_page->post_comment(captcha_post_id, comment_to_post); + PostResult post_result = thread_page->post_comment(captcha_post_id, comment_to_post, file_to_upload); if(post_result == PostResult::OK) { show_notification("QuickMedia", "Comment posted!"); navigation_stage = NavigationStage::VIEWING_COMMENTS; // TODO: Append posted comment to the thread so the user can see their posted comment. // TODO: Asynchronously update the thread periodically to show new comments. + selected_file_for_upload.clear(); // TODO: Remove from here, this is async } else if(post_result == PostResult::TRY_AGAIN) { show_notification("QuickMedia", "Error while posting, did the captcha expire? Please try again"); // TODO: Check if the response contains a new captcha instead of requesting a new one manually @@ -3089,8 +3086,17 @@ namespace QuickMedia { } else if(post_result == PostResult::BANNED) { show_notification("QuickMedia", "Failed to post comment because you are banned", Urgency::CRITICAL); navigation_stage = NavigationStage::VIEWING_COMMENTS; + //} else if(post_result == PostResult::FILE_TOO_LARGE) { + // show_notification("QuickMedia", "Failed to post comment because the file you are trying to upload is larger than " + std::to_string((double)thread_page->get_max_upload_file_size() * 1024.0 * 1024.0) + " mb", Urgency::CRITICAL); + // navigation_stage = NavigationStage::VIEWING_COMMENTS; + } else if(post_result == PostResult::NO_SUCH_FILE) { + show_notification("QuickMedia", "Failed to post comment because the file you are trying to upload no longer exists", Urgency::CRITICAL); + navigation_stage = NavigationStage::VIEWING_COMMENTS; + } else if(post_result == PostResult::FILE_TYPE_NOT_ALLOWED) { + show_notification("QuickMedia", "Failed to post comment because you are trying to upload a file of a type that is not allowed", Urgency::CRITICAL); + navigation_stage = NavigationStage::VIEWING_COMMENTS; } else if(post_result == PostResult::ERR) { - show_notification("QuickMedia", "Failed to post comment. Is " + std::string(plugin_name) + " down or is your internet down?", Urgency::CRITICAL); + show_notification("QuickMedia", "Failed to post comment", Urgency::CRITICAL); navigation_stage = NavigationStage::VIEWING_COMMENTS; } else { assert(false && "Unhandled post result"); @@ -3101,7 +3107,7 @@ namespace QuickMedia { bool frame_skip_text_entry = false; - comment_input.on_submit_callback = [&frame_skip_text_entry, &comment_input, &post_comment_future, &navigation_stage, &request_new_google_captcha_challenge, &comment_to_post, &captcha_post_id, &captcha_solved_time, &post_comment, &thread_page](std::string text) -> bool { + comment_input.on_submit_callback = [&frame_skip_text_entry, &comment_input, &post_comment_future, &navigation_stage, &request_new_google_captcha_challenge, &comment_to_post, &captcha_post_id, &captcha_solved_time, &post_comment, &selected_file_for_upload, &thread_page](std::string text) -> bool { if(text.empty()) return false; @@ -3111,15 +3117,15 @@ namespace QuickMedia { comment_to_post = std::move(text); if(!captcha_post_id.empty() && captcha_solved_time.getElapsedTime().asSeconds() < 120) { - post_comment_future = AsyncTask<bool>([&post_comment]() -> bool { - post_comment(); + post_comment_future = AsyncTask<bool>([&post_comment, comment_to_post, selected_file_for_upload]() -> bool { + post_comment(comment_to_post, selected_file_for_upload); return true; }); } else if(thread_page->get_pass_id().empty()) { request_new_google_captcha_challenge(); } else if(!thread_page->get_pass_id().empty()) { - post_comment_future = AsyncTask<bool>([&post_comment]() -> bool { - post_comment(); + post_comment_future = AsyncTask<bool>([&post_comment, comment_to_post, selected_file_for_upload]() -> bool { + post_comment(comment_to_post, selected_file_for_upload); return true; }); } @@ -3131,7 +3137,12 @@ namespace QuickMedia { sf::Sprite logo_sprite(plugin_logo); logo_sprite.setScale(0.8f * get_ui_scale(), 0.8f * get_ui_scale()); - sf::Vector2f logo_size(plugin_logo.getSize().x * logo_sprite.getScale().x, plugin_logo.getSize().y * logo_sprite.getScale().y); + sf::Vector2f logo_size(std::floor(plugin_logo.getSize().x * logo_sprite.getScale().x), std::floor(plugin_logo.getSize().y * logo_sprite.getScale().y)); + + sf::Sprite file_to_upload_sprite; + auto file_to_upload_thumbnail_data = std::make_shared<ThumbnailData>(); + + const float logo_file_to_upload_spacing = std::floor(10.0f * get_ui_scale()); float prev_chat_height = comment_input.get_height(); float chat_input_height_full = 0.0f; @@ -3193,15 +3204,15 @@ namespace QuickMedia { current_page = pop_page_stack(); } else if(event.key.code == sf::Keyboard::P) { BodyItem *selected_item = thread_body->get_selected(); - if(selected_item && !selected_item->attached_content_url.empty()) { - if(is_url_video(selected_item->attached_content_url)) { + if(selected_item && !selected_item->url.empty()) { + if(is_url_video(selected_item->url)) { page_stack.push(PageType::IMAGE_BOARD_THREAD); current_page = PageType::VIDEO_CONTENT; watched_videos.clear(); - thread_page->video_url = selected_item->attached_content_url; + thread_page->video_url = selected_item->url; BodyItems next_items; // TODO: Use real title - video_content_page(thread_page, thread_page, "", true, next_items, 0); + video_content_page(thread_page, thread_page, selected_item->get_title(), true, thread_body->items, thread_body->get_selected_item()); redraw = true; } else { load_image_future.cancel(); @@ -3210,17 +3221,41 @@ namespace QuickMedia { load_image_future = AsyncTask<std::string>([&thread_body]() { std::string image_data; BodyItem *selected_item = thread_body->get_selected(); - if(!selected_item || selected_item->attached_content_url.empty()) { + if(!selected_item || selected_item->url.empty()) { return image_data; } - if(download_to_string_cache(selected_item->attached_content_url, image_data, {}) != DownloadResult::OK) { - show_notification("QuickMedia", "Failed to download image: " + selected_item->attached_content_url, Urgency::CRITICAL); + if(download_to_string_cache(selected_item->url, image_data, {}) != DownloadResult::OK) { + show_notification("QuickMedia", "Failed to download image: " + selected_item->url, Urgency::CRITICAL); image_data.clear(); } return image_data; }); } } + } else if(event.key.code == sf::Keyboard::U) { + auto file_manager_page = std::make_unique<FileManagerPage>(this, (FileManagerMimeType)(FILE_MANAGER_MIME_TYPE_IMAGE|FILE_MANAGER_MIME_TYPE_VIDEO)); + file_manager_page->set_current_directory(get_home_dir().data); + auto file_manager_body = create_body(); + file_manager_page->get_files_in_directory(file_manager_body->items); + std::vector<Tab> file_manager_tabs; + file_manager_tabs.push_back(Tab{std::move(file_manager_body), std::move(file_manager_page), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); + + sf::Event event; + while(window.pollEvent(event)) {} + + selected_files.clear(); + page_loop(file_manager_tabs); + + if(selected_files.empty()) { + fprintf(stderr, "No files selected!\n"); + } else { + selected_file_for_upload = selected_files[0]; + } + + redraw = true; + frame_skip_text_entry = true; + } else if(event.key.code == sf::Keyboard::D && event.key.control) { + selected_file_for_upload.clear(); } BodyItem *selected_item = thread_body->get_selected(); @@ -3293,14 +3328,14 @@ namespace QuickMedia { } else if(event.key.code == sf::Keyboard::Enter) { navigation_stage = NavigationStage::POSTING_SOLUTION; captcha_post_solution_future = google_captcha_post_solution(fourchan_google_captcha_api_key, challenge_info.id, selected_captcha_images, - [&navigation_stage, &captcha_post_id, &captcha_solved_time, &selected_captcha_images, &challenge_info, &request_google_captcha_image, &post_comment](std::optional<std::string> new_captcha_post_id, std::optional<GoogleCaptchaChallengeInfo> new_challenge_info) { + [&navigation_stage, &captcha_post_id, &captcha_solved_time, &selected_captcha_images, &challenge_info, &request_google_captcha_image, &post_comment, comment_to_post, selected_file_for_upload](std::optional<std::string> new_captcha_post_id, std::optional<GoogleCaptchaChallengeInfo> new_challenge_info) { if(navigation_stage != NavigationStage::POSTING_SOLUTION) return; if(new_captcha_post_id) { captcha_post_id = new_captcha_post_id.value(); captcha_solved_time.restart(); - post_comment(); + post_comment(comment_to_post, selected_file_for_upload); } else if(new_challenge_info) { show_notification("QuickMedia", "Failed to solve captcha, please try again"); challenge_info = new_challenge_info.value(); @@ -3331,7 +3366,35 @@ namespace QuickMedia { update_idle_state(); handle_window_close(); - chat_input_height_full = comment_input.get_height() + chat_input_padding_y * 2.0f; + if(!selected_file_for_upload.empty() && file_to_upload_thumbnail_data->loading_state == LoadingState::NOT_LOADED) + AsyncImageLoader::get_instance().load_thumbnail(selected_file_for_upload, true, sf::Vector2i(logo_size.x, logo_size.y * 4), file_to_upload_thumbnail_data); + + if(selected_file_for_upload.empty() && file_to_upload_thumbnail_data->loading_state == LoadingState::APPLIED_TO_TEXTURE) { + file_to_upload_thumbnail_data = std::make_unique<ThumbnailData>(); + redraw = true; + } + + if(file_to_upload_thumbnail_data->loading_state == LoadingState::FINISHED_LOADING && file_to_upload_thumbnail_data->image->getSize().x > 0 && file_to_upload_thumbnail_data->image->getSize().y > 0) { + if(!file_to_upload_thumbnail_data->texture.loadFromImage(*file_to_upload_thumbnail_data->image)) + fprintf(stderr, "Warning: failed to load texture for attached file\n"); + file_to_upload_thumbnail_data->texture.setSmooth(true); + //room_avatar_thumbnail_data->texture.generateMipmap(); + file_to_upload_thumbnail_data->image.reset(); + file_to_upload_thumbnail_data->loading_state = LoadingState::APPLIED_TO_TEXTURE; + file_to_upload_sprite.setTexture(file_to_upload_thumbnail_data->texture, true); + + sf::Vector2f texture_size_f(file_to_upload_thumbnail_data->texture.getSize().x, file_to_upload_thumbnail_data->texture.getSize().y); + sf::Vector2f image_scale = get_ratio(texture_size_f, clamp_to_size_x(texture_size_f, logo_size)); + file_to_upload_sprite.setScale(image_scale); + redraw = true; + } + + float chat_input_height_full_images = logo_size.y; + if(file_to_upload_thumbnail_data->loading_state == LoadingState::APPLIED_TO_TEXTURE) { + const float file_to_upload_height = std::floor(logo_file_to_upload_spacing + file_to_upload_sprite.getTexture()->getSize().y * file_to_upload_sprite.getScale().y); + chat_input_height_full_images += file_to_upload_height; + } + chat_input_height_full = chat_input_padding_y + std::max(comment_input.get_height(), chat_input_height_full_images) + chat_input_padding_y; const float chat_height = comment_input.get_height(); if(std::abs(chat_height - prev_chat_height) > 1.0f) { @@ -3359,7 +3422,8 @@ namespace QuickMedia { body_pos = sf::Vector2f(body_padding_horizontal, comment_input_shade.getSize().y + body_padding_vertical); body_size = sf::Vector2f(body_width, window_size.y - comment_input_shade.getSize().y - body_padding_vertical); - logo_sprite.setPosition(logo_padding_x, std::floor(comment_input_shade.getSize().y * 0.5f - logo_size.y * 0.5f)); + logo_sprite.setPosition(logo_padding_x, chat_input_padding_y); + file_to_upload_sprite.setPosition(logo_sprite.getPosition() + sf::Vector2f(0.0f, logo_size.y + logo_file_to_upload_spacing)); } //comment_input.update(); @@ -3416,8 +3480,8 @@ namespace QuickMedia { BodyItem *selected_item = thread_body->get_selected(); std::string selected_item_attached_url; if(selected_item) - selected_item_attached_url = selected_item->attached_content_url; - show_notification("QuickMedia", "Failed to load image downloaded from url: " + selected_item->attached_content_url, Urgency::CRITICAL); + selected_item_attached_url = selected_item->url; + show_notification("QuickMedia", "Failed to load image downloaded from url: " + selected_item_attached_url, Urgency::CRITICAL); } } @@ -3446,11 +3510,15 @@ namespace QuickMedia { } else if(navigation_stage == NavigationStage::REPLYING) { window.draw(comment_input_shade); window.draw(logo_sprite); + if(file_to_upload_thumbnail_data->loading_state == LoadingState::APPLIED_TO_TEXTURE) + window.draw(file_to_upload_sprite); comment_input.draw(window); thread_body->draw(window, body_pos, body_size); } else if(navigation_stage == NavigationStage::VIEWING_COMMENTS) { window.draw(comment_input_shade); window.draw(logo_sprite); + if(file_to_upload_thumbnail_data->loading_state == LoadingState::APPLIED_TO_TEXTURE) + window.draw(file_to_upload_sprite); comment_input.draw(window); thread_body->draw(window, body_pos, body_size); } |