main.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "DesktopWidget.h"
  8. #include "DirectoryView.h"
  9. #include "FileUtils.h"
  10. #include "PropertiesWindow.h"
  11. #include <AK/LexicalPath.h>
  12. #include <AK/StringBuilder.h>
  13. #include <AK/URL.h>
  14. #include <Applications/FileManager/FileManagerWindowGML.h>
  15. #include <LibConfig/Client.h>
  16. #include <LibCore/ArgsParser.h>
  17. #include <LibCore/File.h>
  18. #include <LibCore/StandardPaths.h>
  19. #include <LibDesktop/Launcher.h>
  20. #include <LibGUI/Action.h>
  21. #include <LibGUI/ActionGroup.h>
  22. #include <LibGUI/Application.h>
  23. #include <LibGUI/BoxLayout.h>
  24. #include <LibGUI/Breadcrumbbar.h>
  25. #include <LibGUI/Clipboard.h>
  26. #include <LibGUI/Desktop.h>
  27. #include <LibGUI/FileIconProvider.h>
  28. #include <LibGUI/FileSystemModel.h>
  29. #include <LibGUI/Menu.h>
  30. #include <LibGUI/Menubar.h>
  31. #include <LibGUI/MessageBox.h>
  32. #include <LibGUI/Painter.h>
  33. #include <LibGUI/Progressbar.h>
  34. #include <LibGUI/Splitter.h>
  35. #include <LibGUI/Statusbar.h>
  36. #include <LibGUI/Toolbar.h>
  37. #include <LibGUI/ToolbarContainer.h>
  38. #include <LibGUI/TreeView.h>
  39. #include <LibGUI/Widget.h>
  40. #include <LibGUI/Window.h>
  41. #include <LibGfx/Palette.h>
  42. #include <pthread.h>
  43. #include <signal.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include <sys/wait.h>
  47. #include <unistd.h>
  48. using namespace FileManager;
  49. static int run_in_desktop_mode();
  50. static int run_in_windowed_mode(String initial_location, String entry_focused_on_init);
  51. static void do_copy(Vector<String> const& selected_file_paths, FileOperation file_operation);
  52. static void do_paste(String const& target_directory, GUI::Window* window);
  53. static void do_create_link(Vector<String> const& selected_file_paths, GUI::Window* window);
  54. static void do_unzip_archive(Vector<String> const& selected_file_paths, GUI::Window* window);
  55. static void show_properties(String const& container_dir_path, String const& path, Vector<String> const& selected, GUI::Window* window);
  56. static bool add_launch_handler_actions_to_menu(RefPtr<GUI::Menu>& menu, DirectoryView const& directory_view, String const& full_path, RefPtr<GUI::Action>& default_action, NonnullRefPtrVector<LauncherHandler>& current_file_launch_handlers);
  57. int main(int argc, char** argv)
  58. {
  59. if (pledge("stdio thread recvfd sendfd unix cpath rpath wpath fattr proc exec sigaction", nullptr) < 0) {
  60. perror("pledge");
  61. return 1;
  62. }
  63. struct sigaction act;
  64. memset(&act, 0, sizeof(act));
  65. act.sa_flags = SA_NOCLDWAIT;
  66. act.sa_handler = SIG_IGN;
  67. int rc = sigaction(SIGCHLD, &act, nullptr);
  68. if (rc < 0) {
  69. perror("sigaction");
  70. return 1;
  71. }
  72. Core::ArgsParser args_parser;
  73. bool is_desktop_mode { false }, is_selection_mode { false }, ignore_path_resolution { false };
  74. String initial_location;
  75. args_parser.add_option(is_desktop_mode, "Run in desktop mode", "desktop", 'd');
  76. args_parser.add_option(is_selection_mode, "Show entry in parent folder", "select", 's');
  77. args_parser.add_option(ignore_path_resolution, "Use raw path, do not resolve real path", "raw", 'r');
  78. args_parser.add_positional_argument(initial_location, "Path to open", "path", Core::ArgsParser::Required::No);
  79. args_parser.parse(argc, argv);
  80. auto app = GUI::Application::construct(argc, argv);
  81. if (pledge("stdio thread recvfd sendfd cpath rpath wpath fattr proc exec unix", nullptr) < 0) {
  82. perror("pledge");
  83. return 1;
  84. }
  85. Config::pledge_domains({ "FileManager", "WindowManager" });
  86. Config::monitor_domain("FileManager");
  87. if (is_desktop_mode)
  88. return run_in_desktop_mode();
  89. // our initial location is defined as, in order of precedence:
  90. // 1. the command-line path argument (e.g. FileManager /bin)
  91. // 2. the current directory
  92. // 3. the user's home directory
  93. // 4. the root directory
  94. if (!initial_location.is_empty()) {
  95. if (!ignore_path_resolution)
  96. initial_location = Core::File::real_path_for(initial_location);
  97. if (!Core::File::is_directory(initial_location))
  98. is_selection_mode = true;
  99. }
  100. if (initial_location.is_empty())
  101. initial_location = Core::File::current_working_directory();
  102. if (initial_location.is_empty())
  103. initial_location = Core::StandardPaths::home_directory();
  104. if (initial_location.is_empty())
  105. initial_location = "/";
  106. String focused_entry;
  107. if (is_selection_mode) {
  108. LexicalPath path(initial_location);
  109. initial_location = path.dirname();
  110. focused_entry = path.basename();
  111. }
  112. return run_in_windowed_mode(initial_location, focused_entry);
  113. }
  114. void do_copy(Vector<String> const& selected_file_paths, FileOperation file_operation)
  115. {
  116. if (selected_file_paths.is_empty())
  117. VERIFY_NOT_REACHED();
  118. StringBuilder copy_text;
  119. if (file_operation == FileOperation::Move) {
  120. copy_text.append("#cut\n"); // This exploits the comment lines in the text/uri-list specification, which might be a bit hackish
  121. }
  122. for (auto& path : selected_file_paths) {
  123. auto url = URL::create_with_file_protocol(path);
  124. copy_text.appendff("{}\n", url);
  125. }
  126. GUI::Clipboard::the().set_data(copy_text.build().bytes(), "text/uri-list");
  127. }
  128. void do_paste(String const& target_directory, GUI::Window* window)
  129. {
  130. auto data_and_type = GUI::Clipboard::the().data_and_type();
  131. if (data_and_type.mime_type != "text/uri-list") {
  132. dbgln("Cannot paste clipboard type {}", data_and_type.mime_type);
  133. return;
  134. }
  135. auto copied_lines = String::copy(data_and_type.data).split('\n');
  136. if (copied_lines.is_empty()) {
  137. dbgln("No files to paste");
  138. return;
  139. }
  140. FileOperation file_operation = FileOperation::Copy;
  141. if (copied_lines[0] == "#cut") { // cut operation encoded as a text/uri-list comment
  142. file_operation = FileOperation::Move;
  143. copied_lines.remove(0);
  144. }
  145. Vector<String> source_paths;
  146. for (auto& uri_as_string : copied_lines) {
  147. if (uri_as_string.is_empty())
  148. continue;
  149. URL url = uri_as_string;
  150. if (!url.is_valid() || url.protocol() != "file") {
  151. dbgln("Cannot paste URI {}", uri_as_string);
  152. continue;
  153. }
  154. auto new_path = String::formatted("{}/{}", target_directory, url.basename());
  155. if (url.path() == new_path)
  156. continue;
  157. source_paths.append(url.path());
  158. }
  159. if (!source_paths.is_empty())
  160. run_file_operation(file_operation, source_paths, target_directory, window);
  161. }
  162. void do_create_link(Vector<String> const& selected_file_paths, GUI::Window* window)
  163. {
  164. auto path = selected_file_paths.first();
  165. auto destination = String::formatted("{}/{}", Core::StandardPaths::desktop_directory(), LexicalPath::basename(path));
  166. if (auto result = Core::File::link_file(destination, path); result.is_error()) {
  167. GUI::MessageBox::show(window, String::formatted("Could not create desktop shortcut:\n{}", result.error()), "File Manager",
  168. GUI::MessageBox::Type::Error);
  169. }
  170. }
  171. void do_unzip_archive(Vector<String> const& selected_file_paths, GUI::Window* window)
  172. {
  173. String archive_file_path = selected_file_paths.first();
  174. String output_directory_path = archive_file_path.substring(0, archive_file_path.length() - 4);
  175. pid_t unzip_pid = fork();
  176. if (unzip_pid < 0) {
  177. perror("fork");
  178. VERIFY_NOT_REACHED();
  179. }
  180. if (!unzip_pid) {
  181. int rc = execlp("/bin/unzip", "/bin/unzip", "-d", output_directory_path.characters(), archive_file_path.characters(), nullptr);
  182. if (rc < 0) {
  183. perror("execlp");
  184. _exit(1);
  185. }
  186. } else {
  187. // FIXME: this could probably be tied in with the new file operation progress tracking
  188. int status;
  189. int rc = waitpid(unzip_pid, &status, 0);
  190. if (rc < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
  191. GUI::MessageBox::show(window, "Could not extract archive", "Extract Archive Error", GUI::MessageBox::Type::Error);
  192. }
  193. }
  194. void show_properties(String const& container_dir_path, String const& path, Vector<String> const& selected, GUI::Window* window)
  195. {
  196. RefPtr<PropertiesWindow> properties;
  197. if (selected.is_empty()) {
  198. properties = window->add<PropertiesWindow>(path, true);
  199. } else {
  200. properties = window->add<PropertiesWindow>(selected.first(), access(container_dir_path.characters(), W_OK) != 0);
  201. }
  202. properties->on_close = [properties = properties.ptr()] {
  203. properties->remove_from_parent();
  204. };
  205. properties->center_on_screen();
  206. properties->show();
  207. }
  208. bool add_launch_handler_actions_to_menu(RefPtr<GUI::Menu>& menu, DirectoryView const& directory_view, String const& full_path, RefPtr<GUI::Action>& default_action, NonnullRefPtrVector<LauncherHandler>& current_file_launch_handlers)
  209. {
  210. current_file_launch_handlers = directory_view.get_launch_handlers(full_path);
  211. bool added_open_menu_items = false;
  212. auto default_file_handler = directory_view.get_default_launch_handler(current_file_launch_handlers);
  213. if (default_file_handler) {
  214. auto file_open_action = default_file_handler->create_launch_action([&, full_path = move(full_path)](auto& launcher_handler) {
  215. directory_view.launch(URL::create_with_file_protocol(full_path), launcher_handler);
  216. });
  217. if (default_file_handler->details().launcher_type == Desktop::Launcher::LauncherType::Application)
  218. file_open_action->set_text(String::formatted("Run {}", file_open_action->text()));
  219. else
  220. file_open_action->set_text(String::formatted("Open in {}", file_open_action->text()));
  221. default_action = file_open_action;
  222. menu->add_action(move(file_open_action));
  223. added_open_menu_items = true;
  224. } else {
  225. default_action.clear();
  226. }
  227. if (current_file_launch_handlers.size() > 1) {
  228. added_open_menu_items = true;
  229. auto& file_open_with_menu = menu->add_submenu("Open with");
  230. for (auto& handler : current_file_launch_handlers) {
  231. if (&handler == default_file_handler.ptr())
  232. continue;
  233. file_open_with_menu.add_action(handler.create_launch_action([&, full_path = move(full_path)](auto& launcher_handler) {
  234. directory_view.launch(URL::create_with_file_protocol(full_path), launcher_handler);
  235. }));
  236. }
  237. }
  238. return added_open_menu_items;
  239. }
  240. int run_in_desktop_mode()
  241. {
  242. static constexpr char const* process_name = "FileManager (Desktop)";
  243. set_process_name(process_name, strlen(process_name));
  244. pthread_setname_np(pthread_self(), process_name);
  245. auto window = GUI::Window::construct();
  246. window->set_title("Desktop Manager");
  247. window->set_window_type(GUI::WindowType::Desktop);
  248. window->set_has_alpha_channel(true);
  249. auto& desktop_widget = window->set_main_widget<FileManager::DesktopWidget>();
  250. desktop_widget.set_layout<GUI::VerticalBoxLayout>();
  251. auto& directory_view = desktop_widget.add<DirectoryView>(DirectoryView::Mode::Desktop);
  252. directory_view.set_name("directory_view");
  253. auto cut_action = GUI::CommonActions::make_cut_action(
  254. [&](auto&) {
  255. auto paths = directory_view.selected_file_paths();
  256. if (paths.is_empty())
  257. VERIFY_NOT_REACHED();
  258. do_copy(paths, FileOperation::Move);
  259. },
  260. window);
  261. cut_action->set_enabled(false);
  262. auto copy_action = GUI::CommonActions::make_copy_action(
  263. [&](auto&) {
  264. auto paths = directory_view.selected_file_paths();
  265. if (paths.is_empty())
  266. VERIFY_NOT_REACHED();
  267. do_copy(paths, FileOperation::Copy);
  268. },
  269. window);
  270. copy_action->set_enabled(false);
  271. auto unzip_archive_action
  272. = GUI::Action::create(
  273. "E&xtract Here",
  274. [&](GUI::Action const&) {
  275. auto paths = directory_view.selected_file_paths();
  276. if (paths.is_empty())
  277. return;
  278. do_unzip_archive(paths, directory_view.window());
  279. },
  280. window);
  281. directory_view.on_selection_change = [&](GUI::AbstractView const& view) {
  282. cut_action->set_enabled(!view.selection().is_empty());
  283. copy_action->set_enabled(!view.selection().is_empty());
  284. };
  285. auto properties_action = GUI::CommonActions::make_properties_action(
  286. [&](auto&) {
  287. String path = directory_view.path();
  288. Vector<String> selected = directory_view.selected_file_paths();
  289. show_properties(path, path, selected, directory_view.window());
  290. },
  291. window);
  292. auto paste_action = GUI::CommonActions::make_paste_action(
  293. [&](GUI::Action const&) {
  294. do_paste(directory_view.path(), directory_view.window());
  295. },
  296. window);
  297. paste_action->set_enabled(GUI::Clipboard::the().mime_type() == "text/uri-list" && access(directory_view.path().characters(), W_OK) == 0);
  298. GUI::Clipboard::the().on_change = [&](String const& data_type) {
  299. paste_action->set_enabled(data_type == "text/uri-list" && access(directory_view.path().characters(), W_OK) == 0);
  300. };
  301. auto desktop_view_context_menu = GUI::Menu::construct("Directory View");
  302. auto file_manager_action = GUI::Action::create("Open in File &Manager", {}, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-file-manager.png"), [&](auto&) {
  303. auto paths = directory_view.selected_file_paths();
  304. if (paths.is_empty()) {
  305. Desktop::Launcher::open(URL::create_with_file_protocol(directory_view.path()));
  306. return;
  307. }
  308. for (auto& path : paths) {
  309. if (Core::File::is_directory(path))
  310. Desktop::Launcher::open(URL::create_with_file_protocol(path));
  311. }
  312. });
  313. auto display_properties_action = GUI::Action::create("&Display Settings", {}, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-display-settings.png"), [&](GUI::Action const&) {
  314. Desktop::Launcher::open(URL::create_with_file_protocol("/bin/DisplaySettings"));
  315. });
  316. desktop_view_context_menu->add_action(directory_view.mkdir_action());
  317. desktop_view_context_menu->add_action(directory_view.touch_action());
  318. desktop_view_context_menu->add_action(paste_action);
  319. desktop_view_context_menu->add_separator();
  320. desktop_view_context_menu->add_action(file_manager_action);
  321. desktop_view_context_menu->add_action(open_terminal_action);
  322. desktop_view_context_menu->add_separator();
  323. desktop_view_context_menu->add_action(display_properties_action);
  324. auto desktop_context_menu = GUI::Menu::construct("Directory View Directory");
  325. desktop_context_menu->add_action(file_manager_action);
  326. desktop_context_menu->add_separator();
  327. desktop_context_menu->add_action(cut_action);
  328. desktop_context_menu->add_action(copy_action);
  329. desktop_context_menu->add_action(paste_action);
  330. desktop_context_menu->add_action(directory_view.delete_action());
  331. desktop_context_menu->add_action(directory_view.rename_action());
  332. desktop_context_menu->add_separator();
  333. desktop_context_menu->add_action(properties_action);
  334. RefPtr<GUI::Menu> file_context_menu;
  335. NonnullRefPtrVector<LauncherHandler> current_file_handlers;
  336. RefPtr<GUI::Action> file_context_menu_action_default_action;
  337. directory_view.on_context_menu_request = [&](GUI::ModelIndex const& index, GUI::ContextMenuEvent const& event) {
  338. if (index.is_valid()) {
  339. auto& node = directory_view.node(index);
  340. if (node.is_directory()) {
  341. desktop_context_menu->popup(event.screen_position(), file_manager_action);
  342. } else {
  343. file_context_menu = GUI::Menu::construct("Directory View File");
  344. bool added_open_menu_items = add_launch_handler_actions_to_menu(file_context_menu, directory_view, node.full_path(), file_context_menu_action_default_action, current_file_handlers);
  345. if (added_open_menu_items)
  346. file_context_menu->add_separator();
  347. file_context_menu->add_action(cut_action);
  348. file_context_menu->add_action(copy_action);
  349. file_context_menu->add_action(paste_action);
  350. file_context_menu->add_action(directory_view.delete_action());
  351. file_context_menu->add_action(directory_view.rename_action());
  352. file_context_menu->add_separator();
  353. if (node.full_path().ends_with(".zip", AK::CaseSensitivity::CaseInsensitive)) {
  354. file_context_menu->add_action(unzip_archive_action);
  355. file_context_menu->add_separator();
  356. }
  357. file_context_menu->add_action(properties_action);
  358. file_context_menu->popup(event.screen_position(), file_context_menu_action_default_action);
  359. }
  360. } else {
  361. desktop_view_context_menu->popup(event.screen_position());
  362. }
  363. };
  364. auto selected_wallpaper = Config::read_string("WindowManager", "Background", "Wallpaper", "");
  365. if (!selected_wallpaper.is_empty()) {
  366. GUI::Desktop::the().set_wallpaper(selected_wallpaper, false);
  367. }
  368. window->show();
  369. return GUI::Application::the()->exec();
  370. }
  371. int run_in_windowed_mode(String initial_location, String entry_focused_on_init)
  372. {
  373. auto window = GUI::Window::construct();
  374. window->set_title("File Manager");
  375. auto left = Config::read_i32("FileManager", "Window", "Left", 150);
  376. auto top = Config::read_i32("FileManager", "Window", "Top", 75);
  377. auto width = Config::read_i32("FileManager", "Window", "Width", 640);
  378. auto height = Config::read_i32("FileManager", "Window", "Height", 480);
  379. auto was_maximized = Config::read_bool("FileManager", "Window", "Maximized", false);
  380. auto& widget = window->set_main_widget<GUI::Widget>();
  381. widget.load_from_gml(file_manager_window_gml);
  382. auto& toolbar_container = *widget.find_descendant_of_type_named<GUI::ToolbarContainer>("toolbar_container");
  383. auto& main_toolbar = *widget.find_descendant_of_type_named<GUI::Toolbar>("main_toolbar");
  384. auto& location_toolbar = *widget.find_descendant_of_type_named<GUI::Toolbar>("location_toolbar");
  385. location_toolbar.layout()->set_margins({ 3, 6 });
  386. auto& location_textbox = *widget.find_descendant_of_type_named<GUI::TextBox>("location_textbox");
  387. auto& breadcrumb_toolbar = *widget.find_descendant_of_type_named<GUI::Toolbar>("breadcrumb_toolbar");
  388. breadcrumb_toolbar.layout()->set_margins({ 0, 6 });
  389. auto& breadcrumbbar = *widget.find_descendant_of_type_named<GUI::Breadcrumbbar>("breadcrumbbar");
  390. auto& splitter = *widget.find_descendant_of_type_named<GUI::HorizontalSplitter>("splitter");
  391. auto& tree_view = *widget.find_descendant_of_type_named<GUI::TreeView>("tree_view");
  392. auto directories_model = GUI::FileSystemModel::create({}, GUI::FileSystemModel::Mode::DirectoriesOnly);
  393. tree_view.set_model(directories_model);
  394. tree_view.set_column_visible(GUI::FileSystemModel::Column::Icon, false);
  395. tree_view.set_column_visible(GUI::FileSystemModel::Column::Size, false);
  396. tree_view.set_column_visible(GUI::FileSystemModel::Column::Owner, false);
  397. tree_view.set_column_visible(GUI::FileSystemModel::Column::Group, false);
  398. tree_view.set_column_visible(GUI::FileSystemModel::Column::Permissions, false);
  399. tree_view.set_column_visible(GUI::FileSystemModel::Column::ModificationTime, false);
  400. tree_view.set_column_visible(GUI::FileSystemModel::Column::Inode, false);
  401. tree_view.set_column_visible(GUI::FileSystemModel::Column::SymlinkTarget, false);
  402. bool is_reacting_to_tree_view_selection_change = false;
  403. auto& directory_view = splitter.add<DirectoryView>(DirectoryView::Mode::Normal);
  404. directory_view.set_name("directory_view");
  405. location_textbox.on_escape_pressed = [&] {
  406. directory_view.set_focus(true);
  407. };
  408. // Open the root directory. FIXME: This is awkward.
  409. tree_view.toggle_index(directories_model->index(0, 0, {}));
  410. auto& statusbar = *widget.find_descendant_of_type_named<GUI::Statusbar>("statusbar");
  411. GUI::Application::the()->on_action_enter = [&statusbar](GUI::Action& action) {
  412. auto text = action.status_tip();
  413. if (text.is_empty())
  414. text = Gfx::parse_ampersand_string(action.text());
  415. statusbar.set_override_text(move(text));
  416. };
  417. GUI::Application::the()->on_action_leave = [&statusbar](GUI::Action&) {
  418. statusbar.set_override_text({});
  419. };
  420. auto& progressbar = *widget.find_descendant_of_type_named<GUI::Progressbar>("progressbar");
  421. progressbar.set_format(GUI::Progressbar::Format::ValueSlashMax);
  422. progressbar.set_frame_shape(Gfx::FrameShape::Panel);
  423. progressbar.set_frame_shadow(Gfx::FrameShadow::Sunken);
  424. progressbar.set_frame_thickness(1);
  425. location_textbox.on_return_pressed = [&] {
  426. if (directory_view.open(location_textbox.text()))
  427. location_textbox.set_focus(false);
  428. };
  429. auto refresh_tree_view = [&] {
  430. directories_model->invalidate();
  431. auto current_path = directory_view.path();
  432. struct stat st;
  433. // If the directory no longer exists, we find a parent that does.
  434. while (stat(current_path.characters(), &st) != 0) {
  435. directory_view.open_parent_directory();
  436. current_path = directory_view.path();
  437. if (current_path == directories_model->root_path()) {
  438. break;
  439. }
  440. }
  441. // Reselect the existing folder in the tree.
  442. auto new_index = directories_model->index(current_path, GUI::FileSystemModel::Column::Name);
  443. if (new_index.is_valid()) {
  444. tree_view.expand_all_parents_of(new_index);
  445. tree_view.set_cursor(new_index, GUI::AbstractView::SelectionUpdate::Set, true);
  446. }
  447. directory_view.refresh();
  448. };
  449. auto directory_context_menu = GUI::Menu::construct("Directory View Directory");
  450. auto directory_view_context_menu = GUI::Menu::construct("Directory View");
  451. auto tree_view_directory_context_menu = GUI::Menu::construct("Tree View Directory");
  452. auto tree_view_context_menu = GUI::Menu::construct("Tree View");
  453. auto open_parent_directory_action = GUI::Action::create("Open &Parent Directory", { Mod_Alt, Key_Up }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/open-parent-directory.png"), [&](GUI::Action const&) {
  454. directory_view.open_parent_directory();
  455. });
  456. RefPtr<GUI::Action> layout_toolbar_action;
  457. RefPtr<GUI::Action> layout_location_action;
  458. RefPtr<GUI::Action> layout_statusbar_action;
  459. RefPtr<GUI::Action> layout_folderpane_action;
  460. auto show_toolbar = Config::read_bool("FileManager", "Layout", "ShowToolbar", true);
  461. layout_toolbar_action = GUI::Action::create_checkable("&Toolbar", [&](auto& action) {
  462. if (action.is_checked()) {
  463. main_toolbar.set_visible(true);
  464. toolbar_container.set_visible(true);
  465. } else {
  466. main_toolbar.set_visible(false);
  467. if (!location_toolbar.is_visible() && !breadcrumb_toolbar.is_visible())
  468. toolbar_container.set_visible(false);
  469. }
  470. show_toolbar = action.is_checked();
  471. Config::write_bool("FileManager", "Layout", "ShowToolbar", action.is_checked());
  472. });
  473. layout_toolbar_action->set_checked(show_toolbar);
  474. main_toolbar.set_visible(show_toolbar);
  475. auto show_location = Config::read_bool("FileManager", "Layout", "ShowLocationBar", true);
  476. layout_location_action = GUI::Action::create_checkable("&Location Bar", [&](auto& action) {
  477. if (action.is_checked()) {
  478. breadcrumb_toolbar.set_visible(true);
  479. location_toolbar.set_visible(false);
  480. toolbar_container.set_visible(true);
  481. } else {
  482. breadcrumb_toolbar.set_visible(false);
  483. location_toolbar.set_visible(false);
  484. if (!main_toolbar.is_visible())
  485. toolbar_container.set_visible(false);
  486. }
  487. show_location = action.is_checked();
  488. Config::write_bool("FileManager", "Layout", "ShowLocationBar", action.is_checked());
  489. });
  490. layout_location_action->set_checked(show_location);
  491. breadcrumb_toolbar.set_visible(show_location);
  492. toolbar_container.set_visible(show_location | show_toolbar);
  493. layout_statusbar_action = GUI::Action::create_checkable("&Status Bar", [&](auto& action) {
  494. action.is_checked() ? statusbar.set_visible(true) : statusbar.set_visible(false);
  495. Config::write_bool("FileManager", "Layout", "ShowStatusbar", action.is_checked());
  496. });
  497. auto show_statusbar = Config::read_bool("FileManager", "Layout", "ShowStatusbar", true);
  498. layout_statusbar_action->set_checked(show_statusbar);
  499. statusbar.set_visible(show_statusbar);
  500. layout_folderpane_action = GUI::Action::create_checkable("&Folder Pane", { Mod_Ctrl, Key_P }, [&](auto& action) {
  501. action.is_checked() ? tree_view.set_visible(true) : tree_view.set_visible(false);
  502. Config::write_bool("FileManager", "Layout", "ShowFolderPane", action.is_checked());
  503. });
  504. auto show_folderpane = Config::read_bool("FileManager", "Layout", "ShowFolderPane", true);
  505. layout_folderpane_action->set_checked(show_folderpane);
  506. tree_view.set_visible(show_folderpane);
  507. location_textbox.on_focusout = [&] {
  508. if (show_location)
  509. breadcrumb_toolbar.set_visible(true);
  510. if (!(show_location | show_toolbar))
  511. toolbar_container.set_visible(false);
  512. location_toolbar.set_visible(false);
  513. };
  514. auto view_type_action_group = make<GUI::ActionGroup>();
  515. view_type_action_group->set_exclusive(true);
  516. view_type_action_group->add_action(directory_view.view_as_icons_action());
  517. view_type_action_group->add_action(directory_view.view_as_table_action());
  518. view_type_action_group->add_action(directory_view.view_as_columns_action());
  519. auto tree_view_selected_file_paths = [&] {
  520. Vector<String> paths;
  521. auto& view = tree_view;
  522. view.selection().for_each_index([&](GUI::ModelIndex const& index) {
  523. paths.append(directories_model->full_path(index));
  524. });
  525. return paths;
  526. };
  527. auto select_all_action = GUI::CommonActions::make_select_all_action([&](auto&) {
  528. directory_view.current_view().select_all();
  529. });
  530. auto cut_action = GUI::CommonActions::make_cut_action(
  531. [&](auto&) {
  532. auto paths = directory_view.selected_file_paths();
  533. if (paths.is_empty())
  534. paths = tree_view_selected_file_paths();
  535. if (paths.is_empty())
  536. VERIFY_NOT_REACHED();
  537. do_copy(paths, FileOperation::Move);
  538. refresh_tree_view();
  539. },
  540. window);
  541. cut_action->set_enabled(false);
  542. auto copy_action = GUI::CommonActions::make_copy_action(
  543. [&](auto&) {
  544. auto paths = directory_view.selected_file_paths();
  545. if (paths.is_empty())
  546. paths = tree_view_selected_file_paths();
  547. if (paths.is_empty())
  548. VERIFY_NOT_REACHED();
  549. do_copy(paths, FileOperation::Copy);
  550. refresh_tree_view();
  551. },
  552. window);
  553. copy_action->set_enabled(false);
  554. auto open_in_new_window_action
  555. = GUI::Action::create(
  556. "Open in New &Window",
  557. {},
  558. Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-file-manager.png"),
  559. [&](GUI::Action const& action) {
  560. Vector<String> paths;
  561. if (action.activator() == tree_view_directory_context_menu)
  562. paths = tree_view_selected_file_paths();
  563. else
  564. paths = directory_view.selected_file_paths();
  565. for (auto& path : paths) {
  566. if (Core::File::is_directory(path))
  567. Desktop::Launcher::open(URL::create_with_file_protocol(path));
  568. }
  569. },
  570. window);
  571. auto open_in_new_terminal_action
  572. = GUI::Action::create(
  573. "Open in &Terminal",
  574. {},
  575. Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-terminal.png"),
  576. [&](GUI::Action const& action) {
  577. Vector<String> paths;
  578. if (action.activator() == tree_view_directory_context_menu)
  579. paths = tree_view_selected_file_paths();
  580. else
  581. paths = directory_view.selected_file_paths();
  582. for (auto& path : paths) {
  583. if (Core::File::is_directory(path)) {
  584. spawn_terminal(path);
  585. }
  586. }
  587. },
  588. window);
  589. auto shortcut_action
  590. = GUI::Action::create(
  591. "Create Desktop &Shortcut",
  592. {},
  593. Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-symlink.png"),
  594. [&](GUI::Action const&) {
  595. auto paths = directory_view.selected_file_paths();
  596. if (paths.is_empty()) {
  597. return;
  598. }
  599. do_create_link(paths, directory_view.window());
  600. },
  601. window);
  602. auto unzip_archive_action
  603. = GUI::Action::create(
  604. "E&xtract Here",
  605. [&](GUI::Action const&) {
  606. auto paths = directory_view.selected_file_paths();
  607. if (paths.is_empty())
  608. return;
  609. do_unzip_archive(paths, directory_view.window());
  610. refresh_tree_view();
  611. },
  612. window);
  613. auto properties_action = GUI::CommonActions::make_properties_action(
  614. [&](auto& action) {
  615. String container_dir_path;
  616. String path;
  617. Vector<String> selected;
  618. if (action.activator() == directory_context_menu || directory_view.active_widget()->is_focused()) {
  619. path = directory_view.path();
  620. container_dir_path = path;
  621. selected = directory_view.selected_file_paths();
  622. } else {
  623. path = directories_model->full_path(tree_view.selection().first());
  624. container_dir_path = LexicalPath::basename(path);
  625. selected = tree_view_selected_file_paths();
  626. }
  627. show_properties(container_dir_path, path, selected, directory_view.window());
  628. },
  629. window);
  630. auto paste_action = GUI::CommonActions::make_paste_action(
  631. [&](GUI::Action const& action) {
  632. String target_directory;
  633. if (action.activator() == directory_context_menu)
  634. target_directory = directory_view.selected_file_paths()[0];
  635. else
  636. target_directory = directory_view.path();
  637. do_paste(target_directory, directory_view.window());
  638. refresh_tree_view();
  639. },
  640. window);
  641. auto folder_specific_paste_action = GUI::CommonActions::make_paste_action(
  642. [&](GUI::Action const& action) {
  643. String target_directory;
  644. if (action.activator() == directory_context_menu)
  645. target_directory = directory_view.selected_file_paths()[0];
  646. else
  647. target_directory = directory_view.path();
  648. do_paste(target_directory, directory_view.window());
  649. refresh_tree_view();
  650. },
  651. window);
  652. auto go_back_action = GUI::CommonActions::make_go_back_action(
  653. [&](auto&) {
  654. directory_view.open_previous_directory();
  655. },
  656. window);
  657. auto go_forward_action = GUI::CommonActions::make_go_forward_action(
  658. [&](auto&) {
  659. directory_view.open_next_directory();
  660. },
  661. window);
  662. auto go_home_action = GUI::CommonActions::make_go_home_action(
  663. [&](auto&) {
  664. directory_view.open(Core::StandardPaths::home_directory());
  665. },
  666. window);
  667. GUI::Clipboard::the().on_change = [&](String const& data_type) {
  668. auto current_location = directory_view.path();
  669. paste_action->set_enabled(data_type == "text/uri-list" && access(current_location.characters(), W_OK) == 0);
  670. };
  671. auto tree_view_delete_action = GUI::CommonActions::make_delete_action(
  672. [&](auto&) {
  673. delete_paths(tree_view_selected_file_paths(), true, window);
  674. refresh_tree_view();
  675. },
  676. &tree_view);
  677. // This is a little awkward. The menu action does something different depending on which view has focus.
  678. // It would be nice to find a good abstraction for this instead of creating a branching action like this.
  679. auto focus_dependent_delete_action = GUI::CommonActions::make_delete_action([&](auto&) {
  680. if (tree_view.is_focused())
  681. tree_view_delete_action->activate();
  682. else
  683. directory_view.delete_action().activate();
  684. refresh_tree_view();
  685. });
  686. focus_dependent_delete_action->set_enabled(false);
  687. auto mkdir_action = GUI::Action::create("&New Directory...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/mkdir.png"), [&](GUI::Action const&) {
  688. directory_view.mkdir_action().activate();
  689. refresh_tree_view();
  690. });
  691. auto touch_action = GUI::Action::create("New &File...", { Mod_Ctrl | Mod_Shift, Key_F }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new.png"), [&](GUI::Action const&) {
  692. directory_view.touch_action().activate();
  693. refresh_tree_view();
  694. });
  695. auto& file_menu = window->add_menu("&File");
  696. file_menu.add_action(mkdir_action);
  697. file_menu.add_action(touch_action);
  698. file_menu.add_action(focus_dependent_delete_action);
  699. file_menu.add_action(directory_view.rename_action());
  700. file_menu.add_separator();
  701. file_menu.add_action(properties_action);
  702. file_menu.add_separator();
  703. file_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  704. GUI::Application::the()->quit();
  705. }));
  706. auto& edit_menu = window->add_menu("&Edit");
  707. edit_menu.add_action(cut_action);
  708. edit_menu.add_action(copy_action);
  709. edit_menu.add_action(paste_action);
  710. edit_menu.add_separator();
  711. edit_menu.add_action(select_all_action);
  712. auto action_show_dotfiles = GUI::Action::create_checkable("&Show Dotfiles", { Mod_Ctrl, Key_H }, [&](auto& action) {
  713. directory_view.set_should_show_dotfiles(action.is_checked());
  714. directories_model->set_should_show_dotfiles(action.is_checked());
  715. refresh_tree_view();
  716. Config::write_bool("FileManager", "DirectoryView", "ShowDotFiles", action.is_checked());
  717. });
  718. auto show_dotfiles = Config::read_bool("FileManager", "DirectoryView", "ShowDotFiles", false);
  719. directory_view.set_should_show_dotfiles(show_dotfiles);
  720. action_show_dotfiles->set_checked(show_dotfiles);
  721. auto& view_menu = window->add_menu("&View");
  722. auto& layout_menu = view_menu.add_submenu("&Layout");
  723. layout_menu.add_action(*layout_toolbar_action);
  724. layout_menu.add_action(*layout_location_action);
  725. layout_menu.add_action(*layout_statusbar_action);
  726. layout_menu.add_action(*layout_folderpane_action);
  727. view_menu.add_separator();
  728. view_menu.add_action(directory_view.view_as_icons_action());
  729. view_menu.add_action(directory_view.view_as_table_action());
  730. view_menu.add_action(directory_view.view_as_columns_action());
  731. view_menu.add_separator();
  732. view_menu.add_action(action_show_dotfiles);
  733. auto go_to_location_action = GUI::Action::create("Go to &Location...", { Mod_Ctrl, Key_L }, [&](auto&) {
  734. toolbar_container.set_visible(true);
  735. location_toolbar.set_visible(true);
  736. breadcrumb_toolbar.set_visible(false);
  737. location_textbox.select_all();
  738. location_textbox.set_focus(true);
  739. });
  740. auto& go_menu = window->add_menu("&Go");
  741. go_menu.add_action(go_back_action);
  742. go_menu.add_action(go_forward_action);
  743. go_menu.add_action(open_parent_directory_action);
  744. go_menu.add_action(go_home_action);
  745. go_menu.add_action(go_to_location_action);
  746. go_menu.add_separator();
  747. go_menu.add_action(directory_view.open_terminal_action());
  748. auto& help_menu = window->add_menu("&Help");
  749. help_menu.add_action(GUI::CommonActions::make_about_action("File Manager", GUI::Icon::default_icon("app-file-manager"), window));
  750. main_toolbar.add_action(go_back_action);
  751. main_toolbar.add_action(go_forward_action);
  752. main_toolbar.add_action(open_parent_directory_action);
  753. main_toolbar.add_action(go_home_action);
  754. main_toolbar.add_separator();
  755. main_toolbar.add_action(directory_view.open_terminal_action());
  756. main_toolbar.add_separator();
  757. main_toolbar.add_action(mkdir_action);
  758. main_toolbar.add_action(touch_action);
  759. main_toolbar.add_separator();
  760. main_toolbar.add_action(focus_dependent_delete_action);
  761. main_toolbar.add_action(directory_view.rename_action());
  762. main_toolbar.add_separator();
  763. main_toolbar.add_action(cut_action);
  764. main_toolbar.add_action(copy_action);
  765. main_toolbar.add_action(paste_action);
  766. main_toolbar.add_separator();
  767. main_toolbar.add_action(directory_view.view_as_icons_action());
  768. main_toolbar.add_action(directory_view.view_as_table_action());
  769. main_toolbar.add_action(directory_view.view_as_columns_action());
  770. directory_view.on_path_change = [&](String const& new_path, bool can_read_in_path, bool can_write_in_path) {
  771. auto icon = GUI::FileIconProvider::icon_for_path(new_path);
  772. auto* bitmap = icon.bitmap_for_size(16);
  773. window->set_icon(bitmap);
  774. location_textbox.set_icon(bitmap);
  775. window->set_title(String::formatted("{} - File Manager", new_path));
  776. location_textbox.set_text(new_path);
  777. {
  778. LexicalPath lexical_path(new_path);
  779. auto segment_index_of_new_path_in_breadcrumbbar = breadcrumbbar.find_segment_with_data(new_path);
  780. if (segment_index_of_new_path_in_breadcrumbbar.has_value()) {
  781. auto new_segment_index = segment_index_of_new_path_in_breadcrumbbar.value();
  782. breadcrumbbar.set_selected_segment(new_segment_index);
  783. // If the path change was because the directory we were in was deleted,
  784. // remove the breadcrumbs for it.
  785. if ((new_segment_index + 1 < breadcrumbbar.segment_count())
  786. && !Core::File::is_directory(breadcrumbbar.segment_data(new_segment_index + 1))) {
  787. breadcrumbbar.remove_end_segments(new_segment_index + 1);
  788. }
  789. } else {
  790. breadcrumbbar.clear_segments();
  791. breadcrumbbar.append_segment("/", GUI::FileIconProvider::icon_for_path("/").bitmap_for_size(16), "/", "/");
  792. StringBuilder builder;
  793. for (auto& part : lexical_path.parts()) {
  794. // NOTE: We rebuild the path as we go, so we have something to pass to GUI::FileIconProvider.
  795. builder.append('/');
  796. builder.append(part);
  797. breadcrumbbar.append_segment(part, GUI::FileIconProvider::icon_for_path(builder.string_view()).bitmap_for_size(16), builder.string_view(), builder.string_view());
  798. }
  799. breadcrumbbar.set_selected_segment(breadcrumbbar.segment_count() - 1);
  800. breadcrumbbar.on_segment_click = [&](size_t segment_index) {
  801. auto selected_path = breadcrumbbar.segment_data(segment_index);
  802. if (Core::File::is_directory(selected_path)) {
  803. directory_view.open(selected_path);
  804. } else {
  805. dbgln("Breadcrumb path '{}' doesn't exist", selected_path);
  806. breadcrumbbar.remove_end_segments(segment_index);
  807. auto existing_path_segment = breadcrumbbar.find_segment_with_data(directory_view.path());
  808. breadcrumbbar.set_selected_segment(existing_path_segment.value());
  809. }
  810. };
  811. }
  812. }
  813. if (!is_reacting_to_tree_view_selection_change) {
  814. auto new_index = directories_model->index(new_path, GUI::FileSystemModel::Column::Name);
  815. if (new_index.is_valid()) {
  816. tree_view.expand_all_parents_of(new_index);
  817. tree_view.set_cursor(new_index, GUI::AbstractView::SelectionUpdate::Set);
  818. }
  819. }
  820. struct stat st;
  821. if (lstat(new_path.characters(), &st)) {
  822. perror("stat");
  823. return;
  824. }
  825. mkdir_action->set_enabled(can_write_in_path);
  826. touch_action->set_enabled(can_write_in_path);
  827. paste_action->set_enabled(can_write_in_path && GUI::Clipboard::the().mime_type() == "text/uri-list");
  828. go_forward_action->set_enabled(directory_view.path_history_position() < directory_view.path_history_size() - 1);
  829. go_back_action->set_enabled(directory_view.path_history_position() > 0);
  830. open_parent_directory_action->set_enabled(new_path != "/");
  831. directory_view.view_as_table_action().set_enabled(can_read_in_path);
  832. directory_view.view_as_icons_action().set_enabled(can_read_in_path);
  833. directory_view.view_as_columns_action().set_enabled(can_read_in_path);
  834. };
  835. directory_view.on_accepted_drop = [&] {
  836. refresh_tree_view();
  837. };
  838. directory_view.on_status_message = [&](StringView const& message) {
  839. statusbar.set_text(message);
  840. };
  841. directory_view.on_thumbnail_progress = [&](int done, int total) {
  842. if (done == total) {
  843. progressbar.set_visible(false);
  844. return;
  845. }
  846. progressbar.set_range(0, total);
  847. progressbar.set_value(done);
  848. progressbar.set_visible(true);
  849. };
  850. directory_view.on_selection_change = [&](GUI::AbstractView& view) {
  851. auto& selection = view.selection();
  852. cut_action->set_enabled(!selection.is_empty());
  853. copy_action->set_enabled(!selection.is_empty());
  854. focus_dependent_delete_action->set_enabled((!tree_view.selection().is_empty() && tree_view.is_focused())
  855. || !directory_view.current_view().selection().is_empty());
  856. };
  857. auto directory_open_action = GUI::Action::create("Open", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/open.png"), [&](auto&) {
  858. directory_view.open(directory_view.selected_file_paths().first());
  859. });
  860. directory_context_menu->add_action(directory_open_action);
  861. directory_context_menu->add_action(open_in_new_window_action);
  862. directory_context_menu->add_action(open_in_new_terminal_action);
  863. directory_context_menu->add_separator();
  864. directory_context_menu->add_action(cut_action);
  865. directory_context_menu->add_action(copy_action);
  866. directory_context_menu->add_action(folder_specific_paste_action);
  867. directory_context_menu->add_action(directory_view.delete_action());
  868. directory_context_menu->add_action(directory_view.rename_action());
  869. directory_context_menu->add_action(shortcut_action);
  870. directory_context_menu->add_separator();
  871. directory_context_menu->add_action(properties_action);
  872. directory_view_context_menu->add_action(mkdir_action);
  873. directory_view_context_menu->add_action(touch_action);
  874. directory_view_context_menu->add_action(paste_action);
  875. directory_view_context_menu->add_action(directory_view.open_terminal_action());
  876. directory_view_context_menu->add_separator();
  877. directory_view_context_menu->add_action(action_show_dotfiles);
  878. directory_view_context_menu->add_separator();
  879. directory_view_context_menu->add_action(properties_action);
  880. tree_view_directory_context_menu->add_action(open_in_new_window_action);
  881. tree_view_directory_context_menu->add_action(open_in_new_terminal_action);
  882. tree_view_directory_context_menu->add_separator();
  883. tree_view_directory_context_menu->add_action(mkdir_action);
  884. tree_view_directory_context_menu->add_action(touch_action);
  885. tree_view_directory_context_menu->add_action(cut_action);
  886. tree_view_directory_context_menu->add_action(copy_action);
  887. tree_view_directory_context_menu->add_action(paste_action);
  888. tree_view_directory_context_menu->add_action(tree_view_delete_action);
  889. tree_view_directory_context_menu->add_separator();
  890. tree_view_directory_context_menu->add_action(properties_action);
  891. RefPtr<GUI::Menu> file_context_menu;
  892. NonnullRefPtrVector<LauncherHandler> current_file_handlers;
  893. RefPtr<GUI::Action> file_context_menu_action_default_action;
  894. directory_view.on_context_menu_request = [&](GUI::ModelIndex const& index, GUI::ContextMenuEvent const& event) {
  895. if (index.is_valid()) {
  896. auto& node = directory_view.node(index);
  897. if (node.is_directory()) {
  898. auto should_get_enabled = access(node.full_path().characters(), W_OK) == 0 && GUI::Clipboard::the().mime_type() == "text/uri-list";
  899. folder_specific_paste_action->set_enabled(should_get_enabled);
  900. directory_context_menu->popup(event.screen_position(), directory_open_action);
  901. } else {
  902. file_context_menu = GUI::Menu::construct("Directory View File");
  903. bool added_launch_file_handlers = add_launch_handler_actions_to_menu(file_context_menu, directory_view, node.full_path(), file_context_menu_action_default_action, current_file_handlers);
  904. if (added_launch_file_handlers)
  905. file_context_menu->add_separator();
  906. file_context_menu->add_action(cut_action);
  907. file_context_menu->add_action(copy_action);
  908. file_context_menu->add_action(paste_action);
  909. file_context_menu->add_action(directory_view.delete_action());
  910. file_context_menu->add_action(directory_view.rename_action());
  911. file_context_menu->add_action(shortcut_action);
  912. file_context_menu->add_separator();
  913. if (node.full_path().ends_with(".zip", AK::CaseSensitivity::CaseInsensitive)) {
  914. file_context_menu->add_action(unzip_archive_action);
  915. file_context_menu->add_separator();
  916. }
  917. file_context_menu->add_action(properties_action);
  918. file_context_menu->popup(event.screen_position(), file_context_menu_action_default_action);
  919. }
  920. } else {
  921. directory_view_context_menu->popup(event.screen_position());
  922. }
  923. };
  924. tree_view.on_selection_change = [&] {
  925. focus_dependent_delete_action->set_enabled((!tree_view.selection().is_empty() && tree_view.is_focused())
  926. || !directory_view.current_view().selection().is_empty());
  927. if (tree_view.selection().is_empty())
  928. return;
  929. if (directories_model->m_previously_selected_index.is_valid())
  930. directories_model->update_node_on_selection(directories_model->m_previously_selected_index, false);
  931. auto const& index = tree_view.selection().first();
  932. directories_model->update_node_on_selection(index, true);
  933. directories_model->m_previously_selected_index = index;
  934. auto path = directories_model->full_path(index);
  935. if (directory_view.path() == path)
  936. return;
  937. TemporaryChange change(is_reacting_to_tree_view_selection_change, true);
  938. directory_view.open(path);
  939. cut_action->set_enabled(!tree_view.selection().is_empty());
  940. copy_action->set_enabled(!tree_view.selection().is_empty());
  941. directory_view.delete_action().set_enabled(!tree_view.selection().is_empty());
  942. };
  943. tree_view.on_focus_change = [&](bool has_focus, [[maybe_unused]] GUI::FocusSource const source) {
  944. focus_dependent_delete_action->set_enabled((!tree_view.selection().is_empty() && has_focus)
  945. || !directory_view.current_view().selection().is_empty());
  946. };
  947. tree_view.on_context_menu_request = [&](GUI::ModelIndex const& index, GUI::ContextMenuEvent const& event) {
  948. if (index.is_valid()) {
  949. tree_view_directory_context_menu->popup(event.screen_position());
  950. }
  951. };
  952. auto copy_urls_to_directory = [&](Vector<URL> const& urls, String const& directory) {
  953. if (urls.is_empty()) {
  954. dbgln("No files to copy");
  955. return;
  956. }
  957. bool had_accepted_copy = false;
  958. for (auto& url_to_copy : urls) {
  959. if (!url_to_copy.is_valid() || url_to_copy.path() == directory)
  960. continue;
  961. auto new_path = String::formatted("{}/{}", directory, LexicalPath::basename(url_to_copy.path()));
  962. if (url_to_copy.path() == new_path)
  963. continue;
  964. if (auto result = Core::File::copy_file_or_directory(url_to_copy.path(), new_path); result.is_error()) {
  965. auto error_message = String::formatted("Could not copy {} into {}:\n {}", url_to_copy.to_string(), new_path, result.error().error_code);
  966. GUI::MessageBox::show(window, error_message, "File Manager", GUI::MessageBox::Type::Error);
  967. } else {
  968. had_accepted_copy = true;
  969. }
  970. }
  971. if (had_accepted_copy)
  972. refresh_tree_view();
  973. };
  974. breadcrumbbar.on_segment_drop = [&](size_t segment_index, GUI::DropEvent const& event) {
  975. if (!event.mime_data().has_urls())
  976. return;
  977. copy_urls_to_directory(event.mime_data().urls(), breadcrumbbar.segment_data(segment_index));
  978. };
  979. breadcrumbbar.on_segment_drag_enter = [&](size_t, GUI::DragEvent& event) {
  980. if (event.mime_types().contains_slow("text/uri-list"))
  981. event.accept();
  982. };
  983. breadcrumbbar.on_doubleclick = [&](GUI::MouseEvent const&) {
  984. go_to_location_action->activate();
  985. };
  986. tree_view.on_drop = [&](GUI::ModelIndex const& index, GUI::DropEvent const& event) {
  987. if (!event.mime_data().has_urls())
  988. return;
  989. auto& target_node = directories_model->node(index);
  990. if (!target_node.is_directory())
  991. return;
  992. copy_urls_to_directory(event.mime_data().urls(), target_node.full_path());
  993. const_cast<GUI::DropEvent&>(event).accept();
  994. };
  995. directory_view.open(initial_location);
  996. directory_view.set_focus(true);
  997. paste_action->set_enabled(GUI::Clipboard::the().mime_type() == "text/uri-list" && access(initial_location.characters(), W_OK) == 0);
  998. window->set_rect({ left, top, width, height });
  999. if (was_maximized)
  1000. window->set_maximized(true);
  1001. window->show();
  1002. directory_view.set_view_mode_from_string(Config::read_string("FileManager", "DirectoryView", "ViewMode", "Icon"));
  1003. if (!entry_focused_on_init.is_empty()) {
  1004. auto matches = directory_view.current_view().model()->matches(entry_focused_on_init, GUI::Model::MatchesFlag::MatchFull | GUI::Model::MatchesFlag::FirstMatchOnly);
  1005. if (!matches.is_empty())
  1006. directory_view.current_view().set_cursor(matches.first(), GUI::AbstractView::SelectionUpdate::Set);
  1007. }
  1008. // Write window position to config file on close request.
  1009. window->on_close_request = [&] {
  1010. Config::write_bool("FileManager", "Window", "Maximized", window->is_maximized());
  1011. if (!window->is_maximized()) {
  1012. Config::write_i32("FileManager", "Window", "Left", window->x());
  1013. Config::write_i32("FileManager", "Window", "Top", window->y());
  1014. Config::write_i32("FileManager", "Window", "Width", window->width());
  1015. Config::write_i32("FileManager", "Window", "Height", window->height());
  1016. }
  1017. return GUI::Window::CloseRequestDecision::Close;
  1018. };
  1019. return GUI::Application::the()->exec();
  1020. }