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