forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectoryView.cpp
326 lines (285 loc) Β· 10.5 KB
/
DirectoryView.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "DirectoryView.h"
#include <AK/FileSystemPath.h>
#include <AK/StringBuilder.h>
#include <LibGUI/GSortingProxyModel.h>
#include <stdio.h>
#include <unistd.h>
// FIXME: Remove this hackery once printf() supports floats.
static String number_string_with_one_decimal(float number, const char* suffix)
{
float decimals = number - (int)number;
return String::format("%d.%d %s", (int)number, (int)(decimals * 10), suffix);
}
static String human_readable_size(size_t size)
{
if (size < 1 * KB)
return String::format("%zu bytes", size);
if (size < 1 * MB)
return number_string_with_one_decimal((float)size / (float)KB, "KB");
if (size < 1 * GB)
return number_string_with_one_decimal((float)size / (float)MB, "MB");
return number_string_with_one_decimal((float)size / (float)GB, "GB");
}
void DirectoryView::handle_activation(const GUI::ModelIndex& index)
{
if (!index.is_valid())
return;
dbgprintf("on activation: %d,%d, this=%p, m_model=%p\n", index.row(), index.column(), this, m_model.ptr());
auto& node = model().node(index);
auto path = node.full_path(model());
struct stat st;
if (stat(path.characters(), &st) < 0) {
perror("stat");
return;
}
if (S_ISDIR(st.st_mode)) {
open(path);
return;
}
ASSERT(!S_ISLNK(st.st_mode));
if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
if (fork() == 0) {
int rc = execl(path.characters(), path.characters(), nullptr);
if (rc < 0)
perror("exec");
ASSERT_NOT_REACHED();
}
return;
}
if (path.to_lowercase().ends_with(".png")) {
if (fork() == 0) {
int rc = execl("/bin/qs", "/bin/qs", path.characters(), nullptr);
if (rc < 0)
perror("exec");
ASSERT_NOT_REACHED();
}
return;
}
if (path.to_lowercase().ends_with(".html")) {
if (fork() == 0) {
int rc = execl("/bin/Browser", "/bin/Browser", path.characters(), nullptr);
if (rc < 0)
perror("exec");
ASSERT_NOT_REACHED();
}
return;
}
if (path.to_lowercase().ends_with(".wav")) {
if (fork() == 0) {
int rc = execl("/bin/SoundPlayer", "/bin/SoundPlayer", path.characters(), nullptr);
if (rc < 0)
perror("exec");
ASSERT_NOT_REACHED();
}
return;
}
if (fork() == 0) {
int rc = execl("/bin/TextEditor", "/bin/TextEditor", path.characters(), nullptr);
if (rc < 0)
perror("exec");
ASSERT_NOT_REACHED();
}
};
DirectoryView::DirectoryView(GUI::Widget* parent)
: GUI::StackWidget(parent)
, m_model(GUI::FileSystemModel::create())
{
set_active_widget(nullptr);
m_item_view = GUI::ItemView::construct(this);
m_item_view->set_model(model());
m_columns_view = GUI::ColumnsView::construct(this);
m_columns_view->set_model(model());
m_table_view = GUI::TableView::construct(this);
m_table_view->set_model(GUI::SortingProxyModel::create(m_model));
m_table_view->model()->set_key_column_and_sort_order(GUI::FileSystemModel::Column::Name, GUI::SortOrder::Ascending);
m_item_view->set_model_column(GUI::FileSystemModel::Column::Name);
m_columns_view->set_model_column(GUI::FileSystemModel::Column::Name);
m_model->on_root_path_change = [this] {
m_table_view->selection().clear();
m_item_view->selection().clear();
if (on_path_change)
on_path_change(model().root_path());
};
// NOTE: We're using the on_update hook on the GSortingProxyModel here instead of
// the GFileSystemModel's hook. This is because GSortingProxyModel has already
// installed an on_update hook on the GFileSystemModel internally.
// FIXME: This is an unfortunate design. We should come up with something better.
m_table_view->model()->on_update = [this] {
for_each_view_implementation([](auto& view) {
view.selection().clear();
});
update_statusbar();
};
m_model->on_thumbnail_progress = [this](int done, int total) {
if (on_thumbnail_progress)
on_thumbnail_progress(done, total);
};
m_item_view->on_activation = [&](const GUI::ModelIndex& index) {
handle_activation(index);
};
m_columns_view->on_activation = [&](const GUI::ModelIndex& index) {
handle_activation(index);
};
m_table_view->on_activation = [&](auto& index) {
auto& filter_model = (GUI::SortingProxyModel&)*m_table_view->model();
handle_activation(filter_model.map_to_target(index));
};
m_table_view->on_selection_change = [this] {
update_statusbar();
if (on_selection_change)
on_selection_change(*m_table_view);
};
m_item_view->on_selection_change = [this] {
update_statusbar();
if (on_selection_change)
on_selection_change(*m_item_view);
};
m_columns_view->on_selection_change = [this] {
update_statusbar();
if (on_selection_change)
on_selection_change(*m_columns_view);
};
m_table_view->on_context_menu_request = [this](auto& index, auto& event) {
if (on_context_menu_request)
on_context_menu_request(*m_table_view, index, event);
};
m_item_view->on_context_menu_request = [this](auto& index, auto& event) {
if (on_context_menu_request)
on_context_menu_request(*m_item_view, index, event);
};
m_columns_view->on_context_menu_request = [this](auto& index, auto& event) {
if (on_context_menu_request)
on_context_menu_request(*m_columns_view, index, event);
};
set_view_mode(ViewMode::Icon);
}
DirectoryView::~DirectoryView()
{
}
void DirectoryView::set_view_mode(ViewMode mode)
{
if (m_view_mode == mode)
return;
m_view_mode = mode;
update();
if (mode == ViewMode::List) {
set_active_widget(m_table_view);
return;
}
if (mode == ViewMode::Columns) {
set_active_widget(m_columns_view);
return;
}
if (mode == ViewMode::Icon) {
set_active_widget(m_item_view);
return;
}
ASSERT_NOT_REACHED();
}
void DirectoryView::add_path_to_history(const StringView& path)
{
if (m_path_history_position < m_path_history.size())
m_path_history.resize(m_path_history_position + 1);
m_path_history.append(path);
m_path_history_position = m_path_history.size() - 1;
}
void DirectoryView::open(const StringView& path)
{
add_path_to_history(path);
model().set_root_path(path);
}
void DirectoryView::set_status_message(const StringView& message)
{
if (on_status_message)
on_status_message(message);
}
void DirectoryView::open_parent_directory()
{
auto path = String::format("%s/..", model().root_path().characters());
add_path_to_history(path);
model().set_root_path(path);
}
void DirectoryView::refresh()
{
model().update();
}
void DirectoryView::open_previous_directory()
{
if (m_path_history_position > 0) {
m_path_history_position--;
model().set_root_path(m_path_history[m_path_history_position]);
}
}
void DirectoryView::open_next_directory()
{
if (m_path_history_position < m_path_history.size() - 1) {
m_path_history_position++;
model().set_root_path(m_path_history[m_path_history_position]);
}
}
void DirectoryView::update_statusbar()
{
size_t total_size = model().node({}).total_size;
if (current_view().selection().is_empty()) {
set_status_message(String::format("%d item%s (%s)",
model().row_count(),
model().row_count() != 1 ? "s" : "",
human_readable_size(total_size).characters()));
return;
}
int selected_item_count = current_view().selection().size();
size_t selected_byte_count = 0;
current_view().selection().for_each_index([&](auto& index) {
auto& model = *current_view().model();
auto size_index = model.sibling(index.row(), GUI::FileSystemModel::Column::Size, model.parent_index(index));
auto file_size = model.data(size_index).to_i32();
selected_byte_count += file_size;
});
StringBuilder builder;
builder.append(String::number(selected_item_count));
builder.append(" item");
if (selected_item_count != 1)
builder.append('s');
builder.append(" selected (");
builder.append(human_readable_size(selected_byte_count).characters());
builder.append(')');
if (selected_item_count == 1) {
auto index = current_view().selection().first();
// FIXME: This is disgusting. This code should not even be aware that there is a GSortingProxyModel in the table view.
if (m_view_mode == ViewMode::List) {
auto& filter_model = (GUI::SortingProxyModel&)*m_table_view->model();
index = filter_model.map_to_target(index);
}
auto& node = model().node(index);
if (!node.symlink_target.is_empty()) {
builder.append(" -> ");
builder.append(node.symlink_target);
}
}
set_status_message(builder.to_string());
}