main.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CharacterMapWidget.h"
  7. #include "SearchCharacters.h"
  8. #include <AK/URL.h>
  9. #include <LibConfig/Client.h>
  10. #include <LibCore/ArgsParser.h>
  11. #include <LibCore/System.h>
  12. #include <LibDesktop/Launcher.h>
  13. #include <LibGUI/Application.h>
  14. #include <LibGUI/Icon.h>
  15. #include <LibGUI/Window.h>
  16. #include <LibGfx/Font/FontDatabase.h>
  17. #include <LibMain/Main.h>
  18. static void search_and_print_results(DeprecatedString const& query)
  19. {
  20. outln("Searching for '{}'", query);
  21. u32 result_count = 0;
  22. for_each_character_containing(query, [&](auto code_point, auto display_name) {
  23. StringBuilder builder;
  24. builder.append_code_point(code_point);
  25. builder.append(" - "sv);
  26. builder.append(display_name);
  27. outln(builder.string_view());
  28. result_count++;
  29. return IterationDecision::Continue;
  30. });
  31. if (result_count == 0)
  32. outln("No results found.");
  33. else if (result_count == 1)
  34. outln("1 result found.");
  35. else
  36. outln("{} results found.", result_count);
  37. }
  38. ErrorOr<int> serenity_main(Main::Arguments arguments)
  39. {
  40. TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
  41. auto app = TRY(GUI::Application::create(arguments));
  42. Config::pledge_domain("CharacterMap");
  43. TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme("/usr/share/man/man1/Applications/CharacterMap.md") }));
  44. TRY(Desktop::Launcher::seal_allowlist());
  45. TRY(Core::System::pledge("stdio recvfd sendfd rpath"));
  46. TRY(Core::System::unveil("/res", "r"));
  47. TRY(Core::System::unveil(nullptr, nullptr));
  48. DeprecatedString query;
  49. Core::ArgsParser args_parser;
  50. args_parser.add_option(query, "Search character names using this query, and print them as a list.", "search", 's', "query");
  51. args_parser.parse(arguments);
  52. if (!query.is_empty()) {
  53. search_and_print_results(query);
  54. return 0;
  55. }
  56. auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-character-map"sv));
  57. auto window = GUI::Window::construct();
  58. window->set_title("Character Map");
  59. window->set_icon(app_icon.bitmap_for_size(16));
  60. window->resize(600, 400);
  61. auto character_map_widget = TRY(window->set_main_widget<CharacterMapWidget>());
  62. TRY(character_map_widget->initialize_menubar(*window));
  63. auto font_query = Config::read_string("CharacterMap"sv, "History"sv, "Font"sv, Gfx::FontDatabase::the().default_font_query());
  64. character_map_widget->set_font(Gfx::FontDatabase::the().get_by_name(font_query));
  65. window->show();
  66. return app->exec();
  67. }