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