From 1026360c595818eabe75813bcd02258c4d0dddc7 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 24 May 2021 07:53:33 +0200 Subject: Save the last accessed file manager directory and use that the next time. Add ctrl+s for 4chan without viewing the image/video in fullscreen mode --- src/DownloadUtils.cpp | 11 ++++----- src/QuickMedia.cpp | 58 ++++++++++++++++++++++----------------------- src/plugins/FileManager.cpp | 29 +++++++++++++++++++++-- 3 files changed, 60 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/DownloadUtils.cpp b/src/DownloadUtils.cpp index 7d2cbb8..b6d21b9 100644 --- a/src/DownloadUtils.cpp +++ b/src/DownloadUtils.cpp @@ -222,13 +222,12 @@ namespace QuickMedia { return DownloadResult::OK; } - bool download_async_gui(const std::string &url, bool use_youtube_dl, bool no_video) { - // TODO: Figure out why /proc/self/exe doesn't work when installed to /usr/bin/quickmedia - char quickmedia_path[PATH_MAX] = "quickmedia"; - //if(readlink("/proc/self/exe", quickmedia_path, sizeof(quickmedia_path)) == -1) - // return false; + bool download_async_gui(const std::string &url, const std::string &file_manager_start_dir, bool use_youtube_dl, bool no_video) { + char quickmedia_path[PATH_MAX]; + if(readlink("/proc/self/exe", quickmedia_path, sizeof(quickmedia_path)) == -1) + strcpy(quickmedia_path, "quickmedia"); - std::vector args = { quickmedia_path, "download", "-u", url.c_str() }; + std::vector args = { quickmedia_path, "download", "-u", url.c_str(), "--dir", file_manager_start_dir.c_str() }; if(use_youtube_dl) args.push_back("--youtube-dl"); if(no_video) args.push_back("--no-video"); args.push_back(nullptr); diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 498de29..e93536b 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -319,7 +319,7 @@ namespace QuickMedia { fprintf(stderr, " --use-system-mpv-config Use system mpv config instead of no config. Disabled by default\n"); fprintf(stderr, " --upscale-images Upscale low-resolution manga pages using waifu2x-ncnn-vulkan. Disabled by default\n"); fprintf(stderr, " --upscale-images-always Upscale manga pages using waifu2x-ncnn-vulkan, no matter what the original image resolution is. Disabled by default\n"); - fprintf(stderr, " --dir Set the start directory when using file-manager. Default is the user home directory\n"); + fprintf(stderr, " --dir Set the start directory when using file-manager. Default is the last accessed directory in QuickMedia or the users home directory\n"); fprintf(stderr, " -e Embed QuickMedia into another window\n"); fprintf(stderr, "EXAMPLES:\n"); fprintf(stderr, " quickmedia launcher\n"); @@ -350,7 +350,6 @@ namespace QuickMedia { return -1; } - const char *start_dir = nullptr; Window parent_window = None; std::vector tabs; const char *url = nullptr; @@ -377,7 +376,7 @@ namespace QuickMedia { upscale_image_action = UpscaleImageAction::FORCE; } else if(strcmp(argv[i], "--dir") == 0) { if(i < argc - 1) { - start_dir = argv[i + 1]; + file_manager_start_dir = argv[i + 1]; ++i; } else { fprintf(stderr, "Missing directory after --dir argument\n"); @@ -471,15 +470,9 @@ namespace QuickMedia { }); } - if(strcmp(plugin_name, "file-manager") != 0 && start_dir) { - fprintf(stderr, "Option --dir is only valid with file-manager\n"); - usage(); - return -1; - } - - Path home_dir = get_home_dir(); - if(!start_dir) - start_dir = home_dir.data.c_str(); + std::filesystem::path home_path = get_home_dir().data; + if(file_manager_start_dir.empty()) + file_manager_start_dir = FileManagerPage::get_last_accessed_directory(home_path); int start_tab_index = 0; FileManagerMimeType fm_mine_type = FILE_MANAGER_MIME_TYPE_ALL; @@ -510,7 +503,7 @@ namespace QuickMedia { file_selection_handler = std::move(saucenao_file_selection_handler); } - load_plugin_by_name(tabs, start_dir, start_tab_index, fm_mine_type, std::move(file_selection_handler)); + load_plugin_by_name(tabs, start_tab_index, fm_mine_type, std::move(file_selection_handler)); while(!tabs.empty() || matrix) { if(matrix) { @@ -535,7 +528,7 @@ namespace QuickMedia { fm_mine_type = FILE_MANAGER_MIME_TYPE_IMAGE; file_selection_handler = std::move(saucenao_file_selection_handler); } - load_plugin_by_name(tabs, start_dir, start_tab_index, fm_mine_type, std::move(file_selection_handler)); + load_plugin_by_name(tabs, start_tab_index, fm_mine_type, std::move(file_selection_handler)); } } @@ -671,6 +664,9 @@ namespace QuickMedia { if(create_directory_recursive(get_cache_dir().join("thumbnails")) != 0) throw std::runtime_error("Failed to create thumbnails directory"); + if(create_directory_recursive(get_storage_dir().join("file-manager")) != 0) + throw std::runtime_error("Failed to create file-manager directory"); + const char *qm_phone_factor = getenv("QM_PHONE_FACTOR"); if(qm_phone_factor && atoi(qm_phone_factor) == 1) show_room_side_panel = false; @@ -984,7 +980,7 @@ namespace QuickMedia { return PluginResult::OK; } - void Program::load_plugin_by_name(std::vector &tabs, const char *start_dir, int &start_tab_index, FileManagerMimeType fm_mime_type, FileSelectionHandler file_selection_handler) { + void Program::load_plugin_by_name(std::vector &tabs, int &start_tab_index, FileManagerMimeType fm_mime_type, FileSelectionHandler file_selection_handler) { if(!plugin_name || plugin_name[0] == '\0') return; @@ -1116,11 +1112,8 @@ namespace QuickMedia { tabs.push_back(Tab{std::move(boards_body), std::move(boards_page), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); } else if(strcmp(plugin_name, "file-manager") == 0) { auto file_manager_page = std::make_unique(this, fm_mime_type, file_selection_handler); - if(start_dir && !file_manager_page->set_current_directory(start_dir)) { - fprintf(stderr, "Invalid directory provided with --dir: %s\n", start_dir); - exit_code = -3; - return; - } + if(!file_manager_page->set_current_directory(file_manager_start_dir)) + fprintf(stderr, "Warning: Invalid directory provided with --dir\n"); auto file_manager_body = create_body(); file_manager_page->get_files_in_directory(file_manager_body->items); tabs.push_back(Tab{std::move(file_manager_body), std::move(file_manager_page), create_search_bar("Search...", SEARCH_DELAY_FILTER)}); @@ -2453,7 +2446,7 @@ namespace QuickMedia { } else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::C && event.key.control) { save_video_url_to_clipboard(); } else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::S && event.key.control) { - download_async_gui(original_video_url, !is_matrix, no_video); + download_async_gui(original_video_url, FileManagerPage::get_last_accessed_directory(file_manager_start_dir).string(), !is_matrix, no_video); } } handle_window_close(); @@ -2470,7 +2463,7 @@ namespace QuickMedia { } else if(pressed_keysym == XK_f && pressing_ctrl) { window_set_fullscreen(disp, window.getSystemHandle(), WindowFullscreenState::TOGGLE); } else if(pressed_keysym == XK_s && pressing_ctrl) { - download_async_gui(original_video_url, !is_matrix, no_video); + download_async_gui(original_video_url, FileManagerPage::get_last_accessed_directory(file_manager_start_dir).string(), !is_matrix, no_video); } else if(pressed_keysym == XK_r && pressing_ctrl) { if(!cursor_visible) window.setMouseCursorVisible(true); @@ -3411,8 +3404,9 @@ namespace QuickMedia { } } } else if(event.key.code == sf::Keyboard::U) { + std::filesystem::path &fm_dir = FileManagerPage::get_last_accessed_directory(file_manager_start_dir); auto file_manager_page = std::make_unique(this, (FileManagerMimeType)(FILE_MANAGER_MIME_TYPE_IMAGE|FILE_MANAGER_MIME_TYPE_VIDEO)); - file_manager_page->set_current_directory(get_home_dir().data); + file_manager_page->set_current_directory(fm_dir.string()); auto file_manager_body = create_body(); file_manager_page->get_files_in_directory(file_manager_body->items); std::vector file_manager_tabs; @@ -3453,6 +3447,10 @@ namespace QuickMedia { redraw = true; frame_skip_text_entry = true; } + } else if(event.key.code == sf::Keyboard::S && event.key.control) { + BodyItem *selected_item = thread_body->get_selected(); + if(selected_item && !selected_item->url.empty()) + download_async_gui(selected_item->url, FileManagerPage::get_last_accessed_directory(file_manager_start_dir).string(), false, false); } BodyItem *selected_item = thread_body->get_selected(); @@ -3572,7 +3570,7 @@ namespace QuickMedia { redraw = true; frame_skip_text_entry = true; } else if(event.key.code == sf::Keyboard::S && event.key.control) { - download_async_gui(attached_image_url, false, false); + download_async_gui(attached_image_url, FileManagerPage::get_last_accessed_directory(file_manager_start_dir).string(), false, false); } } } @@ -4965,7 +4963,7 @@ namespace QuickMedia { redraw = true; return true; } else if(message_type == MessageType::FILE) { - download_async_gui(selected->url, false, no_video); + download_async_gui(selected->url, FileManagerPage::get_last_accessed_directory(file_manager_start_dir).string(), false, no_video); return true; } @@ -5006,7 +5004,7 @@ namespace QuickMedia { if(selected_item_message) { MessageType message_type = selected_item_message->type; if(!selected->url.empty() && message_type >= MessageType::IMAGE && message_type <= MessageType::FILE) { - download_async_gui(selected->url, false, no_video); + download_async_gui(selected->url, FileManagerPage::get_last_accessed_directory(file_manager_start_dir).string(), false, no_video); return true; } } @@ -5436,8 +5434,9 @@ namespace QuickMedia { tab.body->clear_cache(); } + std::filesystem::path &fm_dir = FileManagerPage::get_last_accessed_directory(file_manager_start_dir); auto file_manager_page = std::make_unique(this); - file_manager_page->set_current_directory(get_home_dir().data); + file_manager_page->set_current_directory(fm_dir.string()); auto file_manager_body = create_body(); file_manager_page->get_files_in_directory(file_manager_body->items); std::vector file_manager_tabs; @@ -6510,14 +6509,13 @@ namespace QuickMedia { sf::Event event; auto file_manager_page = std::make_unique(this); - std::string home_dir = get_home_dir().data; - file_manager_page->set_current_directory(home_dir); + file_manager_page->set_current_directory(file_manager_start_dir); auto file_manager_body = create_body(); file_manager_page->get_files_in_directory(file_manager_body->items); auto search_bar = create_search_bar("Search...", SEARCH_DELAY_FILTER); Tabs ui_tabs(&rounded_rectangle_shader); - const int tab_path_index = ui_tabs.add_tab(home_dir); + const int tab_path_index = ui_tabs.add_tab(file_manager_start_dir); search_bar->onTextUpdateCallback = [&file_manager_body](const std::string &text) { file_manager_body->filter_search_fuzzy(text); diff --git a/src/plugins/FileManager.cpp b/src/plugins/FileManager.cpp index 5ca7dc7..2207c65 100644 --- a/src/plugins/FileManager.cpp +++ b/src/plugins/FileManager.cpp @@ -4,6 +4,16 @@ #include "../../include/QuickMedia.hpp" namespace QuickMedia { + static bool last_accessed_dir_loaded = false; + static std::filesystem::path last_accessed_dir; + static Path last_accessed_dir_path; + static void set_last_accessed_dir(const std::filesystem::path &path) { + if(last_accessed_dir_path.data.empty()) + last_accessed_dir_path = get_storage_dir().join("file-manager").join("last_accessed_dir"); + file_overwrite_atomic(last_accessed_dir_path, path.string()); + last_accessed_dir = path; + } + // Returns empty string if no extension static const char* get_ext(const std::filesystem::path &path) { const char *path_c = path.c_str(); @@ -50,6 +60,7 @@ namespace QuickMedia { if(result != PluginResult::OK) return result; + set_last_accessed_dir(current_dir); auto body = create_body(); body->items = std::move(result_items); result_tabs.push_back(Tab{std::move(body), nullptr, nullptr}); @@ -57,9 +68,8 @@ namespace QuickMedia { } bool FileManagerPage::set_current_directory(const std::string &path) { - if(!std::filesystem::is_directory(path)) - return false; current_dir = path; + set_last_accessed_dir(current_dir); return true; } @@ -144,4 +154,19 @@ namespace QuickMedia { return PluginResult::OK; } + + // static + std::filesystem::path& FileManagerPage::get_last_accessed_directory(std::filesystem::path &fallback) { + if(!last_accessed_dir_loaded) { + last_accessed_dir_loaded = true; + std::string last_accessed_dir_str; + file_get_content(get_storage_dir().join("file-manager").join("last_accessed_dir"), last_accessed_dir_str); + last_accessed_dir = std::move(last_accessed_dir_str); + } + + if(last_accessed_dir.empty()) + return fallback; + else + return last_accessed_dir; + } } \ No newline at end of file -- cgit v1.2.3