Workbook.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "Workbook.h"
  27. #include "JSIntegration.h"
  28. #include <AK/JsonArray.h>
  29. #include <AK/JsonObject.h>
  30. #include <AK/JsonObjectSerializer.h>
  31. #include <AK/JsonParser.h>
  32. #include <LibCore/File.h>
  33. #include <LibJS/Parser.h>
  34. #include <LibJS/Runtime/GlobalObject.h>
  35. #include <string.h>
  36. namespace Spreadsheet {
  37. Workbook::Workbook(NonnullRefPtrVector<Sheet>&& sheets)
  38. : m_sheets(move(sheets))
  39. , m_interpreter(JS::Interpreter::create<JS::GlobalObject>())
  40. {
  41. m_workbook_object = interpreter().heap().allocate<WorkbookObject>(global_object(), *this);
  42. global_object().put("workbook", workbook_object());
  43. }
  44. bool Workbook::set_filename(const String& filename)
  45. {
  46. if (m_current_filename == filename)
  47. return false;
  48. m_current_filename = filename;
  49. return true;
  50. }
  51. Result<bool, String> Workbook::load(const StringView& filename)
  52. {
  53. auto file_or_error = Core::File::open(filename, Core::IODevice::OpenMode::ReadOnly);
  54. if (file_or_error.is_error()) {
  55. StringBuilder sb;
  56. sb.append("Failed to open ");
  57. sb.append(filename);
  58. sb.append(" for reading. Error: ");
  59. sb.append(file_or_error.error());
  60. return sb.to_string();
  61. }
  62. auto json_value_option = JsonParser(file_or_error.value()->read_all()).parse();
  63. if (!json_value_option.has_value()) {
  64. StringBuilder sb;
  65. sb.append("Failed to parse ");
  66. sb.append(filename);
  67. return sb.to_string();
  68. }
  69. auto& json_value = json_value_option.value();
  70. if (!json_value.is_array()) {
  71. StringBuilder sb;
  72. sb.append("Did not find a spreadsheet in ");
  73. sb.append(filename);
  74. return sb.to_string();
  75. }
  76. NonnullRefPtrVector<Sheet> sheets;
  77. auto& json_array = json_value.as_array();
  78. json_array.for_each([&](auto& sheet_json) {
  79. if (!sheet_json.is_object())
  80. return IterationDecision::Continue;
  81. auto sheet = Sheet::from_json(sheet_json.as_object(), *this);
  82. if (!sheet)
  83. return IterationDecision::Continue;
  84. sheets.append(sheet.release_nonnull());
  85. return IterationDecision::Continue;
  86. });
  87. m_sheets.clear();
  88. m_sheets = move(sheets);
  89. set_filename(filename);
  90. return true;
  91. }
  92. Result<bool, String> Workbook::save(const StringView& filename)
  93. {
  94. JsonArray array;
  95. for (auto& sheet : m_sheets)
  96. array.append(sheet.to_json());
  97. auto file_content = array.to_string();
  98. auto file = Core::File::construct(filename);
  99. file->open(Core::IODevice::WriteOnly);
  100. if (!file->is_open()) {
  101. StringBuilder sb;
  102. sb.append("Failed to open ");
  103. sb.append(filename);
  104. sb.append(" for write. Error: ");
  105. sb.append(file->error_string());
  106. return sb.to_string();
  107. }
  108. bool result = file->write(file_content);
  109. if (!result) {
  110. int error_number = errno;
  111. StringBuilder sb;
  112. sb.append("Unable to save file. Error: ");
  113. sb.append(strerror(error_number));
  114. return sb.to_string();
  115. }
  116. set_filename(filename);
  117. return true;
  118. }
  119. }