2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-09-13 17:48:22 -03:00
|
|
|
* Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
|
2021-12-31 18:49:55 -03:00
|
|
|
* Copyright (c) 2021, Conor Byrne <conor@cbyrne.dev>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-10-12 20:08:12 -03:00
|
|
|
#include "HexEditorWidget.h"
|
2021-08-26 21:07:43 -03:00
|
|
|
#include <LibConfig/Client.h>
|
2021-12-31 18:49:55 -03:00
|
|
|
#include <LibCore/System.h>
|
2022-08-28 16:36:15 -03:00
|
|
|
#include <LibDesktop/Launcher.h>
|
2021-09-06 02:52:17 -03:00
|
|
|
#include <LibFileSystemAccessClient/Client.h>
|
2020-11-02 16:30:17 -03:00
|
|
|
#include <LibGUI/Icon.h>
|
2021-04-13 11:18:20 -03:00
|
|
|
#include <LibGUI/Menubar.h>
|
2021-09-02 08:03:44 -03:00
|
|
|
#include <LibGUI/MessageBox.h>
|
2021-12-31 18:49:55 -03:00
|
|
|
#include <LibMain/Main.h>
|
2020-01-13 08:22:49 -03:00
|
|
|
#include <stdio.h>
|
2021-03-12 13:29:37 -03:00
|
|
|
#include <unistd.h>
|
2019-10-12 20:08:12 -03:00
|
|
|
|
2021-12-31 18:49:55 -03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-10-12 20:08:12 -03:00
|
|
|
{
|
2021-12-31 18:49:55 -03:00
|
|
|
TRY(Core::System::pledge("stdio recvfd sendfd rpath unix cpath wpath thread"));
|
2020-01-13 08:22:49 -03:00
|
|
|
|
2021-12-31 18:49:55 -03:00
|
|
|
auto app = TRY(GUI::Application::try_create(arguments));
|
2019-10-12 20:08:12 -03:00
|
|
|
|
2022-08-28 16:36:15 -03:00
|
|
|
TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_protocol("/usr/share/man/man1/HexEditor.md") }));
|
|
|
|
|
TRY(Desktop::Launcher::seal_allowlist());
|
|
|
|
|
|
2022-02-11 13:33:09 -03:00
|
|
|
Config::pledge_domain("HexEditor");
|
2021-08-26 21:07:43 -03:00
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-hex-editor"sv));
|
2020-11-02 16:30:17 -03:00
|
|
|
|
2021-12-31 18:49:55 -03:00
|
|
|
auto window = TRY(GUI::Window::try_create());
|
2019-10-12 20:08:12 -03:00
|
|
|
window->set_title("Hex Editor");
|
2020-07-31 19:12:11 -03:00
|
|
|
window->resize(640, 400);
|
2019-10-12 20:08:12 -03:00
|
|
|
|
2021-12-31 18:49:55 -03:00
|
|
|
auto hex_editor_widget = TRY(window->try_set_main_widget<HexEditorWidget>());
|
2019-10-12 20:08:12 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
|
2021-12-31 18:49:55 -03:00
|
|
|
if (hex_editor_widget->request_close())
|
2020-02-02 11:07:41 -03:00
|
|
|
return GUI::Window::CloseRequestDecision::Close;
|
|
|
|
|
return GUI::Window::CloseRequestDecision::StayOpen;
|
2019-10-12 20:08:12 -03:00
|
|
|
};
|
|
|
|
|
|
2021-12-31 18:49:55 -03:00
|
|
|
TRY(Core::System::unveil("/res", "r"));
|
2022-07-24 10:28:42 -03:00
|
|
|
TRY(Core::System::unveil("/tmp/user/%uid/portal/filesystemaccess", "rw"));
|
2021-12-31 18:49:55 -03:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-09-02 08:03:44 -03:00
|
|
|
|
2021-12-31 18:49:55 -03:00
|
|
|
hex_editor_widget->initialize_menubar(*window);
|
2019-10-12 20:08:12 -03:00
|
|
|
window->show();
|
2020-11-02 16:30:17 -03:00
|
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
2019-10-12 20:08:12 -03:00
|
|
|
|
2021-12-31 18:49:55 -03:00
|
|
|
if (arguments.argc > 1) {
|
2022-01-16 05:41:34 -03:00
|
|
|
// FIXME: Using `try_request_file_read_only_approved` doesn't work here since the file stored in the editor is only readable.
|
|
|
|
|
auto response = FileSystemAccessClient::Client::the().try_request_file(window, arguments.strings[1], Core::OpenMode::ReadWrite);
|
|
|
|
|
if (response.is_error())
|
2021-09-02 08:03:44 -03:00
|
|
|
return 1;
|
2022-01-16 05:41:34 -03:00
|
|
|
hex_editor_widget->open_file(response.value());
|
2021-09-02 08:03:44 -03:00
|
|
|
}
|
2019-10-23 19:51:29 -03:00
|
|
|
|
2020-07-04 09:05:19 -03:00
|
|
|
return app->exec();
|
2019-10-12 20:08:12 -03:00
|
|
|
}
|