main.cpp 52 KB

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