GJsonArrayModel.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/CFile.h>
  28. #include <LibGUI/GJsonArrayModel.h>
  29. void GJsonArrayModel::update()
  30. {
  31. auto file = CFile::construct(m_json_path);
  32. if (!file->open(CIODevice::ReadOnly)) {
  33. dbg() << "Unable to open " << file->filename();
  34. m_array.clear();
  35. did_update();
  36. return;
  37. }
  38. auto json = JsonValue::from_string(file->read_all());
  39. ASSERT(json.is_array());
  40. m_array = json.as_array();
  41. did_update();
  42. }
  43. GModel::ColumnMetadata GJsonArrayModel::column_metadata(int column) const
  44. {
  45. ASSERT(column < m_fields.size());
  46. return { 100, m_fields[column].text_alignment };
  47. }
  48. GVariant GJsonArrayModel::data(const GModelIndex& index, Role role) const
  49. {
  50. auto& field_spec = m_fields[index.column()];
  51. auto& object = m_array.at(index.row()).as_object();
  52. if (role == GModel::Role::Display) {
  53. auto& json_field_name = field_spec.json_field_name;
  54. auto data = object.get(json_field_name);
  55. if (field_spec.massage_for_display)
  56. return field_spec.massage_for_display(object);
  57. if (data.is_number())
  58. return data.to_i32();
  59. return object.get(json_field_name).to_string();
  60. }
  61. if (role == GModel::Role::Sort) {
  62. if (field_spec.massage_for_sort)
  63. return field_spec.massage_for_sort(object);
  64. return data(index, Role::Display);
  65. }
  66. if (role == GModel::Role::Custom) {
  67. if (field_spec.massage_for_custom)
  68. return field_spec.massage_for_custom(object);
  69. return {};
  70. }
  71. return {};
  72. }
  73. void GJsonArrayModel::set_json_path(const String& json_path)
  74. {
  75. if (m_json_path == json_path)
  76. return;
  77. m_json_path = json_path;
  78. update();
  79. }