DirectoryView.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "DirectoryView.h"
  7. #include "FileOperationProgressWidget.h"
  8. #include "FileUtils.h"
  9. #include <AK/LexicalPath.h>
  10. #include <AK/NumberFormat.h>
  11. #include <AK/StringBuilder.h>
  12. #include <LibCore/File.h>
  13. #include <LibCore/MimeData.h>
  14. #include <LibCore/StandardPaths.h>
  15. #include <LibGUI/FileIconProvider.h>
  16. #include <LibGUI/InputBox.h>
  17. #include <LibGUI/Label.h>
  18. #include <LibGUI/MessageBox.h>
  19. #include <LibGUI/ModelEditingDelegate.h>
  20. #include <LibGUI/SortingProxyModel.h>
  21. #include <serenity.h>
  22. #include <spawn.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. namespace FileManager {
  26. void spawn_terminal(String const& directory)
  27. {
  28. posix_spawn_file_actions_t spawn_actions;
  29. posix_spawn_file_actions_init(&spawn_actions);
  30. posix_spawn_file_actions_addchdir(&spawn_actions, directory.characters());
  31. pid_t pid;
  32. const char* argv[] = { "Terminal", nullptr };
  33. if ((errno = posix_spawn(&pid, "/bin/Terminal", &spawn_actions, nullptr, const_cast<char**>(argv), environ))) {
  34. perror("posix_spawn");
  35. } else {
  36. if (disown(pid) < 0)
  37. perror("disown");
  38. }
  39. posix_spawn_file_actions_destroy(&spawn_actions);
  40. }
  41. enum class FileOperation {
  42. Copy,
  43. };
  44. static HashTable<RefPtr<GUI::Window>> file_operation_windows;
  45. static void run_file_operation([[maybe_unused]] FileOperation operation, const String& source, const String& destination, GUI::Window* parent_window)
  46. {
  47. int pipe_fds[2];
  48. if (pipe(pipe_fds) < 0) {
  49. perror("pipe");
  50. VERIFY_NOT_REACHED();
  51. }
  52. pid_t child_pid = fork();
  53. if (child_pid < 0) {
  54. perror("fork");
  55. VERIFY_NOT_REACHED();
  56. }
  57. if (!child_pid) {
  58. if (close(pipe_fds[0]) < 0) {
  59. perror("close");
  60. _exit(1);
  61. }
  62. if (dup2(pipe_fds[1], STDOUT_FILENO) < 0) {
  63. perror("dup2");
  64. _exit(1);
  65. }
  66. if (execlp("/bin/FileOperation", "/bin/FileOperation", "Copy", source.characters(), LexicalPath::dirname(destination).characters(), nullptr) < 0) {
  67. perror("execlp");
  68. _exit(1);
  69. }
  70. VERIFY_NOT_REACHED();
  71. } else {
  72. if (close(pipe_fds[1]) < 0) {
  73. perror("close");
  74. _exit(1);
  75. }
  76. }
  77. auto window = GUI::Window::construct();
  78. file_operation_windows.set(window);
  79. auto pipe_input_file = Core::File::construct();
  80. pipe_input_file->open(pipe_fds[0], Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes);
  81. window->set_title("Copying Files...");
  82. window->set_main_widget<FileOperationProgressWidget>(pipe_input_file);
  83. window->resize(320, 190);
  84. if (parent_window)
  85. window->center_within(*parent_window);
  86. window->show();
  87. }
  88. NonnullRefPtr<GUI::Action> LauncherHandler::create_launch_action(Function<void(const LauncherHandler&)> launch_handler)
  89. {
  90. auto icon = GUI::FileIconProvider::icon_for_executable(details().executable).bitmap_for_size(16);
  91. return GUI::Action::create(details().name, move(icon), [this, launch_handler = move(launch_handler)](auto&) {
  92. launch_handler(*this);
  93. });
  94. }
  95. RefPtr<LauncherHandler> DirectoryView::get_default_launch_handler(const NonnullRefPtrVector<LauncherHandler>& handlers)
  96. {
  97. // If this is an application, pick it first
  98. for (size_t i = 0; i < handlers.size(); i++) {
  99. if (handlers[i].details().launcher_type == Desktop::Launcher::LauncherType::Application)
  100. return handlers[i];
  101. }
  102. // If there's a handler preferred by the user, pick this first
  103. for (size_t i = 0; i < handlers.size(); i++) {
  104. if (handlers[i].details().launcher_type == Desktop::Launcher::LauncherType::UserPreferred)
  105. return handlers[i];
  106. }
  107. // Otherwise, use the user's default, if available
  108. for (size_t i = 0; i < handlers.size(); i++) {
  109. if (handlers[i].details().launcher_type == Desktop::Launcher::LauncherType::UserDefault)
  110. return handlers[i];
  111. }
  112. // If still no match, use the first one we find
  113. if (!handlers.is_empty()) {
  114. return handlers[0];
  115. }
  116. return {};
  117. }
  118. NonnullRefPtrVector<LauncherHandler> DirectoryView::get_launch_handlers(const URL& url)
  119. {
  120. NonnullRefPtrVector<LauncherHandler> handlers;
  121. for (auto& h : Desktop::Launcher::get_handlers_with_details_for_url(url)) {
  122. handlers.append(adopt_ref(*new LauncherHandler(h)));
  123. }
  124. return handlers;
  125. }
  126. NonnullRefPtrVector<LauncherHandler> DirectoryView::get_launch_handlers(const String& path)
  127. {
  128. return get_launch_handlers(URL::create_with_file_protocol(path));
  129. }
  130. void DirectoryView::handle_activation(const GUI::ModelIndex& index)
  131. {
  132. if (!index.is_valid())
  133. return;
  134. dbgln("on activation: {},{}, this={:p}, m_model={:p}", index.row(), index.column(), this, m_model.ptr());
  135. auto& node = this->node(index);
  136. auto path = node.full_path();
  137. struct stat st;
  138. if (stat(path.characters(), &st) < 0) {
  139. perror("stat");
  140. return;
  141. }
  142. if (S_ISDIR(st.st_mode)) {
  143. if (is_desktop()) {
  144. Desktop::Launcher::open(URL::create_with_file_protocol(path));
  145. return;
  146. }
  147. open(path);
  148. return;
  149. }
  150. auto url = URL::create_with_file_protocol(path);
  151. auto launcher_handlers = get_launch_handlers(url);
  152. auto default_launcher = get_default_launch_handler(launcher_handlers);
  153. if (default_launcher) {
  154. auto launch_origin_rect = current_view().to_widget_rect(current_view().content_rect(index)).translated(current_view().screen_relative_rect().location());
  155. setenv("__libgui_launch_origin_rect", String::formatted("{},{},{},{}", launch_origin_rect.x(), launch_origin_rect.y(), launch_origin_rect.width(), launch_origin_rect.height()).characters(), 1);
  156. launch(url, *default_launcher);
  157. unsetenv("__libgui_launch_origin_rect");
  158. } else {
  159. auto error_message = String::formatted("Could not open {}", path);
  160. GUI::MessageBox::show(window(), error_message, "File Manager", GUI::MessageBox::Type::Error);
  161. }
  162. }
  163. DirectoryView::DirectoryView(Mode mode)
  164. : m_mode(mode)
  165. , m_model(GUI::FileSystemModel::create({}))
  166. , m_sorting_model(GUI::SortingProxyModel::create(m_model))
  167. {
  168. set_active_widget(nullptr);
  169. set_content_margins({ 2, 2, 2, 2 });
  170. setup_actions();
  171. m_error_label = add<GUI::Label>();
  172. m_error_label->set_font(m_error_label->font().bold_variant());
  173. setup_model();
  174. setup_icon_view();
  175. if (mode != Mode::Desktop) {
  176. setup_columns_view();
  177. setup_table_view();
  178. }
  179. set_view_mode(ViewMode::Icon);
  180. }
  181. const GUI::FileSystemModel::Node& DirectoryView::node(const GUI::ModelIndex& index) const
  182. {
  183. return model().node(m_sorting_model->map_to_source(index));
  184. }
  185. void DirectoryView::setup_model()
  186. {
  187. m_model->on_directory_change_error = [this](int, const char* error_string) {
  188. auto failed_path = m_model->root_path();
  189. auto error_message = String::formatted("Could not read {}:\n{}", failed_path, error_string);
  190. m_error_label->set_text(error_message);
  191. set_active_widget(m_error_label);
  192. m_mkdir_action->set_enabled(false);
  193. m_touch_action->set_enabled(false);
  194. add_path_to_history(model().root_path());
  195. if (on_path_change)
  196. on_path_change(failed_path, false, false);
  197. };
  198. m_model->on_rename_error = [this](int, const char* error_string) {
  199. GUI::MessageBox::show_error(window(), String::formatted("Unable to rename file: {}", error_string));
  200. };
  201. m_model->on_complete = [this] {
  202. if (m_table_view)
  203. m_table_view->selection().clear();
  204. if (m_icon_view)
  205. m_icon_view->selection().clear();
  206. add_path_to_history(model().root_path());
  207. bool can_write_in_path = access(model().root_path().characters(), W_OK) == 0;
  208. m_mkdir_action->set_enabled(can_write_in_path);
  209. m_touch_action->set_enabled(can_write_in_path);
  210. if (on_path_change)
  211. on_path_change(model().root_path(), true, can_write_in_path);
  212. };
  213. m_model->register_client(*this);
  214. m_model->on_thumbnail_progress = [this](int done, int total) {
  215. if (on_thumbnail_progress)
  216. on_thumbnail_progress(done, total);
  217. };
  218. if (is_desktop())
  219. m_model->set_root_path(Core::StandardPaths::desktop_directory());
  220. }
  221. void DirectoryView::setup_icon_view()
  222. {
  223. m_icon_view = add<GUI::IconView>();
  224. m_icon_view->set_should_hide_unnecessary_scrollbars(true);
  225. m_icon_view->set_selection_mode(GUI::AbstractView::SelectionMode::MultiSelection);
  226. m_icon_view->set_editable(true);
  227. m_icon_view->set_edit_triggers(GUI::AbstractView::EditTrigger::EditKeyPressed);
  228. m_icon_view->aid_create_editing_delegate = [](auto&) {
  229. return make<GUI::StringModelEditingDelegate>();
  230. };
  231. if (is_desktop()) {
  232. m_icon_view->set_frame_shape(Gfx::FrameShape::NoFrame);
  233. m_icon_view->set_frame_thickness(0);
  234. m_icon_view->set_scrollbars_enabled(false);
  235. m_icon_view->set_fill_with_background_color(false);
  236. m_icon_view->set_draw_item_text_with_shadow(true);
  237. m_icon_view->set_flow_direction(GUI::IconView::FlowDirection::TopToBottom);
  238. }
  239. m_icon_view->set_model(m_sorting_model);
  240. m_icon_view->set_model_column(GUI::FileSystemModel::Column::Name);
  241. m_icon_view->on_activation = [&](auto& index) {
  242. handle_activation(index);
  243. };
  244. m_icon_view->on_selection_change = [this] {
  245. handle_selection_change();
  246. };
  247. m_icon_view->on_context_menu_request = [this](auto& index, auto& event) {
  248. if (on_context_menu_request)
  249. on_context_menu_request(index, event);
  250. };
  251. m_icon_view->on_drop = [this](auto& index, auto& event) {
  252. handle_drop(index, event);
  253. };
  254. }
  255. void DirectoryView::setup_columns_view()
  256. {
  257. m_columns_view = add<GUI::ColumnsView>();
  258. m_columns_view->set_should_hide_unnecessary_scrollbars(true);
  259. m_columns_view->set_selection_mode(GUI::AbstractView::SelectionMode::MultiSelection);
  260. m_columns_view->set_editable(true);
  261. m_columns_view->set_edit_triggers(GUI::AbstractView::EditTrigger::EditKeyPressed);
  262. m_columns_view->aid_create_editing_delegate = [](auto&) {
  263. return make<GUI::StringModelEditingDelegate>();
  264. };
  265. m_columns_view->set_model(m_sorting_model);
  266. m_columns_view->set_model_column(GUI::FileSystemModel::Column::Name);
  267. m_columns_view->on_activation = [&](auto& index) {
  268. handle_activation(index);
  269. };
  270. m_columns_view->on_selection_change = [this] {
  271. handle_selection_change();
  272. };
  273. m_columns_view->on_context_menu_request = [this](auto& index, auto& event) {
  274. if (on_context_menu_request)
  275. on_context_menu_request(index, event);
  276. };
  277. m_columns_view->on_drop = [this](auto& index, auto& event) {
  278. handle_drop(index, event);
  279. };
  280. }
  281. void DirectoryView::setup_table_view()
  282. {
  283. m_table_view = add<GUI::TableView>();
  284. m_table_view->set_should_hide_unnecessary_scrollbars(true);
  285. m_table_view->set_selection_mode(GUI::AbstractView::SelectionMode::MultiSelection);
  286. m_table_view->set_editable(true);
  287. m_table_view->set_edit_triggers(GUI::AbstractView::EditTrigger::EditKeyPressed);
  288. m_table_view->aid_create_editing_delegate = [](auto&) {
  289. return make<GUI::StringModelEditingDelegate>();
  290. };
  291. m_table_view->set_model(m_sorting_model);
  292. m_table_view->set_key_column_and_sort_order(GUI::FileSystemModel::Column::Name, GUI::SortOrder::Ascending);
  293. m_table_view->on_activation = [&](auto& index) {
  294. handle_activation(index);
  295. };
  296. m_table_view->on_selection_change = [this] {
  297. handle_selection_change();
  298. };
  299. m_table_view->on_context_menu_request = [this](auto& index, auto& event) {
  300. if (on_context_menu_request)
  301. on_context_menu_request(index, event);
  302. };
  303. m_table_view->on_drop = [this](auto& index, auto& event) {
  304. handle_drop(index, event);
  305. };
  306. }
  307. DirectoryView::~DirectoryView()
  308. {
  309. m_model->unregister_client(*this);
  310. }
  311. void DirectoryView::model_did_update(unsigned flags)
  312. {
  313. if (flags & GUI::Model::UpdateFlag::InvalidateAllIndices) {
  314. for_each_view_implementation([](auto& view) {
  315. view.selection().clear();
  316. });
  317. }
  318. update_statusbar();
  319. }
  320. void DirectoryView::set_view_mode(ViewMode mode)
  321. {
  322. if (m_view_mode == mode)
  323. return;
  324. m_view_mode = mode;
  325. update();
  326. if (mode == ViewMode::Table) {
  327. set_active_widget(m_table_view);
  328. return;
  329. }
  330. if (mode == ViewMode::Columns) {
  331. set_active_widget(m_columns_view);
  332. return;
  333. }
  334. if (mode == ViewMode::Icon) {
  335. set_active_widget(m_icon_view);
  336. return;
  337. }
  338. VERIFY_NOT_REACHED();
  339. }
  340. void DirectoryView::add_path_to_history(String path)
  341. {
  342. if (m_path_history.size() && m_path_history.at(m_path_history_position) == path)
  343. return;
  344. if (m_path_history_position < m_path_history.size())
  345. m_path_history.resize(m_path_history_position + 1);
  346. m_path_history.append(move(path));
  347. m_path_history_position = m_path_history.size() - 1;
  348. }
  349. void DirectoryView::open(String const& path)
  350. {
  351. auto real_path = Core::File::real_path_for(path);
  352. if (model().root_path() == real_path) {
  353. model().update();
  354. return;
  355. }
  356. set_active_widget(&current_view());
  357. model().set_root_path(real_path);
  358. }
  359. void DirectoryView::set_status_message(const StringView& message)
  360. {
  361. if (on_status_message)
  362. on_status_message(message);
  363. }
  364. void DirectoryView::open_parent_directory()
  365. {
  366. auto path = String::formatted("{}/..", model().root_path());
  367. model().set_root_path(path);
  368. }
  369. void DirectoryView::refresh()
  370. {
  371. model().update();
  372. }
  373. void DirectoryView::open_previous_directory()
  374. {
  375. if (m_path_history_position > 0) {
  376. set_active_widget(&current_view());
  377. m_path_history_position--;
  378. model().set_root_path(m_path_history[m_path_history_position]);
  379. }
  380. }
  381. void DirectoryView::open_next_directory()
  382. {
  383. if (m_path_history_position < m_path_history.size() - 1) {
  384. set_active_widget(&current_view());
  385. m_path_history_position++;
  386. model().set_root_path(m_path_history[m_path_history_position]);
  387. }
  388. }
  389. void DirectoryView::update_statusbar()
  390. {
  391. // If we're triggered during widget construction, just ignore it.
  392. if (m_view_mode == ViewMode::Invalid)
  393. return;
  394. StringBuilder builder;
  395. if (current_view().selection().is_empty()) {
  396. int total_item_count = model().row_count();
  397. size_t total_size = model().node({}).total_size;
  398. builder.appendff("{} item{} ({})", total_item_count, total_item_count != 1 ? "s" : "", human_readable_size(total_size));
  399. set_status_message(builder.string_view());
  400. return;
  401. }
  402. int selected_item_count = current_view().selection().size();
  403. size_t selected_byte_count = 0;
  404. current_view().selection().for_each_index([&](auto& index) {
  405. const auto& node = this->node(index);
  406. selected_byte_count += node.size;
  407. });
  408. builder.appendff("{} item{} selected ({})", selected_item_count, selected_item_count != 1 ? "s" : "", human_readable_size(selected_byte_count));
  409. if (selected_item_count == 1) {
  410. auto& node = this->node(current_view().selection().first());
  411. if (!node.symlink_target.is_empty()) {
  412. builder.append(" -> ");
  413. builder.append(node.symlink_target);
  414. }
  415. }
  416. set_status_message(builder.string_view());
  417. }
  418. void DirectoryView::set_should_show_dotfiles(bool show_dotfiles)
  419. {
  420. m_model->set_should_show_dotfiles(show_dotfiles);
  421. }
  422. void DirectoryView::launch(const URL&, const LauncherHandler& launcher_handler) const
  423. {
  424. pid_t child;
  425. if (launcher_handler.details().launcher_type == Desktop::Launcher::LauncherType::Application) {
  426. const char* argv[] = { launcher_handler.details().name.characters(), nullptr };
  427. posix_spawn(&child, launcher_handler.details().executable.characters(), nullptr, nullptr, const_cast<char**>(argv), environ);
  428. if (disown(child) < 0)
  429. perror("disown");
  430. } else {
  431. for (auto& path : selected_file_paths()) {
  432. const char* argv[] = { launcher_handler.details().name.characters(), path.characters(), nullptr };
  433. posix_spawn(&child, launcher_handler.details().executable.characters(), nullptr, nullptr, const_cast<char**>(argv), environ);
  434. if (disown(child) < 0)
  435. perror("disown");
  436. }
  437. }
  438. }
  439. Vector<String> DirectoryView::selected_file_paths() const
  440. {
  441. Vector<String> paths;
  442. auto& view = current_view();
  443. auto& model = *view.model();
  444. view.selection().for_each_index([&](const GUI::ModelIndex& index) {
  445. auto parent_index = model.parent_index(index);
  446. auto name_index = model.index(index.row(), GUI::FileSystemModel::Column::Name, parent_index);
  447. auto path = name_index.data(GUI::ModelRole::Custom).to_string();
  448. paths.append(path);
  449. });
  450. return paths;
  451. }
  452. void DirectoryView::do_delete(bool should_confirm)
  453. {
  454. auto paths = selected_file_paths();
  455. VERIFY(!paths.is_empty());
  456. FileUtils::delete_paths(paths, should_confirm, window());
  457. }
  458. void DirectoryView::handle_selection_change()
  459. {
  460. update_statusbar();
  461. bool can_modify = !current_view().selection().is_empty() && access(path().characters(), W_OK) == 0;
  462. m_delete_action->set_enabled(can_modify);
  463. m_force_delete_action->set_enabled(can_modify);
  464. m_rename_action->set_enabled(can_modify);
  465. if (on_selection_change)
  466. on_selection_change(current_view());
  467. }
  468. void DirectoryView::setup_actions()
  469. {
  470. m_mkdir_action = GUI::Action::create("&New Directory...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"), [&](const GUI::Action&) {
  471. String value;
  472. if (GUI::InputBox::show(window(), value, "Enter name:", "New directory") == GUI::InputBox::ExecOK && !value.is_empty()) {
  473. auto new_dir_path = LexicalPath::canonicalized_path(String::formatted("{}/{}", path(), value));
  474. int rc = mkdir(new_dir_path.characters(), 0777);
  475. if (rc < 0) {
  476. auto saved_errno = errno;
  477. GUI::MessageBox::show(window(), String::formatted("mkdir(\"{}\") failed: {}", new_dir_path, strerror(saved_errno)), "Error", GUI::MessageBox::Type::Error);
  478. }
  479. }
  480. });
  481. m_touch_action = GUI::Action::create("New &File...", { Mod_Ctrl | Mod_Shift, Key_F }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GUI::Action&) {
  482. String value;
  483. if (GUI::InputBox::show(window(), value, "Enter name:", "New file") == GUI::InputBox::ExecOK && !value.is_empty()) {
  484. auto new_file_path = LexicalPath::canonicalized_path(String::formatted("{}/{}", path(), value));
  485. struct stat st;
  486. int rc = stat(new_file_path.characters(), &st);
  487. if ((rc < 0 && errno != ENOENT)) {
  488. auto saved_errno = errno;
  489. GUI::MessageBox::show(window(), String::formatted("stat(\"{}\") failed: {}", new_file_path, strerror(saved_errno)), "Error", GUI::MessageBox::Type::Error);
  490. return;
  491. }
  492. if (rc == 0) {
  493. GUI::MessageBox::show(window(), String::formatted("{}: Already exists", new_file_path), "Error", GUI::MessageBox::Type::Error);
  494. return;
  495. }
  496. int fd = creat(new_file_path.characters(), 0666);
  497. if (fd < 0) {
  498. auto saved_errno = errno;
  499. GUI::MessageBox::show(window(), String::formatted("creat(\"{}\") failed: {}", new_file_path, strerror(saved_errno)), "Error", GUI::MessageBox::Type::Error);
  500. return;
  501. }
  502. rc = close(fd);
  503. VERIFY(rc >= 0);
  504. }
  505. });
  506. m_open_terminal_action = GUI::Action::create("Open &Terminal Here", Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"), [&](auto&) {
  507. spawn_terminal(path());
  508. });
  509. m_delete_action = GUI::CommonActions::make_delete_action([this](auto&) { do_delete(true); }, window());
  510. m_rename_action = GUI::CommonActions::make_rename_action([this](auto&) {
  511. current_view().begin_editing(current_view().cursor_index());
  512. },
  513. window());
  514. m_force_delete_action = GUI::Action::create(
  515. "Delete Without Confirmation", { Mod_Shift, Key_Delete },
  516. [this](auto&) { do_delete(false); },
  517. window());
  518. }
  519. void DirectoryView::handle_drop(const GUI::ModelIndex& index, const GUI::DropEvent& event)
  520. {
  521. if (!event.mime_data().has_urls())
  522. return;
  523. auto urls = event.mime_data().urls();
  524. if (urls.is_empty()) {
  525. dbgln("No files to drop");
  526. return;
  527. }
  528. auto& target_node = node(index);
  529. if (!target_node.is_directory())
  530. return;
  531. bool had_accepted_drop = false;
  532. for (auto& url_to_copy : urls) {
  533. if (!url_to_copy.is_valid() || url_to_copy.path() == target_node.full_path())
  534. continue;
  535. auto new_path = String::formatted("{}/{}", target_node.full_path(), LexicalPath::basename(url_to_copy.path()));
  536. if (url_to_copy.path() == new_path)
  537. continue;
  538. run_file_operation(FileOperation::Copy, url_to_copy.path(), new_path, window());
  539. had_accepted_drop = true;
  540. }
  541. if (had_accepted_drop && on_accepted_drop)
  542. on_accepted_drop();
  543. }
  544. }