GFileSystemModel.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. 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. if (selected_index() == index)
  141. return GIcon::default_icon("filetype-folder-open");
  142. return GIcon::default_icon("filetype-folder");
  143. }
  144. return GIcon::default_icon("filetype-unknown");
  145. }
  146. return { };
  147. }
  148. void GFileSystemModel::activate(const GModelIndex&)
  149. {
  150. }
  151. int GFileSystemModel::column_count(const GModelIndex&) const
  152. {
  153. return 1;
  154. }