GFileSystemModel.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include <LibGUI/GFileSystemModel.h>
  2. #include <AK/FileSystemPath.h>
  3. #include <AK/StringBuilder.h>
  4. #include <sys/stat.h>
  5. #include <dirent.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. struct GFileSystemModel::Node {
  9. String name;
  10. Node* parent { nullptr };
  11. Vector<Node*> children;
  12. enum Type { Unknown, Directory, File };
  13. Type type { Unknown };
  14. bool has_traversed { false };
  15. GModelIndex index(const GFileSystemModel& model) const
  16. {
  17. if (!parent)
  18. return model.create_index(0, 0, (void*)this);
  19. for (int row = 0; row < parent->children.size(); ++row) {
  20. if (parent->children[row] == this)
  21. return model.create_index(row, 0, (void*)this);
  22. }
  23. ASSERT_NOT_REACHED();
  24. }
  25. void traverse_if_needed(const GFileSystemModel& model)
  26. {
  27. if (type != Node::Directory || has_traversed)
  28. return;
  29. has_traversed = true;
  30. auto full_path = this->full_path(model);
  31. DIR* dirp = opendir(full_path.characters());
  32. if (!dirp)
  33. return;
  34. while (auto* de = readdir(dirp)) {
  35. if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
  36. continue;
  37. struct stat st;
  38. int rc = lstat(String::format("%s/%s", full_path.characters(), de->d_name).characters(), &st);
  39. if (rc < 0) {
  40. perror("lstat");
  41. continue;
  42. }
  43. if (model.m_mode == DirectoriesOnly && !S_ISDIR(st.st_mode))
  44. continue;
  45. auto* child = new Node;
  46. child->name = de->d_name;
  47. child->type = S_ISDIR(st.st_mode) ? Node::Type::Directory : Node::Type::File;
  48. child->parent = this;
  49. children.append(child);
  50. }
  51. closedir(dirp);
  52. }
  53. void reify_if_needed(const GFileSystemModel& model)
  54. {
  55. traverse_if_needed(model);
  56. if (type != Node::Type::Unknown)
  57. return;
  58. struct stat st;
  59. auto full_path = this->full_path(model);
  60. int rc = lstat(full_path.characters(), &st);
  61. dbgprintf("lstat(%s) = %d\n", full_path.characters(), rc);
  62. if (rc < 0) {
  63. perror("lstat");
  64. return;
  65. }
  66. type = S_ISDIR(st.st_mode) ? Node::Type::Directory : Node::Type::File;
  67. }
  68. String full_path(const GFileSystemModel& model) const
  69. {
  70. Vector<String, 32> lineage;
  71. for (auto* ancestor = parent; ancestor; ancestor = ancestor->parent) {
  72. lineage.append(ancestor->name);
  73. }
  74. StringBuilder builder;
  75. builder.append(model.root_path());
  76. for (int i = lineage.size() - 1; i >= 0; --i) {
  77. builder.append('/');
  78. builder.append(lineage[i]);
  79. }
  80. builder.append('/');
  81. builder.append(name);
  82. return FileSystemPath(builder.to_string()).string();
  83. }
  84. };
  85. GModelIndex GFileSystemModel::index(const String& path) const
  86. {
  87. FileSystemPath canonical_path(path);
  88. const Node* node = m_root;
  89. if (canonical_path.string() == "/")
  90. return m_root->index(*this);
  91. for (int i = 0; i < canonical_path.parts().size(); ++i) {
  92. auto& part = canonical_path.parts()[i];
  93. bool found = false;
  94. for (auto& child : node->children) {
  95. if (child->name == part) {
  96. node = child;
  97. found = true;
  98. if (i == canonical_path.parts().size() - 1)
  99. return node->index(*this);
  100. break;
  101. }
  102. }
  103. if (!found)
  104. return { };
  105. }
  106. return { };
  107. }
  108. String GFileSystemModel::path(const GModelIndex& index) const
  109. {
  110. if (!index.is_valid())
  111. return { };
  112. auto& node = *(Node*)index.internal_data();
  113. node.reify_if_needed(*this);
  114. return node.full_path(*this);
  115. }
  116. GFileSystemModel::GFileSystemModel(const String& root_path, Mode mode)
  117. : m_root_path(FileSystemPath(root_path).string())
  118. , m_mode(mode)
  119. {
  120. m_open_folder_icon = GIcon::default_icon("filetype-folder-open");
  121. m_closed_folder_icon = GIcon::default_icon("filetype-folder");
  122. m_file_icon = GIcon::default_icon("filetype-unknown");
  123. update();
  124. }
  125. GFileSystemModel::~GFileSystemModel()
  126. {
  127. }
  128. void GFileSystemModel::update()
  129. {
  130. // FIXME: Support refreshing the model!
  131. if (m_root)
  132. return;
  133. m_root = new Node;
  134. m_root->name = m_root_path;
  135. m_root->reify_if_needed(*this);
  136. }
  137. int GFileSystemModel::row_count(const GModelIndex& index) const
  138. {
  139. if (!index.is_valid())
  140. return 1;
  141. auto& node = *(Node*)index.internal_data();
  142. node.reify_if_needed(*this);
  143. if (node.type == Node::Type::Directory)
  144. return node.children.size();
  145. return 0;
  146. }
  147. GModelIndex GFileSystemModel::index(int row, int column, const GModelIndex& parent) const
  148. {
  149. if (!parent.is_valid())
  150. return create_index(row, column, m_root);
  151. auto& node = *(Node*)parent.internal_data();
  152. return create_index(row, column, node.children[row]);
  153. }
  154. GModelIndex GFileSystemModel::parent_index(const GModelIndex& index) const
  155. {
  156. if (!index.is_valid())
  157. return { };
  158. auto& node = *(const Node*)index.internal_data();
  159. if (!node.parent) {
  160. ASSERT(&node == m_root);
  161. return { };
  162. }
  163. return node.parent->index(*this);
  164. }
  165. GVariant GFileSystemModel::data(const GModelIndex& index, Role role) const
  166. {
  167. if (!index.is_valid())
  168. return { };
  169. auto& node = *(const Node*)index.internal_data();
  170. if (role == GModel::Role::Display)
  171. return node.name;
  172. if (role == GModel::Role::Icon) {
  173. if (node.type == Node::Directory) {
  174. if (selected_index() == index)
  175. return m_open_folder_icon;
  176. return m_closed_folder_icon;
  177. }
  178. return m_file_icon;
  179. }
  180. return { };
  181. }
  182. void GFileSystemModel::activate(const GModelIndex&)
  183. {
  184. }
  185. int GFileSystemModel::column_count(const GModelIndex&) const
  186. {
  187. return 1;
  188. }