main.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2021-2022, the SerenityOS developers.
  3. * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "Tree.h"
  8. #include "TreeMapWidget.h"
  9. #include <AK/LexicalPath.h>
  10. #include <AK/String.h>
  11. #include <AK/URL.h>
  12. #include <Applications/SpaceAnalyzer/SpaceAnalyzerGML.h>
  13. #include <LibDesktop/Launcher.h>
  14. #include <LibFileSystem/FileSystem.h>
  15. #include <LibGUI/Application.h>
  16. #include <LibGUI/BoxLayout.h>
  17. #include <LibGUI/Breadcrumbbar.h>
  18. #include <LibGUI/Clipboard.h>
  19. #include <LibGUI/FileIconProvider.h>
  20. #include <LibGUI/Icon.h>
  21. #include <LibGUI/Menu.h>
  22. #include <LibGUI/Menubar.h>
  23. #include <LibGUI/MessageBox.h>
  24. #include <LibGUI/Statusbar.h>
  25. #include <LibGfx/Bitmap.h>
  26. #include <LibMain/Main.h>
  27. static constexpr auto APP_NAME = "Space Analyzer"sv;
  28. static DeprecatedString get_absolute_path_to_selected_node(SpaceAnalyzer::TreeMapWidget const& tree_map_widget, bool include_last_node = true)
  29. {
  30. StringBuilder path_builder;
  31. for (size_t k = 0; k < tree_map_widget.path_size() - (include_last_node ? 0 : 1); k++) {
  32. if (k != 0) {
  33. path_builder.append('/');
  34. }
  35. TreeNode const* node = tree_map_widget.path_node(k);
  36. path_builder.append(node->name());
  37. }
  38. return path_builder.to_deprecated_string();
  39. }
  40. ErrorOr<int> serenity_main(Main::Arguments arguments)
  41. {
  42. auto app = TRY(GUI::Application::try_create(arguments));
  43. // Configure application window.
  44. auto app_icon = GUI::Icon::default_icon("app-space-analyzer"sv);
  45. auto window = TRY(GUI::Window::try_create());
  46. window->set_title(APP_NAME);
  47. window->resize(640, 480);
  48. window->set_icon(app_icon.bitmap_for_size(16));
  49. // Load widgets.
  50. auto main_widget = TRY(window->set_main_widget<GUI::Widget>());
  51. TRY(main_widget->load_from_gml(space_analyzer_gml));
  52. auto& breadcrumbbar = *main_widget->find_descendant_of_type_named<GUI::Breadcrumbbar>("breadcrumbbar");
  53. auto& tree_map_widget = *main_widget->find_descendant_of_type_named<SpaceAnalyzer::TreeMapWidget>("tree_map");
  54. auto& statusbar = *main_widget->find_descendant_of_type_named<GUI::Statusbar>("statusbar");
  55. tree_map_widget.set_focus(true);
  56. auto file_menu = TRY(window->try_add_menu("&File"_short_string));
  57. TRY(file_menu->try_add_action(GUI::Action::create("&Analyze", { KeyCode::Key_F5 }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"sv)), [&](auto&) {
  58. // FIXME: Just modify the tree in memory instead of traversing the entire file system
  59. if (auto result = tree_map_widget.analyze(statusbar); result.is_error()) {
  60. GUI::MessageBox::show_error(window, DeprecatedString::formatted("{}", result.error()));
  61. }
  62. })));
  63. TRY(file_menu->try_add_separator());
  64. TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  65. app->quit();
  66. })));
  67. auto help_menu = TRY(window->try_add_menu("&Help"_short_string));
  68. TRY(help_menu->try_add_action(GUI::CommonActions::make_command_palette_action(window)));
  69. TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action(APP_NAME, app_icon, window)));
  70. auto open_icon = TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"sv));
  71. // Configure the node's context menu.
  72. auto open_action = GUI::Action::create("Open in File Manager", { Mod_Ctrl, Key_O }, open_icon, [&](auto&) {
  73. auto path_string = get_absolute_path_to_selected_node(tree_map_widget);
  74. if (path_string.is_empty())
  75. return;
  76. if (FileSystem::is_directory(path_string)) {
  77. Desktop::Launcher::open(URL::create_with_file_scheme(path_string));
  78. return;
  79. }
  80. LexicalPath path { path_string };
  81. Desktop::Launcher::open(URL::create_with_file_scheme(path.dirname(), path.basename()));
  82. });
  83. auto copy_icon = TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-copy.png"sv));
  84. auto copy_path_action = GUI::Action::create("Copy Path to Clipboard", { Mod_Ctrl, Key_C }, copy_icon, [&](auto&) {
  85. GUI::Clipboard::the().set_plain_text(get_absolute_path_to_selected_node(tree_map_widget));
  86. });
  87. auto delete_action = GUI::CommonActions::make_delete_action([&](auto&) {
  88. DeprecatedString selected_node_path = get_absolute_path_to_selected_node(tree_map_widget);
  89. bool try_again = true;
  90. while (try_again) {
  91. try_again = false;
  92. auto deletion_result = FileSystem::remove(selected_node_path, FileSystem::RecursionMode::Allowed);
  93. if (deletion_result.is_error()) {
  94. auto retry_message_result = GUI::MessageBox::show(window,
  95. DeprecatedString::formatted("Failed to delete \"{}\": {}. Retry?",
  96. selected_node_path,
  97. deletion_result.error()),
  98. "Deletion failed"sv,
  99. GUI::MessageBox::Type::Error,
  100. GUI::MessageBox::InputType::YesNo);
  101. if (retry_message_result == GUI::MessageBox::ExecResult::Yes) {
  102. try_again = true;
  103. }
  104. } else {
  105. GUI::MessageBox::show(window,
  106. DeprecatedString::formatted("Successfully deleted \"{}\".", selected_node_path),
  107. "Deletion completed"sv,
  108. GUI::MessageBox::Type::Information,
  109. GUI::MessageBox::InputType::OK);
  110. }
  111. }
  112. if (auto result = tree_map_widget.analyze(statusbar); result.is_error()) {
  113. GUI::MessageBox::show_error(window, DeprecatedString::formatted("{}", result.error()));
  114. }
  115. });
  116. auto context_menu = TRY(GUI::Menu::try_create());
  117. TRY(context_menu->try_add_action(open_action));
  118. TRY(context_menu->try_add_action(copy_path_action));
  119. TRY(context_menu->try_add_action(delete_action));
  120. // Configure event handlers.
  121. breadcrumbbar.on_segment_click = [&](size_t index) {
  122. VERIFY(index < tree_map_widget.path_size());
  123. tree_map_widget.set_viewpoint(index);
  124. };
  125. tree_map_widget.on_path_change = [&]() {
  126. StringBuilder builder;
  127. breadcrumbbar.clear_segments();
  128. for (size_t k = 0; k < tree_map_widget.path_size(); k++) {
  129. if (k == 0) {
  130. if (tree_map_widget.viewpoint() == 0)
  131. window->set_title("/ - SpaceAnalyzer");
  132. breadcrumbbar.append_segment("/", GUI::FileIconProvider::icon_for_path("/"sv).bitmap_for_size(16), "/", "/");
  133. continue;
  134. }
  135. const TreeNode* node = tree_map_widget.path_node(k);
  136. builder.append('/');
  137. builder.append(node->name());
  138. // Sneakily set the window title here, while the StringBuilder holds the right amount of the path.
  139. if (k == tree_map_widget.viewpoint())
  140. window->set_title(DeprecatedString::formatted("{} - SpaceAnalyzer", builder.string_view()));
  141. breadcrumbbar.append_segment(node->name(), GUI::FileIconProvider::icon_for_path(builder.string_view()).bitmap_for_size(16), builder.string_view(), builder.string_view());
  142. }
  143. breadcrumbbar.set_selected_segment(tree_map_widget.viewpoint());
  144. };
  145. tree_map_widget.on_context_menu_request = [&](const GUI::ContextMenuEvent& event) {
  146. DeprecatedString selected_node_path = get_absolute_path_to_selected_node(tree_map_widget);
  147. if (selected_node_path.is_empty())
  148. return;
  149. delete_action->set_enabled(FileSystem::can_delete_or_move(selected_node_path));
  150. if (FileSystem::is_directory(selected_node_path))
  151. open_action->set_text("Open in File Manager");
  152. else
  153. open_action->set_text("Reveal in File Manager");
  154. context_menu->popup(event.screen_position());
  155. };
  156. // At startup automatically do an analysis of root.
  157. TRY(tree_map_widget.analyze(statusbar));
  158. window->show();
  159. return app->exec();
  160. }