main.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "MainWidget.h"
  7. #include <AK/URL.h>
  8. #include <LibConfig/Client.h>
  9. #include <LibCore/ArgsParser.h>
  10. #include <LibCore/System.h>
  11. #include <LibDesktop/Launcher.h>
  12. #include <LibFileSystemAccessClient/Client.h>
  13. #include <LibGUI/Application.h>
  14. #include <LibGUI/Icon.h>
  15. #include <LibGUI/Window.h>
  16. #include <LibMain/Main.h>
  17. ErrorOr<int> serenity_main(Main::Arguments arguments)
  18. {
  19. TRY(Core::System::pledge("stdio recvfd sendfd thread rpath unix cpath wpath"));
  20. auto app = TRY(GUI::Application::create(arguments));
  21. app->set_config_domain(TRY("FontEditor"_string));
  22. FontEditor::g_resources = FontEditor::Resources::create();
  23. TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme("/usr/share/man/man1/Applications/FontEditor.md") }));
  24. TRY(Desktop::Launcher::seal_allowlist());
  25. Config::pledge_domain("FontEditor");
  26. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  27. TRY(Core::System::unveil("/res", "r"));
  28. TRY(Core::System::unveil(nullptr, nullptr));
  29. StringView path;
  30. Core::ArgsParser args_parser;
  31. args_parser.add_positional_argument(path, "The font file for editing.", "file", Core::ArgsParser::Required::No);
  32. args_parser.parse(arguments);
  33. auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-font-editor"sv));
  34. auto window = TRY(GUI::Window::try_create());
  35. window->set_icon(app_icon.bitmap_for_size(16));
  36. window->resize(640, 470);
  37. auto font_editor = TRY(window->set_main_widget<FontEditor::MainWidget>());
  38. TRY(font_editor->initialize_menubar(*window));
  39. font_editor->reset();
  40. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  41. if (font_editor->request_close())
  42. return GUI::Window::CloseRequestDecision::Close;
  43. return GUI::Window::CloseRequestDecision::StayOpen;
  44. };
  45. window->show();
  46. auto default_path = TRY(String::from_deprecated_string(Config::read_string("FontEditor"sv, "Defaults"sv, "Font"sv, {})));
  47. auto path_to_load = path.is_empty() ? default_path : path;
  48. auto open_or_error = [&]() -> ErrorOr<void> {
  49. if (path_to_load.is_empty())
  50. return {};
  51. auto file = TRY(FileSystemAccessClient::Client::the().request_file_read_only_approved(window, path_to_load));
  52. return TRY(font_editor->open_file(path, file.release_stream()));
  53. }();
  54. if (open_or_error.is_error())
  55. font_editor->show_error(open_or_error.release_error(), "Opening"sv, path_to_load);
  56. return app->exec();
  57. }