ManualModel.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "ManualModel.h"
  27. #include "ManualNode.h"
  28. #include "ManualPageNode.h"
  29. #include "ManualSectionNode.h"
  30. #include <AK/ByteBuffer.h>
  31. #include <LibCore/File.h>
  32. #include <LibGUI/FilteringProxyModel.h>
  33. static ManualSectionNode s_sections[] = {
  34. { "1", "User programs" },
  35. { "2", "System calls" },
  36. { "3", "Libraries" },
  37. { "4", "Special files" },
  38. { "5", "File formats" },
  39. { "6", "Games" },
  40. { "7", "Miscellanea" },
  41. { "8", "Sysadmin tools" }
  42. };
  43. ManualModel::ManualModel()
  44. {
  45. m_section_open_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/book-open.png"));
  46. m_section_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/book.png"));
  47. m_page_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png"));
  48. }
  49. Optional<GUI::ModelIndex> ManualModel::index_from_path(const StringView& path) const
  50. {
  51. for (int section = 0; section < row_count(); ++section) {
  52. auto parent_index = index(section, 0);
  53. for (int row = 0; row < row_count(parent_index); ++row) {
  54. auto child_index = index(row, 0, parent_index);
  55. auto* node = static_cast<const ManualNode*>(child_index.internal_data());
  56. if (!node->is_page())
  57. continue;
  58. auto* page = static_cast<const ManualPageNode*>(node);
  59. if (page->path() != path)
  60. continue;
  61. return child_index;
  62. }
  63. }
  64. return {};
  65. }
  66. String ManualModel::page_path(const GUI::ModelIndex& index) const
  67. {
  68. if (!index.is_valid())
  69. return {};
  70. auto* node = static_cast<const ManualNode*>(index.internal_data());
  71. if (!node->is_page())
  72. return {};
  73. auto* page = static_cast<const ManualPageNode*>(node);
  74. return page->path();
  75. }
  76. Result<StringView, OSError> ManualModel::page_view(const String& path) const
  77. {
  78. if (path.is_empty())
  79. return StringView {};
  80. {
  81. // Check if we've got it cached already.
  82. auto mapped_file = m_mapped_files.get(path);
  83. if (mapped_file.has_value())
  84. return StringView { mapped_file.value()->bytes() };
  85. }
  86. auto file_or_error = MappedFile::map(path);
  87. if (file_or_error.is_error())
  88. return file_or_error.error();
  89. StringView view { file_or_error.value()->bytes() };
  90. m_mapped_files.set(path, file_or_error.release_value());
  91. return view;
  92. }
  93. String ManualModel::page_and_section(const GUI::ModelIndex& index) const
  94. {
  95. if (!index.is_valid())
  96. return {};
  97. auto* node = static_cast<const ManualNode*>(index.internal_data());
  98. if (!node->is_page())
  99. return {};
  100. auto* page = static_cast<const ManualPageNode*>(node);
  101. auto* section = static_cast<const ManualSectionNode*>(page->parent());
  102. return String::formatted("{}({})", page->name(), section->section_name());
  103. }
  104. GUI::ModelIndex ManualModel::index(int row, int column, const GUI::ModelIndex& parent_index) const
  105. {
  106. if (!parent_index.is_valid())
  107. return create_index(row, column, &s_sections[row]);
  108. auto* parent = static_cast<const ManualNode*>(parent_index.internal_data());
  109. auto* child = &parent->children()[row];
  110. return create_index(row, column, child);
  111. }
  112. GUI::ModelIndex ManualModel::parent_index(const GUI::ModelIndex& index) const
  113. {
  114. if (!index.is_valid())
  115. return {};
  116. auto* child = static_cast<const ManualNode*>(index.internal_data());
  117. auto* parent = child->parent();
  118. if (parent == nullptr)
  119. return {};
  120. if (parent->parent() == nullptr) {
  121. for (size_t row = 0; row < sizeof(s_sections) / sizeof(s_sections[0]); row++)
  122. if (&s_sections[row] == parent)
  123. return create_index(row, 0, parent);
  124. ASSERT_NOT_REACHED();
  125. }
  126. for (size_t row = 0; row < parent->parent()->children().size(); row++) {
  127. ManualNode* child_at_row = &parent->parent()->children()[row];
  128. if (child_at_row == parent)
  129. return create_index(row, 0, parent);
  130. }
  131. ASSERT_NOT_REACHED();
  132. }
  133. int ManualModel::row_count(const GUI::ModelIndex& index) const
  134. {
  135. if (!index.is_valid())
  136. return sizeof(s_sections) / sizeof(s_sections[0]);
  137. auto* node = static_cast<const ManualNode*>(index.internal_data());
  138. return node->children().size();
  139. }
  140. int ManualModel::column_count(const GUI::ModelIndex&) const
  141. {
  142. return 1;
  143. }
  144. GUI::Variant ManualModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  145. {
  146. auto* node = static_cast<const ManualNode*>(index.internal_data());
  147. switch (role) {
  148. case GUI::ModelRole::Search:
  149. if (!node->is_page())
  150. return {};
  151. return String(page_view(page_path(index)).value());
  152. case GUI::ModelRole::Display:
  153. return node->name();
  154. case GUI::ModelRole::Icon:
  155. if (node->is_page())
  156. return m_page_icon;
  157. if (node->is_open())
  158. return m_section_open_icon;
  159. return m_section_icon;
  160. default:
  161. return {};
  162. }
  163. }
  164. void ManualModel::update_section_node_on_toggle(const GUI::ModelIndex& index, const bool open)
  165. {
  166. auto* node = static_cast<ManualSectionNode*>(index.internal_data());
  167. node->set_open(open);
  168. }
  169. TriState ManualModel::data_matches(const GUI::ModelIndex& index, const GUI::Variant& term) const
  170. {
  171. auto view_result = page_view(page_path(index));
  172. if (view_result.is_error() || view_result.value().is_empty())
  173. return TriState::False;
  174. return view_result.value().contains(term.as_string()) ? TriState::True : TriState::False;
  175. }
  176. void ManualModel::update()
  177. {
  178. did_update();
  179. }