main.cpp 57 KB

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