JsonArrayModel.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. Model::ColumnMetadata JsonArrayModel::column_metadata(int column) const
  45. {
  46. ASSERT(column < static_cast<int>(m_fields.size()));
  47. return { 100, m_fields[column].text_alignment };
  48. }
  49. Variant JsonArrayModel::data(const ModelIndex& index, Role role) const
  50. {
  51. auto& field_spec = m_fields[index.column()];
  52. auto& object = m_array.at(index.row()).as_object();
  53. if (role == Model::Role::Display) {
  54. auto& json_field_name = field_spec.json_field_name;
  55. auto data = object.get(json_field_name);
  56. if (field_spec.massage_for_display)
  57. return field_spec.massage_for_display(object);
  58. if (data.is_number())
  59. return data.to_i32();
  60. return object.get(json_field_name).to_string();
  61. }
  62. if (role == Model::Role::Sort) {
  63. if (field_spec.massage_for_sort)
  64. return field_spec.massage_for_sort(object);
  65. return data(index, Role::Display);
  66. }
  67. if (role == Model::Role::Custom) {
  68. if (field_spec.massage_for_custom)
  69. return field_spec.massage_for_custom(object);
  70. return {};
  71. }
  72. return {};
  73. }
  74. void JsonArrayModel::set_json_path(const String& json_path)
  75. {
  76. if (m_json_path == json_path)
  77. return;
  78. m_json_path = json_path;
  79. update();
  80. }
  81. }