2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-08-10 10:29:46 +02:00
|
|
|
#include <AK/JsonObject.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/File.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/JsonArrayModel.h>
|
2019-08-10 10:29:46 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2021-05-25 14:13:19 +00:00
|
|
|
void JsonArrayModel::invalidate()
|
2019-08-10 10:29:46 +02:00
|
|
|
{
|
2020-02-02 12:34:39 +01:00
|
|
|
auto file = Core::File::construct(m_json_path);
|
2021-05-12 13:56:43 +04:30
|
|
|
if (!file->open(Core::OpenMode::ReadOnly)) {
|
2021-01-16 15:51:56 +01:00
|
|
|
dbgln("Unable to open {}", file->filename());
|
2020-01-02 20:56:54 +01:00
|
|
|
m_array.clear();
|
|
|
|
|
did_update();
|
2019-08-10 10:29:46 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 01:46:51 +01:00
|
|
|
auto json = JsonValue::from_string(file->read_all()).release_value_but_fixme_should_propagate_errors();
|
2019-08-10 10:29:46 +02:00
|
|
|
|
2021-11-15 01:46:51 +01:00
|
|
|
VERIFY(json.is_array());
|
|
|
|
|
m_array = json.as_array();
|
2019-08-10 10:29:46 +02:00
|
|
|
|
|
|
|
|
did_update();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 08:19:38 +01:00
|
|
|
void JsonArrayModel::update()
|
|
|
|
|
{
|
|
|
|
|
auto file = Core::File::construct(m_json_path);
|
|
|
|
|
if (!file->open(Core::OpenMode::ReadOnly)) {
|
|
|
|
|
dbgln("Unable to open {}", file->filename());
|
|
|
|
|
m_array.clear();
|
|
|
|
|
did_update();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto json = JsonValue::from_string(file->read_all()).release_value_but_fixme_should_propagate_errors();
|
|
|
|
|
|
|
|
|
|
VERIFY(json.is_array());
|
|
|
|
|
m_array = json.as_array();
|
|
|
|
|
|
|
|
|
|
did_update(GUI::Model::UpdateFlag::DontInvalidateIndices);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 20:01:23 +01:00
|
|
|
bool JsonArrayModel::store()
|
|
|
|
|
{
|
|
|
|
|
auto file = Core::File::construct(m_json_path);
|
2021-05-12 13:56:43 +04:30
|
|
|
if (!file->open(Core::OpenMode::WriteOnly)) {
|
2021-01-16 15:51:56 +01:00
|
|
|
dbgln("Unable to open {}", file->filename());
|
2020-03-26 20:01:23 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file->write(m_array.to_string());
|
|
|
|
|
file->close();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool JsonArrayModel::add(Vector<JsonValue> const&& values)
|
2020-03-26 20:01:23 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(values.size() == m_fields.size());
|
2020-03-26 20:01:23 +01: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 07:30:42 -04:00
|
|
|
bool JsonArrayModel::set(int row, Vector<JsonValue>&& values)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(values.size() == m_fields.size());
|
|
|
|
|
|
2021-06-28 11:57:37 +02:00
|
|
|
if ((size_t)row >= m_array.size())
|
2021-03-25 07:30:42 -04:00
|
|
|
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 20:01:23 +01:00
|
|
|
bool JsonArrayModel::remove(int row)
|
|
|
|
|
{
|
2021-06-28 11:57:37 +02:00
|
|
|
if ((size_t)row >= m_array.size())
|
2020-03-26 20:01:23 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
JsonArray new_array;
|
2021-06-28 11:57:37 +02:00
|
|
|
for (size_t i = 0; i < m_array.size(); ++i)
|
|
|
|
|
if (i != (size_t)row)
|
2020-03-26 20:01:23 +01:00
|
|
|
new_array.append(m_array.at(i));
|
|
|
|
|
|
|
|
|
|
m_array = new_array;
|
|
|
|
|
|
|
|
|
|
did_update();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Variant JsonArrayModel::data(ModelIndex const& index, ModelRole role) const
|
2019-08-10 10:29:46 +02:00
|
|
|
{
|
2019-08-10 11:06:29 +02:00
|
|
|
auto& field_spec = m_fields[index.column()];
|
2019-08-10 10:29:46 +02:00
|
|
|
auto& object = m_array.at(index.row()).as_object();
|
|
|
|
|
|
2020-08-16 16:00:07 +02:00
|
|
|
if (role == ModelRole::TextAlignment) {
|
2020-05-21 19:36:09 +02:00
|
|
|
return field_spec.text_alignment;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 16:00:07 +02:00
|
|
|
if (role == ModelRole::Display) {
|
2019-08-10 11:06:29 +02:00
|
|
|
auto& json_field_name = field_spec.json_field_name;
|
2019-08-10 10:29:46 +02:00
|
|
|
auto data = object.get(json_field_name);
|
2019-08-10 11:06:29 +02:00
|
|
|
if (field_spec.massage_for_display)
|
2019-08-10 15:06:29 +02:00
|
|
|
return field_spec.massage_for_display(object);
|
2019-10-29 16:36:50 +01:00
|
|
|
if (data.is_number())
|
2020-09-20 21:01:08 +02:00
|
|
|
return data;
|
2019-08-10 10:29:46 +02:00
|
|
|
return object.get(json_field_name).to_string();
|
|
|
|
|
}
|
2019-08-12 11:54:18 +02:00
|
|
|
|
2020-08-16 16:00:07 +02:00
|
|
|
if (role == ModelRole::Sort) {
|
2019-08-12 11:54:18 +02:00
|
|
|
if (field_spec.massage_for_sort)
|
|
|
|
|
return field_spec.massage_for_sort(object);
|
2020-08-16 16:00:07 +02:00
|
|
|
return data(index, ModelRole::Display);
|
2019-08-12 11:54:18 +02:00
|
|
|
}
|
2019-08-14 20:30:18 +02:00
|
|
|
|
2020-08-16 16:00:07 +02:00
|
|
|
if (role == ModelRole::Custom) {
|
2019-08-14 20:30:18 +02:00
|
|
|
if (field_spec.massage_for_custom)
|
|
|
|
|
return field_spec.massage_for_custom(object);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 10:29:46 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2019-08-10 11:06:29 +02:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void JsonArrayModel::set_json_path(String const& json_path)
|
2019-08-10 11:06:29 +02:00
|
|
|
{
|
|
|
|
|
if (m_json_path == json_path)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_json_path = json_path;
|
2021-05-25 14:13:19 +00:00
|
|
|
invalidate();
|
2019-08-10 11:06:29 +02:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|