From 8e79b2e10827f42d76ec61621f2c484f9cdc2551 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 19 May 2021 23:45:27 +0200 Subject: Start on notifications run matrix delegate on main (ui) thread instead of mutex hell --- src/QuickMedia.cpp | 155 ++++++++++++++++++++++++++++------------------------- 1 file changed, 83 insertions(+), 72 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 297a780..5bbcfd7 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -1057,6 +1057,9 @@ namespace QuickMedia { auto history_page = std::make_unique(this, tabs.front().page.get(), HistoryType::MANGA); tabs.push_back(Tab{create_body(), std::move(history_page), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); } else if(strcmp(plugin_name, "manga") == 0) { + auto mangadex = std::make_unique(this); + upgrade_legacy_mangadex_ids(this, mangadex.get()); + auto manganelo = std::make_unique(this); auto manganelos = std::make_unique(this, "manganelos", "http://manganelos.com/"); add_manganelos_handlers(manganelos.get()); @@ -1067,13 +1070,14 @@ namespace QuickMedia { auto readm = std::make_unique(this, "readm", "https://readm.org/"); add_readm_handlers(readm.get()); + // TODO: Use async task pool std::vector pages; pages.push_back({std::move(manganelo), "Manganelo", "manganelo", resources_root + "images/" + get_plugin_logo_name("manganelo")}); pages.push_back({std::move(manganelos), "Manganelos", "manganelos", resources_root + "images/" + get_plugin_logo_name("manganelos")}); pages.push_back({std::move(mangatown), "Mangatown", "mangatown", resources_root + "images/" + get_plugin_logo_name("mangatown")}); pages.push_back({std::move(mangakatana), "Mangakatana", "mangakatana", resources_root + "images/" + get_plugin_logo_name("mangakatana")}); pages.push_back({std::move(readm), "Readm", "readm", resources_root + "images/" + get_plugin_logo_name("readm")}); - // TODO: Add mangadex + pages.push_back({std::move(mangadex), "Mangadex", "mangadex", resources_root + "images/" + get_plugin_logo_name("mangadex")}); tabs.push_back(Tab{create_body(), std::make_unique(this, std::move(pages)), create_search_bar("Search...", 400)}); } else if(strcmp(plugin_name, "nyaa.si") == 0) { @@ -1478,6 +1482,9 @@ namespace QuickMedia { window.draw(tab_associated_data.search_result_text); } + if(matrix) + matrix->update(); + if(matrix && !matrix->is_initial_sync_finished()) { // if(is_login_sync) { load_sprite.setPosition(body_pos.x + body_size.x * 0.5f, body_pos.y + body_size.y * 0.5f); @@ -1558,10 +1565,18 @@ namespace QuickMedia { std::vector new_tabs; auto prev_selected_item = tabs[selected_tab].page->submit_body_item; tabs[selected_tab].page->submit_body_item = selected_item; - TaskResult submit_result = run_task_with_loading_screen([&tabs, selected_tab, &selected_item, &search_text, &new_tabs](){ - PluginResult submit_result = tabs[selected_tab].page->submit(selected_item ? selected_item->get_title() : search_text, selected_item ? selected_item->url : "", new_tabs); - return submit_result == PluginResult::OK; - }); + + auto plugin_submit_handler = [&tabs, selected_tab, &selected_item, &search_text, &new_tabs]() { + PluginResult plugin_result = tabs[selected_tab].page->submit(selected_item ? selected_item->get_title() : search_text, selected_item ? selected_item->url : "", new_tabs); + return plugin_result == PluginResult::OK; + }; + + TaskResult submit_result; + if(tabs[selected_tab].page->submit_is_async()) { + submit_result = run_task_with_loading_screen(std::move(plugin_submit_handler)); + } else { + submit_result = plugin_submit_handler() ? TaskResult::TRUE : TaskResult::FALSE; + } if(submit_result == TaskResult::CANCEL) { return; @@ -1883,8 +1898,6 @@ namespace QuickMedia { for(size_t i = 0; i < tabs.size(); ++i) { TabAssociatedData &associated_data = tab_associated_data[i]; - tabs[i].page->update(); - if(associated_data.fetching_next_page_running && associated_data.next_page_future.ready()) { BodyItems new_body_items = associated_data.next_page_future.get(); fprintf(stderr, "Finished fetching page %d, num new items: %zu\n", associated_data.fetched_page + 1, new_body_items.size()); @@ -4612,7 +4625,7 @@ namespace QuickMedia { // Fetch replied to message if(event_data->status == FetchStatus::FINISHED_LOADING && event_data->message) { - if(event_data->message->related_event_id.empty() || (body_item->embedded_item_status != FetchStatus::NONE && body_item->embedded_item_status != FetchStatus::QUEUED_LOADING)) + if(event_data->message->related_event_id.empty() || event_data->message->related_event_type != RelatedEventType::REPLY || (body_item->embedded_item_status != FetchStatus::NONE && body_item->embedded_item_status != FetchStatus::QUEUED_LOADING)) return; if(load_cached_related_embedded_item(body_item, event_data->message, me, current_room, tabs[MESSAGES_TAB_INDEX].body->items)) @@ -4670,7 +4683,7 @@ namespace QuickMedia { if(message_is_timeline(message) && (!last_visible_timeline_message || message->timestamp > last_visible_timeline_message->timestamp)) last_visible_timeline_message = message; - if(message->related_event_id.empty() || (body_item->embedded_item_status != FetchStatus::NONE && body_item->embedded_item_status != FetchStatus::QUEUED_LOADING)) + if(message->related_event_id.empty() || message->related_event_type != RelatedEventType::REPLY || (body_item->embedded_item_status != FetchStatus::NONE && body_item->embedded_item_status != FetchStatus::QUEUED_LOADING)) return; if(fetch_message_future.valid()) { @@ -5375,37 +5388,66 @@ namespace QuickMedia { update_idle_state(); handle_window_close(); + matrix->update(); mention.update(); - matrix_chat_page->update(); + std::unique_ptr matrix_event; + while((matrix_event = matrix->pop_event()) != nullptr) { + if(matrix_event) { + switch(matrix_event->type) { + case MatrixEvent::Type::ADD_USER: + on_add_user_event(static_cast(matrix_event.get())); + break; + case MatrixEvent::Type::REMOVE_USER: + on_remove_user_event(static_cast(matrix_event.get())); + break; + case MatrixEvent::Type::USER_INFO: + on_user_info_event(static_cast(matrix_event.get())); + break; + } + } + } + + while((provisional_message = provisional_message_queue.pop_if_available()) != std::nullopt) { + if(!provisional_message->body_item || !provisional_message->message) + continue; + + if(!provisional_message->event_id.empty()) { + provisional_message->message->event_id = std::move(provisional_message->event_id); + provisional_message->body_item->set_description_color(sf::Color::White); + sent_messages[provisional_message->message->event_id] = std::move(provisional_message.value()); + } else if(provisional_message->body_item) { + provisional_message->body_item->set_description("Failed to send: " + provisional_message->body_item->get_description()); + provisional_message->body_item->set_description_color(sf::Color::Red); + provisional_message->body_item->userdata = nullptr; + } + } switch(new_page) { case PageType::FILE_MANAGER: { new_page = PageType::CHAT; - if(current_room) { - for(ChatTab &tab : tabs) { - tab.body->clear_cache(); - } + for(ChatTab &tab : tabs) { + tab.body->clear_cache(); + } - auto file_manager_page = std::make_unique(this); - 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 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)}); + auto file_manager_page = std::make_unique(this); + 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 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)}); - selected_files.clear(); - page_loop(file_manager_tabs); + selected_files.clear(); + page_loop(file_manager_tabs); - if(selected_files.empty()) { - fprintf(stderr, "No files selected!\n"); - } else { - // TODO: Upload multiple files. - upload_file(selected_files[0]); - } - redraw = true; + if(selected_files.empty()) { + fprintf(stderr, "No files selected!\n"); + } else { + // TODO: Upload multiple files. + upload_file(selected_files[0]); } + redraw = true; break; } case PageType::CHAT_LOGIN: { @@ -5519,38 +5561,6 @@ namespace QuickMedia { logo_sprite.setPosition(body_pos.x - body_padding_horizontal + logo_padding_x, std::floor(window_size.y - chat_input_height_full * 0.5f - logo_size.y * 0.5f)); } - while((provisional_message = provisional_message_queue.pop_if_available()) != std::nullopt) { - if(!provisional_message->body_item || !provisional_message->message) - continue; - - if(!provisional_message->event_id.empty()) { - provisional_message->message->event_id = std::move(provisional_message->event_id); - provisional_message->body_item->set_description_color(sf::Color::White); - sent_messages[provisional_message->message->event_id] = std::move(provisional_message.value()); - } else if(provisional_message->body_item) { - provisional_message->body_item->set_description("Failed to send: " + provisional_message->body_item->get_description()); - provisional_message->body_item->set_description_color(sf::Color::Red); - provisional_message->body_item->userdata = nullptr; - } - } - - std::unique_ptr matrix_event; - while((matrix_event = matrix->pop_event()) != nullptr) { - if(matrix_event) { - switch(matrix_event->type) { - case MatrixEvent::Type::ADD_USER: - on_add_user_event(static_cast(matrix_event.get())); - break; - case MatrixEvent::Type::REMOVE_USER: - on_remove_user_event(static_cast(matrix_event.get())); - break; - case MatrixEvent::Type::USER_INFO: - on_user_info_event(static_cast(matrix_event.get())); - break; - } - } - } - sync_data.messages.clear(); sync_data.pinned_events = std::nullopt; matrix->get_room_sync_data(current_room, sync_data); @@ -5908,17 +5918,17 @@ namespace QuickMedia { if(!window.isOpen()) exit(exit_code); - auto rooms_body = create_body(); - auto matrix_rooms_page_search_bar = create_search_bar("Search...", SEARCH_DELAY_FILTER); - auto matrix_rooms_page = std::make_unique(this, rooms_body.get(), "All rooms", nullptr, matrix_rooms_page_search_bar.get()); + auto notifications_body = create_body(); + auto matrix_notifications_page = std::make_unique(this, notifications_body.get()); auto rooms_tags_body = create_body(); - auto matrix_rooms_tage_page_search_bar = create_search_bar("Search...", SEARCH_DELAY_FILTER); - auto matrix_rooms_tag_page = std::make_unique(this, rooms_tags_body.get(), matrix_rooms_tage_page_search_bar.get()); + auto matrix_rooms_tag_page = std::make_unique(this, rooms_tags_body.get()); + + auto rooms_body = create_body(); + auto matrix_rooms_page = std::make_unique(this, rooms_body.get(), "All rooms", nullptr); auto invites_body = create_body(); - auto matrix_invites_page_search_bar = create_search_bar("Search...", SEARCH_DELAY_FILTER); - auto matrix_invites_page = std::make_unique(this, matrix, invites_body.get(), matrix_invites_page_search_bar.get()); + auto matrix_invites_page = std::make_unique(this, matrix, invites_body.get()); auto room_directory_body = create_body(); add_body_item_unique_title(room_directory_body->items, matrix->get_homeserver_domain()); @@ -5948,13 +5958,14 @@ namespace QuickMedia { is_login_sync = !sync_cached; std::vector tabs; - tabs.push_back(Tab{std::move(rooms_body), std::move(matrix_rooms_page), std::move(matrix_rooms_page_search_bar)}); - tabs.push_back(Tab{std::move(rooms_tags_body), std::move(matrix_rooms_tag_page), std::move(matrix_rooms_tage_page_search_bar)}); - tabs.push_back(Tab{std::move(invites_body), std::move(matrix_invites_page), std::move(matrix_invites_page_search_bar)}); + tabs.push_back(Tab{std::move(notifications_body), std::move(matrix_notifications_page), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); + tabs.push_back(Tab{std::move(rooms_tags_body), std::move(matrix_rooms_tag_page), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); + tabs.push_back(Tab{std::move(rooms_body), std::move(matrix_rooms_page), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); + tabs.push_back(Tab{std::move(invites_body), std::move(matrix_invites_page), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); tabs.push_back(Tab{std::move(room_directory_body), std::move(matrix_room_directory_page), create_search_bar("Server to search on...", SEARCH_DELAY_FILTER)}); while(window.isOpen()) { - page_loop(tabs); + page_loop(tabs, 2); } matrix->stop_sync(); -- cgit v1.2.3