ManualModel.cpp 5.6 KB

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