2019-04-02 15:43:56 +03:00
|
|
|
#include "DirectoryView.h"
|
2019-05-09 05:56:52 +03:00
|
|
|
#include <AK/FileSystemPath.h>
|
2019-06-07 12:48:03 +03:00
|
|
|
#include <LibGUI/GSortingProxyModel.h>
|
2019-05-09 05:56:52 +03:00
|
|
|
#include <stdio.h>
|
2019-06-07 12:48:03 +03:00
|
|
|
#include <unistd.h>
|
2019-05-09 05:56:52 +03:00
|
|
|
|
2019-09-15 18:49:43 +03:00
|
|
|
// 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");
|
|
|
|
}
|
|
|
|
|
2019-05-09 05:56:52 +03:00
|
|
|
void DirectoryView::handle_activation(const GModelIndex& 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());
|
2020-01-10 18:58:00 +03:00
|
|
|
auto& node = model().node(index);
|
|
|
|
auto path = node.full_path(model());
|
|
|
|
if (node.is_directory()) {
|
2019-07-15 07:49:28 +03:00
|
|
|
open(path);
|
2019-05-09 05:56:52 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-01-10 18:58:00 +03:00
|
|
|
if (node.is_executable()) {
|
2019-05-09 05:56:52 +03:00
|
|
|
if (fork() == 0) {
|
2019-07-15 07:49:28 +03:00
|
|
|
int rc = execl(path.characters(), path.characters(), nullptr);
|
2019-05-09 05:56:52 +03:00
|
|
|
if (rc < 0)
|
|
|
|
perror("exec");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-15 07:49:28 +03:00
|
|
|
if (path.to_lowercase().ends_with(".png")) {
|
2019-05-09 05:56:52 +03:00
|
|
|
if (fork() == 0) {
|
2019-07-15 07:49:28 +03:00
|
|
|
int rc = execl("/bin/qs", "/bin/qs", path.characters(), nullptr);
|
2019-05-09 05:56:52 +03:00
|
|
|
if (rc < 0)
|
|
|
|
perror("exec");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-29 22:00:41 +03:00
|
|
|
if (path.to_lowercase().ends_with(".html")) {
|
|
|
|
if (fork() == 0) {
|
2019-10-07 20:52:35 +03:00
|
|
|
int rc = execl("/bin/Browser", "/bin/Browser", path.characters(), nullptr);
|
2019-09-29 22:00:41 +03:00
|
|
|
if (rc < 0)
|
|
|
|
perror("exec");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-04 21:20:36 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-09 05:56:52 +03:00
|
|
|
if (fork() == 0) {
|
2019-07-15 07:49:28 +03:00
|
|
|
int rc = execl("/bin/TextEditor", "/bin/TextEditor", path.characters(), nullptr);
|
2019-05-09 05:56:52 +03:00
|
|
|
if (rc < 0)
|
|
|
|
perror("exec");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
};
|
2019-03-01 15:54:28 +03:00
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
DirectoryView::DirectoryView(GWidget* parent)
|
|
|
|
: GStackWidget(parent)
|
2020-01-10 18:58:00 +03:00
|
|
|
, m_model(GFileSystemModel::create())
|
2019-03-01 15:54:28 +03:00
|
|
|
{
|
2019-03-23 05:53:51 +03:00
|
|
|
set_active_widget(nullptr);
|
2019-09-21 16:47:58 +03:00
|
|
|
m_item_view = GItemView::construct(this);
|
2019-03-23 05:53:51 +03:00
|
|
|
m_item_view->set_model(model());
|
|
|
|
|
2019-09-21 17:03:59 +03:00
|
|
|
m_table_view = GTableView::construct(this);
|
2019-07-11 16:59:06 +03:00
|
|
|
m_table_view->set_model(GSortingProxyModel::create(m_model));
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2020-01-10 18:58:00 +03:00
|
|
|
m_table_view->model()->set_key_column_and_sort_order(GFileSystemModel::Column::Name, GSortOrder::Ascending);
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2020-01-10 18:58:00 +03:00
|
|
|
m_item_view->set_model_column(GFileSystemModel::Column::Name);
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2020-01-10 18:58:00 +03:00
|
|
|
m_model->on_root_path_change = [this] {
|
2019-09-13 23:00:47 +03:00
|
|
|
m_table_view->selection().clear();
|
|
|
|
m_item_view->selection().clear();
|
2019-08-20 20:43:12 +03:00
|
|
|
if (on_path_change)
|
2020-01-10 18:58:00 +03:00
|
|
|
on_path_change(model().root_path());
|
2019-03-23 05:53:51 +03:00
|
|
|
};
|
|
|
|
|
2019-09-13 23:00:47 +03:00
|
|
|
// NOTE: We're using the on_update hook on the GSortingProxyModel here instead of
|
2020-01-10 18:58:00 +03:00
|
|
|
// the GFileSystemModel's hook. This is because GSortingProxyModel has already
|
|
|
|
// installed an on_update hook on the GFileSystemModel internally.
|
2019-09-13 23:00:47 +03:00
|
|
|
// FIXME: This is an unfortunate design. We should come up with something better.
|
|
|
|
m_table_view->model()->on_update = [this] {
|
2020-01-10 18:58:00 +03:00
|
|
|
for_each_view_implementation([](auto& view) {
|
|
|
|
view.selection().clear();
|
|
|
|
});
|
2019-09-13 23:00:47 +03:00
|
|
|
update_statusbar();
|
|
|
|
};
|
|
|
|
|
2019-06-07 12:48:03 +03:00
|
|
|
m_model->on_thumbnail_progress = [this](int done, int total) {
|
2019-03-25 06:25:25 +03:00
|
|
|
if (on_thumbnail_progress)
|
|
|
|
on_thumbnail_progress(done, total);
|
|
|
|
};
|
|
|
|
|
2019-06-07 12:48:03 +03:00
|
|
|
m_item_view->on_activation = [&](const GModelIndex& index) {
|
2019-05-09 05:56:52 +03:00
|
|
|
handle_activation(index);
|
|
|
|
};
|
2019-06-07 12:48:03 +03:00
|
|
|
m_table_view->on_activation = [&](auto& index) {
|
2019-05-09 05:56:52 +03:00
|
|
|
auto& filter_model = (GSortingProxyModel&)*m_table_view->model();
|
|
|
|
handle_activation(filter_model.map_to_target(index));
|
|
|
|
};
|
|
|
|
|
2019-09-12 19:59:13 +03:00
|
|
|
m_table_view->on_selection_change = [this] {
|
|
|
|
update_statusbar();
|
|
|
|
if (on_selection_change)
|
|
|
|
on_selection_change(*m_table_view);
|
2019-09-10 16:37:55 +03:00
|
|
|
};
|
2019-09-12 19:59:13 +03:00
|
|
|
m_item_view->on_selection_change = [this] {
|
|
|
|
update_statusbar();
|
|
|
|
if (on_selection_change)
|
|
|
|
on_selection_change(*m_item_view);
|
2019-09-10 16:37:55 +03:00
|
|
|
};
|
|
|
|
|
2019-09-13 23:00:47 +03:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
set_view_mode(ViewMode::Icon);
|
2019-03-01 15:54:28 +03:00
|
|
|
}
|
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
DirectoryView::~DirectoryView()
|
2019-03-01 15:54:28 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
void DirectoryView::set_view_mode(ViewMode mode)
|
2019-03-01 15:54:28 +03:00
|
|
|
{
|
2019-03-23 05:53:51 +03:00
|
|
|
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::Icon) {
|
|
|
|
set_active_widget(m_item_view);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
2019-03-01 15:54:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-02 13:26:28 +03:00
|
|
|
void DirectoryView::add_path_to_history(const StringView& path)
|
2019-05-24 00:15:57 +03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-02 13:26:28 +03:00
|
|
|
void DirectoryView::open(const StringView& path)
|
2019-03-01 15:54:28 +03:00
|
|
|
{
|
2019-05-24 00:15:57 +03:00
|
|
|
add_path_to_history(path);
|
2020-01-10 18:58:00 +03:00
|
|
|
model().set_root_path(path);
|
2019-03-01 15:54:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-02 13:26:28 +03:00
|
|
|
void DirectoryView::set_status_message(const StringView& message)
|
2019-03-01 15:54:28 +03:00
|
|
|
{
|
|
|
|
if (on_status_message)
|
|
|
|
on_status_message(message);
|
|
|
|
}
|
2019-03-02 11:16:57 +03:00
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
void DirectoryView::open_parent_directory()
|
2019-03-02 11:16:57 +03:00
|
|
|
{
|
2020-01-10 18:58:00 +03:00
|
|
|
auto path = String::format("%s/..", model().root_path().characters());
|
2019-05-24 00:15:57 +03:00
|
|
|
add_path_to_history(path);
|
2020-01-10 18:58:00 +03:00
|
|
|
model().set_root_path(path);
|
2019-03-02 11:16:57 +03:00
|
|
|
}
|
2019-03-21 00:31:21 +03:00
|
|
|
|
2019-03-23 05:53:51 +03:00
|
|
|
void DirectoryView::refresh()
|
2019-03-21 00:31:21 +03:00
|
|
|
{
|
|
|
|
model().update();
|
|
|
|
}
|
2019-05-24 00:15:57 +03:00
|
|
|
|
|
|
|
void DirectoryView::open_previous_directory()
|
|
|
|
{
|
|
|
|
if (m_path_history_position > 0) {
|
|
|
|
m_path_history_position--;
|
2020-01-10 18:58:00 +03:00
|
|
|
model().set_root_path(m_path_history[m_path_history_position]);
|
2019-05-24 00:15:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void DirectoryView::open_next_directory()
|
|
|
|
{
|
|
|
|
if (m_path_history_position < m_path_history.size() - 1) {
|
|
|
|
m_path_history_position++;
|
2020-01-10 18:58:00 +03:00
|
|
|
model().set_root_path(m_path_history[m_path_history_position]);
|
2019-05-24 00:15:57 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-12 19:59:13 +03:00
|
|
|
|
|
|
|
void DirectoryView::update_statusbar()
|
|
|
|
{
|
2020-01-10 18:58:00 +03:00
|
|
|
size_t total_size = model().node({}).total_size;
|
2019-09-12 19:59:13 +03:00
|
|
|
if (current_view().selection().is_empty()) {
|
2019-09-15 18:49:43 +03:00
|
|
|
set_status_message(String::format("%d item%s (%s)",
|
2019-09-12 19:59:13 +03:00
|
|
|
model().row_count(),
|
|
|
|
model().row_count() != 1 ? "s" : "",
|
2020-01-10 18:58:00 +03:00
|
|
|
human_readable_size(total_size).characters()));
|
2019-09-12 19:59:13 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int selected_item_count = current_view().selection().size();
|
|
|
|
size_t selected_byte_count = 0;
|
|
|
|
|
|
|
|
current_view().selection().for_each_index([&](auto& index) {
|
2020-01-10 18:58:00 +03:00
|
|
|
auto& model = *current_view().model();
|
|
|
|
auto size_index = model.sibling(index.row(), GFileSystemModel::Column::Size, model.parent_index(index));
|
|
|
|
auto file_size = model.data(size_index).to_int();
|
2019-09-12 19:59:13 +03:00
|
|
|
selected_byte_count += file_size;
|
|
|
|
});
|
|
|
|
|
2019-09-15 18:49:43 +03:00
|
|
|
set_status_message(String::format("%d item%s selected (%s)",
|
2019-09-12 19:59:13 +03:00
|
|
|
selected_item_count,
|
|
|
|
selected_item_count != 1 ? "s" : "",
|
2019-09-15 18:49:43 +03:00
|
|
|
human_readable_size(selected_byte_count).characters()));
|
2019-09-12 19:59:13 +03:00
|
|
|
}
|