GFileSystemModel.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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> 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. update();
  121. }
  122. GFileSystemModel::~GFileSystemModel()
  123. {
  124. }
  125. void GFileSystemModel::update()
  126. {
  127. // FIXME: Support refreshing the model!
  128. if (m_root)
  129. return;
  130. m_root = new Node;
  131. m_root->name = m_root_path;
  132. m_root->reify_if_needed(*this);
  133. }
  134. int GFileSystemModel::row_count(const GModelIndex& index) const
  135. {
  136. if (!index.is_valid())
  137. return 1;
  138. auto& node = *(Node*)index.internal_data();
  139. node.reify_if_needed(*this);
  140. if (node.type == Node::Type::Directory)
  141. return node.children.size();
  142. return 0;
  143. }
  144. GModelIndex GFileSystemModel::index(int row, int column, const GModelIndex& parent) const
  145. {
  146. if (!parent.is_valid())
  147. return create_index(row, column, m_root);
  148. auto& node = *(Node*)parent.internal_data();
  149. return create_index(row, column, node.children[row]);
  150. }
  151. GModelIndex GFileSystemModel::parent_index(const GModelIndex& index) const
  152. {
  153. if (!index.is_valid())
  154. return { };
  155. auto& node = *(const Node*)index.internal_data();
  156. if (!node.parent) {
  157. ASSERT(&node == m_root);
  158. return { };
  159. }
  160. return node.parent->index(*this);
  161. }
  162. GVariant GFileSystemModel::data(const GModelIndex& index, Role role) const
  163. {
  164. if (!index.is_valid())
  165. return { };
  166. auto& node = *(const Node*)index.internal_data();
  167. if (role == GModel::Role::Display)
  168. return node.name;
  169. if (role == GModel::Role::Icon) {
  170. if (node.type == Node::Directory) {
  171. if (selected_index() == index)
  172. return GIcon::default_icon("filetype-folder-open");
  173. return GIcon::default_icon("filetype-folder");
  174. }
  175. return GIcon::default_icon("filetype-unknown");
  176. }
  177. return { };
  178. }
  179. void GFileSystemModel::activate(const GModelIndex&)
  180. {
  181. }
  182. int GFileSystemModel::column_count(const GModelIndex&) const
  183. {
  184. return 1;
  185. }