main.cpp 2.2 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::try_create(arguments));
  23. TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_protocol("/usr/share/man/man1/HexEditor.md") }));
  24. TRY(Desktop::Launcher::seal_allowlist());
  25. Config::pledge_domain("HexEditor");
  26. auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-hex-editor"sv));
  27. auto window = TRY(GUI::Window::try_create());
  28. window->set_title("Hex Editor");
  29. window->resize(640, 400);
  30. auto hex_editor_widget = TRY(window->try_set_main_widget<HexEditorWidget>());
  31. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  32. if (hex_editor_widget->request_close())
  33. return GUI::Window::CloseRequestDecision::Close;
  34. return GUI::Window::CloseRequestDecision::StayOpen;
  35. };
  36. TRY(Core::System::unveil("/res", "r"));
  37. TRY(Core::System::unveil("/tmp/user/%uid/portal/filesystemaccess", "rw"));
  38. TRY(Core::System::unveil(nullptr, nullptr));
  39. hex_editor_widget->initialize_menubar(*window);
  40. window->show();
  41. window->set_icon(app_icon.bitmap_for_size(16));
  42. if (arguments.argc > 1) {
  43. // FIXME: Using `try_request_file_read_only_approved` doesn't work here since the file stored in the editor is only readable.
  44. auto response = FileSystemAccessClient::Client::the().try_request_file(window, arguments.strings[1], Core::OpenMode::ReadWrite);
  45. if (response.is_error())
  46. return 1;
  47. hex_editor_widget->open_file(response.value());
  48. }
  49. return app->exec();
  50. }