From 6b347e7310c501b826785e9639d962ba1d448b4b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 23 Sep 2020 00:56:54 +0200 Subject: Add matrix image upload --- src/QuickMedia.cpp | 90 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 22 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index e7a310d..a2fd42b 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -216,9 +216,14 @@ namespace QuickMedia { } else { running = false; } - delete related_media_body; - delete body; - delete current_plugin; + if(related_media_body) + delete related_media_body; + if(body) + delete body; + if(file_manager) + delete file_manager; + if(current_plugin) + delete current_plugin; if(disp) XCloseDisplay(disp); } @@ -310,7 +315,7 @@ namespace QuickMedia { plugin_logo_path = resources_root + "images/nyaa_si_logo.png"; } else if(strcmp(argv[i], "matrix") == 0) { current_plugin = new Matrix(); - plugin_logo_path = resources_root + "images/matrix_logo.png"; + //plugin_logo_path = resources_root + "images/matrix_logo.png"; } else if(strcmp(argv[i], "file-manager") == 0) { current_plugin = new FileManager(); } else if(strcmp(argv[i], "dmenu") == 0) { @@ -359,6 +364,7 @@ namespace QuickMedia { if(current_plugin->name == "file-manager") { current_page = Page::FILE_MANAGER; + file_manager = static_cast(current_plugin); } else { if(start_dir) { fprintf(stderr, "Option --dir is only valid with file-manager\n"); @@ -444,7 +450,6 @@ namespace QuickMedia { fprintf(stderr, "Failed to load session cache, redirecting to login page\n"); current_page = Page::CHAT_LOGIN; } - search_placeholder = "Send a message..."; } if(search_placeholder.empty()) @@ -2489,12 +2494,9 @@ namespace QuickMedia { void Program::file_manager_page() { selected_files.clear(); int prev_autosearch_delay = search_bar->text_autosearch_delay; - search_bar->text_autosearch_delay = current_plugin->get_search_delay(); + search_bar->text_autosearch_delay = file_manager->get_search_delay(); Page previous_page = pop_page_stack(); - assert(current_plugin->name == "file-manager"); - FileManager *file_manager = static_cast(current_plugin); - sf::Text current_dir_text(file_manager->get_current_dir().string(), bold_font, 18); // TODO: Make asynchronous. @@ -2514,7 +2516,6 @@ namespace QuickMedia { if(!selected_item) return false; - FileManager *file_manager = static_cast(current_plugin); if(file_manager->set_child_directory(selected_item->get_title())) { std::string current_dir_str = file_manager->get_current_dir().string(); current_dir_text.setString(current_dir_str); @@ -3200,14 +3201,56 @@ namespace QuickMedia { // TODO: Allow empty initial room (if the user hasn't joined any room yet) assert(!current_room_id.empty()); + { + std::string plugin_logo_path = resources_root + "images/matrix_logo.png"; + if(!plugin_logo.loadFromFile(plugin_logo_path)) { + show_notification("QuickMedia", "Failed to load plugin logo, path: " + plugin_logo_path, Urgency::CRITICAL); + exit(1); + } + plugin_logo.generateMipmap(); + plugin_logo.setSmooth(true); + } + + SearchBar chat_input(font, &plugin_logo, "Send a message..."); + // TODO: Filer for rooms and settings - search_bar->onTextUpdateCallback = nullptr; + chat_input.onTextUpdateCallback = nullptr; - search_bar->onTextSubmitCallback = [matrix, &tabs, &selected_tab, &room_message_index, ¤t_room_id](const std::string &text) -> bool { + // TODO: Show post message immediately, instead of waiting for sync. Otherwise it can take a while until we receive the message, + // which happens when uploading an image. + chat_input.onTextSubmitCallback = [this, matrix, &tabs, &selected_tab, &room_message_index, ¤t_room_id](const std::string &text) -> bool { if(tabs[selected_tab].type == ChatTabType::MESSAGES) { if(text.empty()) return false; + if(text[0] == '/') { + std::string command = text; + strip(command); + if(command == "/upload") { + if(!file_manager) + file_manager = new FileManager(); + page_stack.push(Page::CHAT); + current_page = Page::FILE_MANAGER; + file_manager_page(); + if(selected_files.empty()) { + fprintf(stderr, "No files selected!\n"); + return true; + } else { + // TODO: Make asynchronous. + // TODO: Upload multiple files. + if(matrix->post_file(current_room_id, selected_files[0]) != PluginResult::OK) { + show_notification("QuickMedia", "Failed to upload image to room", Urgency::CRITICAL); + return false; + } else { + return true; + } + } + } else { + fprintf(stderr, "Error: invalid command: %s, expected /upload\n", command.c_str()); + return false; + } + } + // TODO: Make asynchronous if(matrix->post_message(current_room_id, text) != PluginResult::OK) { show_notification("QuickMedia", "Failed to post matrix message", Urgency::CRITICAL); @@ -3262,7 +3305,7 @@ namespace QuickMedia { while (current_page == Page::CHAT) { while (window.pollEvent(event)) { - base_event_handler(event, Page::EXIT, false, false); + base_event_handler(event, Page::EXIT, false, false, false); if(event.type == sf::Event::Resized || event.type == sf::Event::GainedFocus) { redraw = true; } else if(event.type == sf::Event::KeyPressed) { @@ -3274,17 +3317,16 @@ namespace QuickMedia { current_page = Page::EXIT; body->clear_items(); body->reset_selected(); - search_bar->clear(); } else if(event.key.code == sf::Keyboard::Left) { tabs[selected_tab].body->filter_search_fuzzy(""); tabs[selected_tab].body->clamp_selection(); selected_tab = std::max(0, selected_tab - 1); - search_bar->clear(); + chat_input.clear(); } else if(event.key.code == sf::Keyboard::Right) { tabs[selected_tab].body->filter_search_fuzzy(""); tabs[selected_tab].body->clamp_selection(); selected_tab = std::min((int)tabs.size() - 1, selected_tab + 1); - search_bar->clear(); + chat_input.clear(); } if(tabs[selected_tab].type == ChatTabType::MESSAGES && event.key.control && event.key.code == sf::Keyboard::I) { @@ -3302,12 +3344,16 @@ namespace QuickMedia { video_content_page(); } } + + if(event.type == sf::Event::TextEntered) + chat_input.onTextEntered(event.text.unicode); + chat_input.on_event(event); } if(redraw) { redraw = false; - search_bar->onWindowResize(window_size); - search_bar->set_vertical_position(window_size.y - search_bar->getBottomWithoutShadow()); + chat_input.onWindowResize(window_size); + chat_input.set_vertical_position(window_size.y - chat_input.getBottomWithoutShadow()); float body_padding_horizontal = 25.0f; float body_padding_vertical = 25.0f; @@ -3318,10 +3364,10 @@ namespace QuickMedia { body_padding_vertical = 10.0f; } - float search_bottom = search_bar->getBottomWithoutShadow(); + float search_bottom = chat_input.getBottomWithoutShadow(); body_pos = sf::Vector2f(body_padding_horizontal, body_padding_vertical + tab_height); body_size = sf::Vector2f(body_width, window_size.y - search_bottom - body_padding_vertical - tab_height); - //get_body_dimensions(window_size, search_bar.get(), body_pos, body_size, true); + //get_body_dimensions(window_size, &chat_input, body_pos, body_size, true); } if(!sync_running && sync_timer.getElapsedTime().asMilliseconds() >= sync_min_time_ms) { @@ -3361,7 +3407,7 @@ namespace QuickMedia { sync_running = false; } - search_bar->update(); + chat_input.update(); window.clear(back_color); @@ -3391,7 +3437,7 @@ namespace QuickMedia { tab_drop_shadow.setPosition(0.0f, std::floor(tab_vertical_offset + tab_height)); window.draw(tab_drop_shadow); - search_bar->draw(window, false); + chat_input.draw(window, false); window.display(); } -- cgit v1.2.3