main.cpp 60 KB

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