From e92b6047ad101d071016734cb2c95993ca42d47c Mon Sep 17 00:00:00 2001 From: Conor Byrne <71222289+cbyrneee@users.noreply.github.com> Date: Fri, 31 Dec 2021 21:49:55 +0000 Subject: [PATCH] HexEditor: Port HexEditor to LibMain --- .../Applications/HexEditor/CMakeLists.txt | 2 +- Userland/Applications/HexEditor/main.cpp | 45 +++++++------------ 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/Userland/Applications/HexEditor/CMakeLists.txt b/Userland/Applications/HexEditor/CMakeLists.txt index e51ef619ec..c40ee14c21 100644 --- a/Userland/Applications/HexEditor/CMakeLists.txt +++ b/Userland/Applications/HexEditor/CMakeLists.txt @@ -21,4 +21,4 @@ set(SOURCES ) serenity_app(HexEditor ICON app-hex-editor) -target_link_libraries(HexEditor LibGUI LibConfig LibFileSystemAccessClient) +target_link_libraries(HexEditor LibGUI LibConfig LibFileSystemAccessClient LibMain) diff --git a/Userland/Applications/HexEditor/main.cpp b/Userland/Applications/HexEditor/main.cpp index 3e9154bb4a..2b05310605 100644 --- a/Userland/Applications/HexEditor/main.cpp +++ b/Userland/Applications/HexEditor/main.cpp @@ -1,65 +1,54 @@ /* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2021, Mustafa Quraish + * Copyright (c) 2021, Conor Byrne * * SPDX-License-Identifier: BSD-2-Clause */ #include "HexEditorWidget.h" #include +#include #include #include #include #include +#include #include #include -int main(int argc, char** argv) +ErrorOr serenity_main(Main::Arguments arguments) { - if (pledge("stdio recvfd sendfd rpath unix cpath wpath thread", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(Core::System::pledge("stdio recvfd sendfd rpath unix cpath wpath thread")); - auto app = GUI::Application::construct(argc, argv); + auto app = TRY(GUI::Application::try_create(arguments)); Config::pledge_domains("HexEditor"); - auto app_icon = GUI::Icon::default_icon("app-hex-editor"); + auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-hex-editor")); - auto window = GUI::Window::construct(); + auto window = TRY(GUI::Window::try_create()); window->set_title("Hex Editor"); window->resize(640, 400); - auto& hex_editor_widget = window->set_main_widget(); + auto hex_editor_widget = TRY(window->try_set_main_widget()); window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision { - if (hex_editor_widget.request_close()) + if (hex_editor_widget->request_close()) return GUI::Window::CloseRequestDecision::Close; return GUI::Window::CloseRequestDecision::StayOpen; }; - if (unveil("/res", "r") < 0) { - perror("unveil"); - return 1; - } + TRY(Core::System::unveil("/res", "r")); + TRY(Core::System::unveil("/tmp/portal/filesystemaccess", "rw")); + TRY(Core::System::unveil(nullptr, nullptr)); - if (unveil("/tmp/portal/filesystemaccess", "rw") < 0) { - perror("unveil"); - return 1; - } - - if (unveil(nullptr, nullptr) < 0) { - perror("unveil"); - return 1; - } - - hex_editor_widget.initialize_menubar(*window); + hex_editor_widget->initialize_menubar(*window); window->show(); window->set_icon(app_icon.bitmap_for_size(16)); - if (argc > 1) { - auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window->window_id(), argv[1]); + if (arguments.argc > 1) { + auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window->window_id(), arguments.strings[1]); if (response.error != 0) { if (response.error != -1) @@ -67,7 +56,7 @@ int main(int argc, char** argv) return 1; } - hex_editor_widget.open_file(*response.fd, *response.chosen_file); + hex_editor_widget->open_file(*response.fd, *response.chosen_file); } return app->exec();