main.cpp 52 KB

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