main.cpp 52 KB

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