mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-29 06:02:07 +03:00
FileManager: Move mass-copy code from DirectoryView to FileUtils
This is preparation for calling it from FileManager/main.cpp Also made file_operation_windows take NonnullRefPtrs.
This commit is contained in:
parent
9060310840
commit
d8fb8b9583
Notes:
sideshowbarker
2024-07-18 08:34:44 +09:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/SerenityOS/serenity/commit/d8fb8b95830 Pull-request: https://github.com/SerenityOS/serenity/pull/8117 Reviewed-by: https://github.com/MaxWipfli ✅
@ -44,72 +44,6 @@ void spawn_terminal(String const& directory)
|
||||
posix_spawn_file_actions_destroy(&spawn_actions);
|
||||
}
|
||||
|
||||
enum class FileOperation {
|
||||
Copy,
|
||||
};
|
||||
|
||||
static HashTable<RefPtr<GUI::Window>> file_operation_windows;
|
||||
|
||||
static void run_file_operation([[maybe_unused]] FileOperation operation, Vector<String> const& sources, String const& destination, GUI::Window* parent_window)
|
||||
{
|
||||
int pipe_fds[2];
|
||||
if (pipe(pipe_fds) < 0) {
|
||||
perror("pipe");
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
pid_t child_pid = fork();
|
||||
if (child_pid < 0) {
|
||||
perror("fork");
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
if (!child_pid) {
|
||||
if (close(pipe_fds[0]) < 0) {
|
||||
perror("close");
|
||||
_exit(1);
|
||||
}
|
||||
if (dup2(pipe_fds[1], STDOUT_FILENO) < 0) {
|
||||
perror("dup2");
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
Vector<char const*> file_operation_args;
|
||||
file_operation_args.append("/bin/FileOperation");
|
||||
file_operation_args.append("Copy");
|
||||
|
||||
for (auto& source : sources)
|
||||
file_operation_args.append(source.characters());
|
||||
|
||||
file_operation_args.append(destination.characters());
|
||||
file_operation_args.append(nullptr);
|
||||
|
||||
if (execvp(file_operation_args.first(), const_cast<char**>(file_operation_args.data())) < 0) {
|
||||
perror("execvp");
|
||||
_exit(1);
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
} else {
|
||||
if (close(pipe_fds[1]) < 0) {
|
||||
perror("close");
|
||||
_exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
auto window = GUI::Window::construct();
|
||||
file_operation_windows.set(window);
|
||||
|
||||
auto pipe_input_file = Core::File::construct();
|
||||
pipe_input_file->open(pipe_fds[0], Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes);
|
||||
|
||||
window->set_title("Copying Files...");
|
||||
window->set_main_widget<FileOperationProgressWidget>(pipe_input_file);
|
||||
window->resize(320, 190);
|
||||
if (parent_window)
|
||||
window->center_within(*parent_window);
|
||||
window->show();
|
||||
}
|
||||
|
||||
NonnullRefPtr<GUI::Action> LauncherHandler::create_launch_action(Function<void(LauncherHandler const&)> launch_handler)
|
||||
{
|
||||
auto icon = GUI::FileIconProvider::icon_for_executable(details().executable).bitmap_for_size(16);
|
||||
@ -547,7 +481,7 @@ void DirectoryView::do_delete(bool should_confirm)
|
||||
{
|
||||
auto paths = selected_file_paths();
|
||||
VERIFY(!paths.is_empty());
|
||||
FileUtils::delete_paths(paths, should_confirm, window());
|
||||
delete_paths(paths, should_confirm, window());
|
||||
}
|
||||
|
||||
void DirectoryView::handle_selection_change()
|
||||
|
@ -1,10 +1,12 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "FileUtils.h"
|
||||
#include "FileOperationProgressWidget.h"
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <LibCore/DirIterator.h>
|
||||
#include <LibCore/File.h>
|
||||
@ -12,7 +14,9 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace FileUtils {
|
||||
namespace FileManager {
|
||||
|
||||
HashTable<NonnullRefPtr<GUI::Window>> file_operation_windows;
|
||||
|
||||
void delete_path(String const& path, GUI::Window* parent_window)
|
||||
{
|
||||
@ -67,4 +71,64 @@ void delete_paths(Vector<String> const& paths, bool should_confirm, GUI::Window*
|
||||
}
|
||||
}
|
||||
|
||||
void run_file_operation([[maybe_unused]] FileOperation operation, Vector<String> const& sources, String const& destination, GUI::Window* parent_window)
|
||||
{
|
||||
int pipe_fds[2];
|
||||
if (pipe(pipe_fds) < 0) {
|
||||
perror("pipe");
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
pid_t child_pid = fork();
|
||||
if (child_pid < 0) {
|
||||
perror("fork");
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
if (!child_pid) {
|
||||
if (close(pipe_fds[0]) < 0) {
|
||||
perror("close");
|
||||
_exit(1);
|
||||
}
|
||||
if (dup2(pipe_fds[1], STDOUT_FILENO) < 0) {
|
||||
perror("dup2");
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
Vector<char const*> file_operation_args;
|
||||
file_operation_args.append("/bin/FileOperation");
|
||||
file_operation_args.append("Copy");
|
||||
|
||||
for (auto& source : sources)
|
||||
file_operation_args.append(source.characters());
|
||||
|
||||
file_operation_args.append(destination.characters());
|
||||
file_operation_args.append(nullptr);
|
||||
|
||||
if (execvp(file_operation_args.first(), const_cast<char**>(file_operation_args.data())) < 0) {
|
||||
perror("execvp");
|
||||
_exit(1);
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
} else {
|
||||
if (close(pipe_fds[1]) < 0) {
|
||||
perror("close");
|
||||
_exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
auto window = GUI::Window::construct();
|
||||
file_operation_windows.set(window);
|
||||
|
||||
auto pipe_input_file = Core::File::construct();
|
||||
pipe_input_file->open(pipe_fds[0], Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes);
|
||||
|
||||
window->set_title("Copying Files...");
|
||||
window->set_main_widget<FileOperationProgressWidget>(pipe_input_file);
|
||||
window->resize(320, 190);
|
||||
if (parent_window)
|
||||
window->center_within(*parent_window);
|
||||
window->show();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
#include <LibGUI/Forward.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace FileUtils {
|
||||
namespace FileManager {
|
||||
|
||||
enum class FileOperation {
|
||||
Copy = 0,
|
||||
@ -20,4 +20,6 @@ enum class FileOperation {
|
||||
|
||||
void delete_path(String const&, GUI::Window*);
|
||||
void delete_paths(Vector<String> const&, bool should_confirm, GUI::Window*);
|
||||
|
||||
void run_file_operation([[maybe_unused]] FileOperation operation, Vector<String> const& sources, String const& destination, GUI::Window* parent_window);
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ using namespace FileManager;
|
||||
|
||||
static int run_in_desktop_mode(RefPtr<Core::ConfigFile>);
|
||||
static int run_in_windowed_mode(RefPtr<Core::ConfigFile>, String initial_location, String entry_focused_on_init);
|
||||
static void do_copy(Vector<String> const& selected_file_paths, FileUtils::FileOperation file_operation);
|
||||
static void do_copy(Vector<String> const& selected_file_paths, FileOperation file_operation);
|
||||
static void do_paste(String const& target_directory, GUI::Window* window);
|
||||
static void do_create_link(Vector<String> const& selected_file_paths, GUI::Window* window);
|
||||
static void do_unzip_archive(Vector<String> const& selected_file_paths, GUI::Window* window);
|
||||
@ -131,13 +131,13 @@ int main(int argc, char** argv)
|
||||
return run_in_windowed_mode(move(config), initial_location, focused_entry);
|
||||
}
|
||||
|
||||
void do_copy(Vector<String> const& selected_file_paths, FileUtils::FileOperation file_operation)
|
||||
void do_copy(Vector<String> const& selected_file_paths, FileOperation file_operation)
|
||||
{
|
||||
if (selected_file_paths.is_empty())
|
||||
VERIFY_NOT_REACHED();
|
||||
|
||||
StringBuilder copy_text;
|
||||
if (file_operation == FileUtils::FileOperation::Cut) {
|
||||
if (file_operation == FileOperation::Cut) {
|
||||
copy_text.append("#cut\n"); // This exploits the comment lines in the text/uri-list specification, which might be a bit hackish
|
||||
}
|
||||
for (auto& path : selected_file_paths) {
|
||||
@ -180,7 +180,7 @@ void do_paste(String const& target_directory, GUI::Window* window)
|
||||
auto error_message = String::formatted("Could not paste '{}': {}", url.path(), result.error().error_code);
|
||||
GUI::MessageBox::show(window, error_message, "File Manager", GUI::MessageBox::Type::Error);
|
||||
} else if (should_delete_src) {
|
||||
FileUtils::delete_path(url.path(), window);
|
||||
delete_path(url.path(), window);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -297,7 +297,7 @@ int run_in_desktop_mode([[maybe_unused]] RefPtr<Core::ConfigFile> config)
|
||||
if (paths.is_empty())
|
||||
VERIFY_NOT_REACHED();
|
||||
|
||||
do_copy(paths, FileUtils::FileOperation::Cut);
|
||||
do_copy(paths, FileOperation::Cut);
|
||||
},
|
||||
window);
|
||||
cut_action->set_enabled(false);
|
||||
@ -309,7 +309,7 @@ int run_in_desktop_mode([[maybe_unused]] RefPtr<Core::ConfigFile> config)
|
||||
if (paths.is_empty())
|
||||
VERIFY_NOT_REACHED();
|
||||
|
||||
do_copy(paths, FileUtils::FileOperation::Copy);
|
||||
do_copy(paths, FileOperation::Copy);
|
||||
},
|
||||
window);
|
||||
copy_action->set_enabled(false);
|
||||
@ -660,7 +660,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
||||
if (paths.is_empty())
|
||||
VERIFY_NOT_REACHED();
|
||||
|
||||
do_copy(paths, FileUtils::FileOperation::Cut);
|
||||
do_copy(paths, FileOperation::Cut);
|
||||
refresh_tree_view();
|
||||
},
|
||||
window);
|
||||
@ -676,7 +676,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
||||
if (paths.is_empty())
|
||||
VERIFY_NOT_REACHED();
|
||||
|
||||
do_copy(paths, FileUtils::FileOperation::Copy);
|
||||
do_copy(paths, FileOperation::Copy);
|
||||
refresh_tree_view();
|
||||
},
|
||||
window);
|
||||
@ -816,7 +816,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
||||
|
||||
auto tree_view_delete_action = GUI::CommonActions::make_delete_action(
|
||||
[&](auto&) {
|
||||
FileUtils::delete_paths(tree_view_selected_file_paths(), true, window);
|
||||
delete_paths(tree_view_selected_file_paths(), true, window);
|
||||
refresh_tree_view();
|
||||
},
|
||||
&tree_view);
|
||||
|
Loading…
Reference in New Issue
Block a user