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>
|
2024-01-30 15:41:21 +03:00
|
|
|
* Copyright (c) 2024, Sam Atkins <atkinssj@serenityos.org>
|
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>
|
2024-01-30 15:41:21 +03:00
|
|
|
#include <LibCore/ArgsParser.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
|
|
|
|
2023-05-05 07:24:53 +03:00
|
|
|
auto app = TRY(GUI::Application::create(arguments));
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2024-01-30 15:41:21 +03:00
|
|
|
StringView filename;
|
2024-01-30 15:53:36 +03:00
|
|
|
StringView annotations_filename;
|
2024-01-30 15:41:21 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2024-01-30 15:53:36 +03:00
|
|
|
args_parser.add_option(annotations_filename, "Annotations file to load", "annotations", 'a', "path");
|
2024-01-30 15:41:21 +03:00
|
|
|
args_parser.add_positional_argument(filename, "File to open", "path", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.parse(arguments);
|
|
|
|
|
2023-04-08 12:48:22 +03:00
|
|
|
TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme("/usr/share/man/man1/Applications/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");
|
2023-08-07 12:12:38 +03:00
|
|
|
app->set_config_domain("HexEditor"_string);
|
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
|
|
|
|
2023-09-16 14:51:33 +03:00
|
|
|
auto window = GUI::Window::construct();
|
2019-10-13 02:08:12 +03:00
|
|
|
window->set_title("Hex Editor");
|
2023-09-21 19:05:11 +03:00
|
|
|
window->restore_size_and_position("HexEditor"sv, "Window"sv, { { 640, 400 } });
|
|
|
|
window->save_size_and_position_on_close("HexEditor"sv, "Window"sv);
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2023-09-28 18:06:49 +03:00
|
|
|
auto hex_editor_widget = TRY(HexEditor::HexEditorWidget::create());
|
|
|
|
window->set_main_widget(hex_editor_widget);
|
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
|
|
|
|
2023-04-16 15:56:27 +03:00
|
|
|
TRY(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
|
|
|
|
2024-01-30 15:41:21 +03:00
|
|
|
if (!filename.is_empty()) {
|
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.
|
2024-01-30 15:41:21 +03:00
|
|
|
auto response = FileSystemAccessClient::Client::the().request_file(window, filename, Core::File::OpenMode::ReadWrite);
|
2023-05-18 15:50:38 +03:00
|
|
|
if (!response.is_error())
|
|
|
|
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
|
|
|
|
2024-01-30 15:53:36 +03:00
|
|
|
if (!annotations_filename.is_empty())
|
|
|
|
hex_editor_widget->open_annotations_file(annotations_filename);
|
|
|
|
|
2020-07-04 15:05:19 +03:00
|
|
|
return app->exec();
|
2019-10-13 02:08:12 +03:00
|
|
|
}
|