main.cpp 49 KB

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