GFileSystemModel.cpp 4.6 KB

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