main.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "PDFViewerWidget.h"
  8. #include <LibConfig/Client.h>
  9. #include <LibCore/ArgsParser.h>
  10. #include <LibCore/System.h>
  11. #include <LibFileSystemAccessClient/Client.h>
  12. #include <LibGUI/Application.h>
  13. #include <LibGUI/Icon.h>
  14. #include <LibGUI/Menubar.h>
  15. #include <LibGUI/Window.h>
  16. #include <LibMain/Main.h>
  17. ErrorOr<int> serenity_main(Main::Arguments arguments)
  18. {
  19. StringView file_path;
  20. Core::ArgsParser args_parser;
  21. args_parser.add_positional_argument(file_path, "PDF file to open", "path", Core::ArgsParser::Required::No);
  22. args_parser.parse(arguments);
  23. auto app = TRY(GUI::Application::create(arguments));
  24. auto app_icon = GUI::Icon::default_icon("app-pdf-viewer"sv);
  25. Config::pledge_domain("PDFViewer");
  26. app->set_config_domain("PDFViewer"_string);
  27. auto window = GUI::Window::construct();
  28. window->set_title("PDF Viewer");
  29. window->resize(640, 400);
  30. TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
  31. TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
  32. TRY(Core::System::unveil("/res", "r"));
  33. TRY(Core::System::unveil(nullptr, nullptr));
  34. auto pdf_viewer_widget = TRY(window->set_main_widget<PDFViewerWidget>());
  35. TRY(pdf_viewer_widget->initialize_menubar(*window));
  36. window->show();
  37. window->set_icon(app_icon.bitmap_for_size(16));
  38. if (!file_path.is_empty()) {
  39. auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, file_path);
  40. if (response.is_error())
  41. return 1;
  42. pdf_viewer_widget->open_file(response.value().filename(), response.value().release_stream());
  43. }
  44. return app->exec();
  45. }