main.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "DirectoryView.h"
  27. #include "FileUtils.h"
  28. #include "PropertiesDialog.h"
  29. #include <AK/LexicalPath.h>
  30. #include <AK/StringBuilder.h>
  31. #include <AK/URL.h>
  32. #include <LibCore/ConfigFile.h>
  33. #include <LibCore/MimeData.h>
  34. #include <LibCore/StandardPaths.h>
  35. #include <LibDesktop/Launcher.h>
  36. #include <LibGUI/AboutDialog.h>
  37. #include <LibGUI/Action.h>
  38. #include <LibGUI/ActionGroup.h>
  39. #include <LibGUI/Application.h>
  40. #include <LibGUI/BoxLayout.h>
  41. #include <LibGUI/Clipboard.h>
  42. #include <LibGUI/Desktop.h>
  43. #include <LibGUI/FileSystemModel.h>
  44. #include <LibGUI/InputBox.h>
  45. #include <LibGUI/Label.h>
  46. #include <LibGUI/Menu.h>
  47. #include <LibGUI/MenuBar.h>
  48. #include <LibGUI/MessageBox.h>
  49. #include <LibGUI/Painter.h>
  50. #include <LibGUI/ProgressBar.h>
  51. #include <LibGUI/Splitter.h>
  52. #include <LibGUI/StatusBar.h>
  53. #include <LibGUI/TextEditor.h>
  54. #include <LibGUI/ToolBar.h>
  55. #include <LibGUI/ToolBarContainer.h>
  56. #include <LibGUI/TreeView.h>
  57. #include <LibGUI/Widget.h>
  58. #include <LibGUI/Window.h>
  59. #include <LibGfx/Palette.h>
  60. #include <pthread.h>
  61. #include <serenity.h>
  62. #include <signal.h>
  63. #include <spawn.h>
  64. #include <stdio.h>
  65. #include <string.h>
  66. #include <unistd.h>
  67. static int run_in_desktop_mode(RefPtr<Core::ConfigFile>);
  68. static int run_in_windowed_mode(RefPtr<Core::ConfigFile>, String initial_location);
  69. static Gfx::Bitmap& folder_icon()
  70. {
  71. static RefPtr<Gfx::Bitmap> icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-folder.png");
  72. return *icon;
  73. }
  74. static Gfx::Bitmap& home_directory_icon()
  75. {
  76. static RefPtr<Gfx::Bitmap> icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/home-directory.png");
  77. return *icon;
  78. }
  79. int main(int argc, char** argv)
  80. {
  81. if (pledge("stdio thread shared_buffer accept unix cpath rpath wpath fattr proc exec sigaction", nullptr) < 0) {
  82. perror("pledge");
  83. return 1;
  84. }
  85. struct sigaction act;
  86. memset(&act, 0, sizeof(act));
  87. act.sa_flags = SA_NOCLDWAIT;
  88. act.sa_handler = SIG_IGN;
  89. int rc = sigaction(SIGCHLD, &act, nullptr);
  90. if (rc < 0) {
  91. perror("sigaction");
  92. return 1;
  93. }
  94. RefPtr<Core::ConfigFile> config = Core::ConfigFile::get_for_app("FileManager");
  95. auto app = GUI::Application::construct(argc, argv);
  96. if (pledge("stdio thread shared_buffer accept cpath rpath wpath fattr proc exec unix", nullptr) < 0) {
  97. perror("pledge");
  98. return 1;
  99. }
  100. if (app->args().contains_slow("--desktop") || app->args().contains_slow("-d"))
  101. return run_in_desktop_mode(move(config));
  102. // our initial location is defined as, in order of precedence:
  103. // 1. the first command-line argument (e.g. FileManager /bin)
  104. // 2. the user's home directory
  105. // 3. the root directory
  106. String initial_location;
  107. if (argc >= 2) {
  108. char* buffer = realpath(argv[1], nullptr);
  109. initial_location = buffer;
  110. free(buffer);
  111. }
  112. if (initial_location.is_empty())
  113. initial_location = Core::StandardPaths::home_directory();
  114. if (initial_location.is_empty())
  115. initial_location = "/";
  116. return run_in_windowed_mode(move(config), initial_location);
  117. }
  118. class DesktopWidget final : public GUI::Widget {
  119. C_OBJECT(DesktopWidget);
  120. private:
  121. virtual void paint_event(GUI::PaintEvent& event) override
  122. {
  123. GUI::Painter painter(*this);
  124. painter.add_clip_rect(event.rect());
  125. painter.clear_rect(event.rect(), Color(0, 0, 0, 0));
  126. }
  127. DesktopWidget()
  128. {
  129. }
  130. };
  131. int run_in_desktop_mode(RefPtr<Core::ConfigFile> config)
  132. {
  133. static constexpr const char* process_name = "FileManager (Desktop)";
  134. set_process_name(process_name, strlen(process_name));
  135. pthread_setname_np(pthread_self(), process_name);
  136. (void)config;
  137. auto window = GUI::Window::construct();
  138. window->set_title("Desktop Manager");
  139. window->set_window_type(GUI::WindowType::Desktop);
  140. window->set_has_alpha_channel(true);
  141. auto& desktop_widget = window->set_main_widget<DesktopWidget>();
  142. desktop_widget.set_layout<GUI::VerticalBoxLayout>();
  143. auto& directory_view = desktop_widget.add<DirectoryView>(DirectoryView::Mode::Desktop);
  144. (void)directory_view;
  145. auto desktop_view_context_menu = GUI::Menu::construct("Directory View");
  146. auto file_manager_action = GUI::Action::create("Show in FileManager...", {}, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-folder.png"), [&](const GUI::Action&) {
  147. Desktop::Launcher::open(URL::create_with_file_protocol(directory_view.path()));
  148. });
  149. auto display_properties_action = GUI::Action::create("Display settings...", {}, Gfx::Bitmap::load_from_file("/res/icons/16x16/app-display-settings.png"), [&](const GUI::Action&) {
  150. Desktop::Launcher::open(URL::create_with_file_protocol("/bin/DisplaySettings"));
  151. });
  152. desktop_view_context_menu->add_action(directory_view.mkdir_action());
  153. desktop_view_context_menu->add_action(directory_view.touch_action());
  154. desktop_view_context_menu->add_separator();
  155. desktop_view_context_menu->add_action(file_manager_action);
  156. desktop_view_context_menu->add_separator();
  157. desktop_view_context_menu->add_action(display_properties_action);
  158. directory_view.on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  159. if (!index.is_valid())
  160. desktop_view_context_menu->popup(event.screen_position());
  161. };
  162. auto wm_config = Core::ConfigFile::get_for_app("WindowManager");
  163. auto selected_wallpaper = wm_config->read_entry("Background", "Wallpaper", "");
  164. if (!selected_wallpaper.is_empty()) {
  165. GUI::Desktop::the().set_wallpaper(selected_wallpaper, false);
  166. }
  167. window->show();
  168. return GUI::Application::the()->exec();
  169. }
  170. int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_location)
  171. {
  172. auto window = GUI::Window::construct();
  173. window->set_title("File Manager");
  174. auto left = config->read_num_entry("Window", "Left", 150);
  175. auto top = config->read_num_entry("Window", "Top", 75);
  176. auto width = config->read_num_entry("Window", "Width", 640);
  177. auto heigth = config->read_num_entry("Window", "Heigth", 480);
  178. window->set_rect({ left, top, width, heigth });
  179. auto& widget = window->set_main_widget<GUI::Widget>();
  180. widget.set_layout<GUI::VerticalBoxLayout>();
  181. widget.set_fill_with_background_color(true);
  182. widget.layout()->set_spacing(2);
  183. auto& toolbar_container = widget.add<GUI::ToolBarContainer>();
  184. auto& main_toolbar = toolbar_container.add<GUI::ToolBar>();
  185. auto& location_toolbar = toolbar_container.add<GUI::ToolBar>();
  186. location_toolbar.layout()->set_margins({ 6, 3, 6, 3 });
  187. auto& location_label = location_toolbar.add<GUI::Label>("Location: ");
  188. location_label.size_to_fit();
  189. auto& location_textbox = location_toolbar.add<GUI::TextBox>();
  190. location_textbox.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  191. location_textbox.set_preferred_size(0, 22);
  192. auto& splitter = widget.add<GUI::HorizontalSplitter>();
  193. auto& tree_view = splitter.add<GUI::TreeView>();
  194. auto directories_model = GUI::FileSystemModel::create("/", GUI::FileSystemModel::Mode::DirectoriesOnly);
  195. tree_view.set_model(directories_model);
  196. tree_view.set_column_hidden(GUI::FileSystemModel::Column::Icon, true);
  197. tree_view.set_column_hidden(GUI::FileSystemModel::Column::Size, true);
  198. tree_view.set_column_hidden(GUI::FileSystemModel::Column::Owner, true);
  199. tree_view.set_column_hidden(GUI::FileSystemModel::Column::Group, true);
  200. tree_view.set_column_hidden(GUI::FileSystemModel::Column::Permissions, true);
  201. tree_view.set_column_hidden(GUI::FileSystemModel::Column::ModificationTime, true);
  202. tree_view.set_column_hidden(GUI::FileSystemModel::Column::Inode, true);
  203. tree_view.set_column_hidden(GUI::FileSystemModel::Column::SymlinkTarget, true);
  204. tree_view.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  205. tree_view.set_preferred_size(150, 0);
  206. auto& directory_view = splitter.add<DirectoryView>(DirectoryView::Mode::Normal);
  207. auto& statusbar = widget.add<GUI::StatusBar>();
  208. auto& progressbar = statusbar.add<GUI::ProgressBar>();
  209. progressbar.set_caption("Generating thumbnails: ");
  210. progressbar.set_format(GUI::ProgressBar::Format::ValueSlashMax);
  211. progressbar.set_visible(false);
  212. progressbar.set_frame_shape(Gfx::FrameShape::Panel);
  213. progressbar.set_frame_shadow(Gfx::FrameShadow::Sunken);
  214. progressbar.set_frame_thickness(1);
  215. location_textbox.on_return_pressed = [&] {
  216. directory_view.open(location_textbox.text());
  217. };
  218. auto refresh_tree_view = [&] {
  219. directories_model->update();
  220. auto current_path = directory_view.path();
  221. struct stat st;
  222. // If the directory no longer exists, we find a parent that does.
  223. while (stat(current_path.characters(), &st) != 0) {
  224. directory_view.open_parent_directory();
  225. current_path = directory_view.path();
  226. if (current_path == directories_model->root_path()) {
  227. break;
  228. }
  229. }
  230. // Reselect the existing folder in the tree.
  231. auto new_index = directories_model->index(current_path, GUI::FileSystemModel::Column::Name);
  232. tree_view.selection().set(new_index);
  233. tree_view.scroll_into_view(new_index, Orientation::Vertical);
  234. tree_view.update();
  235. directory_view.refresh();
  236. };
  237. auto directory_context_menu = GUI::Menu::construct("Directory View Directory");
  238. auto directory_view_context_menu = GUI::Menu::construct("Directory View");
  239. auto tree_view_directory_context_menu = GUI::Menu::construct("Tree View Directory");
  240. auto tree_view_context_menu = GUI::Menu::construct("Tree View");
  241. 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&) {
  242. directory_view.open_parent_directory();
  243. });
  244. auto open_terminal_action = GUI::Action::create("Open Terminal here...", Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"), [&](const GUI::Action&) {
  245. posix_spawn_file_actions_t spawn_actions;
  246. posix_spawn_file_actions_init(&spawn_actions);
  247. posix_spawn_file_actions_addchdir(&spawn_actions, directory_view.path().characters());
  248. pid_t pid;
  249. const char* argv[] = { "Terminal", nullptr };
  250. if ((errno = posix_spawn(&pid, "/bin/Terminal", &spawn_actions, nullptr, const_cast<char**>(argv), environ))) {
  251. perror("posix_spawn");
  252. } else {
  253. if (disown(pid) < 0)
  254. perror("disown");
  255. }
  256. posix_spawn_file_actions_destroy(&spawn_actions);
  257. });
  258. RefPtr<GUI::Action> view_as_table_action;
  259. RefPtr<GUI::Action> view_as_icons_action;
  260. RefPtr<GUI::Action> view_as_columns_action;
  261. view_as_icons_action = GUI::Action::create_checkable(
  262. "Icon view", { Mod_Ctrl, KeyCode::Key_1 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/icon-view.png"), [&](const GUI::Action&) {
  263. directory_view.set_view_mode(DirectoryView::ViewMode::Icon);
  264. config->write_entry("DirectoryView", "ViewMode", "Icon");
  265. config->sync();
  266. },
  267. window);
  268. view_as_table_action = GUI::Action::create_checkable(
  269. "Table view", { Mod_Ctrl, KeyCode::Key_2 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/table-view.png"), [&](const GUI::Action&) {
  270. directory_view.set_view_mode(DirectoryView::ViewMode::Table);
  271. config->write_entry("DirectoryView", "ViewMode", "Table");
  272. config->sync();
  273. },
  274. window);
  275. view_as_columns_action = GUI::Action::create_checkable(
  276. "Columns view", { Mod_Ctrl, KeyCode::Key_3 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/columns-view.png"), [&](const GUI::Action&) {
  277. directory_view.set_view_mode(DirectoryView::ViewMode::Columns);
  278. config->write_entry("DirectoryView", "ViewMode", "Columns");
  279. config->sync();
  280. },
  281. window);
  282. auto view_type_action_group = make<GUI::ActionGroup>();
  283. view_type_action_group->set_exclusive(true);
  284. view_type_action_group->add_action(*view_as_icons_action);
  285. view_type_action_group->add_action(*view_as_table_action);
  286. view_type_action_group->add_action(*view_as_columns_action);
  287. auto tree_view_selected_file_paths = [&] {
  288. Vector<String> paths;
  289. auto& view = tree_view;
  290. view.selection().for_each_index([&](const GUI::ModelIndex& index) {
  291. paths.append(directories_model->full_path(index));
  292. });
  293. return paths;
  294. };
  295. auto select_all_action = GUI::Action::create("Select all", { Mod_Ctrl, KeyCode::Key_A }, [&](const GUI::Action&) {
  296. directory_view.current_view().select_all();
  297. });
  298. auto copy_action = GUI::CommonActions::make_copy_action(
  299. [&](auto&) {
  300. auto paths = directory_view.selected_file_paths();
  301. if (!paths.size())
  302. paths = tree_view_selected_file_paths();
  303. if (paths.is_empty())
  304. ASSERT_NOT_REACHED();
  305. StringBuilder copy_text;
  306. for (auto& path : paths) {
  307. auto url = URL::create_with_file_protocol(path);
  308. copy_text.appendf("%s\n", url.to_string().characters());
  309. }
  310. GUI::Clipboard::the().set_data(copy_text.build(), "text/uri-list");
  311. },
  312. window);
  313. copy_action->set_enabled(false);
  314. auto properties_action
  315. = GUI::Action::create(
  316. "Properties...", { Mod_Alt, Key_Return }, Gfx::Bitmap::load_from_file("/res/icons/16x16/properties.png"), [&](const GUI::Action& action) {
  317. String container_dir_path;
  318. String path;
  319. Vector<String> selected;
  320. if (action.activator() == directory_context_menu || directory_view.active_widget()->is_focused()) {
  321. path = directory_view.path();
  322. container_dir_path = path;
  323. selected = directory_view.selected_file_paths();
  324. } else {
  325. path = directories_model->full_path(tree_view.selection().first());
  326. container_dir_path = LexicalPath(path).basename();
  327. selected = tree_view_selected_file_paths();
  328. }
  329. RefPtr<PropertiesDialog> properties;
  330. if (selected.is_empty()) {
  331. properties = window->add<PropertiesDialog>(path, true);
  332. } else {
  333. properties = window->add<PropertiesDialog>(selected.first(), access(container_dir_path.characters(), W_OK) != 0);
  334. }
  335. properties->exec();
  336. },
  337. window);
  338. enum class ConfirmBeforeDelete {
  339. No,
  340. Yes
  341. };
  342. auto do_paste = [&](const GUI::Action& action) {
  343. auto data_and_type = GUI::Clipboard::the().data_and_type();
  344. if (data_and_type.type != "text/uri-list") {
  345. dbg() << "Cannot paste clipboard type " << data_and_type.type;
  346. return;
  347. }
  348. auto copied_lines = data_and_type.data.split('\n');
  349. if (copied_lines.is_empty()) {
  350. dbg() << "No files to paste";
  351. return;
  352. }
  353. AK::String target_directory;
  354. if (action.activator() == directory_context_menu)
  355. target_directory = directory_view.selected_file_paths()[0];
  356. else
  357. target_directory = directory_view.path();
  358. for (auto& uri_as_string : copied_lines) {
  359. if (uri_as_string.is_empty())
  360. continue;
  361. URL url = uri_as_string;
  362. if (!url.is_valid() || url.protocol() != "file") {
  363. dbg() << "Cannot paste URI " << uri_as_string;
  364. continue;
  365. }
  366. auto new_path = String::format("%s/%s", target_directory.characters(), url.basename().characters());
  367. if (!FileUtils::copy_file_or_directory(url.path(), new_path)) {
  368. auto error_message = String::format("Could not paste %s.", url.path().characters());
  369. GUI::MessageBox::show(window, error_message, "File Manager", GUI::MessageBox::Type::Error);
  370. } else {
  371. refresh_tree_view();
  372. }
  373. }
  374. };
  375. auto do_delete = [&](ConfirmBeforeDelete confirm, const GUI::Action&) {
  376. auto paths = directory_view.selected_file_paths();
  377. if (!paths.size())
  378. paths = tree_view_selected_file_paths();
  379. if (paths.is_empty())
  380. ASSERT_NOT_REACHED();
  381. String message;
  382. if (paths.size() == 1) {
  383. message = String::format("Really delete %s?", LexicalPath(paths[0]).basename().characters());
  384. } else {
  385. message = String::format("Really delete %d files?", paths.size());
  386. }
  387. if (confirm == ConfirmBeforeDelete::Yes) {
  388. auto result = GUI::MessageBox::show(window,
  389. message,
  390. "Confirm deletion",
  391. GUI::MessageBox::Type::Warning,
  392. GUI::MessageBox::InputType::OKCancel);
  393. if (result == GUI::MessageBox::ExecCancel)
  394. return;
  395. }
  396. for (auto& path : paths) {
  397. struct stat st;
  398. if (lstat(path.characters(), &st)) {
  399. GUI::MessageBox::show(window,
  400. String::format("lstat(%s) failed: %s", path.characters(), strerror(errno)),
  401. "Delete failed",
  402. GUI::MessageBox::Type::Error);
  403. break;
  404. } else {
  405. refresh_tree_view();
  406. }
  407. if (S_ISDIR(st.st_mode)) {
  408. String error_path;
  409. int error = FileUtils::delete_directory(path, error_path);
  410. if (error) {
  411. GUI::MessageBox::show(window,
  412. String::format("Failed to delete directory \"%s\": %s", error_path.characters(), strerror(error)),
  413. "Delete failed",
  414. GUI::MessageBox::Type::Error);
  415. break;
  416. } else {
  417. refresh_tree_view();
  418. }
  419. } else if (unlink(path.characters()) < 0) {
  420. int saved_errno = errno;
  421. GUI::MessageBox::show(window,
  422. String::format("unlink(%s) failed: %s", path.characters(), strerror(saved_errno)),
  423. "Delete failed",
  424. GUI::MessageBox::Type::Error);
  425. break;
  426. }
  427. }
  428. };
  429. auto paste_action = GUI::CommonActions::make_paste_action(
  430. [&](const GUI::Action& action) {
  431. do_paste(action);
  432. },
  433. window);
  434. auto folder_specific_paste_action = GUI::CommonActions::make_paste_action(
  435. [&](const GUI::Action& action) {
  436. do_paste(action);
  437. },
  438. window);
  439. auto force_delete_action = GUI::Action::create(
  440. "Delete without confirmation", { Mod_Shift, Key_Delete }, [&](const GUI::Action& action) {
  441. do_delete(ConfirmBeforeDelete::No, action);
  442. },
  443. window);
  444. auto delete_action = GUI::CommonActions::make_delete_action(
  445. [&](const GUI::Action& action) {
  446. do_delete(ConfirmBeforeDelete::Yes, action);
  447. },
  448. window);
  449. delete_action->set_enabled(false);
  450. auto go_back_action = GUI::CommonActions::make_go_back_action(
  451. [&](auto&) {
  452. directory_view.open_previous_directory();
  453. },
  454. window);
  455. auto go_forward_action = GUI::CommonActions::make_go_forward_action(
  456. [&](auto&) {
  457. directory_view.open_next_directory();
  458. },
  459. window);
  460. auto go_home_action = GUI::CommonActions::make_go_home_action(
  461. [&](auto&) {
  462. directory_view.open(Core::StandardPaths::home_directory());
  463. },
  464. window);
  465. GUI::Clipboard::the().on_change = [&](const String& data_type) {
  466. auto current_location = directory_view.path();
  467. paste_action->set_enabled(data_type == "text/uri-list" && access(current_location.characters(), W_OK) == 0);
  468. };
  469. auto menubar = GUI::MenuBar::construct();
  470. auto& app_menu = menubar->add_menu("File Manager");
  471. app_menu.add_action(directory_view.mkdir_action());
  472. app_menu.add_action(directory_view.touch_action());
  473. app_menu.add_action(copy_action);
  474. app_menu.add_action(paste_action);
  475. app_menu.add_action(delete_action);
  476. app_menu.add_action(open_terminal_action);
  477. app_menu.add_separator();
  478. app_menu.add_action(properties_action);
  479. app_menu.add_separator();
  480. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  481. GUI::Application::the()->quit();
  482. }));
  483. auto& view_menu = menubar->add_menu("View");
  484. view_menu.add_action(*view_as_icons_action);
  485. view_menu.add_action(*view_as_table_action);
  486. view_menu.add_action(*view_as_columns_action);
  487. view_menu.add_separator();
  488. view_menu.add_action(GUI::Action::create_checkable("Show dotfiles", { Mod_Ctrl, Key_H }, [&](auto& action) {
  489. directory_view.set_should_show_dotfiles(action.is_checked());
  490. }));
  491. auto& go_menu = menubar->add_menu("Go");
  492. go_menu.add_action(go_back_action);
  493. go_menu.add_action(go_forward_action);
  494. go_menu.add_action(open_parent_directory_action);
  495. go_menu.add_action(go_home_action);
  496. go_menu.add_action(GUI::Action::create(
  497. "Go to location...", { Mod_Ctrl, Key_L }, [&](auto&) {
  498. location_textbox.select_all();
  499. location_textbox.set_focus(true);
  500. }));
  501. auto& help_menu = menubar->add_menu("Help");
  502. help_menu.add_action(GUI::Action::create("About", [&](auto&) {
  503. GUI::AboutDialog::show("File Manager", Gfx::Bitmap::load_from_file("/res/icons/32x32/filetype-folder.png"), window);
  504. }));
  505. GUI::Application::the()->set_menubar(move(menubar));
  506. main_toolbar.add_action(go_back_action);
  507. main_toolbar.add_action(go_forward_action);
  508. main_toolbar.add_action(open_parent_directory_action);
  509. main_toolbar.add_action(go_home_action);
  510. main_toolbar.add_separator();
  511. main_toolbar.add_action(directory_view.mkdir_action());
  512. main_toolbar.add_action(directory_view.touch_action());
  513. main_toolbar.add_action(copy_action);
  514. main_toolbar.add_action(paste_action);
  515. main_toolbar.add_action(delete_action);
  516. main_toolbar.add_action(open_terminal_action);
  517. main_toolbar.add_separator();
  518. main_toolbar.add_action(*view_as_icons_action);
  519. main_toolbar.add_action(*view_as_table_action);
  520. main_toolbar.add_action(*view_as_columns_action);
  521. directory_view.on_path_change = [&](const String& new_path, bool can_write_in_path) {
  522. const Gfx::Bitmap* icon = nullptr;
  523. if (new_path == Core::StandardPaths::home_directory())
  524. icon = &home_directory_icon();
  525. else
  526. icon = &folder_icon();
  527. window->set_icon(icon);
  528. location_textbox.set_icon(icon);
  529. window->set_title(String::format("%s - File Manager", new_path.characters()));
  530. location_textbox.set_text(new_path);
  531. auto new_index = directories_model->index(new_path, GUI::FileSystemModel::Column::Name);
  532. if (new_index.is_valid()) {
  533. tree_view.selection().set(new_index);
  534. tree_view.scroll_into_view(new_index, Orientation::Vertical);
  535. tree_view.update();
  536. }
  537. struct stat st;
  538. if (lstat(new_path.characters(), &st)) {
  539. perror("stat");
  540. return;
  541. }
  542. paste_action->set_enabled(can_write_in_path && GUI::Clipboard::the().type() == "text/uri-list");
  543. go_forward_action->set_enabled(directory_view.path_history_position() < directory_view.path_history_size() - 1);
  544. go_back_action->set_enabled(directory_view.path_history_position() > 0);
  545. open_parent_directory_action->set_enabled(new_path != "/");
  546. };
  547. directory_view.on_error = [&](int, const char* error_string, bool quit) {
  548. auto error_message = String::format("Could not read directory: %s", error_string);
  549. GUI::MessageBox::show(window, error_message, "File Manager", GUI::MessageBox::Type::Error);
  550. if (quit)
  551. exit(1);
  552. };
  553. directory_view.on_status_message = [&](const StringView& message) {
  554. statusbar.set_text(message);
  555. };
  556. directory_view.on_thumbnail_progress = [&](int done, int total) {
  557. if (done == total) {
  558. progressbar.set_visible(false);
  559. return;
  560. }
  561. progressbar.set_range(0, total);
  562. progressbar.set_value(done);
  563. progressbar.set_visible(true);
  564. };
  565. directory_view.on_selection_change = [&](GUI::AbstractView& view) {
  566. // FIXME: Figure out how we can enable/disable the paste action, based on clipboard contents.
  567. auto& selection = view.selection();
  568. delete_action->set_enabled(!selection.is_empty() && access(directory_view.path().characters(), W_OK) == 0);
  569. copy_action->set_enabled(!selection.is_empty());
  570. };
  571. directory_context_menu->add_action(copy_action);
  572. directory_context_menu->add_action(folder_specific_paste_action);
  573. directory_context_menu->add_action(delete_action);
  574. directory_context_menu->add_separator();
  575. directory_context_menu->add_action(properties_action);
  576. directory_view_context_menu->add_action(directory_view.mkdir_action());
  577. directory_view_context_menu->add_action(directory_view.touch_action());
  578. directory_view_context_menu->add_action(paste_action);
  579. directory_view_context_menu->add_action(open_terminal_action);
  580. directory_view_context_menu->add_separator();
  581. directory_view_context_menu->add_action(properties_action);
  582. tree_view_directory_context_menu->add_action(copy_action);
  583. tree_view_directory_context_menu->add_action(paste_action);
  584. tree_view_directory_context_menu->add_action(delete_action);
  585. tree_view_directory_context_menu->add_separator();
  586. tree_view_directory_context_menu->add_action(properties_action);
  587. tree_view_directory_context_menu->add_separator();
  588. tree_view_directory_context_menu->add_action(directory_view.mkdir_action());
  589. tree_view_directory_context_menu->add_action(directory_view.touch_action());
  590. RefPtr<GUI::Menu> file_context_menu;
  591. NonnullRefPtrVector<LauncherHandler> current_file_handlers;
  592. RefPtr<GUI::Action> file_context_menu_action_default_action;
  593. directory_view.on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  594. if (index.is_valid()) {
  595. auto& node = directory_view.node(index);
  596. if (node.is_directory()) {
  597. auto should_get_enabled = access(node.full_path().characters(), W_OK) == 0 && GUI::Clipboard::the().type() == "text/uri-list";
  598. folder_specific_paste_action->set_enabled(should_get_enabled);
  599. directory_context_menu->popup(event.screen_position());
  600. } else {
  601. auto full_path = node.full_path();
  602. current_file_handlers = directory_view.get_launch_handlers(full_path);
  603. file_context_menu = GUI::Menu::construct("Directory View File");
  604. file_context_menu->add_action(copy_action);
  605. file_context_menu->add_action(paste_action);
  606. file_context_menu->add_action(delete_action);
  607. file_context_menu->add_separator();
  608. bool added_open_menu_items = false;
  609. auto default_file_handler = directory_view.get_default_launch_handler(current_file_handlers);
  610. if (default_file_handler) {
  611. auto file_open_action = default_file_handler->create_launch_action([&, full_path = move(full_path)](auto& launcher_handler) {
  612. directory_view.launch(URL::create_with_file_protocol(full_path), launcher_handler);
  613. });
  614. if (default_file_handler->details().launcher_type == Desktop::Launcher::LauncherType::Application)
  615. file_open_action->set_text(String::format("Run %s", file_open_action->text().characters()));
  616. else
  617. file_open_action->set_text(String::format("Open in %s", file_open_action->text().characters()));
  618. file_context_menu_action_default_action = file_open_action;
  619. file_context_menu->add_action(move(file_open_action));
  620. added_open_menu_items = true;
  621. } else {
  622. file_context_menu_action_default_action.clear();
  623. }
  624. if (current_file_handlers.size() > 1) {
  625. added_open_menu_items = true;
  626. auto& file_open_with_menu = file_context_menu->add_submenu("Open with");
  627. for (auto& handler : current_file_handlers) {
  628. if (&handler == default_file_handler.ptr())
  629. continue;
  630. file_open_with_menu.add_action(handler.create_launch_action([&, full_path = move(full_path)](auto& launcher_handler) {
  631. directory_view.launch(URL::create_with_file_protocol(full_path), launcher_handler);
  632. }));
  633. }
  634. }
  635. if (added_open_menu_items)
  636. file_context_menu->add_separator();
  637. file_context_menu->add_action(properties_action);
  638. file_context_menu->popup(event.screen_position(), file_context_menu_action_default_action);
  639. }
  640. } else {
  641. directory_view_context_menu->popup(event.screen_position());
  642. }
  643. };
  644. directory_view.on_drop = [&](const GUI::ModelIndex& index, const GUI::DropEvent& event) {
  645. if (!event.mime_data().has_urls())
  646. return;
  647. auto urls = event.mime_data().urls();
  648. if (urls.is_empty()) {
  649. dbg() << "No files to drop";
  650. return;
  651. }
  652. auto& target_node = directory_view.node(index);
  653. if (!target_node.is_directory())
  654. return;
  655. for (auto& url_to_copy : urls) {
  656. if (!url_to_copy.is_valid() || url_to_copy.path() == target_node.full_path())
  657. continue;
  658. auto new_path = String::format("%s/%s",
  659. target_node.full_path().characters(),
  660. LexicalPath(url_to_copy.path()).basename().characters());
  661. if (url_to_copy.path() == new_path)
  662. continue;
  663. if (!FileUtils::copy_file_or_directory(url_to_copy.path(), new_path)) {
  664. auto error_message = String::format("Could not copy %s into %s.",
  665. url_to_copy.to_string().characters(),
  666. new_path.characters());
  667. GUI::MessageBox::show(window, error_message, "File Manager", GUI::MessageBox::Type::Error);
  668. } else {
  669. refresh_tree_view();
  670. }
  671. }
  672. };
  673. tree_view.on_selection = [&](const GUI::ModelIndex& index) {
  674. if (directories_model->m_previously_selected_index.is_valid())
  675. directories_model->update_node_on_selection(directories_model->m_previously_selected_index, false);
  676. directories_model->update_node_on_selection(index, true);
  677. directories_model->m_previously_selected_index = index;
  678. };
  679. tree_view.on_selection_change = [&] {
  680. if (tree_view.selection().is_empty())
  681. return;
  682. auto path = directories_model->full_path(tree_view.selection().first());
  683. if (directory_view.path() == path)
  684. return;
  685. directory_view.open(path);
  686. copy_action->set_enabled(!tree_view.selection().is_empty());
  687. delete_action->set_enabled(!tree_view.selection().is_empty());
  688. };
  689. tree_view.on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  690. if (index.is_valid()) {
  691. tree_view_directory_context_menu->popup(event.screen_position());
  692. }
  693. };
  694. directory_view.open(initial_location);
  695. directory_view.set_focus(true);
  696. paste_action->set_enabled(GUI::Clipboard::the().type() == "text/uri-list" && access(initial_location.characters(), W_OK) == 0);
  697. window->show();
  698. // Read direcory read mode from config.
  699. auto dir_view_mode = config->read_entry("DirectoryView", "ViewMode", "Icon");
  700. if (dir_view_mode.contains("Table")) {
  701. directory_view.set_view_mode(DirectoryView::ViewMode::Table);
  702. view_as_table_action->set_checked(true);
  703. } else if (dir_view_mode.contains("Columns")) {
  704. directory_view.set_view_mode(DirectoryView::ViewMode::Columns);
  705. view_as_columns_action->set_checked(true);
  706. } else {
  707. directory_view.set_view_mode(DirectoryView::ViewMode::Icon);
  708. view_as_icons_action->set_checked(true);
  709. }
  710. // Write window position to config file on close request.
  711. window->on_close_request = [&] {
  712. config->write_num_entry("Window", "Left", window->x());
  713. config->write_num_entry("Window", "Top", window->y());
  714. config->write_num_entry("Window", "Width", window->width());
  715. config->write_num_entry("Window", "Heigth", window->height());
  716. config->sync();
  717. return GUI::Window::CloseRequestDecision::Close;
  718. };
  719. return GUI::Application::the()->exec();
  720. }