main.cpp 52 KB

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