Serenity Operating System
at hosted 139 lines 4.1 kB view raw
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 27#include <AK/JsonObject.h> 28#include <LibCore/File.h> 29#include <LibGUI/JsonArrayModel.h> 30 31namespace GUI { 32 33void JsonArrayModel::update() 34{ 35 auto file = Core::File::construct(m_json_path); 36 if (!file->open(Core::IODevice::ReadOnly)) { 37 dbg() << "Unable to open " << file->filename(); 38 m_array.clear(); 39 did_update(); 40 return; 41 } 42 43 auto json = JsonValue::from_string(file->read_all()); 44 45 ASSERT(json.is_array()); 46 m_array = json.as_array(); 47 48 did_update(); 49} 50 51bool JsonArrayModel::store() 52{ 53 auto file = Core::File::construct(m_json_path); 54 if (!file->open(Core::IODevice::WriteOnly)) { 55 dbg() << "Unable to open " << file->filename(); 56 return false; 57 } 58 59 file->write(m_array.to_string()); 60 file->close(); 61 return true; 62} 63 64bool JsonArrayModel::add(const Vector<JsonValue>&& values) 65{ 66 ASSERT(values.size() == m_fields.size()); 67 JsonObject obj; 68 for (size_t i = 0; i < m_fields.size(); ++i) { 69 auto& field_spec = m_fields[i]; 70 obj.set(field_spec.json_field_name, values.at(i)); 71 } 72 m_array.append(move(obj)); 73 did_update(); 74 return true; 75} 76 77bool JsonArrayModel::remove(int row) 78{ 79 if (row >= m_array.size()) 80 return false; 81 82 JsonArray new_array; 83 for (int i = 0; i < m_array.size(); ++i) 84 if (i != row) 85 new_array.append(m_array.at(i)); 86 87 m_array = new_array; 88 89 did_update(); 90 91 return true; 92} 93 94Model::ColumnMetadata JsonArrayModel::column_metadata(int column) const 95{ 96 ASSERT(column < static_cast<int>(m_fields.size())); 97 return { 100, m_fields[column].text_alignment }; 98} 99 100Variant JsonArrayModel::data(const ModelIndex& index, Role role) const 101{ 102 auto& field_spec = m_fields[index.column()]; 103 auto& object = m_array.at(index.row()).as_object(); 104 105 if (role == Model::Role::Display) { 106 auto& json_field_name = field_spec.json_field_name; 107 auto data = object.get(json_field_name); 108 if (field_spec.massage_for_display) 109 return field_spec.massage_for_display(object); 110 if (data.is_number()) 111 return data.to_i32(); 112 return object.get(json_field_name).to_string(); 113 } 114 115 if (role == Model::Role::Sort) { 116 if (field_spec.massage_for_sort) 117 return field_spec.massage_for_sort(object); 118 return data(index, Role::Display); 119 } 120 121 if (role == Model::Role::Custom) { 122 if (field_spec.massage_for_custom) 123 return field_spec.massage_for_custom(object); 124 return {}; 125 } 126 127 return {}; 128} 129 130void JsonArrayModel::set_json_path(const String& json_path) 131{ 132 if (m_json_path == json_path) 133 return; 134 135 m_json_path = json_path; 136 update(); 137} 138 139}