main.cpp 18 KB

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