JsonArrayModel.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.is_array());
  41. m_array = json.as_array();
  42. did_update();
  43. }
  44. bool JsonArrayModel::store()
  45. {
  46. auto file = Core::File::construct(m_json_path);
  47. if (!file->open(Core::IODevice::WriteOnly)) {
  48. dbg() << "Unable to open " << file->filename();
  49. return false;
  50. }
  51. file->write(m_array.to_string());
  52. file->close();
  53. return true;
  54. }
  55. bool JsonArrayModel::add(const Vector<JsonValue>&& values)
  56. {
  57. ASSERT(values.size() == m_fields.size());
  58. JsonObject obj;
  59. for (size_t i = 0; i < m_fields.size(); ++i) {
  60. auto& field_spec = m_fields[i];
  61. obj.set(field_spec.json_field_name, values.at(i));
  62. }
  63. m_array.append(move(obj));
  64. did_update();
  65. return true;
  66. }
  67. bool JsonArrayModel::remove(int row)
  68. {
  69. if (row >= m_array.size())
  70. return false;
  71. JsonArray new_array;
  72. for (int i = 0; i < m_array.size(); ++i)
  73. if (i != row)
  74. new_array.append(m_array.at(i));
  75. m_array = new_array;
  76. did_update();
  77. return true;
  78. }
  79. Model::ColumnMetadata JsonArrayModel::column_metadata(int column) const
  80. {
  81. ASSERT(column < static_cast<int>(m_fields.size()));
  82. return { 100, m_fields[column].text_alignment };
  83. }
  84. Variant JsonArrayModel::data(const ModelIndex& index, Role role) const
  85. {
  86. auto& field_spec = m_fields[index.column()];
  87. auto& object = m_array.at(index.row()).as_object();
  88. if (role == Model::Role::Display) {
  89. auto& json_field_name = field_spec.json_field_name;
  90. auto data = object.get(json_field_name);
  91. if (field_spec.massage_for_display)
  92. return field_spec.massage_for_display(object);
  93. if (data.is_number())
  94. return data.to_i32();
  95. return object.get(json_field_name).to_string();
  96. }
  97. if (role == Model::Role::Sort) {
  98. if (field_spec.massage_for_sort)
  99. return field_spec.massage_for_sort(object);
  100. return data(index, Role::Display);
  101. }
  102. if (role == Model::Role::Custom) {
  103. if (field_spec.massage_for_custom)
  104. return field_spec.massage_for_custom(object);
  105. return {};
  106. }
  107. return {};
  108. }
  109. void JsonArrayModel::set_json_path(const String& json_path)
  110. {
  111. if (m_json_path == json_path)
  112. return;
  113. m_json_path = json_path;
  114. update();
  115. }
  116. }