main.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2020-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "HelpWindow.h"
  7. #include "LibFileSystemAccessClient/Client.h"
  8. #include "Spreadsheet.h"
  9. #include "SpreadsheetWidget.h"
  10. #include <AK/ScopeGuard.h>
  11. #include <AK/Try.h>
  12. #include <LibCore/ArgsParser.h>
  13. #include <LibCore/File.h>
  14. #include <LibCore/System.h>
  15. #include <LibGUI/Application.h>
  16. #include <LibGUI/Clipboard.h>
  17. #include <LibGUI/FilePicker.h>
  18. #include <LibGUI/Icon.h>
  19. #include <LibGUI/Menu.h>
  20. #include <LibGUI/Menubar.h>
  21. #include <LibGUI/MessageBox.h>
  22. #include <LibGUI/Window.h>
  23. #include <LibMain/Main.h>
  24. #include <unistd.h>
  25. ErrorOr<int> serenity_main(Main::Arguments arguments)
  26. {
  27. TRY(Core::System::pledge("stdio recvfd sendfd rpath fattr unix cpath wpath thread"));
  28. auto app = TRY(GUI::Application::try_create(arguments));
  29. char const* filename = nullptr;
  30. Core::ArgsParser args_parser;
  31. args_parser.add_positional_argument(filename, "File to read from", "file", Core::ArgsParser::Required::No);
  32. args_parser.parse(arguments);
  33. if (filename) {
  34. if (!Core::File::exists(filename) || Core::File::is_directory(filename)) {
  35. warnln("File does not exist or is a directory: {}", filename);
  36. return 1;
  37. }
  38. }
  39. TRY(Core::System::unveil("/tmp/user/%uid/portal/webcontent", "rw"));
  40. // For writing temporary files when exporting.
  41. TRY(Core::System::unveil("/tmp", "crw"));
  42. TRY(Core::System::unveil("/etc", "r"));
  43. TRY(Core::System::unveil(Core::StandardPaths::home_directory(), "rwc"sv));
  44. TRY(Core::System::unveil("/res", "r"));
  45. TRY(Core::System::unveil(nullptr, nullptr));
  46. auto app_icon = GUI::Icon::default_icon("app-spreadsheet"sv);
  47. auto window = GUI::Window::construct();
  48. window->resize(640, 480);
  49. window->set_icon(app_icon.bitmap_for_size(16));
  50. auto& spreadsheet_widget = window->set_main_widget<Spreadsheet::SpreadsheetWidget>(*window, NonnullRefPtrVector<Spreadsheet::Sheet> {}, filename == nullptr);
  51. spreadsheet_widget.initialize_menubar(*window);
  52. spreadsheet_widget.update_window_title();
  53. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  54. if (spreadsheet_widget.request_close())
  55. return GUI::Window::CloseRequestDecision::Close;
  56. return GUI::Window::CloseRequestDecision::StayOpen;
  57. };
  58. window->show();
  59. if (filename) {
  60. auto file = TRY(FileSystemAccessClient::Client::the().try_request_file_read_only_approved(window, filename));
  61. spreadsheet_widget.load_file(file);
  62. }
  63. return app->exec();
  64. }