main.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. tree_view->on_selection_change = [&] {
  79. auto path = file_system_model->path(tree_view->selection().first());
  80. if (directory_view->path() == path)
  81. return;
  82. directory_view->open(path);
  83. };
  84. auto refresh_tree_view = [&] {
  85. file_system_model->update();
  86. auto current_path = directory_view->path();
  87. // not exactly sure why i have to reselect the root node first, but the index() fails if I dont
  88. auto root_index = file_system_model->index(file_system_model->root_path());
  89. tree_view->selection().set(root_index);
  90. // reselect the existing folder in the tree
  91. auto new_index = file_system_model->index(current_path);
  92. tree_view->selection().set(new_index);
  93. tree_view->scroll_into_view(new_index, Orientation::Vertical);
  94. tree_view->update();
  95. directory_view->refresh();
  96. };
  97. 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&) {
  98. directory_view->open_parent_directory();
  99. });
  100. auto mkdir_action = GAction::create("New directory...", GraphicsBitmap::load_from_file("/res/icons/16x16/mkdir.png"), [&](const GAction&) {
  101. auto input_box = GInputBox::construct("Enter name:", "New directory", window);
  102. if (input_box->exec() == GInputBox::ExecOK && !input_box->text_value().is_empty()) {
  103. auto new_dir_path = canonicalized_path(
  104. String::format("%s/%s",
  105. directory_view->path().characters(),
  106. input_box->text_value().characters()));
  107. int rc = mkdir(new_dir_path.characters(), 0777);
  108. if (rc < 0) {
  109. GMessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window);
  110. } else {
  111. refresh_tree_view();
  112. }
  113. }
  114. });
  115. RefPtr<GAction> view_as_table_action;
  116. RefPtr<GAction> view_as_icons_action;
  117. 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&) {
  118. directory_view->set_view_mode(DirectoryView::ViewMode::List);
  119. view_as_table_action->set_checked(true);
  120. config->write_entry("DirectoryView", "ViewMode", "List");
  121. config->sync();
  122. });
  123. view_as_table_action->set_checkable(true);
  124. 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&) {
  125. directory_view->set_view_mode(DirectoryView::ViewMode::Icon);
  126. view_as_icons_action->set_checked(true);
  127. config->write_entry("DirectoryView", "ViewMode", "Icon");
  128. config->sync();
  129. });
  130. view_as_icons_action->set_checkable(true);
  131. auto view_type_action_group = make<GActionGroup>();
  132. view_type_action_group->set_exclusive(true);
  133. view_type_action_group->add_action(*view_as_table_action);
  134. view_type_action_group->add_action(*view_as_icons_action);
  135. auto selected_file_paths = [&] {
  136. Vector<String> paths;
  137. auto& view = directory_view->current_view();
  138. auto& model = *view.model();
  139. view.selection().for_each_index([&](const GModelIndex& index) {
  140. auto name_index = model.index(index.row(), GDirectoryModel::Column::Name);
  141. auto path = model.data(name_index, GModel::Role::Custom).to_string();
  142. paths.append(path);
  143. });
  144. return paths;
  145. };
  146. auto copy_action = GCommonActions::make_copy_action([&](const GAction&) {
  147. auto paths = selected_file_paths();
  148. if (paths.is_empty())
  149. return;
  150. StringBuilder copy_text;
  151. for (auto& path : paths) {
  152. copy_text.appendf("%s\n", path.characters());
  153. }
  154. GClipboard::the().set_data(copy_text.build(), "file-list");
  155. });
  156. copy_action->set_enabled(false);
  157. auto paste_action = GCommonActions::make_paste_action([&](const GAction&) {
  158. auto data_and_type = GClipboard::the().data_and_type();
  159. if (data_and_type.type != "file-list") {
  160. dbg() << "Cannot paste clipboard type " << data_and_type.type;
  161. return;
  162. }
  163. auto copied_lines = data_and_type.data.split('\n');
  164. if (copied_lines.is_empty()) {
  165. dbg() << "No files to paste";
  166. return;
  167. }
  168. for (auto& current_path : copied_lines) {
  169. if (current_path.is_empty())
  170. continue;
  171. auto current_directory = directory_view->path();
  172. auto new_path = String::format("%s/%s",
  173. current_directory.characters(),
  174. FileSystemPath(current_path).basename().characters());
  175. if (!FileUtils::copy_file_or_directory(current_path, new_path)) {
  176. auto error_message = String::format("Could not paste %s.",
  177. current_path.characters());
  178. GMessageBox::show(error_message, "File Manager", GMessageBox::Type::Error);
  179. }
  180. }
  181. });
  182. paste_action->set_enabled(GClipboard::the().type() == "file-list");
  183. GClipboard::the().on_content_change = [&](const String& data_type) {
  184. paste_action->set_enabled(data_type == "file-list");
  185. };
  186. auto properties_action
  187. = GAction::create("Properties...", { Mod_Alt, Key_Return }, GraphicsBitmap::load_from_file("/res/icons/16x16/properties.png"), [&](auto&) {
  188. auto& model = directory_view->model();
  189. auto selected = selected_file_paths();
  190. RefPtr<PropertiesDialog> properties;
  191. if (selected.is_empty()) {
  192. properties = PropertiesDialog::construct(model, directory_view->path(), true, window);
  193. } else {
  194. properties = PropertiesDialog::construct(model, selected.first(), false, window);
  195. }
  196. properties->exec();
  197. });
  198. enum class ConfirmBeforeDelete { No,
  199. Yes };
  200. auto do_delete = [&](ConfirmBeforeDelete confirm) {
  201. auto paths = selected_file_paths();
  202. if (paths.is_empty())
  203. return;
  204. {
  205. String message;
  206. if (paths.size() == 1) {
  207. message = String::format("Really delete %s?", FileSystemPath(paths[0]).basename().characters());
  208. } else {
  209. message = String::format("Really delete %d files?", paths.size());
  210. }
  211. if (confirm == ConfirmBeforeDelete::Yes) {
  212. auto result = GMessageBox::show(
  213. message,
  214. "Confirm deletion",
  215. GMessageBox::Type::Warning,
  216. GMessageBox::InputType::OKCancel,
  217. window);
  218. if (result == GMessageBox::ExecCancel)
  219. return;
  220. }
  221. }
  222. for (auto& path : paths) {
  223. struct stat st;
  224. if (lstat(path.characters(), &st)) {
  225. GMessageBox::show(
  226. String::format("lstat(%s) failed: %s", path.characters(), strerror(errno)),
  227. "Delete failed",
  228. GMessageBox::Type::Error,
  229. GMessageBox::InputType::OK,
  230. window);
  231. break;
  232. } else {
  233. refresh_tree_view();
  234. }
  235. if (S_ISDIR(st.st_mode)) {
  236. String error_path;
  237. int error = FileUtils::delete_directory(path, error_path);
  238. if (error) {
  239. GMessageBox::show(
  240. String::format("Failed to delete directory \"%s\": %s", error_path.characters(), strerror(error)),
  241. "Delete failed",
  242. GMessageBox::Type::Error,
  243. GMessageBox::InputType::OK,
  244. window);
  245. break;
  246. } else {
  247. refresh_tree_view();
  248. }
  249. } else if (unlink(path.characters()) < 0) {
  250. int saved_errno = errno;
  251. GMessageBox::show(
  252. String::format("unlink(%s) failed: %s", path.characters(), strerror(saved_errno)),
  253. "Delete failed",
  254. GMessageBox::Type::Error,
  255. GMessageBox::InputType::OK,
  256. window);
  257. break;
  258. }
  259. }
  260. };
  261. auto force_delete_action = GAction::create("Delete without confirmation", { Mod_Shift, Key_Delete }, [&](const GAction&) {
  262. do_delete(ConfirmBeforeDelete::No);
  263. });
  264. auto delete_action = GCommonActions::make_delete_action([&](const GAction&) {
  265. do_delete(ConfirmBeforeDelete::Yes);
  266. });
  267. delete_action->set_enabled(false);
  268. auto go_back_action = GCommonActions::make_go_back_action([&](auto&) {
  269. directory_view->open_previous_directory();
  270. });
  271. auto go_forward_action = GCommonActions::make_go_forward_action([&](auto&) {
  272. directory_view->open_next_directory();
  273. });
  274. auto go_home_action = GCommonActions::make_go_home_action([&](auto&) {
  275. directory_view->open(get_current_user_home_path());
  276. });
  277. auto menubar = make<GMenuBar>();
  278. auto app_menu = GMenu::construct("File Manager");
  279. app_menu->add_action(mkdir_action);
  280. app_menu->add_action(copy_action);
  281. app_menu->add_action(paste_action);
  282. app_menu->add_action(delete_action);
  283. app_menu->add_separator();
  284. app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
  285. GApplication::the().quit(0);
  286. }));
  287. menubar->add_menu(move(app_menu));
  288. auto view_menu = GMenu::construct("View");
  289. view_menu->add_action(*view_as_icons_action);
  290. view_menu->add_action(*view_as_table_action);
  291. menubar->add_menu(move(view_menu));
  292. auto go_menu = GMenu::construct("Go");
  293. go_menu->add_action(go_back_action);
  294. go_menu->add_action(go_forward_action);
  295. go_menu->add_action(open_parent_directory_action);
  296. go_menu->add_action(go_home_action);
  297. menubar->add_menu(move(go_menu));
  298. auto help_menu = GMenu::construct("Help");
  299. help_menu->add_action(GAction::create("About", [](const GAction&) {
  300. dbgprintf("FIXME: Implement Help/About\n");
  301. }));
  302. menubar->add_menu(move(help_menu));
  303. app.set_menubar(move(menubar));
  304. main_toolbar->add_action(go_back_action);
  305. main_toolbar->add_action(go_forward_action);
  306. main_toolbar->add_action(open_parent_directory_action);
  307. main_toolbar->add_action(go_home_action);
  308. main_toolbar->add_separator();
  309. main_toolbar->add_action(mkdir_action);
  310. main_toolbar->add_action(copy_action);
  311. main_toolbar->add_action(paste_action);
  312. main_toolbar->add_action(delete_action);
  313. main_toolbar->add_separator();
  314. main_toolbar->add_action(*view_as_icons_action);
  315. main_toolbar->add_action(*view_as_table_action);
  316. directory_view->on_path_change = [&](const String& new_path) {
  317. window->set_title(String::format("File Manager: %s", new_path.characters()));
  318. location_textbox->set_text(new_path);
  319. auto new_index = file_system_model->index(new_path);
  320. tree_view->selection().set(new_index);
  321. tree_view->scroll_into_view(new_index, Orientation::Vertical);
  322. tree_view->update();
  323. go_forward_action->set_enabled(directory_view->path_history_position()
  324. < directory_view->path_history_size() - 1);
  325. go_back_action->set_enabled(directory_view->path_history_position() > 0);
  326. };
  327. directory_view->on_status_message = [&](const StringView& message) {
  328. statusbar->set_text(message);
  329. };
  330. directory_view->on_thumbnail_progress = [&](int done, int total) {
  331. if (done == total) {
  332. progressbar->set_visible(false);
  333. return;
  334. }
  335. progressbar->set_range(0, total);
  336. progressbar->set_value(done);
  337. progressbar->set_visible(true);
  338. };
  339. directory_view->on_selection_change = [&](GAbstractView& view) {
  340. // FIXME: Figure out how we can enable/disable the paste action, based on clipboard contents.
  341. copy_action->set_enabled(!view.selection().is_empty());
  342. delete_action->set_enabled(!view.selection().is_empty());
  343. };
  344. auto open_in_text_editor_action = GAction::create("Open in TextEditor...", GraphicsBitmap::load_from_file("/res/icons/TextEditor16.png"), [&](auto&) {
  345. for (auto& path : selected_file_paths()) {
  346. if (!fork()) {
  347. int rc = execl("/bin/TextEditor", "TextEditor", path.characters(), nullptr);
  348. if (rc < 0)
  349. perror("execl");
  350. exit(1);
  351. }
  352. }
  353. });
  354. auto directory_context_menu = GMenu::construct();
  355. directory_context_menu->add_action(copy_action);
  356. directory_context_menu->add_action(paste_action);
  357. directory_context_menu->add_action(delete_action);
  358. directory_context_menu->add_separator();
  359. directory_context_menu->add_action(properties_action);
  360. auto file_context_menu = GMenu::construct();
  361. file_context_menu->add_action(copy_action);
  362. file_context_menu->add_action(paste_action);
  363. file_context_menu->add_action(delete_action);
  364. file_context_menu->add_separator();
  365. file_context_menu->add_action(open_in_text_editor_action);
  366. file_context_menu->add_separator();
  367. file_context_menu->add_action(properties_action);
  368. auto directory_view_context_menu = GMenu::construct();
  369. directory_view_context_menu->add_action(mkdir_action);
  370. directory_view->on_context_menu_request = [&](const GAbstractView&, const GModelIndex& index, const GContextMenuEvent& event) {
  371. if (index.is_valid()) {
  372. auto& entry = directory_view->model().entry(index.row());
  373. if (entry.is_directory()) {
  374. directory_context_menu->popup(event.screen_position());
  375. } else {
  376. file_context_menu->popup(event.screen_position());
  377. }
  378. } else {
  379. directory_view_context_menu->popup(event.screen_position());
  380. }
  381. };
  382. // our initial location is defined as, in order of precedence:
  383. // 1. the first command-line argument (e.g. FileManager /bin)
  384. // 2. the user's home directory
  385. // 3. the root directory
  386. String initial_location;
  387. if (argc >= 2)
  388. initial_location = argv[1];
  389. if (initial_location.is_empty())
  390. initial_location = get_current_user_home_path();
  391. if (initial_location.is_empty())
  392. initial_location = "/";
  393. directory_view->open(initial_location);
  394. directory_view->set_focus(true);
  395. window->set_main_widget(widget);
  396. window->show();
  397. window->set_icon(load_png("/res/icons/16x16/filetype-folder.png"));
  398. // Read direcory read mode from config.
  399. auto dir_view_mode = config->read_entry("DirectoryView", "ViewMode", "Icon");
  400. if (dir_view_mode.contains("List")) {
  401. directory_view->set_view_mode(DirectoryView::ViewMode::List);
  402. view_as_table_action->set_checked(true);
  403. } else {
  404. directory_view->set_view_mode(DirectoryView::ViewMode::Icon);
  405. view_as_icons_action->set_checked(true);
  406. }
  407. // Write window position to config file on close request.
  408. window->on_close_request = [&] {
  409. config->write_num_entry("Window", "Left", window->x());
  410. config->write_num_entry("Window", "Top", window->y());
  411. config->write_num_entry("Window", "Width", window->width());
  412. config->write_num_entry("Window", "Heigth", window->height());
  413. config->sync();
  414. return GWindow::CloseRequestDecision::Close;
  415. };
  416. return app.exec();
  417. }