main.cpp 8.9 KB

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