main.cpp 35 KB

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