GFileSystemModel.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. String full_path(const GFileSystemModel& model) const
  26. {
  27. Vector<String> lineage;
  28. for (auto* ancestor = parent; ancestor; ancestor = ancestor->parent) {
  29. lineage.append(ancestor->name);
  30. }
  31. StringBuilder builder;
  32. builder.append(model.root_path());
  33. for (int i = lineage.size() - 1; i >= 0; --i) {
  34. builder.append('/');
  35. builder.append(lineage[i]);
  36. }
  37. builder.append('/');
  38. builder.append(name);
  39. return FileSystemPath(builder.to_string()).string();
  40. }
  41. void traverse_if_needed(const GFileSystemModel& model)
  42. {
  43. if (type != Node::Directory || has_traversed)
  44. return;
  45. has_traversed = true;
  46. auto full_path = this->full_path(model);
  47. DIR* dirp = opendir(full_path.characters());
  48. if (!dirp)
  49. return;
  50. while (auto* de = readdir(dirp)) {
  51. if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
  52. continue;
  53. struct stat st;
  54. int rc = lstat(String::format("%s/%s", full_path.characters(), de->d_name).characters(), &st);
  55. if (rc < 0) {
  56. perror("lstat");
  57. continue;
  58. }
  59. if (model.m_mode == DirectoriesOnly && !S_ISDIR(st.st_mode))
  60. continue;
  61. auto* child = new Node;
  62. child->name = de->d_name;
  63. child->type = S_ISDIR(st.st_mode) ? Node::Type::Directory : Node::Type::File;
  64. child->parent = this;
  65. children.append(child);
  66. }
  67. closedir(dirp);
  68. }
  69. void reify_if_needed(const GFileSystemModel& model)
  70. {
  71. traverse_if_needed(model);
  72. if (type != Node::Type::Unknown)
  73. return;
  74. struct stat st;
  75. auto full_path = this->full_path(model);
  76. int rc = lstat(full_path.characters(), &st);
  77. dbgprintf("lstat(%s) = %d\n", full_path.characters(), rc);
  78. if (rc < 0) {
  79. perror("lstat");
  80. return;
  81. }
  82. type = S_ISDIR(st.st_mode) ? Node::Type::Directory : Node::Type::File;
  83. }
  84. };
  85. GFileSystemModel::GFileSystemModel(const String& root_path, Mode mode)
  86. : m_root_path(FileSystemPath(root_path).string())
  87. , m_mode(mode)
  88. {
  89. update();
  90. }
  91. GFileSystemModel::~GFileSystemModel()
  92. {
  93. }
  94. void GFileSystemModel::update()
  95. {
  96. // FIXME: Support refreshing the model!
  97. if (m_root)
  98. return;
  99. m_root = new Node;
  100. m_root->name = m_root_path;
  101. m_root->reify_if_needed(*this);
  102. }
  103. int GFileSystemModel::row_count(const GModelIndex& index) const
  104. {
  105. if (!index.is_valid())
  106. return 1;
  107. auto& node = *(Node*)index.internal_data();
  108. node.reify_if_needed(*this);
  109. if (node.type == Node::Type::Directory)
  110. return node.children.size();
  111. return 0;
  112. }
  113. GModelIndex GFileSystemModel::index(int row, int column, const GModelIndex& parent) const
  114. {
  115. if (!parent.is_valid())
  116. return create_index(row, column, m_root);
  117. auto& node = *(Node*)parent.internal_data();
  118. return create_index(row, column, node.children[row]);
  119. }
  120. GModelIndex GFileSystemModel::parent_index(const GModelIndex& index) const
  121. {
  122. if (!index.is_valid())
  123. return { };
  124. auto& node = *(const Node*)index.internal_data();
  125. if (!node.parent) {
  126. ASSERT(&node == m_root);
  127. return { };
  128. }
  129. return node.parent->index(*this);
  130. }
  131. GVariant GFileSystemModel::data(const GModelIndex& index, Role role) const
  132. {
  133. if (!index.is_valid())
  134. return { };
  135. auto& node = *(const Node*)index.internal_data();
  136. if (role == GModel::Role::Display)
  137. return node.name;
  138. if (role == GModel::Role::Icon) {
  139. if (node.type == Node::Directory)
  140. return GIcon::default_icon("filetype-folder");
  141. return GIcon::default_icon("filetype-unknown");
  142. }
  143. return { };
  144. }
  145. void GFileSystemModel::activate(const GModelIndex&)
  146. {
  147. }
  148. int GFileSystemModel::column_count(const GModelIndex&) const
  149. {
  150. return 1;
  151. }