JsonArrayModel.cpp 3.2 KB

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