JsonArrayModel.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/JsonObject.h>
  7. #include <LibCore/File.h>
  8. #include <LibGUI/JsonArrayModel.h>
  9. namespace GUI {
  10. void JsonArrayModel::invalidate()
  11. {
  12. auto file = Core::File::construct(m_json_path);
  13. if (!file->open(Core::OpenMode::ReadOnly)) {
  14. dbgln("Unable to open {}", file->filename());
  15. m_array.clear();
  16. did_update();
  17. return;
  18. }
  19. auto json = JsonValue::from_string(file->read_all()).release_value_but_fixme_should_propagate_errors();
  20. VERIFY(json.is_array());
  21. m_array = json.as_array();
  22. did_update();
  23. }
  24. void JsonArrayModel::update()
  25. {
  26. auto file = Core::File::construct(m_json_path);
  27. if (!file->open(Core::OpenMode::ReadOnly)) {
  28. dbgln("Unable to open {}", file->filename());
  29. m_array.clear();
  30. did_update();
  31. return;
  32. }
  33. auto json = JsonValue::from_string(file->read_all()).release_value_but_fixme_should_propagate_errors();
  34. VERIFY(json.is_array());
  35. m_array = json.as_array();
  36. did_update(GUI::Model::UpdateFlag::DontInvalidateIndices);
  37. }
  38. bool JsonArrayModel::store()
  39. {
  40. auto file = Core::File::construct(m_json_path);
  41. if (!file->open(Core::OpenMode::WriteOnly)) {
  42. dbgln("Unable to open {}", file->filename());
  43. return false;
  44. }
  45. file->write(m_array.to_string());
  46. file->close();
  47. return true;
  48. }
  49. bool JsonArrayModel::add(Vector<JsonValue> const&& values)
  50. {
  51. VERIFY(values.size() == m_fields.size());
  52. JsonObject obj;
  53. for (size_t i = 0; i < m_fields.size(); ++i) {
  54. auto& field_spec = m_fields[i];
  55. obj.set(field_spec.json_field_name, values.at(i));
  56. }
  57. m_array.append(move(obj));
  58. did_update();
  59. return true;
  60. }
  61. bool JsonArrayModel::set(int row, Vector<JsonValue>&& values)
  62. {
  63. VERIFY(values.size() == m_fields.size());
  64. if ((size_t)row >= m_array.size())
  65. return false;
  66. JsonObject obj;
  67. for (size_t i = 0; i < m_fields.size(); ++i) {
  68. auto& field_spec = m_fields[i];
  69. obj.set(field_spec.json_field_name, move(values.at(i)));
  70. }
  71. m_array.set(row, move(obj));
  72. did_update();
  73. return true;
  74. }
  75. bool JsonArrayModel::remove(int row)
  76. {
  77. if ((size_t)row >= m_array.size())
  78. return false;
  79. JsonArray new_array;
  80. for (size_t i = 0; i < m_array.size(); ++i)
  81. if (i != (size_t)row)
  82. new_array.append(m_array.at(i));
  83. m_array = new_array;
  84. did_update();
  85. return true;
  86. }
  87. Variant JsonArrayModel::data(ModelIndex const& index, ModelRole role) const
  88. {
  89. auto& field_spec = m_fields[index.column()];
  90. auto& object = m_array.at(index.row()).as_object();
  91. if (role == ModelRole::TextAlignment) {
  92. return field_spec.text_alignment;
  93. }
  94. if (role == ModelRole::Display) {
  95. auto& json_field_name = field_spec.json_field_name;
  96. auto data = object.get(json_field_name);
  97. if (field_spec.massage_for_display)
  98. return field_spec.massage_for_display(object);
  99. if (data.is_number())
  100. return data;
  101. return object.get(json_field_name).to_string();
  102. }
  103. if (role == ModelRole::Sort) {
  104. if (field_spec.massage_for_sort)
  105. return field_spec.massage_for_sort(object);
  106. return data(index, ModelRole::Display);
  107. }
  108. if (role == ModelRole::Custom) {
  109. if (field_spec.massage_for_custom)
  110. return field_spec.massage_for_custom(object);
  111. return {};
  112. }
  113. return {};
  114. }
  115. void JsonArrayModel::set_json_path(String const& json_path)
  116. {
  117. if (m_json_path == json_path)
  118. return;
  119. m_json_path = json_path;
  120. invalidate();
  121. }
  122. }