Serenity Operating System
at portability 96 lines 3.2 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 51Model::ColumnMetadata JsonArrayModel::column_metadata(int column) const 52{ 53 ASSERT(column < static_cast<int>(m_fields.size())); 54 return { 100, m_fields[column].text_alignment }; 55} 56 57Variant JsonArrayModel::data(const ModelIndex& index, Role role) const 58{ 59 auto& field_spec = m_fields[index.column()]; 60 auto& object = m_array.at(index.row()).as_object(); 61 62 if (role == Model::Role::Display) { 63 auto& json_field_name = field_spec.json_field_name; 64 auto data = object.get(json_field_name); 65 if (field_spec.massage_for_display) 66 return field_spec.massage_for_display(object); 67 if (data.is_number()) 68 return data.to_i32(); 69 return object.get(json_field_name).to_string(); 70 } 71 72 if (role == Model::Role::Sort) { 73 if (field_spec.massage_for_sort) 74 return field_spec.massage_for_sort(object); 75 return data(index, Role::Display); 76 } 77 78 if (role == Model::Role::Custom) { 79 if (field_spec.massage_for_custom) 80 return field_spec.massage_for_custom(object); 81 return {}; 82 } 83 84 return {}; 85} 86 87void JsonArrayModel::set_json_path(const String& json_path) 88{ 89 if (m_json_path == json_path) 90 return; 91 92 m_json_path = json_path; 93 update(); 94} 95 96}