main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Spreadsheet.h"
  27. #include "SpreadsheetWidget.h"
  28. #include <LibCore/ArgsParser.h>
  29. #include <LibCore/File.h>
  30. #include <LibGUI/Application.h>
  31. #include <LibGUI/FilePicker.h>
  32. #include <LibGUI/Forward.h>
  33. #include <LibGUI/Menu.h>
  34. #include <LibGUI/MenuBar.h>
  35. #include <LibGUI/Window.h>
  36. int main(int argc, char* argv[])
  37. {
  38. const char* filename = nullptr;
  39. Core::ArgsParser args_parser;
  40. args_parser.add_positional_argument(filename, "File to read from", "file", Core::ArgsParser::Required::No);
  41. args_parser.parse(argc, argv);
  42. if (filename) {
  43. if (!Core::File::exists(filename) || Core::File::is_directory(filename)) {
  44. fprintf(stderr, "File does not exist or is a directory: %s\n", filename);
  45. return 1;
  46. }
  47. }
  48. auto app = GUI::Application::construct(argc, argv);
  49. if (pledge("stdio thread rpath accept cpath wpath shared_buffer unix", nullptr) < 0) {
  50. perror("pledge");
  51. return 1;
  52. }
  53. if (unveil("/tmp/portal/webcontent", "rw") < 0) {
  54. perror("unveil");
  55. return 1;
  56. }
  57. if (unveil("/etc", "r") < 0) {
  58. perror("unveil");
  59. return 1;
  60. }
  61. if (unveil(Core::StandardPaths::home_directory().characters(), "rwc") < 0) {
  62. perror("unveil");
  63. return 1;
  64. }
  65. if (unveil("/res", "r") < 0) {
  66. perror("unveil");
  67. return 1;
  68. }
  69. if (unveil(nullptr, nullptr) < 0) {
  70. perror("unveil");
  71. return 1;
  72. }
  73. auto window = GUI::Window::construct();
  74. window->set_title("Spreadsheet");
  75. window->resize(640, 480);
  76. auto& spreadsheet_widget = window->set_main_widget<Spreadsheet::SpreadsheetWidget>(NonnullRefPtrVector<Spreadsheet::Sheet> {}, filename == nullptr);
  77. if (filename)
  78. spreadsheet_widget.load(filename);
  79. auto menubar = GUI::MenuBar::construct();
  80. auto& app_menu = menubar->add_menu("Spreadsheet");
  81. app_menu.add_action(GUI::Action::create("Add New Sheet", Gfx::Bitmap::load_from_file("/res/icons/16x16/new-tab.png"), [&](auto&) {
  82. spreadsheet_widget.add_sheet();
  83. }));
  84. app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  85. app->quit(0);
  86. }));
  87. auto& file_menu = menubar->add_menu("File");
  88. file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
  89. Optional<String> load_path = GUI::FilePicker::get_open_filepath(window);
  90. if (!load_path.has_value())
  91. return;
  92. spreadsheet_widget.load(load_path.value());
  93. }));
  94. file_menu.add_action(GUI::CommonActions::make_save_action([&](auto&) {
  95. if (spreadsheet_widget.current_filename().is_empty()) {
  96. String name = "sheet";
  97. Optional<String> save_path = GUI::FilePicker::get_save_filepath(window, name, "json");
  98. if (!save_path.has_value())
  99. return;
  100. spreadsheet_widget.save(save_path.value());
  101. } else {
  102. spreadsheet_widget.save(spreadsheet_widget.current_filename());
  103. }
  104. }));
  105. file_menu.add_action(GUI::CommonActions::make_save_as_action([&](auto&) {
  106. auto current_filename = spreadsheet_widget.current_filename();
  107. String name = "sheet";
  108. Optional<String> save_path = GUI::FilePicker::get_save_filepath(window, name, "json");
  109. if (!save_path.has_value())
  110. return;
  111. spreadsheet_widget.save(save_path.value());
  112. if (!current_filename.is_empty())
  113. spreadsheet_widget.set_filename(current_filename);
  114. }));
  115. app->set_menubar(move(menubar));
  116. window->show();
  117. return app->exec();
  118. }