main.cpp 57 KB

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