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