JsonArrayModel.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. bool JsonArrayModel::store()
  25. {
  26. auto file = Core::File::construct(m_json_path);
  27. if (!file->open(Core::OpenMode::WriteOnly)) {
  28. dbgln("Unable to open {}", file->filename());
  29. return false;
  30. }
  31. file->write(m_array.to_string());
  32. file->close();
  33. return true;
  34. }
  35. bool JsonArrayModel::add(const Vector<JsonValue>&& values)
  36. {
  37. VERIFY(values.size() == m_fields.size());
  38. JsonObject obj;
  39. for (size_t i = 0; i < m_fields.size(); ++i) {
  40. auto& field_spec = m_fields[i];
  41. obj.set(field_spec.json_field_name, values.at(i));
  42. }
  43. m_array.append(move(obj));
  44. did_update();
  45. return true;
  46. }
  47. bool JsonArrayModel::set(int row, Vector<JsonValue>&& values)
  48. {
  49. VERIFY(values.size() == m_fields.size());
  50. if ((size_t)row >= m_array.size())
  51. return false;
  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, move(values.at(i)));
  56. }
  57. m_array.set(row, move(obj));
  58. did_update();
  59. return true;
  60. }
  61. bool JsonArrayModel::remove(int row)
  62. {
  63. if ((size_t)row >= m_array.size())
  64. return false;
  65. JsonArray new_array;
  66. for (size_t i = 0; i < m_array.size(); ++i)
  67. if (i != (size_t)row)
  68. new_array.append(m_array.at(i));
  69. m_array = new_array;
  70. did_update();
  71. return true;
  72. }
  73. Variant JsonArrayModel::data(const ModelIndex& index, ModelRole role) const
  74. {
  75. auto& field_spec = m_fields[index.column()];
  76. auto& object = m_array.at(index.row()).as_object();
  77. if (role == ModelRole::TextAlignment) {
  78. return field_spec.text_alignment;
  79. }
  80. if (role == ModelRole::Display) {
  81. auto& json_field_name = field_spec.json_field_name;
  82. auto data = object.get(json_field_name);
  83. if (field_spec.massage_for_display)
  84. return field_spec.massage_for_display(object);
  85. if (data.is_number())
  86. return data;
  87. return object.get(json_field_name).to_string();
  88. }
  89. if (role == ModelRole::Sort) {
  90. if (field_spec.massage_for_sort)
  91. return field_spec.massage_for_sort(object);
  92. return data(index, ModelRole::Display);
  93. }
  94. if (role == ModelRole::Custom) {
  95. if (field_spec.massage_for_custom)
  96. return field_spec.massage_for_custom(object);
  97. return {};
  98. }
  99. return {};
  100. }
  101. void JsonArrayModel::set_json_path(const String& json_path)
  102. {
  103. if (m_json_path == json_path)
  104. return;
  105. m_json_path = json_path;
  106. invalidate();
  107. }
  108. }