main.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2020-2021, 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 <LibCore/ArgsParser.h>
  12. #include <LibCore/File.h>
  13. #include <LibGUI/Application.h>
  14. #include <LibGUI/Clipboard.h>
  15. #include <LibGUI/FilePicker.h>
  16. #include <LibGUI/Icon.h>
  17. #include <LibGUI/Menu.h>
  18. #include <LibGUI/Menubar.h>
  19. #include <LibGUI/MessageBox.h>
  20. #include <LibGUI/Window.h>
  21. #include <unistd.h>
  22. int main(int argc, char* argv[])
  23. {
  24. if (pledge("stdio recvfd sendfd rpath fattr unix cpath wpath thread", nullptr) < 0) {
  25. perror("pledge");
  26. return 1;
  27. }
  28. auto app = GUI::Application::construct(argc, argv);
  29. const char* 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(argc, argv);
  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. if (unveil("/tmp/portal/webcontent", "rw") < 0) {
  40. perror("unveil");
  41. return 1;
  42. }
  43. // For writing temporary files when exporting.
  44. if (unveil("/tmp", "crw") < 0) {
  45. perror("unveil");
  46. return 1;
  47. }
  48. if (unveil("/etc", "r") < 0) {
  49. perror("unveil");
  50. return 1;
  51. }
  52. if (unveil(Core::StandardPaths::home_directory().characters(), "rwc") < 0) {
  53. perror("unveil");
  54. return 1;
  55. }
  56. if (unveil("/res", "r") < 0) {
  57. perror("unveil");
  58. return 1;
  59. }
  60. if (unveil(nullptr, nullptr) < 0) {
  61. perror("unveil");
  62. return 1;
  63. }
  64. auto app_icon = GUI::Icon::default_icon("app-spreadsheet");
  65. auto window = GUI::Window::construct();
  66. window->set_title("Spreadsheet");
  67. window->resize(640, 480);
  68. window->set_icon(app_icon.bitmap_for_size(16));
  69. auto& spreadsheet_widget = window->set_main_widget<Spreadsheet::SpreadsheetWidget>(*window, NonnullRefPtrVector<Spreadsheet::Sheet> {}, filename == nullptr);
  70. spreadsheet_widget.initialize_menubar(*window);
  71. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  72. if (spreadsheet_widget.request_close())
  73. return GUI::Window::CloseRequestDecision::Close;
  74. return GUI::Window::CloseRequestDecision::StayOpen;
  75. };
  76. window->show();
  77. if (filename) {
  78. auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window->window_id(), filename);
  79. if (response.error != 0) {
  80. if (response.error != -1)
  81. GUI::MessageBox::show_error(window, String::formatted("Opening \"{}\" failed: {}", *response.chosen_file, strerror(response.error)));
  82. return 1;
  83. }
  84. spreadsheet_widget.load_file(*response.fd, filename);
  85. }
  86. return app->exec();
  87. }