2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2020-01-24 16:45:29 +03:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-09-21 00:47:31 +03:00
|
|
|
#include "ManualModel.h"
|
2021-11-07 02:37:07 +03:00
|
|
|
#include <AK/Try.h>
|
2022-07-13 01:20:27 +03:00
|
|
|
#include <AK/Utf8View.h>
|
|
|
|
#include <LibManual/Node.h>
|
|
|
|
#include <LibManual/PageNode.h>
|
|
|
|
#include <LibManual/SectionNode.h>
|
2019-09-21 00:47:31 +03:00
|
|
|
|
|
|
|
ManualModel::ManualModel()
|
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
m_section_open_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/book-open.png"sv).release_value_but_fixme_should_propagate_errors());
|
|
|
|
m_section_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/book.png"sv).release_value_but_fixme_should_propagate_errors());
|
|
|
|
m_page_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-unknown.png"sv).release_value_but_fixme_should_propagate_errors());
|
2019-09-21 00:47:31 +03:00
|
|
|
}
|
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
Optional<GUI::ModelIndex> ManualModel::index_from_path(StringView path) const
|
2020-05-05 14:58:31 +03:00
|
|
|
{
|
|
|
|
for (int section = 0; section < row_count(); ++section) {
|
|
|
|
auto parent_index = index(section, 0);
|
|
|
|
for (int row = 0; row < row_count(parent_index); ++row) {
|
|
|
|
auto child_index = index(row, 0, parent_index);
|
2022-07-13 01:20:27 +03:00
|
|
|
auto* node = static_cast<Manual::Node const*>(child_index.internal_data());
|
2020-05-05 14:58:31 +03:00
|
|
|
if (!node->is_page())
|
|
|
|
continue;
|
2022-07-13 01:20:27 +03:00
|
|
|
auto* page = static_cast<Manual::PageNode const*>(node);
|
|
|
|
auto const maybe_path = page->path();
|
|
|
|
if (maybe_path.is_error())
|
|
|
|
return {};
|
|
|
|
if (maybe_path.value().bytes_as_string_view() != path)
|
2020-05-05 14:58:31 +03:00
|
|
|
continue;
|
|
|
|
return child_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-07-13 01:20:27 +03:00
|
|
|
Optional<String> ManualModel::page_name(const GUI::ModelIndex& index) const
|
2021-12-20 18:38:36 +03:00
|
|
|
{
|
|
|
|
if (!index.is_valid())
|
|
|
|
return {};
|
2022-07-13 01:20:27 +03:00
|
|
|
auto* node = static_cast<Manual::Node const*>(index.internal_data());
|
2021-12-20 18:38:36 +03:00
|
|
|
if (!node->is_page())
|
|
|
|
return {};
|
2022-07-13 01:20:27 +03:00
|
|
|
auto* page = static_cast<Manual::PageNode const*>(node);
|
2022-06-18 22:53:44 +03:00
|
|
|
auto path = page->name();
|
|
|
|
if (path.is_error())
|
|
|
|
return {};
|
|
|
|
return path.release_value();
|
2021-12-20 18:38:36 +03:00
|
|
|
}
|
|
|
|
|
2022-07-13 01:20:27 +03:00
|
|
|
Optional<String> ManualModel::page_path(const GUI::ModelIndex& index) const
|
2019-09-21 00:47:31 +03:00
|
|
|
{
|
|
|
|
if (!index.is_valid())
|
|
|
|
return {};
|
2022-07-13 01:20:27 +03:00
|
|
|
auto* node = static_cast<Manual::Node const*>(index.internal_data());
|
2019-09-21 00:47:31 +03:00
|
|
|
if (!node->is_page())
|
|
|
|
return {};
|
2022-07-13 01:20:27 +03:00
|
|
|
auto* page = static_cast<Manual::PageNode const*>(node);
|
|
|
|
auto path = page->path();
|
|
|
|
if (path.is_error())
|
|
|
|
return {};
|
|
|
|
return path.release_value();
|
2019-09-21 00:47:31 +03:00
|
|
|
}
|
|
|
|
|
2022-07-13 01:20:27 +03:00
|
|
|
ErrorOr<StringView> ManualModel::page_view(String const& path) const
|
2020-07-04 21:08:34 +03:00
|
|
|
{
|
|
|
|
if (path.is_empty())
|
|
|
|
return StringView {};
|
|
|
|
|
2021-01-10 17:55:54 +03:00
|
|
|
{
|
|
|
|
// Check if we've got it cached already.
|
|
|
|
auto mapped_file = m_mapped_files.get(path);
|
|
|
|
if (mapped_file.has_value())
|
|
|
|
return StringView { mapped_file.value()->bytes() };
|
|
|
|
}
|
2020-07-04 21:08:34 +03:00
|
|
|
|
2021-11-23 13:32:25 +03:00
|
|
|
auto file = TRY(Core::MappedFile::map(path));
|
2020-07-04 21:08:34 +03:00
|
|
|
|
2021-11-07 02:37:07 +03:00
|
|
|
StringView view { file->bytes() };
|
|
|
|
m_mapped_files.set(path, move(file));
|
2020-07-04 21:08:34 +03:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2022-07-13 01:20:27 +03:00
|
|
|
Optional<String> ManualModel::page_and_section(const GUI::ModelIndex& index) const
|
2019-09-21 00:47:31 +03:00
|
|
|
{
|
|
|
|
if (!index.is_valid())
|
|
|
|
return {};
|
2022-07-13 01:20:27 +03:00
|
|
|
auto* node = static_cast<Manual::Node const*>(index.internal_data());
|
2019-09-21 00:47:31 +03:00
|
|
|
if (!node->is_page())
|
|
|
|
return {};
|
2022-07-13 01:20:27 +03:00
|
|
|
auto* page = static_cast<Manual::PageNode const*>(node);
|
|
|
|
auto* section = static_cast<Manual::SectionNode const*>(page->parent());
|
|
|
|
auto page_name = page->name();
|
|
|
|
if (page_name.is_error())
|
|
|
|
return {};
|
|
|
|
auto name = String::formatted("{}({})", page_name.release_value(), section->section_name());
|
|
|
|
if (name.is_error())
|
|
|
|
return {};
|
|
|
|
return name.release_value();
|
2019-09-21 00:47:31 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
GUI::ModelIndex ManualModel::index(int row, int column, const GUI::ModelIndex& parent_index) const
|
2019-09-21 00:47:31 +03:00
|
|
|
{
|
|
|
|
if (!parent_index.is_valid())
|
2022-07-13 01:20:27 +03:00
|
|
|
return create_index(row, column, Manual::sections[row].ptr());
|
|
|
|
auto* parent = static_cast<Manual::Node const*>(parent_index.internal_data());
|
2019-09-21 00:47:31 +03:00
|
|
|
auto* child = &parent->children()[row];
|
|
|
|
return create_index(row, column, child);
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
GUI::ModelIndex ManualModel::parent_index(const GUI::ModelIndex& index) const
|
2019-09-21 00:47:31 +03:00
|
|
|
{
|
|
|
|
if (!index.is_valid())
|
|
|
|
return {};
|
2022-07-13 01:20:27 +03:00
|
|
|
auto* child = static_cast<Manual::Node const*>(index.internal_data());
|
2019-09-21 00:47:31 +03:00
|
|
|
auto* parent = child->parent();
|
|
|
|
if (parent == nullptr)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (parent->parent() == nullptr) {
|
2022-07-13 01:20:27 +03:00
|
|
|
for (size_t row = 0; row < sizeof(Manual::sections) / sizeof(Manual::sections[0]); row++)
|
|
|
|
if (Manual::sections[row].ptr() == parent)
|
2019-09-21 00:47:31 +03:00
|
|
|
return create_index(row, 0, parent);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-09-21 00:47:31 +03:00
|
|
|
}
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t row = 0; row < parent->parent()->children().size(); row++) {
|
2022-07-13 01:20:27 +03:00
|
|
|
Manual::Node* child_at_row = &parent->parent()->children()[row];
|
2019-09-21 00:47:31 +03:00
|
|
|
if (child_at_row == parent)
|
|
|
|
return create_index(row, 0, parent);
|
|
|
|
}
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-09-21 00:47:31 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
int ManualModel::row_count(const GUI::ModelIndex& index) const
|
2019-09-21 00:47:31 +03:00
|
|
|
{
|
|
|
|
if (!index.is_valid())
|
2022-07-13 01:20:27 +03:00
|
|
|
return sizeof(Manual::sections) / sizeof(Manual::sections[0]);
|
|
|
|
auto* node = static_cast<Manual::Node const*>(index.internal_data());
|
2019-09-21 00:47:31 +03:00
|
|
|
return node->children().size();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
int ManualModel::column_count(const GUI::ModelIndex&) const
|
2019-09-21 00:47:31 +03:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-08-16 17:00:07 +03:00
|
|
|
GUI::Variant ManualModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
|
2019-09-21 00:47:31 +03:00
|
|
|
{
|
2022-07-13 01:20:27 +03:00
|
|
|
auto* node = static_cast<Manual::Node const*>(index.internal_data());
|
2019-09-21 00:47:31 +03:00
|
|
|
switch (role) {
|
2020-08-16 17:00:07 +03:00
|
|
|
case GUI::ModelRole::Search:
|
2020-07-04 21:08:34 +03:00
|
|
|
if (!node->is_page())
|
|
|
|
return {};
|
2022-07-13 01:20:27 +03:00
|
|
|
if (auto path = page_path(index); path.has_value())
|
|
|
|
if (auto page = page_view(path.release_value()); !page.is_error())
|
|
|
|
// FIXME: We already provide String, but GUI::Variant still needs DeprecatedString.
|
|
|
|
return DeprecatedString(page.release_value());
|
|
|
|
return {};
|
2020-08-16 17:00:07 +03:00
|
|
|
case GUI::ModelRole::Display:
|
2022-06-18 22:53:44 +03:00
|
|
|
if (auto name = node->name(); !name.is_error())
|
|
|
|
return name.release_value();
|
|
|
|
return {};
|
2020-08-16 17:00:07 +03:00
|
|
|
case GUI::ModelRole::Icon:
|
2019-09-21 00:47:31 +03:00
|
|
|
if (node->is_page())
|
|
|
|
return m_page_icon;
|
2020-07-07 14:19:30 +03:00
|
|
|
if (node->is_open())
|
|
|
|
return m_section_open_icon;
|
2019-09-21 00:47:31 +03:00
|
|
|
return m_section_icon;
|
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void ManualModel::update_section_node_on_toggle(const GUI::ModelIndex& index, bool const open)
|
2020-07-07 14:19:30 +03:00
|
|
|
{
|
2022-07-13 01:20:27 +03:00
|
|
|
auto* node = static_cast<Manual::SectionNode*>(index.internal_data());
|
2020-07-07 14:19:30 +03:00
|
|
|
node->set_open(open);
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:08:15 +03:00
|
|
|
TriState ManualModel::data_matches(const GUI::ModelIndex& index, const GUI::Variant& term) const
|
2020-07-04 21:08:34 +03:00
|
|
|
{
|
2021-12-20 18:38:36 +03:00
|
|
|
auto name = page_name(index);
|
2022-07-13 01:20:27 +03:00
|
|
|
if (!name.has_value())
|
|
|
|
return TriState::False;
|
|
|
|
|
|
|
|
if (name.value().bytes_as_string_view().contains(term.as_string(), CaseSensitivity::CaseInsensitive))
|
2021-12-20 18:38:36 +03:00
|
|
|
return TriState::True;
|
|
|
|
|
2022-07-13 01:20:27 +03:00
|
|
|
auto path = page_path(index);
|
|
|
|
// NOTE: This is slightly inaccurate, as page_path can also fail due to OOM. We consider it acceptable to have a data mismatch in that case.
|
|
|
|
if (!path.has_value())
|
|
|
|
return TriState::False;
|
|
|
|
auto view_result = page_view(path.release_value());
|
2020-07-04 21:08:34 +03:00
|
|
|
if (view_result.is_error() || view_result.value().is_empty())
|
|
|
|
return TriState::False;
|
|
|
|
|
2022-02-28 19:25:43 +03:00
|
|
|
return view_result.value().contains(term.as_string(), CaseSensitivity::CaseInsensitive) ? TriState::True : TriState::False;
|
2020-07-04 21:08:34 +03:00
|
|
|
}
|