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"
|
2023-04-26 19:22:47 +03:00
|
|
|
#include <AK/FuzzyMatch.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>
|
2022-12-15 17:12:51 +03:00
|
|
|
#include <LibManual/Path.h>
|
2022-07-13 01:20:27 +03:00
|
|
|
#include <LibManual/SectionNode.h>
|
2019-09-21 00:47:31 +03:00
|
|
|
|
|
|
|
ManualModel::ManualModel()
|
|
|
|
{
|
2023-01-20 22:06:05 +03:00
|
|
|
m_section_open_icon.set_bitmap_for_size(16, Gfx::Bitmap::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::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::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
|
|
|
{
|
2022-12-15 17:12:51 +03:00
|
|
|
// The first slice removes the man pages base path plus the `/man` from the main section subdirectory.
|
|
|
|
// The second slice removes the trailing `.md`.
|
|
|
|
auto path_without_base = path.substring_view(Manual::manual_base_path.string().length() + 4);
|
|
|
|
auto url = URL::create_with_help_scheme(path_without_base.substring_view(0, path_without_base.length() - 3), {}, "man");
|
|
|
|
|
|
|
|
auto maybe_page = Manual::Node::try_find_from_help_url(url);
|
|
|
|
if (maybe_page.is_error())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto page = maybe_page.release_value();
|
|
|
|
// Main section
|
|
|
|
if (page->parent() == nullptr) {
|
|
|
|
for (size_t section = 0; section < Manual::number_of_sections; ++section) {
|
|
|
|
auto main_section_index = index(static_cast<int>(section), 0);
|
|
|
|
if (main_section_index.internal_data() == page.ptr())
|
|
|
|
return main_section_index;
|
2020-05-05 14:58:31 +03:00
|
|
|
}
|
2022-12-15 17:12:51 +03:00
|
|
|
return {};
|
2020-05-05 14:58:31 +03:00
|
|
|
}
|
2022-12-15 17:12:51 +03:00
|
|
|
auto maybe_siblings = page->parent()->children();
|
|
|
|
if (maybe_siblings.is_error())
|
|
|
|
return {};
|
|
|
|
auto siblings = maybe_siblings.release_value();
|
|
|
|
for (size_t row = 0; row < siblings.size(); ++row) {
|
|
|
|
if (siblings[row] == page)
|
|
|
|
return create_index(static_cast<int>(row), 0, page.ptr());
|
|
|
|
}
|
|
|
|
|
2020-05-05 14:58:31 +03:00
|
|
|
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 {};
|
2023-04-16 12:29:16 +03:00
|
|
|
auto const* node = static_cast<Manual::Node const*>(index.internal_data());
|
|
|
|
auto const* page = node->document();
|
|
|
|
if (!page)
|
2021-12-20 18:38:36 +03:00
|
|
|
return {};
|
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());
|
2022-12-14 19:53:41 +03:00
|
|
|
auto page = node->document();
|
|
|
|
if (!page)
|
2019-09-21 00:47:31 +03:00
|
|
|
return {};
|
2022-07-13 01:20:27 +03:00
|
|
|
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 {};
|
2023-04-16 12:29:16 +03:00
|
|
|
auto const* node = static_cast<Manual::Node const*>(index.internal_data());
|
|
|
|
auto const* page = node->document();
|
|
|
|
if (!page)
|
2019-09-21 00:47:31 +03:00
|
|
|
return {};
|
2023-04-16 12:29:16 +03:00
|
|
|
|
|
|
|
auto const* section = static_cast<Manual::SectionNode const*>(page->parent());
|
2022-07-13 01:20:27 +03:00
|
|
|
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());
|
2022-12-14 15:33:28 +03:00
|
|
|
auto const children = parent->children();
|
|
|
|
if (children.is_error())
|
|
|
|
return {};
|
|
|
|
auto child = children.value()[row];
|
|
|
|
return create_index(row, column, child.ptr());
|
2019-09-21 00:47:31 +03:00
|
|
|
}
|
|
|
|
|
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-12-14 15:23:02 +03:00
|
|
|
for (size_t row = 0; row < Manual::sections.size(); row++)
|
2022-07-13 01:20:27 +03:00
|
|
|
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
|
|
|
}
|
2022-12-14 15:33:28 +03:00
|
|
|
auto maybe_children = parent->parent()->children();
|
|
|
|
if (maybe_children.is_error())
|
|
|
|
return {};
|
|
|
|
auto children = maybe_children.release_value();
|
|
|
|
for (size_t row = 0; row < children.size(); row++) {
|
2023-02-20 21:03:59 +03:00
|
|
|
Manual::Node const* child_at_row = 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-12-14 15:23:02 +03:00
|
|
|
return static_cast<int>(Manual::sections.size());
|
2022-07-13 01:20:27 +03:00
|
|
|
auto* node = static_cast<Manual::Node const*>(index.internal_data());
|
2022-12-14 15:33:28 +03:00
|
|
|
auto maybe_children = node->children();
|
|
|
|
if (maybe_children.is_error())
|
|
|
|
return 0;
|
|
|
|
return static_cast<int>(maybe_children.value().size());
|
2019-09-21 00:47:31 +03:00
|
|
|
}
|
|
|
|
|
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-12-15 17:15:27 +03:00
|
|
|
auto* node = static_cast<Manual::Node*>(index.internal_data());
|
|
|
|
if (is<Manual::SectionNode>(*node))
|
|
|
|
static_cast<Manual::SectionNode*>(node)->set_open(open);
|
2020-07-07 14:19:30 +03:00
|
|
|
}
|
|
|
|
|
2023-04-26 19:23:08 +03:00
|
|
|
GUI::Model::MatchResult 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())
|
2023-04-26 19:23:08 +03:00
|
|
|
return { TriState::False };
|
2022-07-13 01:20:27 +03:00
|
|
|
|
2023-04-26 19:22:47 +03:00
|
|
|
auto match_result = fuzzy_match(term.as_string(), name.value());
|
|
|
|
if (match_result.score > 0)
|
2023-04-26 19:23:08 +03:00
|
|
|
return { TriState::True, match_result.score };
|
2021-12-20 18:38:36 +03:00
|
|
|
|
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())
|
2023-04-26 19:23:08 +03:00
|
|
|
return { TriState::False };
|
2022-07-13 01:20:27 +03:00
|
|
|
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())
|
2023-04-26 19:23:08 +03:00
|
|
|
return { TriState::False };
|
2020-07-04 21:08:34 +03:00
|
|
|
|
2023-04-26 19:23:08 +03:00
|
|
|
if (view_result.value().contains(term.as_string(), CaseSensitivity::CaseInsensitive))
|
|
|
|
return { TriState::True, 0 };
|
|
|
|
|
|
|
|
return { TriState::False };
|
2020-07-04 21:08:34 +03:00
|
|
|
}
|