main.cpp 10 KB

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