2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2021-01-16 17:51:56 +03:00
|
|
|
#include <AK/Debug.h>
|
2019-08-10 11:29:46 +03:00
|
|
|
#include <AK/JsonObject.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/File.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/JsonArrayModel.h>
|
2019-08-10 11:29:46 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
void JsonArrayModel::update()
|
2019-08-10 11:29:46 +03:00
|
|
|
{
|
2020-02-02 14:34:39 +03:00
|
|
|
auto file = Core::File::construct(m_json_path);
|
|
|
|
if (!file->open(Core::IODevice::ReadOnly)) {
|
2021-01-16 17:51:56 +03:00
|
|
|
dbgln("Unable to open {}", file->filename());
|
2020-01-02 22:56:54 +03:00
|
|
|
m_array.clear();
|
|
|
|
did_update();
|
2019-08-10 11:29:46 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-21 21:50:06 +03:00
|
|
|
auto json = JsonValue::from_string(file->read_all());
|
2019-08-10 11:29:46 +03:00
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(json.has_value());
|
|
|
|
VERIFY(json.value().is_array());
|
2020-06-11 07:40:27 +03:00
|
|
|
m_array = json.value().as_array();
|
2019-08-10 11:29:46 +03:00
|
|
|
|
|
|
|
did_update();
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:01:23 +03:00
|
|
|
bool JsonArrayModel::store()
|
|
|
|
{
|
|
|
|
auto file = Core::File::construct(m_json_path);
|
|
|
|
if (!file->open(Core::IODevice::WriteOnly)) {
|
2021-01-16 17:51:56 +03:00
|
|
|
dbgln("Unable to open {}", file->filename());
|
2020-03-26 22:01:23 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
file->write(m_array.to_string());
|
|
|
|
file->close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonArrayModel::add(const Vector<JsonValue>&& values)
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(values.size() == m_fields.size());
|
2020-03-26 22:01:23 +03:00
|
|
|
JsonObject obj;
|
|
|
|
for (size_t i = 0; i < m_fields.size(); ++i) {
|
|
|
|
auto& field_spec = m_fields[i];
|
|
|
|
obj.set(field_spec.json_field_name, values.at(i));
|
|
|
|
}
|
|
|
|
m_array.append(move(obj));
|
|
|
|
did_update();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:30:42 +03:00
|
|
|
bool JsonArrayModel::set(int row, Vector<JsonValue>&& values)
|
|
|
|
{
|
|
|
|
VERIFY(values.size() == m_fields.size());
|
|
|
|
|
|
|
|
if (row >= m_array.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
JsonObject obj;
|
|
|
|
for (size_t i = 0; i < m_fields.size(); ++i) {
|
|
|
|
auto& field_spec = m_fields[i];
|
|
|
|
obj.set(field_spec.json_field_name, move(values.at(i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_array.set(row, move(obj));
|
|
|
|
did_update();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:01:23 +03:00
|
|
|
bool JsonArrayModel::remove(int row)
|
|
|
|
{
|
|
|
|
if (row >= m_array.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
JsonArray new_array;
|
|
|
|
for (int i = 0; i < m_array.size(); ++i)
|
|
|
|
if (i != row)
|
|
|
|
new_array.append(m_array.at(i));
|
|
|
|
|
|
|
|
m_array = new_array;
|
|
|
|
|
|
|
|
did_update();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-16 17:00:07 +03:00
|
|
|
Variant JsonArrayModel::data(const ModelIndex& index, ModelRole role) const
|
2019-08-10 11:29:46 +03:00
|
|
|
{
|
2019-08-10 12:06:29 +03:00
|
|
|
auto& field_spec = m_fields[index.column()];
|
2019-08-10 11:29:46 +03:00
|
|
|
auto& object = m_array.at(index.row()).as_object();
|
|
|
|
|
2020-08-16 17:00:07 +03:00
|
|
|
if (role == ModelRole::TextAlignment) {
|
2020-05-21 20:36:09 +03:00
|
|
|
return field_spec.text_alignment;
|
|
|
|
}
|
|
|
|
|
2020-08-16 17:00:07 +03:00
|
|
|
if (role == ModelRole::Display) {
|
2019-08-10 12:06:29 +03:00
|
|
|
auto& json_field_name = field_spec.json_field_name;
|
2019-08-10 11:29:46 +03:00
|
|
|
auto data = object.get(json_field_name);
|
2019-08-10 12:06:29 +03:00
|
|
|
if (field_spec.massage_for_display)
|
2019-08-10 16:06:29 +03:00
|
|
|
return field_spec.massage_for_display(object);
|
2019-10-29 18:36:50 +03:00
|
|
|
if (data.is_number())
|
2020-09-20 22:01:08 +03:00
|
|
|
return data;
|
2019-08-10 11:29:46 +03:00
|
|
|
return object.get(json_field_name).to_string();
|
|
|
|
}
|
2019-08-12 12:54:18 +03:00
|
|
|
|
2020-08-16 17:00:07 +03:00
|
|
|
if (role == ModelRole::Sort) {
|
2019-08-12 12:54:18 +03:00
|
|
|
if (field_spec.massage_for_sort)
|
|
|
|
return field_spec.massage_for_sort(object);
|
2020-08-16 17:00:07 +03:00
|
|
|
return data(index, ModelRole::Display);
|
2019-08-12 12:54:18 +03:00
|
|
|
}
|
2019-08-14 21:30:18 +03:00
|
|
|
|
2020-08-16 17:00:07 +03:00
|
|
|
if (role == ModelRole::Custom) {
|
2019-08-14 21:30:18 +03:00
|
|
|
if (field_spec.massage_for_custom)
|
|
|
|
return field_spec.massage_for_custom(object);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-08-10 11:29:46 +03:00
|
|
|
return {};
|
|
|
|
}
|
2019-08-10 12:06:29 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void JsonArrayModel::set_json_path(const String& json_path)
|
2019-08-10 12:06:29 +03:00
|
|
|
{
|
|
|
|
if (m_json_path == json_path)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_json_path = json_path;
|
|
|
|
update();
|
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
|
|
|
|
}
|