2022-04-02 00:14:04 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Valtteri Koskivuori <vkoskiv@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2022-05-07 22:45:43 +02:00
|
|
|
#include "StorageModel.h"
|
2022-04-02 00:14:04 +03:00
|
|
|
|
2022-05-06 20:30:39 +02:00
|
|
|
#include <AK/FuzzyMatch.h>
|
|
|
|
|
2022-04-02 00:14:04 +03:00
|
|
|
namespace Browser {
|
|
|
|
|
2023-08-26 16:30:02 +12:00
|
|
|
void StorageModel::set_items(OrderedHashMap<String, String> map)
|
2022-04-02 00:14:04 +03:00
|
|
|
{
|
|
|
|
begin_insert_rows({}, m_local_storage_entries.size(), m_local_storage_entries.size());
|
2023-08-26 16:30:02 +12:00
|
|
|
m_local_storage_entries = move(map);
|
2022-04-02 00:14:04 +03:00
|
|
|
end_insert_rows();
|
|
|
|
|
|
|
|
did_update(DontInvalidateIndices);
|
|
|
|
}
|
|
|
|
|
2022-05-07 22:45:43 +02:00
|
|
|
void StorageModel::clear_items()
|
2022-04-02 00:14:04 +03:00
|
|
|
{
|
|
|
|
begin_insert_rows({}, m_local_storage_entries.size(), m_local_storage_entries.size());
|
|
|
|
m_local_storage_entries.clear();
|
|
|
|
end_insert_rows();
|
|
|
|
|
|
|
|
did_update(DontInvalidateIndices);
|
|
|
|
}
|
|
|
|
|
2022-05-07 22:45:43 +02:00
|
|
|
int StorageModel::row_count(GUI::ModelIndex const& index) const
|
2022-05-06 20:30:39 +02:00
|
|
|
{
|
|
|
|
if (!index.is_valid())
|
|
|
|
return m_local_storage_entries.size();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-13 16:30:15 +01:00
|
|
|
ErrorOr<String> StorageModel::column_name(int column) const
|
2022-04-02 00:14:04 +03:00
|
|
|
{
|
|
|
|
switch (column) {
|
|
|
|
case Column::Key:
|
2023-08-07 22:26:17 -04:00
|
|
|
return "Key"_string;
|
2022-04-02 00:14:04 +03:00
|
|
|
case Column::Value:
|
2023-08-07 22:26:17 -04:00
|
|
|
return "Value"_string;
|
2022-04-02 00:14:04 +03:00
|
|
|
case Column::__Count:
|
2023-06-13 16:30:15 +01:00
|
|
|
return String {};
|
2022-04-02 00:14:04 +03:00
|
|
|
}
|
|
|
|
|
2023-06-13 16:30:15 +01:00
|
|
|
return String {};
|
2022-04-02 00:14:04 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 22:45:43 +02:00
|
|
|
GUI::ModelIndex StorageModel::index(int row, int column, GUI::ModelIndex const&) const
|
2022-04-02 00:14:04 +03:00
|
|
|
{
|
|
|
|
if (static_cast<size_t>(row) < m_local_storage_entries.size())
|
|
|
|
return create_index(row, column, NULL);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-05-07 22:45:43 +02:00
|
|
|
GUI::Variant StorageModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
|
2022-04-02 00:14:04 +03:00
|
|
|
{
|
|
|
|
if (role != GUI::ModelRole::Display)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto const& keys = m_local_storage_entries.keys();
|
|
|
|
auto const& local_storage_key = keys[index.row()];
|
|
|
|
auto const& local_storage_value = m_local_storage_entries.get(local_storage_key).value_or({});
|
|
|
|
|
|
|
|
switch (index.column()) {
|
|
|
|
case Column::Key:
|
|
|
|
return local_storage_key;
|
|
|
|
case Column::Value:
|
|
|
|
return local_storage_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2023-04-26 17:23:08 +01:00
|
|
|
GUI::Model::MatchResult StorageModel::data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const
|
2022-05-06 20:30:39 +02:00
|
|
|
{
|
|
|
|
auto needle = term.as_string();
|
|
|
|
if (needle.is_empty())
|
2023-04-26 17:23:08 +01:00
|
|
|
return { TriState::True };
|
2022-05-06 20:30:39 +02:00
|
|
|
|
|
|
|
auto const& keys = m_local_storage_entries.keys();
|
|
|
|
auto const& local_storage_key = keys[index.row()];
|
|
|
|
auto const& local_storage_value = m_local_storage_entries.get(local_storage_key).value_or({});
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
auto haystack = DeprecatedString::formatted("{} {}", local_storage_key, local_storage_value);
|
2023-04-26 17:23:08 +01:00
|
|
|
auto match_result = fuzzy_match(needle, haystack);
|
|
|
|
if (match_result.score > 0)
|
|
|
|
return { TriState::True, match_result.score };
|
|
|
|
return { TriState::False };
|
2022-05-06 20:30:39 +02:00
|
|
|
}
|
|
|
|
|
2022-04-02 00:14:04 +03:00
|
|
|
}
|