main.cpp 57 KB

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