main.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2022, Samuel Bowman <sam@sambowman.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Applications/PartitionEditor/PartitionEditorWindowGML.h>
  7. #include <Applications/PartitionEditor/PartitionModel.h>
  8. #include <LibCore/Directory.h>
  9. #include <LibCore/System.h>
  10. #include <LibFileSystem/FileSystem.h>
  11. #include <LibGUI/Application.h>
  12. #include <LibGUI/ComboBox.h>
  13. #include <LibGUI/ItemListModel.h>
  14. #include <LibGUI/Menu.h>
  15. #include <LibGUI/MessageBox.h>
  16. #include <LibGUI/TableView.h>
  17. #include <unistd.h>
  18. static Vector<DeprecatedString> get_device_paths()
  19. {
  20. auto device_paths = Vector<DeprecatedString>();
  21. // FIXME: Propagate errors.
  22. (void)Core::Directory::for_each_entry("/dev"sv, Core::DirIterator::Flags::SkipParentAndBaseDir, [&](auto const& entry, auto const& directory) -> ErrorOr<IterationDecision> {
  23. auto full_path = LexicalPath::join(directory.path().string(), entry.name).string();
  24. if (FileSystem::is_block_device(full_path))
  25. device_paths.append(full_path);
  26. return IterationDecision::Continue;
  27. });
  28. return device_paths;
  29. }
  30. ErrorOr<int> serenity_main(Main::Arguments arguments)
  31. {
  32. TRY(Core::System::unveil("/dev", "r"));
  33. TRY(Core::System::unveil("/res", "r"));
  34. TRY(Core::System::unveil("/proc", "r"));
  35. TRY(Core::System::unveil("/tmp/session/%sid/portal/clipboard", "rw"));
  36. TRY(Core::System::unveil("/tmp/portal/window", "rw"));
  37. TRY(Core::System::unveil(nullptr, nullptr));
  38. TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
  39. auto app = TRY(GUI::Application::create(arguments));
  40. TRY(Core::System::pledge("stdio recvfd sendfd rpath"));
  41. auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-partition-editor"sv));
  42. auto window = TRY(GUI::Window::try_create());
  43. window->set_title("Partition Editor");
  44. window->resize(640, 400);
  45. window->set_icon(app_icon.bitmap_for_size(16));
  46. if (getuid() != 0) {
  47. auto error_message = "PartitionEditor must be run as root in order to open raw block devices and read partition tables."sv;
  48. GUI::MessageBox::show_error(window, error_message);
  49. return Error::from_string_view(error_message);
  50. }
  51. auto widget = TRY(window->set_main_widget<GUI::Widget>());
  52. TRY(widget->load_from_gml(partition_editor_window_gml));
  53. auto device_paths = get_device_paths();
  54. auto partition_model = PartitionEditor::PartitionModel::create();
  55. TRY(partition_model->set_device_path(device_paths.first()));
  56. auto& device_combobox = *widget->find_descendant_of_type_named<GUI::ComboBox>("device_combobox");
  57. device_combobox.set_model(GUI::ItemListModel<DeprecatedString>::create(device_paths));
  58. device_combobox.set_only_allow_values_from_model(true);
  59. device_combobox.set_selected_index(0);
  60. device_combobox.on_change = [&](auto const& path, auto const&) {
  61. auto result = partition_model->set_device_path(path);
  62. if (result.is_error())
  63. GUI::MessageBox::show_error(window, DeprecatedString::formatted("No partition table found for device {}", path));
  64. };
  65. auto& partition_table_view = *widget->find_descendant_of_type_named<GUI::TableView>("partition_table_view");
  66. partition_table_view.set_model(partition_model);
  67. partition_table_view.set_focus(true);
  68. auto file_menu = window->add_menu("&File"_string);
  69. file_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  70. app->quit();
  71. }));
  72. auto help_menu = window->add_menu("&Help"_string);
  73. help_menu->add_action(GUI::CommonActions::make_command_palette_action(window));
  74. help_menu->add_action(GUI::CommonActions::make_about_action("Partition Editor"_string, app_icon, window));
  75. window->show();
  76. return app->exec();
  77. }