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>
|
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-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>
|
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
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
|
{
|
2021-05-13 18:20:26 -03:00
|
|
|
if (pledge("stdio recvfd sendfd rpath unix cpath wpath thread", nullptr) < 0) {
|
2020-01-13 08:22:49 -03:00
|
|
|
perror("pledge");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-04 09:05:19 -03:00
|
|
|
auto app = GUI::Application::construct(argc, argv);
|
2019-10-12 20:08:12 -03:00
|
|
|
|
2021-08-26 21:07:43 -03:00
|
|
|
Config::pledge_domains("HexEditor");
|
|
|
|
|
|
2021-02-24 18:23:56 -03:00
|
|
|
auto app_icon = GUI::Icon::default_icon("app-hex-editor");
|
2020-11-02 16:30:17 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
auto window = GUI::Window::construct();
|
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
|
|
|
|
2020-03-04 05:46:23 -03:00
|
|
|
auto& hex_editor_widget = window->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 {
|
2020-03-04 05:46:23 -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-09-02 08:03:44 -03:00
|
|
|
if (unveil("/res", "r") < 0) {
|
|
|
|
|
perror("unveil");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (unveil("/tmp/portal/filesystemaccess", "rw") < 0) {
|
|
|
|
|
perror("unveil");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (unveil(nullptr, nullptr) < 0) {
|
|
|
|
|
perror("unveil");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-21 16:21:03 -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-09-06 02:52:17 -03:00
|
|
|
if (argc > 1) {
|
|
|
|
|
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window->window_id(), argv[1]);
|
2021-09-02 08:03:44 -03:00
|
|
|
|
2021-09-06 02:52:17 -03:00
|
|
|
if (response.error != 0) {
|
|
|
|
|
if (response.error != -1)
|
|
|
|
|
GUI::MessageBox::show_error(window, String::formatted("Opening \"{}\" failed: {}", *response.chosen_file, strerror(response.error)));
|
2021-09-02 08:03:44 -03:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-06 02:52:17 -03:00
|
|
|
hex_editor_widget.open_file(*response.fd, *response.chosen_file);
|
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
|
|
|
}
|