main.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "HexEditorWidget.h"
  9. #include <LibConfig/Client.h>
  10. #include <LibCore/System.h>
  11. #include <LibDesktop/Launcher.h>
  12. #include <LibFileSystemAccessClient/Client.h>
  13. #include <LibGUI/Icon.h>
  14. #include <LibGUI/Menubar.h>
  15. #include <LibGUI/MessageBox.h>
  16. #include <LibMain/Main.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. ErrorOr<int> serenity_main(Main::Arguments arguments)
  20. {
  21. TRY(Core::System::pledge("stdio recvfd sendfd rpath unix cpath wpath thread"));
  22. auto app = TRY(GUI::Application::create(arguments));
  23. TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme("/usr/share/man/man1/Applications/HexEditor.md") }));
  24. TRY(Desktop::Launcher::seal_allowlist());
  25. Config::pledge_domain("HexEditor");
  26. app->set_config_domain("HexEditor"_string);
  27. auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-hex-editor"sv));
  28. auto window = GUI::Window::construct();
  29. window->set_title("Hex Editor");
  30. window->resize(640, 400);
  31. auto hex_editor_widget = TRY(window->set_main_widget<HexEditorWidget>());
  32. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  33. if (hex_editor_widget->request_close())
  34. return GUI::Window::CloseRequestDecision::Close;
  35. return GUI::Window::CloseRequestDecision::StayOpen;
  36. };
  37. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  38. TRY(Core::System::unveil("/res", "r"));
  39. TRY(Core::System::unveil(nullptr, nullptr));
  40. TRY(hex_editor_widget->initialize_menubar(*window));
  41. window->show();
  42. window->set_icon(app_icon.bitmap_for_size(16));
  43. if (arguments.argc > 1) {
  44. // FIXME: Using `try_request_file_read_only_approved` doesn't work here since the file stored in the editor is only readable.
  45. auto response = FileSystemAccessClient::Client::the().request_file(window, arguments.strings[1], Core::File::OpenMode::ReadWrite);
  46. if (!response.is_error())
  47. hex_editor_widget->open_file(response.value().filename(), response.value().release_stream());
  48. }
  49. return app->exec();
  50. }