main.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  4. * Copyright (c) 2021, Conor Byrne <conor@cbyrne.dev>
  5. * Copyright (c) 2024, Sam Atkins <atkinssj@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "HexEditorWidget.h"
  10. #include <LibConfig/Client.h>
  11. #include <LibCore/ArgsParser.h>
  12. #include <LibCore/System.h>
  13. #include <LibDesktop/Launcher.h>
  14. #include <LibFileSystemAccessClient/Client.h>
  15. #include <LibGUI/Icon.h>
  16. #include <LibGUI/Menubar.h>
  17. #include <LibGUI/MessageBox.h>
  18. #include <LibMain/Main.h>
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. ErrorOr<int> serenity_main(Main::Arguments arguments)
  22. {
  23. TRY(Core::System::pledge("stdio recvfd sendfd rpath unix cpath wpath thread"));
  24. auto app = TRY(GUI::Application::create(arguments));
  25. StringView filename;
  26. StringView annotations_filename;
  27. Core::ArgsParser args_parser;
  28. args_parser.add_option(annotations_filename, "Annotations file to load", "annotations", 'a', "path");
  29. args_parser.add_positional_argument(filename, "File to open", "path", Core::ArgsParser::Required::No);
  30. args_parser.parse(arguments);
  31. TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme("/usr/share/man/man1/Applications/HexEditor.md") }));
  32. TRY(Desktop::Launcher::seal_allowlist());
  33. Config::pledge_domain("HexEditor");
  34. app->set_config_domain("HexEditor"_string);
  35. auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-hex-editor"sv));
  36. auto window = GUI::Window::construct();
  37. window->set_title("Hex Editor");
  38. window->restore_size_and_position("HexEditor"sv, "Window"sv, { { 640, 400 } });
  39. window->save_size_and_position_on_close("HexEditor"sv, "Window"sv);
  40. auto hex_editor_widget = TRY(HexEditor::HexEditorWidget::create());
  41. window->set_main_widget(hex_editor_widget);
  42. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  43. if (hex_editor_widget->request_close())
  44. return GUI::Window::CloseRequestDecision::Close;
  45. return GUI::Window::CloseRequestDecision::StayOpen;
  46. };
  47. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  48. TRY(Core::System::unveil("/res", "r"));
  49. TRY(Core::System::unveil(nullptr, nullptr));
  50. TRY(hex_editor_widget->initialize_menubar(*window));
  51. window->show();
  52. window->set_icon(app_icon.bitmap_for_size(16));
  53. if (!filename.is_empty()) {
  54. // FIXME: Using `try_request_file_read_only_approved` doesn't work here since the file stored in the editor is only readable.
  55. auto response = FileSystemAccessClient::Client::the().request_file(window, filename, Core::File::OpenMode::ReadWrite);
  56. if (!response.is_error())
  57. hex_editor_widget->open_file(response.value().filename(), response.value().release_stream());
  58. }
  59. if (!annotations_filename.is_empty())
  60. hex_editor_widget->open_annotations_file(annotations_filename);
  61. return app->exec();
  62. }