main.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. #include "DirectoryView.h"
  2. #include "FileUtils.h"
  3. #include "PropertiesDialog.h"
  4. #include <AK/FileSystemPath.h>
  5. #include <AK/StringBuilder.h>
  6. #include <LibCore/CConfigFile.h>
  7. #include <LibCore/CUserInfo.h>
  8. #include <LibDraw/PNGLoader.h>
  9. #include <LibGUI/GAction.h>
  10. #include <LibGUI/GActionGroup.h>
  11. #include <LibGUI/GApplication.h>
  12. #include <LibGUI/GBoxLayout.h>
  13. #include <LibGUI/GClipboard.h>
  14. #include <LibGUI/GFileSystemModel.h>
  15. #include <LibGUI/GInputBox.h>
  16. #include <LibGUI/GLabel.h>
  17. #include <LibGUI/GMenuBar.h>
  18. #include <LibGUI/GMessageBox.h>
  19. #include <LibGUI/GProgressBar.h>
  20. #include <LibGUI/GSplitter.h>
  21. #include <LibGUI/GStatusBar.h>
  22. #include <LibGUI/GTextEditor.h>
  23. #include <LibGUI/GToolBar.h>
  24. #include <LibGUI/GTreeView.h>
  25. #include <LibGUI/GWidget.h>
  26. #include <LibGUI/GWindow.h>
  27. #include <signal.h>
  28. #include <stdio.h>
  29. #include <unistd.h>
  30. int main(int argc, char** argv)
  31. {
  32. struct sigaction act;
  33. memset(&act, 0, sizeof(act));
  34. act.sa_flags = SA_NOCLDWAIT;
  35. act.sa_handler = SIG_IGN;
  36. int rc = sigaction(SIGCHLD, &act, nullptr);
  37. if (rc < 0) {
  38. perror("sigaction");
  39. return 1;
  40. }
  41. RefPtr<CConfigFile> config = CConfigFile::get_for_app("FileManager");
  42. GApplication app(argc, argv);
  43. auto window = GWindow::construct();
  44. window->set_title("File Manager");
  45. auto left = config->read_num_entry("Window", "Left", 150);
  46. auto top = config->read_num_entry("Window", "Top", 75);
  47. auto width = config->read_num_entry("Window", "Width", 640);
  48. auto heigth = config->read_num_entry("Window", "Heigth", 480);
  49. window->set_rect({ left, top, width, heigth });
  50. auto widget = GWidget::construct();
  51. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  52. widget->layout()->set_spacing(0);
  53. auto main_toolbar = GToolBar::construct(widget);
  54. auto location_toolbar = GToolBar::construct(widget);
  55. location_toolbar->layout()->set_margins({ 6, 3, 6, 3 });
  56. location_toolbar->set_preferred_size(0, 25);
  57. auto location_label = GLabel::construct("Location: ", location_toolbar);
  58. location_label->size_to_fit();
  59. auto location_textbox = GTextEditor::construct(GTextEditor::SingleLine, location_toolbar);
  60. auto splitter = GSplitter::construct(Orientation::Horizontal, widget);
  61. auto tree_view = GTreeView::construct(splitter);
  62. auto file_system_model = GFileSystemModel::create("/", GFileSystemModel::Mode::DirectoriesOnly);
  63. tree_view->set_model(file_system_model);
  64. tree_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  65. tree_view->set_preferred_size(200, 0);
  66. auto directory_view = DirectoryView::construct(splitter);
  67. auto statusbar = GStatusBar::construct(widget);
  68. auto progressbar = GProgressBar::construct(statusbar);
  69. progressbar->set_caption("Generating thumbnails: ");
  70. progressbar->set_format(GProgressBar::Format::ValueSlashMax);
  71. progressbar->set_visible(false);
  72. progressbar->set_frame_shape(FrameShape::Panel);
  73. progressbar->set_frame_shadow(FrameShadow::Sunken);
  74. progressbar->set_frame_thickness(1);
  75. location_textbox->on_return_pressed = [&] {
  76. directory_view->open(location_textbox->text());
  77. };
  78. auto refresh_tree_view = [&] {
  79. file_system_model->update();
  80. auto current_path = directory_view->path();
  81. struct stat st;
  82. // If the directory no longer exists, we find a parent that does.
  83. while (lstat(current_path.characters(), &st) != 0) {
  84. directory_view->open_parent_directory();
  85. current_path = directory_view->path();
  86. if (current_path == file_system_model->root_path()) {
  87. break;
  88. }
  89. }
  90. // not exactly sure why i have to reselect the root node first, but the index() fails if I dont
  91. auto root_index = file_system_model->index(file_system_model->root_path());
  92. tree_view->selection().set(root_index);
  93. // reselect the existing folder in the tree
  94. auto new_index = file_system_model->index(current_path);
  95. tree_view->selection().set(new_index);
  96. tree_view->scroll_into_view(new_index, Orientation::Vertical);
  97. tree_view->update();
  98. directory_view->refresh();
  99. };
  100. auto directory_context_menu = GMenu::construct("Directory View Directory");
  101. auto file_context_menu = GMenu::construct("Directory View File");
  102. auto directory_view_context_menu = GMenu::construct("Directory View");
  103. auto tree_view_directory_context_menu = GMenu::construct("Tree View Directory");
  104. auto tree_view_context_menu = GMenu::construct("Tree View");
  105. auto open_parent_directory_action = GAction::create("Open parent directory", { Mod_Alt, Key_Up }, GraphicsBitmap::load_from_file("/res/icons/16x16/open-parent-directory.png"), [&](const GAction&) {
  106. directory_view->open_parent_directory();
  107. });
  108. auto mkdir_action = GAction::create("New directory...", GraphicsBitmap::load_from_file("/res/icons/16x16/mkdir.png"), [&](const GAction&) {
  109. auto input_box = GInputBox::construct("Enter name:", "New directory", window);
  110. if (input_box->exec() == GInputBox::ExecOK && !input_box->text_value().is_empty()) {
  111. auto new_dir_path = canonicalized_path(
  112. String::format("%s/%s",
  113. directory_view->path().characters(),
  114. input_box->text_value().characters()));
  115. int rc = mkdir(new_dir_path.characters(), 0777);
  116. if (rc < 0) {
  117. GMessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window);
  118. } else {
  119. refresh_tree_view();
  120. }
  121. }
  122. });
  123. RefPtr<GAction> view_as_table_action;
  124. RefPtr<GAction> view_as_icons_action;
  125. view_as_table_action = GAction::create("Table view", { Mod_Ctrl, KeyCode::Key_L }, GraphicsBitmap::load_from_file("/res/icons/16x16/table-view.png"), [&](const GAction&) {
  126. directory_view->set_view_mode(DirectoryView::ViewMode::List);
  127. view_as_table_action->set_checked(true);
  128. config->write_entry("DirectoryView", "ViewMode", "List");
  129. config->sync();
  130. });
  131. view_as_table_action->set_checkable(true);
  132. view_as_icons_action = GAction::create("Icon view", { Mod_Ctrl, KeyCode::Key_I }, GraphicsBitmap::load_from_file("/res/icons/16x16/icon-view.png"), [&](const GAction&) {
  133. directory_view->set_view_mode(DirectoryView::ViewMode::Icon);
  134. view_as_icons_action->set_checked(true);
  135. config->write_entry("DirectoryView", "ViewMode", "Icon");
  136. config->sync();
  137. });
  138. view_as_icons_action->set_checkable(true);
  139. auto view_type_action_group = make<GActionGroup>();
  140. view_type_action_group->set_exclusive(true);
  141. view_type_action_group->add_action(*view_as_table_action);
  142. view_type_action_group->add_action(*view_as_icons_action);
  143. auto selected_file_paths = [&] {
  144. Vector<String> paths;
  145. auto& view = directory_view->current_view();
  146. auto& model = *view.model();
  147. view.selection().for_each_index([&](const GModelIndex& index) {
  148. auto name_index = model.index(index.row(), GDirectoryModel::Column::Name);
  149. auto path = model.data(name_index, GModel::Role::Custom).to_string();
  150. paths.append(path);
  151. });
  152. return paths;
  153. };
  154. auto tree_view_selected_file_paths = [&] {
  155. Vector<String> paths;
  156. auto& view = tree_view;
  157. view->selection().for_each_index([&](const GModelIndex& index) {
  158. paths.append(file_system_model->path(index));
  159. });
  160. return paths;
  161. };
  162. auto copy_action = GCommonActions::make_copy_action([&](const GAction& action) {
  163. Vector<String> paths;
  164. if (action.activator() == directory_context_menu) {
  165. paths = selected_file_paths();
  166. } else {
  167. paths = tree_view_selected_file_paths();
  168. }
  169. if (paths.is_empty())
  170. return;
  171. StringBuilder copy_text;
  172. for (auto& path : paths) {
  173. copy_text.appendf("%s\n", path.characters());
  174. }
  175. GClipboard::the().set_data(copy_text.build(), "file-list");
  176. });
  177. copy_action->set_enabled(false);
  178. auto paste_action = GCommonActions::make_paste_action([&](const GAction&) {
  179. auto data_and_type = GClipboard::the().data_and_type();
  180. if (data_and_type.type != "file-list") {
  181. dbg() << "Cannot paste clipboard type " << data_and_type.type;
  182. return;
  183. }
  184. auto copied_lines = data_and_type.data.split('\n');
  185. if (copied_lines.is_empty()) {
  186. dbg() << "No files to paste";
  187. return;
  188. }
  189. for (auto& current_path : copied_lines) {
  190. if (current_path.is_empty())
  191. continue;
  192. auto current_directory = directory_view->path();
  193. auto new_path = String::format("%s/%s",
  194. current_directory.characters(),
  195. FileSystemPath(current_path).basename().characters());
  196. if (!FileUtils::copy_file_or_directory(current_path, new_path)) {
  197. auto error_message = String::format("Could not paste %s.",
  198. current_path.characters());
  199. GMessageBox::show(error_message, "File Manager", GMessageBox::Type::Error);
  200. } else {
  201. refresh_tree_view();
  202. }
  203. }
  204. });
  205. paste_action->set_enabled(GClipboard::the().type() == "file-list");
  206. GClipboard::the().on_content_change = [&](const String& data_type) {
  207. paste_action->set_enabled(data_type == "file-list");
  208. };
  209. auto properties_action
  210. = GAction::create("Properties...", { Mod_Alt, Key_Return }, GraphicsBitmap::load_from_file("/res/icons/16x16/properties.png"), [&](const GAction& action) {
  211. auto& model = directory_view->model();
  212. String path;
  213. Vector<String> selected;
  214. if (action.activator() == directory_context_menu) {
  215. path = directory_view->path();
  216. selected = selected_file_paths();
  217. } else {
  218. path = file_system_model->path(tree_view->selection().first());
  219. selected = tree_view_selected_file_paths();
  220. }
  221. RefPtr<PropertiesDialog> properties;
  222. if (selected.is_empty()) {
  223. properties = PropertiesDialog::construct(model, path, true, window);
  224. } else {
  225. properties = PropertiesDialog::construct(model, selected.first(), false, window);
  226. }
  227. properties->exec();
  228. });
  229. enum class ConfirmBeforeDelete { No,
  230. Yes };
  231. auto do_delete = [&](ConfirmBeforeDelete confirm, const GAction& action) {
  232. Vector<String> paths;
  233. if (action.activator() == directory_context_menu) {
  234. paths = selected_file_paths();
  235. } else {
  236. paths = tree_view_selected_file_paths();
  237. }
  238. if (paths.is_empty())
  239. return;
  240. {
  241. String message;
  242. if (paths.size() == 1) {
  243. message = String::format("Really delete %s?", FileSystemPath(paths[0]).basename().characters());
  244. } else {
  245. message = String::format("Really delete %d files?", paths.size());
  246. }
  247. if (confirm == ConfirmBeforeDelete::Yes) {
  248. auto result = GMessageBox::show(
  249. message,
  250. "Confirm deletion",
  251. GMessageBox::Type::Warning,
  252. GMessageBox::InputType::OKCancel,
  253. window);
  254. if (result == GMessageBox::ExecCancel)
  255. return;
  256. }
  257. }
  258. for (auto& path : paths) {
  259. struct stat st;
  260. if (lstat(path.characters(), &st)) {
  261. GMessageBox::show(
  262. String::format("lstat(%s) failed: %s", path.characters(), strerror(errno)),
  263. "Delete failed",
  264. GMessageBox::Type::Error,
  265. GMessageBox::InputType::OK,
  266. window);
  267. break;
  268. } else {
  269. refresh_tree_view();
  270. }
  271. if (S_ISDIR(st.st_mode)) {
  272. String error_path;
  273. int error = FileUtils::delete_directory(path, error_path);
  274. if (error) {
  275. GMessageBox::show(
  276. String::format("Failed to delete directory \"%s\": %s", error_path.characters(), strerror(error)),
  277. "Delete failed",
  278. GMessageBox::Type::Error,
  279. GMessageBox::InputType::OK,
  280. window);
  281. break;
  282. } else {
  283. refresh_tree_view();
  284. }
  285. } else if (unlink(path.characters()) < 0) {
  286. int saved_errno = errno;
  287. GMessageBox::show(
  288. String::format("unlink(%s) failed: %s", path.characters(), strerror(saved_errno)),
  289. "Delete failed",
  290. GMessageBox::Type::Error,
  291. GMessageBox::InputType::OK,
  292. window);
  293. break;
  294. }
  295. }
  296. };
  297. auto force_delete_action = GAction::create("Delete without confirmation", { Mod_Shift, Key_Delete }, [&](const GAction& action) {
  298. do_delete(ConfirmBeforeDelete::No, action);
  299. });
  300. auto delete_action = GCommonActions::make_delete_action([&](const GAction& action) {
  301. do_delete(ConfirmBeforeDelete::Yes, action);
  302. });
  303. delete_action->set_enabled(false);
  304. auto go_back_action = GCommonActions::make_go_back_action([&](auto&) {
  305. directory_view->open_previous_directory();
  306. });
  307. auto go_forward_action = GCommonActions::make_go_forward_action([&](auto&) {
  308. directory_view->open_next_directory();
  309. });
  310. auto go_home_action = GCommonActions::make_go_home_action([&](auto&) {
  311. directory_view->open(get_current_user_home_path());
  312. });
  313. auto menubar = make<GMenuBar>();
  314. auto app_menu = GMenu::construct("File Manager");
  315. app_menu->add_action(mkdir_action);
  316. app_menu->add_action(copy_action);
  317. app_menu->add_action(paste_action);
  318. app_menu->add_action(delete_action);
  319. app_menu->add_separator();
  320. app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
  321. GApplication::the().quit(0);
  322. }));
  323. menubar->add_menu(move(app_menu));
  324. auto view_menu = GMenu::construct("View");
  325. view_menu->add_action(*view_as_icons_action);
  326. view_menu->add_action(*view_as_table_action);
  327. menubar->add_menu(move(view_menu));
  328. auto go_menu = GMenu::construct("Go");
  329. go_menu->add_action(go_back_action);
  330. go_menu->add_action(go_forward_action);
  331. go_menu->add_action(open_parent_directory_action);
  332. go_menu->add_action(go_home_action);
  333. menubar->add_menu(move(go_menu));
  334. auto help_menu = GMenu::construct("Help");
  335. help_menu->add_action(GAction::create("About", [](const GAction&) {
  336. dbgprintf("FIXME: Implement Help/About\n");
  337. }));
  338. menubar->add_menu(move(help_menu));
  339. app.set_menubar(move(menubar));
  340. main_toolbar->add_action(go_back_action);
  341. main_toolbar->add_action(go_forward_action);
  342. main_toolbar->add_action(open_parent_directory_action);
  343. main_toolbar->add_action(go_home_action);
  344. main_toolbar->add_separator();
  345. main_toolbar->add_action(mkdir_action);
  346. main_toolbar->add_action(copy_action);
  347. main_toolbar->add_action(paste_action);
  348. main_toolbar->add_action(delete_action);
  349. main_toolbar->add_separator();
  350. main_toolbar->add_action(*view_as_icons_action);
  351. main_toolbar->add_action(*view_as_table_action);
  352. directory_view->on_path_change = [&](const String& new_path) {
  353. window->set_title(String::format("File Manager: %s", new_path.characters()));
  354. location_textbox->set_text(new_path);
  355. auto new_index = file_system_model->index(new_path);
  356. if (new_index.is_valid()) {
  357. tree_view->selection().set(new_index);
  358. tree_view->scroll_into_view(new_index, Orientation::Vertical);
  359. tree_view->update();
  360. }
  361. go_forward_action->set_enabled(directory_view->path_history_position()
  362. < directory_view->path_history_size() - 1);
  363. go_back_action->set_enabled(directory_view->path_history_position() > 0);
  364. };
  365. directory_view->on_status_message = [&](const StringView& message) {
  366. statusbar->set_text(message);
  367. };
  368. directory_view->on_thumbnail_progress = [&](int done, int total) {
  369. if (done == total) {
  370. progressbar->set_visible(false);
  371. return;
  372. }
  373. progressbar->set_range(0, total);
  374. progressbar->set_value(done);
  375. progressbar->set_visible(true);
  376. };
  377. directory_view->on_selection_change = [&](GAbstractView& view) {
  378. // FIXME: Figure out how we can enable/disable the paste action, based on clipboard contents.
  379. copy_action->set_enabled(!view.selection().is_empty());
  380. delete_action->set_enabled(!view.selection().is_empty());
  381. };
  382. auto open_in_text_editor_action = GAction::create("Open in TextEditor...", GraphicsBitmap::load_from_file("/res/icons/TextEditor16.png"), [&](auto&) {
  383. for (auto& path : selected_file_paths()) {
  384. if (!fork()) {
  385. int rc = execl("/bin/TextEditor", "TextEditor", path.characters(), nullptr);
  386. if (rc < 0)
  387. perror("execl");
  388. exit(1);
  389. }
  390. }
  391. });
  392. directory_context_menu->add_action(copy_action);
  393. directory_context_menu->add_action(paste_action);
  394. directory_context_menu->add_action(delete_action);
  395. directory_context_menu->add_separator();
  396. directory_context_menu->add_action(properties_action);
  397. file_context_menu->add_action(copy_action);
  398. file_context_menu->add_action(paste_action);
  399. file_context_menu->add_action(delete_action);
  400. file_context_menu->add_separator();
  401. file_context_menu->add_action(open_in_text_editor_action);
  402. file_context_menu->add_separator();
  403. file_context_menu->add_action(properties_action);
  404. directory_view_context_menu->add_action(mkdir_action);
  405. tree_view_directory_context_menu->add_action(copy_action);
  406. tree_view_directory_context_menu->add_action(paste_action);
  407. tree_view_directory_context_menu->add_action(delete_action);
  408. tree_view_directory_context_menu->add_separator();
  409. tree_view_directory_context_menu->add_action(properties_action);
  410. tree_view_directory_context_menu->add_separator();
  411. tree_view_directory_context_menu->add_action(mkdir_action);
  412. directory_view->on_context_menu_request = [&](const GAbstractView&, const GModelIndex& index, const GContextMenuEvent& event) {
  413. if (index.is_valid()) {
  414. auto& entry = directory_view->model().entry(index.row());
  415. if (entry.is_directory()) {
  416. directory_context_menu->popup(event.screen_position());
  417. } else {
  418. file_context_menu->popup(event.screen_position());
  419. }
  420. } else {
  421. directory_view_context_menu->popup(event.screen_position());
  422. }
  423. };
  424. tree_view->on_selection_change = [&] {
  425. auto path = file_system_model->path(tree_view->selection().first());
  426. if (directory_view->path() == path)
  427. return;
  428. directory_view->open(path);
  429. copy_action->set_enabled(!tree_view->selection().is_empty());
  430. delete_action->set_enabled(!tree_view->selection().is_empty());
  431. };
  432. tree_view->on_context_menu_request = [&](const GModelIndex& index, const GContextMenuEvent& event) {
  433. if (index.is_valid()) {
  434. tree_view_directory_context_menu->popup(event.screen_position());
  435. }
  436. };
  437. // our initial location is defined as, in order of precedence:
  438. // 1. the first command-line argument (e.g. FileManager /bin)
  439. // 2. the user's home directory
  440. // 3. the root directory
  441. String initial_location;
  442. if (argc >= 2)
  443. initial_location = argv[1];
  444. if (initial_location.is_empty())
  445. initial_location = get_current_user_home_path();
  446. if (initial_location.is_empty())
  447. initial_location = "/";
  448. directory_view->open(initial_location);
  449. directory_view->set_focus(true);
  450. window->set_main_widget(widget);
  451. window->show();
  452. window->set_icon(load_png("/res/icons/16x16/filetype-folder.png"));
  453. // Read direcory read mode from config.
  454. auto dir_view_mode = config->read_entry("DirectoryView", "ViewMode", "Icon");
  455. if (dir_view_mode.contains("List")) {
  456. directory_view->set_view_mode(DirectoryView::ViewMode::List);
  457. view_as_table_action->set_checked(true);
  458. } else {
  459. directory_view->set_view_mode(DirectoryView::ViewMode::Icon);
  460. view_as_icons_action->set_checked(true);
  461. }
  462. // Write window position to config file on close request.
  463. window->on_close_request = [&] {
  464. config->write_num_entry("Window", "Left", window->x());
  465. config->write_num_entry("Window", "Top", window->y());
  466. config->write_num_entry("Window", "Width", window->width());
  467. config->write_num_entry("Window", "Heigth", window->height());
  468. config->sync();
  469. return GWindow::CloseRequestDecision::Close;
  470. };
  471. return app.exec();
  472. }