JsonArrayModel.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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 <AK/JsonObject.h>
  27. #include <LibCore/File.h>
  28. #include <LibGUI/JsonArrayModel.h>
  29. namespace GUI {
  30. void JsonArrayModel::update()
  31. {
  32. auto file = Core::File::construct(m_json_path);
  33. if (!file->open(Core::IODevice::ReadOnly)) {
  34. dbg() << "Unable to open " << file->filename();
  35. m_array.clear();
  36. did_update();
  37. return;
  38. }
  39. auto json = JsonValue::from_string(file->read_all());
  40. ASSERT(json.has_value());
  41. ASSERT(json.value().is_array());
  42. m_array = json.value().as_array();
  43. did_update();
  44. }
  45. bool JsonArrayModel::store()
  46. {
  47. auto file = Core::File::construct(m_json_path);
  48. if (!file->open(Core::IODevice::WriteOnly)) {
  49. dbg() << "Unable to open " << file->filename();
  50. return false;
  51. }
  52. file->write(m_array.to_string());
  53. file->close();
  54. return true;
  55. }
  56. bool JsonArrayModel::add(const Vector<JsonValue>&& values)
  57. {
  58. ASSERT(values.size() == m_fields.size());
  59. JsonObject obj;
  60. for (size_t i = 0; i < m_fields.size(); ++i) {
  61. auto& field_spec = m_fields[i];
  62. obj.set(field_spec.json_field_name, values.at(i));
  63. }
  64. m_array.append(move(obj));
  65. did_update();
  66. return true;
  67. }
  68. bool JsonArrayModel::remove(int row)
  69. {
  70. if (row >= m_array.size())
  71. return false;
  72. JsonArray new_array;
  73. for (int i = 0; i < m_array.size(); ++i)
  74. if (i != row)
  75. new_array.append(m_array.at(i));
  76. m_array = new_array;
  77. did_update();
  78. return true;
  79. }
  80. Variant JsonArrayModel::data(const ModelIndex& index, ModelRole role) const
  81. {
  82. auto& field_spec = m_fields[index.column()];
  83. auto& object = m_array.at(index.row()).as_object();
  84. if (role == ModelRole::TextAlignment) {
  85. return field_spec.text_alignment;
  86. }
  87. if (role == ModelRole::Display) {
  88. auto& json_field_name = field_spec.json_field_name;
  89. auto data = object.get(json_field_name);
  90. if (field_spec.massage_for_display)
  91. return field_spec.massage_for_display(object);
  92. if (data.is_number())
  93. return data.to_i32();
  94. return object.get(json_field_name).to_string();
  95. }
  96. if (role == ModelRole::Sort) {
  97. if (field_spec.massage_for_sort)
  98. return field_spec.massage_for_sort(object);
  99. return data(index, ModelRole::Display);
  100. }
  101. if (role == ModelRole::Custom) {
  102. if (field_spec.massage_for_custom)
  103. return field_spec.massage_for_custom(object);
  104. return {};
  105. }
  106. return {};
  107. }
  108. void JsonArrayModel::set_json_path(const String& json_path)
  109. {
  110. if (m_json_path == json_path)
  111. return;
  112. m_json_path = json_path;
  113. update();
  114. }
  115. }