main.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "PDFViewerWidget.h"
  8. #include <LibFileSystemAccessClient/Client.h>
  9. #include <LibGUI/Application.h>
  10. #include <LibGUI/Icon.h>
  11. #include <LibGUI/Menubar.h>
  12. #include <LibGUI/MessageBox.h>
  13. #include <LibGUI/Window.h>
  14. int main(int argc, char** argv)
  15. {
  16. auto app = GUI::Application::construct(argc, argv);
  17. auto app_icon = GUI::Icon::default_icon("app-pdf-viewer");
  18. auto window = GUI::Window::construct();
  19. window->set_title("PDF Viewer");
  20. window->resize(640, 400);
  21. if (unveil("/res", "r") < 0) {
  22. perror("unveil");
  23. return 1;
  24. }
  25. if (unveil("/tmp/portal/filesystemaccess", "rw") < 0) {
  26. perror("unveil");
  27. return 1;
  28. }
  29. if (unveil(nullptr, nullptr) < 0) {
  30. perror("unveil");
  31. return 1;
  32. }
  33. auto& pdf_viewer_widget = window->set_main_widget<PDFViewerWidget>();
  34. pdf_viewer_widget.initialize_menubar(*window);
  35. window->show();
  36. window->set_icon(app_icon.bitmap_for_size(16));
  37. if (argc >= 2) {
  38. auto response = FileSystemAccessClient::Client::the().request_file(window->window_id(), argv[1], Core::OpenMode::ReadOnly);
  39. if (response.error != 0) {
  40. if (response.error != -1)
  41. GUI::MessageBox::show_error(window, String::formatted("Opening \"{}\" failed: {}", *response.chosen_file, strerror(response.error)));
  42. return 1;
  43. }
  44. pdf_viewer_widget.open_file(*response.fd, *response.chosen_file);
  45. }
  46. return app->exec();
  47. }