main.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "HelpWindow.h"
  7. #include "Spreadsheet.h"
  8. #include "SpreadsheetWidget.h"
  9. #include <AK/ScopeGuard.h>
  10. #include <LibCore/ArgsParser.h>
  11. #include <LibCore/File.h>
  12. #include <LibGUI/Application.h>
  13. #include <LibGUI/Clipboard.h>
  14. #include <LibGUI/FilePicker.h>
  15. #include <LibGUI/Icon.h>
  16. #include <LibGUI/Menu.h>
  17. #include <LibGUI/Menubar.h>
  18. #include <LibGUI/MessageBox.h>
  19. #include <LibGUI/Window.h>
  20. #include <unistd.h>
  21. int main(int argc, char* argv[])
  22. {
  23. if (pledge("stdio recvfd sendfd rpath fattr unix cpath wpath thread", nullptr) < 0) {
  24. perror("pledge");
  25. return 1;
  26. }
  27. auto app = GUI::Application::construct(argc, argv);
  28. const char* filename = nullptr;
  29. Core::ArgsParser args_parser;
  30. args_parser.add_positional_argument(filename, "File to read from", "file", Core::ArgsParser::Required::No);
  31. args_parser.parse(argc, argv);
  32. if (filename) {
  33. if (!Core::File::exists(filename) || Core::File::is_directory(filename)) {
  34. warnln("File does not exist or is a directory: {}", filename);
  35. return 1;
  36. }
  37. }
  38. if (unveil("/tmp/portal/webcontent", "rw") < 0) {
  39. perror("unveil");
  40. return 1;
  41. }
  42. // For writing temporary files when exporting.
  43. if (unveil("/tmp", "crw") < 0) {
  44. perror("unveil");
  45. return 1;
  46. }
  47. if (unveil("/etc", "r") < 0) {
  48. perror("unveil");
  49. return 1;
  50. }
  51. if (unveil(Core::StandardPaths::home_directory().characters(), "rwc") < 0) {
  52. perror("unveil");
  53. return 1;
  54. }
  55. if (unveil("/res", "r") < 0) {
  56. perror("unveil");
  57. return 1;
  58. }
  59. if (unveil(nullptr, nullptr) < 0) {
  60. perror("unveil");
  61. return 1;
  62. }
  63. auto app_icon = GUI::Icon::default_icon("app-spreadsheet");
  64. auto window = GUI::Window::construct();
  65. window->set_title("Spreadsheet");
  66. window->resize(640, 480);
  67. window->set_icon(app_icon.bitmap_for_size(16));
  68. auto& spreadsheet_widget = window->set_main_widget<Spreadsheet::SpreadsheetWidget>(NonnullRefPtrVector<Spreadsheet::Sheet> {}, filename == nullptr);
  69. if (filename)
  70. spreadsheet_widget.load(filename);
  71. auto menubar = GUI::Menubar::construct();
  72. auto& file_menu = menubar->add_menu("&File");
  73. file_menu.add_action(GUI::Action::create("Add New Sheet", Gfx::Bitmap::load_from_file("/res/icons/16x16/new-tab.png"), [&](auto&) {
  74. spreadsheet_widget.add_sheet();
  75. }));
  76. file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
  77. Optional<String> load_path = GUI::FilePicker::get_open_filepath(window);
  78. if (!load_path.has_value())
  79. return;
  80. spreadsheet_widget.load(load_path.value());
  81. }));
  82. file_menu.add_action(GUI::CommonActions::make_save_action([&](auto&) {
  83. if (spreadsheet_widget.current_filename().is_empty()) {
  84. String name = "workbook";
  85. Optional<String> save_path = GUI::FilePicker::get_save_filepath(window, name, "sheets");
  86. if (!save_path.has_value())
  87. return;
  88. spreadsheet_widget.save(save_path.value());
  89. } else {
  90. spreadsheet_widget.save(spreadsheet_widget.current_filename());
  91. }
  92. }));
  93. file_menu.add_action(GUI::CommonActions::make_save_as_action([&](auto&) {
  94. auto current_filename = spreadsheet_widget.current_filename();
  95. String name = "workbook";
  96. Optional<String> save_path = GUI::FilePicker::get_save_filepath(window, name, "sheets");
  97. if (!save_path.has_value())
  98. return;
  99. spreadsheet_widget.save(save_path.value());
  100. if (!current_filename.is_empty())
  101. spreadsheet_widget.set_filename(current_filename);
  102. }));
  103. file_menu.add_separator();
  104. file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  105. if (!spreadsheet_widget.request_close())
  106. return;
  107. app->quit(0);
  108. }));
  109. window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
  110. if (spreadsheet_widget.request_close())
  111. return GUI::Window::CloseRequestDecision::Close;
  112. return GUI::Window::CloseRequestDecision::StayOpen;
  113. };
  114. auto& edit_menu = menubar->add_menu("&Edit");
  115. auto clipboard_action = [&](bool is_cut) {
  116. /// text/x-spreadsheet-data:
  117. /// - action: copy/cut
  118. /// - currently selected cell
  119. /// - selected cell+
  120. auto* worksheet_ptr = spreadsheet_widget.current_worksheet_if_available();
  121. if (!worksheet_ptr) {
  122. GUI::MessageBox::show_error(window, "There are no active worksheets");
  123. return;
  124. }
  125. auto& worksheet = *worksheet_ptr;
  126. auto& cells = worksheet.selected_cells();
  127. VERIFY(!cells.is_empty());
  128. StringBuilder text_builder, url_builder;
  129. url_builder.append(is_cut ? "cut\n" : "copy\n");
  130. bool first = true;
  131. auto cursor = spreadsheet_widget.current_selection_cursor();
  132. if (cursor) {
  133. Spreadsheet::Position position { (size_t)cursor->column(), (size_t)cursor->row() };
  134. url_builder.append(position.to_url(worksheet).to_string());
  135. url_builder.append('\n');
  136. }
  137. for (auto& cell : cells) {
  138. if (first && !cursor) {
  139. url_builder.append(cell.to_url(worksheet).to_string());
  140. url_builder.append('\n');
  141. }
  142. url_builder.append(cell.to_url(worksheet).to_string());
  143. url_builder.append('\n');
  144. auto cell_data = worksheet.at(cell);
  145. if (!first)
  146. text_builder.append('\t');
  147. if (cell_data)
  148. text_builder.append(cell_data->data());
  149. first = false;
  150. }
  151. HashMap<String, String> metadata;
  152. metadata.set("text/x-spreadsheet-data", url_builder.to_string());
  153. dbgln(url_builder.to_string());
  154. GUI::Clipboard::the().set_data(text_builder.string_view().bytes(), "text/plain", move(metadata));
  155. };
  156. edit_menu.add_action(GUI::CommonActions::make_cut_action([&](auto&) { clipboard_action(true); }, window));
  157. edit_menu.add_action(GUI::CommonActions::make_copy_action([&](auto&) { clipboard_action(false); }, window));
  158. edit_menu.add_action(GUI::CommonActions::make_paste_action([&](auto&) {
  159. ScopeGuard update_after_paste { [&] { spreadsheet_widget.update(); } };
  160. auto* worksheet_ptr = spreadsheet_widget.current_worksheet_if_available();
  161. if (!worksheet_ptr) {
  162. GUI::MessageBox::show_error(window, "There are no active worksheets");
  163. return;
  164. }
  165. auto& sheet = *worksheet_ptr;
  166. auto& cells = sheet.selected_cells();
  167. VERIFY(!cells.is_empty());
  168. const auto& data = GUI::Clipboard::the().data_and_type();
  169. if (auto spreadsheet_data = data.metadata.get("text/x-spreadsheet-data"); spreadsheet_data.has_value()) {
  170. Vector<Spreadsheet::Position> source_positions, target_positions;
  171. auto lines = spreadsheet_data.value().split_view('\n');
  172. auto action = lines.take_first();
  173. for (auto& line : lines) {
  174. dbgln("Paste line '{}'", line);
  175. auto position = sheet.position_from_url(line);
  176. if (position.has_value())
  177. source_positions.append(position.release_value());
  178. }
  179. for (auto& position : sheet.selected_cells())
  180. target_positions.append(position);
  181. if (source_positions.is_empty())
  182. return;
  183. auto first_position = source_positions.take_first();
  184. sheet.copy_cells(move(source_positions), move(target_positions), first_position, action == "cut" ? Spreadsheet::Sheet::CopyOperation::Cut : Spreadsheet::Sheet::CopyOperation::Copy);
  185. } else {
  186. for (auto& cell : sheet.selected_cells())
  187. sheet.ensure(cell).set_data(StringView { data.data.data(), data.data.size() });
  188. spreadsheet_widget.update();
  189. }
  190. },
  191. window));
  192. auto& help_menu = menubar->add_menu("&Help");
  193. help_menu.add_action(GUI::Action::create(
  194. "&Functions Help", [&](auto&) {
  195. if (auto* worksheet_ptr = spreadsheet_widget.current_worksheet_if_available()) {
  196. auto docs = worksheet_ptr->gather_documentation();
  197. auto help_window = Spreadsheet::HelpWindow::the(window);
  198. help_window->set_docs(move(docs));
  199. help_window->show();
  200. } else {
  201. GUI::MessageBox::show_error(window, "Cannot prepare documentation/help without an active worksheet");
  202. }
  203. },
  204. window));
  205. help_menu.add_action(GUI::CommonActions::make_about_action("Spreadsheet", app_icon, window));
  206. window->set_menubar(move(menubar));
  207. window->show();
  208. return app->exec();
  209. }