ManualModel.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ManualModel.h"
  7. #include <AK/Try.h>
  8. #include <AK/Utf8View.h>
  9. #include <LibManual/Node.h>
  10. #include <LibManual/PageNode.h>
  11. #include <LibManual/Path.h>
  12. #include <LibManual/SectionNode.h>
  13. ManualModel::ManualModel()
  14. {
  15. 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());
  16. 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());
  17. 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());
  18. }
  19. Optional<GUI::ModelIndex> ManualModel::index_from_path(StringView path) const
  20. {
  21. // The first slice removes the man pages base path plus the `/man` from the main section subdirectory.
  22. // The second slice removes the trailing `.md`.
  23. auto path_without_base = path.substring_view(Manual::manual_base_path.string().length() + 4);
  24. auto url = URL::create_with_help_scheme(path_without_base.substring_view(0, path_without_base.length() - 3), {}, "man");
  25. auto maybe_page = Manual::Node::try_find_from_help_url(url);
  26. if (maybe_page.is_error())
  27. return {};
  28. auto page = maybe_page.release_value();
  29. // Main section
  30. if (page->parent() == nullptr) {
  31. for (size_t section = 0; section < Manual::number_of_sections; ++section) {
  32. auto main_section_index = index(static_cast<int>(section), 0);
  33. if (main_section_index.internal_data() == page.ptr())
  34. return main_section_index;
  35. }
  36. return {};
  37. }
  38. auto maybe_siblings = page->parent()->children();
  39. if (maybe_siblings.is_error())
  40. return {};
  41. auto siblings = maybe_siblings.release_value();
  42. for (size_t row = 0; row < siblings.size(); ++row) {
  43. if (siblings[row] == page)
  44. return create_index(static_cast<int>(row), 0, page.ptr());
  45. }
  46. return {};
  47. }
  48. Optional<String> ManualModel::page_name(const GUI::ModelIndex& index) const
  49. {
  50. if (!index.is_valid())
  51. return {};
  52. auto* node = static_cast<Manual::Node const*>(index.internal_data());
  53. if (!node->is_page())
  54. return {};
  55. auto* page = static_cast<Manual::PageNode const*>(node);
  56. auto path = page->name();
  57. if (path.is_error())
  58. return {};
  59. return path.release_value();
  60. }
  61. Optional<String> ManualModel::page_path(const GUI::ModelIndex& index) const
  62. {
  63. if (!index.is_valid())
  64. return {};
  65. auto* node = static_cast<Manual::Node const*>(index.internal_data());
  66. auto page = node->document();
  67. if (!page)
  68. return {};
  69. auto path = page->path();
  70. if (path.is_error())
  71. return {};
  72. return path.release_value();
  73. }
  74. ErrorOr<StringView> ManualModel::page_view(String const& path) const
  75. {
  76. if (path.is_empty())
  77. return StringView {};
  78. {
  79. // Check if we've got it cached already.
  80. auto mapped_file = m_mapped_files.get(path);
  81. if (mapped_file.has_value())
  82. return StringView { mapped_file.value()->bytes() };
  83. }
  84. auto file = TRY(Core::MappedFile::map(path));
  85. StringView view { file->bytes() };
  86. m_mapped_files.set(path, move(file));
  87. return view;
  88. }
  89. Optional<String> ManualModel::page_and_section(const GUI::ModelIndex& index) const
  90. {
  91. if (!index.is_valid())
  92. return {};
  93. auto* node = static_cast<Manual::Node const*>(index.internal_data());
  94. if (!node->is_page())
  95. return {};
  96. auto* page = static_cast<Manual::PageNode const*>(node);
  97. auto* section = static_cast<Manual::SectionNode const*>(page->parent());
  98. auto page_name = page->name();
  99. if (page_name.is_error())
  100. return {};
  101. auto name = String::formatted("{}({})", page_name.release_value(), section->section_name());
  102. if (name.is_error())
  103. return {};
  104. return name.release_value();
  105. }
  106. GUI::ModelIndex ManualModel::index(int row, int column, const GUI::ModelIndex& parent_index) const
  107. {
  108. if (!parent_index.is_valid())
  109. return create_index(row, column, Manual::sections[row].ptr());
  110. auto* parent = static_cast<Manual::Node const*>(parent_index.internal_data());
  111. auto const children = parent->children();
  112. if (children.is_error())
  113. return {};
  114. auto child = children.value()[row];
  115. return create_index(row, column, child.ptr());
  116. }
  117. GUI::ModelIndex ManualModel::parent_index(const GUI::ModelIndex& index) const
  118. {
  119. if (!index.is_valid())
  120. return {};
  121. auto* child = static_cast<Manual::Node const*>(index.internal_data());
  122. auto* parent = child->parent();
  123. if (parent == nullptr)
  124. return {};
  125. if (parent->parent() == nullptr) {
  126. for (size_t row = 0; row < Manual::sections.size(); row++)
  127. if (Manual::sections[row].ptr() == parent)
  128. return create_index(row, 0, parent);
  129. VERIFY_NOT_REACHED();
  130. }
  131. auto maybe_children = parent->parent()->children();
  132. if (maybe_children.is_error())
  133. return {};
  134. auto children = maybe_children.release_value();
  135. for (size_t row = 0; row < children.size(); row++) {
  136. Manual::Node const* child_at_row = children[row];
  137. if (child_at_row == parent)
  138. return create_index(row, 0, parent);
  139. }
  140. VERIFY_NOT_REACHED();
  141. }
  142. int ManualModel::row_count(const GUI::ModelIndex& index) const
  143. {
  144. if (!index.is_valid())
  145. return static_cast<int>(Manual::sections.size());
  146. auto* node = static_cast<Manual::Node const*>(index.internal_data());
  147. auto maybe_children = node->children();
  148. if (maybe_children.is_error())
  149. return 0;
  150. return static_cast<int>(maybe_children.value().size());
  151. }
  152. int ManualModel::column_count(const GUI::ModelIndex&) const
  153. {
  154. return 1;
  155. }
  156. GUI::Variant ManualModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  157. {
  158. auto* node = static_cast<Manual::Node const*>(index.internal_data());
  159. switch (role) {
  160. case GUI::ModelRole::Search:
  161. if (!node->is_page())
  162. return {};
  163. if (auto path = page_path(index); path.has_value())
  164. if (auto page = page_view(path.release_value()); !page.is_error())
  165. // FIXME: We already provide String, but GUI::Variant still needs DeprecatedString.
  166. return DeprecatedString(page.release_value());
  167. return {};
  168. case GUI::ModelRole::Display:
  169. if (auto name = node->name(); !name.is_error())
  170. return name.release_value();
  171. return {};
  172. case GUI::ModelRole::Icon:
  173. if (node->is_page())
  174. return m_page_icon;
  175. if (node->is_open())
  176. return m_section_open_icon;
  177. return m_section_icon;
  178. default:
  179. return {};
  180. }
  181. }
  182. void ManualModel::update_section_node_on_toggle(const GUI::ModelIndex& index, bool const open)
  183. {
  184. auto* node = static_cast<Manual::Node*>(index.internal_data());
  185. if (is<Manual::SectionNode>(*node))
  186. static_cast<Manual::SectionNode*>(node)->set_open(open);
  187. }
  188. TriState ManualModel::data_matches(const GUI::ModelIndex& index, const GUI::Variant& term) const
  189. {
  190. auto name = page_name(index);
  191. if (!name.has_value())
  192. return TriState::False;
  193. if (name.value().bytes_as_string_view().contains(term.as_string(), CaseSensitivity::CaseInsensitive))
  194. return TriState::True;
  195. auto path = page_path(index);
  196. // 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.
  197. if (!path.has_value())
  198. return TriState::False;
  199. auto view_result = page_view(path.release_value());
  200. if (view_result.is_error() || view_result.value().is_empty())
  201. return TriState::False;
  202. return view_result.value().contains(term.as_string(), CaseSensitivity::CaseInsensitive) ? TriState::True : TriState::False;
  203. }