ManualModel.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "ManualNode.h"
  8. #include "ManualPageNode.h"
  9. #include "ManualSectionNode.h"
  10. #include <AK/ByteBuffer.h>
  11. #include <AK/Try.h>
  12. #include <LibCore/File.h>
  13. #include <LibGUI/FilteringProxyModel.h>
  14. static ManualSectionNode s_sections[] = {
  15. { "1", "User programs" },
  16. { "2", "System calls" },
  17. { "3", "Library functions" },
  18. { "4", "Special files" },
  19. { "5", "File formats" },
  20. { "6", "Games" },
  21. { "7", "Miscellanea" },
  22. { "8", "Sysadmin tools" }
  23. };
  24. ManualModel::ManualModel()
  25. {
  26. m_section_open_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/book-open.png").release_value_but_fixme_should_propagate_errors());
  27. m_section_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/book.png").release_value_but_fixme_should_propagate_errors());
  28. m_page_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-unknown.png").release_value_but_fixme_should_propagate_errors());
  29. }
  30. Optional<GUI::ModelIndex> ManualModel::index_from_path(const StringView& path) const
  31. {
  32. for (int section = 0; section < row_count(); ++section) {
  33. auto parent_index = index(section, 0);
  34. for (int row = 0; row < row_count(parent_index); ++row) {
  35. auto child_index = index(row, 0, parent_index);
  36. auto* node = static_cast<const ManualNode*>(child_index.internal_data());
  37. if (!node->is_page())
  38. continue;
  39. auto* page = static_cast<const ManualPageNode*>(node);
  40. if (page->path() != path)
  41. continue;
  42. return child_index;
  43. }
  44. }
  45. return {};
  46. }
  47. String ManualModel::page_path(const GUI::ModelIndex& index) const
  48. {
  49. if (!index.is_valid())
  50. return {};
  51. auto* node = static_cast<const ManualNode*>(index.internal_data());
  52. if (!node->is_page())
  53. return {};
  54. auto* page = static_cast<const ManualPageNode*>(node);
  55. return page->path();
  56. }
  57. ErrorOr<StringView> ManualModel::page_view(String const& path) const
  58. {
  59. if (path.is_empty())
  60. return StringView {};
  61. {
  62. // Check if we've got it cached already.
  63. auto mapped_file = m_mapped_files.get(path);
  64. if (mapped_file.has_value())
  65. return StringView { mapped_file.value()->bytes() };
  66. }
  67. auto file = TRY(MappedFile::map(path));
  68. StringView view { file->bytes() };
  69. m_mapped_files.set(path, move(file));
  70. return view;
  71. }
  72. String ManualModel::page_and_section(const GUI::ModelIndex& index) const
  73. {
  74. if (!index.is_valid())
  75. return {};
  76. auto* node = static_cast<const ManualNode*>(index.internal_data());
  77. if (!node->is_page())
  78. return {};
  79. auto* page = static_cast<const ManualPageNode*>(node);
  80. auto* section = static_cast<const ManualSectionNode*>(page->parent());
  81. return String::formatted("{}({})", page->name(), section->section_name());
  82. }
  83. GUI::ModelIndex ManualModel::index(int row, int column, const GUI::ModelIndex& parent_index) const
  84. {
  85. if (!parent_index.is_valid())
  86. return create_index(row, column, &s_sections[row]);
  87. auto* parent = static_cast<const ManualNode*>(parent_index.internal_data());
  88. auto* child = &parent->children()[row];
  89. return create_index(row, column, child);
  90. }
  91. GUI::ModelIndex ManualModel::parent_index(const GUI::ModelIndex& index) const
  92. {
  93. if (!index.is_valid())
  94. return {};
  95. auto* child = static_cast<const ManualNode*>(index.internal_data());
  96. auto* parent = child->parent();
  97. if (parent == nullptr)
  98. return {};
  99. if (parent->parent() == nullptr) {
  100. for (size_t row = 0; row < sizeof(s_sections) / sizeof(s_sections[0]); row++)
  101. if (&s_sections[row] == parent)
  102. return create_index(row, 0, parent);
  103. VERIFY_NOT_REACHED();
  104. }
  105. for (size_t row = 0; row < parent->parent()->children().size(); row++) {
  106. ManualNode* child_at_row = &parent->parent()->children()[row];
  107. if (child_at_row == parent)
  108. return create_index(row, 0, parent);
  109. }
  110. VERIFY_NOT_REACHED();
  111. }
  112. int ManualModel::row_count(const GUI::ModelIndex& index) const
  113. {
  114. if (!index.is_valid())
  115. return sizeof(s_sections) / sizeof(s_sections[0]);
  116. auto* node = static_cast<const ManualNode*>(index.internal_data());
  117. return node->children().size();
  118. }
  119. int ManualModel::column_count(const GUI::ModelIndex&) const
  120. {
  121. return 1;
  122. }
  123. GUI::Variant ManualModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  124. {
  125. auto* node = static_cast<const ManualNode*>(index.internal_data());
  126. switch (role) {
  127. case GUI::ModelRole::Search:
  128. if (!node->is_page())
  129. return {};
  130. return String(page_view(page_path(index)).value());
  131. case GUI::ModelRole::Display:
  132. return node->name();
  133. case GUI::ModelRole::Icon:
  134. if (node->is_page())
  135. return m_page_icon;
  136. if (node->is_open())
  137. return m_section_open_icon;
  138. return m_section_icon;
  139. default:
  140. return {};
  141. }
  142. }
  143. void ManualModel::update_section_node_on_toggle(const GUI::ModelIndex& index, const bool open)
  144. {
  145. auto* node = static_cast<ManualSectionNode*>(index.internal_data());
  146. node->set_open(open);
  147. }
  148. TriState ManualModel::data_matches(const GUI::ModelIndex& index, const GUI::Variant& term) const
  149. {
  150. auto view_result = page_view(page_path(index));
  151. if (view_result.is_error() || view_result.value().is_empty())
  152. return TriState::False;
  153. return view_result.value().contains(term.as_string()) ? TriState::True : TriState::False;
  154. }