FilesystemEventModel.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2022, Jakub Berkop <jakub.berkop@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "FilesystemEventModel.h"
  7. #include "Profile.h"
  8. #include <AK/StringBuilder.h>
  9. #include <LibGUI/FileIconProvider.h>
  10. #include <LibSymbolication/Symbolication.h>
  11. #include <stdio.h>
  12. namespace Profiler {
  13. FileEventModel::FileEventModel(Profile& profile)
  14. : m_profile(profile)
  15. {
  16. }
  17. FileEventModel::~FileEventModel()
  18. {
  19. }
  20. FileEventNode& FileEventNode::find_or_create_node(String const& searched_path)
  21. {
  22. // TODO: Optimize this function.
  23. if (searched_path == ""sv) {
  24. return *this;
  25. }
  26. auto lex_path = LexicalPath(searched_path);
  27. auto parts = lex_path.parts();
  28. auto current = parts.take_first();
  29. StringBuilder sb;
  30. sb.join('/', parts);
  31. auto new_s = sb.to_string();
  32. for (auto& child : m_children) {
  33. if (child->m_path == current) {
  34. return child->find_or_create_node(new_s);
  35. }
  36. }
  37. if (m_parent) {
  38. for (auto& child : m_children) {
  39. if (child->m_path == current) {
  40. return child->find_or_create_node(new_s);
  41. }
  42. }
  43. return create_recursively(searched_path);
  44. } else {
  45. if (!searched_path.starts_with("/"sv)) {
  46. m_children.append(create(searched_path, this));
  47. return *m_children.last();
  48. }
  49. return create_recursively(searched_path);
  50. }
  51. }
  52. FileEventNode& FileEventNode::create_recursively(String new_path)
  53. {
  54. auto const lex_path = LexicalPath(new_path);
  55. auto parts = lex_path.parts();
  56. if (parts.size() == 1) {
  57. auto new_node = FileEventNode::create(new_path, this);
  58. m_children.append(new_node);
  59. return *new_node.ptr();
  60. } else {
  61. auto new_node = FileEventNode::create(parts.take_first(), this);
  62. m_children.append(new_node);
  63. StringBuilder sb;
  64. sb.join('/', parts);
  65. return new_node->create_recursively(sb.to_string());
  66. }
  67. }
  68. void FileEventNode::for_each_parent_node(Function<void(FileEventNode&)> callback)
  69. {
  70. auto* current = this;
  71. while (current) {
  72. callback(*current);
  73. current = current->m_parent;
  74. }
  75. }
  76. GUI::ModelIndex FileEventModel::index(int row, int column, GUI::ModelIndex const& parent) const
  77. {
  78. if (!parent.is_valid()) {
  79. return create_index(row, column, m_profile.file_event_nodes()->children()[row].ptr());
  80. }
  81. auto& remote_parent = *static_cast<FileEventNode*>(parent.internal_data());
  82. return create_index(row, column, remote_parent.children().at(row).ptr());
  83. }
  84. GUI::ModelIndex FileEventModel::parent_index(GUI::ModelIndex const& index) const
  85. {
  86. if (!index.is_valid())
  87. return {};
  88. auto& node = *static_cast<FileEventNode*>(index.internal_data());
  89. if (!node.parent())
  90. return {};
  91. if (node.parent()->parent()) {
  92. auto const& children = node.parent()->parent()->children();
  93. for (size_t row = 0; row < children.size(); ++row) {
  94. if (children.at(row).ptr() == node.parent()) {
  95. return create_index(row, index.column(), node.parent());
  96. }
  97. }
  98. }
  99. auto const& children = node.parent()->children();
  100. for (size_t row = 0; row < children.size(); ++row) {
  101. if (children.at(row).ptr() == &node) {
  102. return create_index(row, index.column(), node.parent());
  103. }
  104. }
  105. VERIFY_NOT_REACHED();
  106. return {};
  107. }
  108. int FileEventModel::row_count(GUI::ModelIndex const& index) const
  109. {
  110. if (!index.is_valid())
  111. return m_profile.file_event_nodes()->children().size();
  112. auto& node = *static_cast<FileEventNode*>(index.internal_data());
  113. return node.children().size();
  114. }
  115. int FileEventModel::column_count(GUI::ModelIndex const&) const
  116. {
  117. return Column::__Count;
  118. }
  119. String FileEventModel::column_name(int column) const
  120. {
  121. switch (column) {
  122. case Column::Path:
  123. return "Path";
  124. case Column::Count:
  125. return "Event Count";
  126. case Column::Duration:
  127. return "Duration [ms]";
  128. default:
  129. VERIFY_NOT_REACHED();
  130. return {};
  131. }
  132. }
  133. GUI::Variant FileEventModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
  134. {
  135. if (role == GUI::ModelRole::TextAlignment) {
  136. if (index.column() == Path)
  137. return Gfx::TextAlignment::CenterLeft;
  138. return Gfx::TextAlignment::CenterRight;
  139. }
  140. auto* node = static_cast<FileEventNode*>(index.internal_data());
  141. if (role == GUI::ModelRole::Display) {
  142. if (index.column() == Column::Count) {
  143. return node->count();
  144. }
  145. if (index.column() == Column::Path) {
  146. return node->path();
  147. }
  148. if (index.column() == Column::Duration) {
  149. return node->duration();
  150. }
  151. return {};
  152. }
  153. return {};
  154. }
  155. }