1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:57:35 +00:00

FileManager: Factorize code to handle drag-and-drop

The treeview and the breadcrumbbar used to be on one side, sharing
drag-and-drop handling and on the other side the directory view had
its one logic.

This patch factorizes both versions, in the meantime upgrading the
version used by the treeview/breadcrumbbar that was left behind. It now
uses the copy dialog :^).
This commit is contained in:
Lucas CHOLLET 2023-02-06 16:08:53 -05:00 committed by Sam Atkins
parent 0c4bbf5be3
commit be28800e0d
4 changed files with 51 additions and 61 deletions

View file

@ -8,8 +8,11 @@
#include "FileUtils.h"
#include "FileOperationProgressWidget.h"
#include <AK/LexicalPath.h>
#include <LibCore/File.h>
#include <LibCore/MimeData.h>
#include <LibCore/Stream.h>
#include <LibCore/System.h>
#include <LibGUI/Event.h>
#include <LibGUI/MessageBox.h>
#include <unistd.h>
@ -108,4 +111,39 @@ ErrorOr<void> run_file_operation(FileOperation operation, Vector<DeprecatedStrin
return {};
}
ErrorOr<bool> handle_drop(GUI::DropEvent const& event, DeprecatedString const& destination, GUI::Window* window)
{
bool has_accepted_drop = false;
if (!event.mime_data().has_urls())
return has_accepted_drop;
auto const urls = event.mime_data().urls();
if (urls.is_empty()) {
dbgln("No files to drop");
return has_accepted_drop;
}
auto const target = LexicalPath::canonicalized_path(destination);
if (!Core::File::is_directory(target))
return has_accepted_drop;
Vector<DeprecatedString> paths_to_copy;
for (auto& url_to_copy : urls) {
if (!url_to_copy.is_valid() || url_to_copy.path() == target)
continue;
auto new_path = DeprecatedString::formatted("{}/{}", target, LexicalPath::basename(url_to_copy.path()));
if (url_to_copy.path() == new_path)
continue;
paths_to_copy.append(url_to_copy.path());
has_accepted_drop = true;
}
if (!paths_to_copy.is_empty())
TRY(run_file_operation(FileOperation::Copy, paths_to_copy, target, window));
return has_accepted_drop;
}
}