2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-04-02 15:43:56 +03:00
|
|
|
#include "DirectoryView.h"
|
2020-09-13 22:38:44 +03:00
|
|
|
#include "FileUtils.h"
|
2020-08-17 21:06:21 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
2020-05-03 07:30:09 +03:00
|
|
|
#include <AK/NumberFormat.h>
|
2020-01-28 00:36:06 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-09-17 15:30:00 +03:00
|
|
|
#include <LibCore/MimeData.h>
|
2020-08-17 20:52:01 +03:00
|
|
|
#include <LibCore/StandardPaths.h>
|
2020-08-17 21:06:21 +03:00
|
|
|
#include <LibGUI/InputBox.h>
|
2020-07-14 03:58:21 +03:00
|
|
|
#include <LibGUI/MessageBox.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/SortingProxyModel.h>
|
2020-08-17 20:52:01 +03:00
|
|
|
#include <serenity.h>
|
|
|
|
#include <spawn.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
|
|
|
|
2020-09-17 16:26:00 +03:00
|
|
|
namespace FileManager {
|
|
|
|
|
2020-07-14 03:58:21 +03:00
|
|
|
NonnullRefPtr<GUI::Action> LauncherHandler::create_launch_action(Function<void(const LauncherHandler&)> launch_handler)
|
|
|
|
{
|
|
|
|
RefPtr<Gfx::Bitmap> icon;
|
|
|
|
auto icon_file = details().icons.get("16x16");
|
|
|
|
if (icon_file.has_value())
|
|
|
|
icon = Gfx::Bitmap::load_from_file(icon_file.value());
|
|
|
|
return GUI::Action::create(details().name, move(icon), [this, launch_handler = move(launch_handler)](auto&) {
|
|
|
|
launch_handler(*this);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<LauncherHandler> DirectoryView::get_default_launch_handler(const NonnullRefPtrVector<LauncherHandler>& handlers)
|
|
|
|
{
|
2020-07-14 18:36:00 +03:00
|
|
|
// If this is an application, pick it first
|
|
|
|
for (size_t i = 0; i < handlers.size(); i++) {
|
|
|
|
if (handlers[i].details().launcher_type == Desktop::Launcher::LauncherType::Application)
|
|
|
|
return handlers[i];
|
|
|
|
}
|
2020-07-14 03:58:21 +03:00
|
|
|
// If there's a handler preferred by the user, pick this first
|
|
|
|
for (size_t i = 0; i < handlers.size(); i++) {
|
|
|
|
if (handlers[i].details().launcher_type == Desktop::Launcher::LauncherType::UserPreferred)
|
|
|
|
return handlers[i];
|
|
|
|
}
|
|
|
|
// Otherwise, use the user's default, if available
|
|
|
|
for (size_t i = 0; i < handlers.size(); i++) {
|
|
|
|
if (handlers[i].details().launcher_type == Desktop::Launcher::LauncherType::UserDefault)
|
|
|
|
return handlers[i];
|
|
|
|
}
|
|
|
|
// If still no match, use the first one we find
|
|
|
|
if (!handlers.is_empty()) {
|
|
|
|
return handlers[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtrVector<LauncherHandler> DirectoryView::get_launch_handlers(const URL& url)
|
|
|
|
{
|
|
|
|
NonnullRefPtrVector<LauncherHandler> handlers;
|
|
|
|
for (auto& h : Desktop::Launcher::get_handlers_with_details_for_url(url)) {
|
|
|
|
handlers.append(adopt(*new LauncherHandler(h)));
|
|
|
|
}
|
|
|
|
return handlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtrVector<LauncherHandler> DirectoryView::get_launch_handlers(const String& path)
|
|
|
|
{
|
|
|
|
return get_launch_handlers(URL::create_with_file_protocol(path));
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void DirectoryView::handle_activation(const GUI::ModelIndex& index)
|
2019-05-09 05:56:52 +03:00
|
|
|
{
|
|
|
|
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-08-17 23:49:52 +03:00
|
|
|
auto& node = this->node(index);
|
2020-08-17 23:02:21 +03:00
|
|
|
auto path = node.full_path();
|
2020-01-28 00:11:26 +03:00
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (stat(path.characters(), &st) < 0) {
|
|
|
|
perror("stat");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2020-08-17 20:52:01 +03:00
|
|
|
if (is_desktop()) {
|
|
|
|
Desktop::Launcher::open(URL::create_with_file_protocol(path));
|
|
|
|
return;
|
|
|
|
}
|
2019-07-15 07:49:28 +03:00
|
|
|
open(path);
|
2019-05-09 05:56:52 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-01-28 00:11:26 +03:00
|
|
|
|
2020-07-14 03:58:21 +03:00
|
|
|
auto url = URL::create_with_file_protocol(path);
|
|
|
|
auto launcher_handlers = get_launch_handlers(url);
|
|
|
|
auto default_launcher = get_default_launch_handler(launcher_handlers);
|
2020-08-17 20:52:01 +03:00
|
|
|
if (default_launcher) {
|
|
|
|
launch(url, *default_launcher);
|
2020-07-14 03:58:21 +03:00
|
|
|
} else {
|
|
|
|
auto error_message = String::format("Could not open %s", path.characters());
|
2020-07-16 05:45:11 +03:00
|
|
|
GUI::MessageBox::show(window(), error_message, "File Manager", GUI::MessageBox::Type::Error);
|
2020-07-14 03:58:21 +03:00
|
|
|
}
|
2020-04-18 22:58:04 +03:00
|
|
|
}
|
2019-03-01 15:54:28 +03:00
|
|
|
|
2020-08-17 20:52:01 +03:00
|
|
|
DirectoryView::DirectoryView(Mode mode)
|
|
|
|
: m_mode(mode)
|
|
|
|
, m_model(GUI::FileSystemModel::create())
|
2020-08-15 21:05:57 +03:00
|
|
|
, m_sorting_model(GUI::SortingProxyModel::create(m_model))
|
2019-03-01 15:54:28 +03:00
|
|
|
{
|
2019-03-23 05:53:51 +03:00
|
|
|
set_active_widget(nullptr);
|
2020-04-24 20:02:59 +03:00
|
|
|
set_content_margins({ 2, 2, 2, 2 });
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2020-08-17 21:06:21 +03:00
|
|
|
setup_actions();
|
|
|
|
|
2020-08-17 20:52:01 +03:00
|
|
|
setup_model();
|
2020-01-10 19:17:14 +03:00
|
|
|
|
2020-08-17 20:52:01 +03:00
|
|
|
setup_icon_view();
|
|
|
|
if (mode != Mode::Desktop) {
|
|
|
|
setup_columns_view();
|
|
|
|
setup_table_view();
|
|
|
|
}
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2020-08-17 20:52:01 +03:00
|
|
|
set_view_mode(ViewMode::Icon);
|
|
|
|
}
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2020-08-17 23:49:52 +03:00
|
|
|
const GUI::FileSystemModel::Node& DirectoryView::node(const GUI::ModelIndex& index) const
|
|
|
|
{
|
|
|
|
return model().node(m_sorting_model->map_to_source(index));
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:52:01 +03:00
|
|
|
void DirectoryView::setup_model()
|
|
|
|
{
|
|
|
|
m_model->set_root_path(Core::StandardPaths::desktop_directory());
|
2019-03-23 05:53:51 +03:00
|
|
|
|
2020-04-21 22:53:13 +03:00
|
|
|
m_model->on_error = [this](int error, const char* error_string) {
|
|
|
|
bool quit = false;
|
|
|
|
if (m_path_history.size())
|
|
|
|
open(m_path_history.at(m_path_history_position));
|
|
|
|
else
|
|
|
|
quit = true;
|
2020-05-03 07:30:09 +03:00
|
|
|
|
2020-04-21 22:53:13 +03:00
|
|
|
if (on_error)
|
2020-05-03 07:30:09 +03:00
|
|
|
on_error(error, error_string, quit);
|
2020-04-21 22:53:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
m_model->on_complete = [this] {
|
2020-08-17 20:52:01 +03:00
|
|
|
if (m_table_view)
|
|
|
|
m_table_view->selection().clear();
|
|
|
|
if (m_icon_view)
|
|
|
|
m_icon_view->selection().clear();
|
2020-04-21 22:53:13 +03:00
|
|
|
|
|
|
|
add_path_to_history(model().root_path());
|
|
|
|
|
2020-08-17 21:06:21 +03:00
|
|
|
bool can_write_in_path = access(model().root_path().characters(), W_OK) == 0;
|
|
|
|
|
|
|
|
m_mkdir_action->set_enabled(can_write_in_path);
|
2020-08-17 21:10:19 +03:00
|
|
|
m_touch_action->set_enabled(can_write_in_path);
|
2020-08-17 21:06:21 +03:00
|
|
|
|
2019-08-20 20:43:12 +03:00
|
|
|
if (on_path_change)
|
2020-08-17 21:06:21 +03:00
|
|
|
on_path_change(model().root_path(), can_write_in_path);
|
2019-03-23 05:53:51 +03:00
|
|
|
};
|
|
|
|
|
2020-07-11 15:47:26 +03:00
|
|
|
m_model->register_client(*this);
|
2019-09-13 23:00:47 +03:00
|
|
|
|
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);
|
|
|
|
};
|
2020-08-17 20:52:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DirectoryView::setup_icon_view()
|
|
|
|
{
|
|
|
|
m_icon_view = add<GUI::IconView>();
|
|
|
|
|
|
|
|
if (is_desktop()) {
|
|
|
|
m_icon_view->set_frame_shape(Gfx::FrameShape::NoFrame);
|
|
|
|
m_icon_view->set_scrollbars_enabled(false);
|
|
|
|
m_icon_view->set_fill_with_background_color(false);
|
|
|
|
}
|
2019-03-25 06:25:25 +03:00
|
|
|
|
2020-08-17 20:52:01 +03:00
|
|
|
m_icon_view->set_model(m_sorting_model);
|
|
|
|
m_icon_view->set_model_column(GUI::FileSystemModel::Column::Name);
|
2020-08-15 21:05:57 +03:00
|
|
|
m_icon_view->on_activation = [&](auto& index) {
|
2020-08-17 23:49:52 +03:00
|
|
|
handle_activation(index);
|
2019-05-09 05:56:52 +03:00
|
|
|
};
|
2020-08-17 20:52:01 +03:00
|
|
|
m_icon_view->on_selection_change = [this] {
|
2020-09-13 22:38:44 +03:00
|
|
|
handle_selection_change();
|
2020-08-17 20:52:01 +03:00
|
|
|
};
|
|
|
|
m_icon_view->on_context_menu_request = [this](auto& index, auto& event) {
|
|
|
|
if (on_context_menu_request)
|
2020-08-17 23:49:52 +03:00
|
|
|
on_context_menu_request(index, event);
|
2020-08-17 20:52:01 +03:00
|
|
|
};
|
|
|
|
m_icon_view->on_drop = [this](auto& index, auto& event) {
|
2020-09-17 15:30:00 +03:00
|
|
|
handle_drop(index, event);
|
2020-08-17 20:52:01 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectoryView::setup_columns_view()
|
|
|
|
{
|
|
|
|
m_columns_view = add<GUI::ColumnsView>();
|
|
|
|
m_columns_view->set_model(m_sorting_model);
|
|
|
|
m_columns_view->set_model_column(GUI::FileSystemModel::Column::Name);
|
|
|
|
|
2020-08-15 21:05:57 +03:00
|
|
|
m_columns_view->on_activation = [&](auto& index) {
|
2020-08-17 23:49:52 +03:00
|
|
|
handle_activation(index);
|
2020-01-10 19:17:14 +03:00
|
|
|
};
|
2020-08-17 20:52:01 +03:00
|
|
|
|
|
|
|
m_columns_view->on_selection_change = [this] {
|
2020-09-13 22:38:44 +03:00
|
|
|
handle_selection_change();
|
2020-08-17 20:52:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
m_columns_view->on_context_menu_request = [this](auto& index, auto& event) {
|
|
|
|
if (on_context_menu_request)
|
2020-08-17 23:49:52 +03:00
|
|
|
on_context_menu_request(index, event);
|
2020-08-17 20:52:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
m_columns_view->on_drop = [this](auto& index, auto& event) {
|
2020-09-17 15:30:00 +03:00
|
|
|
handle_drop(index, event);
|
2020-08-17 20:52:01 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectoryView::setup_table_view()
|
|
|
|
{
|
|
|
|
m_table_view = add<GUI::TableView>();
|
|
|
|
m_table_view->set_model(m_sorting_model);
|
|
|
|
|
|
|
|
m_table_view->set_key_column_and_sort_order(GUI::FileSystemModel::Column::Name, GUI::SortOrder::Ascending);
|
|
|
|
|
2019-06-07 12:48:03 +03:00
|
|
|
m_table_view->on_activation = [&](auto& index) {
|
2020-08-17 23:49:52 +03:00
|
|
|
handle_activation(index);
|
2019-05-09 05:56:52 +03:00
|
|
|
};
|
|
|
|
|
2019-09-12 19:59:13 +03:00
|
|
|
m_table_view->on_selection_change = [this] {
|
2020-09-13 22:38:44 +03:00
|
|
|
handle_selection_change();
|
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)
|
2020-08-17 23:49:52 +03:00
|
|
|
on_context_menu_request(index, event);
|
2019-09-13 23:00:47 +03:00
|
|
|
};
|
|
|
|
|
2020-02-13 23:55:05 +03:00
|
|
|
m_table_view->on_drop = [this](auto& index, auto& event) {
|
2020-09-17 15:30:00 +03:00
|
|
|
handle_drop(index, event);
|
2020-02-13 23:55:05 +03:00
|
|
|
};
|
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
|
|
|
{
|
2020-07-11 15:47:26 +03:00
|
|
|
m_model->unregister_client(*this);
|
|
|
|
}
|
|
|
|
|
2020-08-13 21:06:14 +03:00
|
|
|
void DirectoryView::model_did_update(unsigned flags)
|
2020-07-11 15:47:26 +03:00
|
|
|
{
|
|
|
|
if (flags & GUI::Model::UpdateFlag::InvalidateAllIndexes) {
|
|
|
|
for_each_view_implementation([](auto& view) {
|
|
|
|
view.selection().clear();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
update_statusbar();
|
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();
|
2020-05-01 03:09:04 +03:00
|
|
|
if (mode == ViewMode::Table) {
|
2019-03-23 05:53:51 +03:00
|
|
|
set_active_widget(m_table_view);
|
|
|
|
return;
|
|
|
|
}
|
2020-01-10 19:17:14 +03:00
|
|
|
if (mode == ViewMode::Columns) {
|
|
|
|
set_active_widget(m_columns_view);
|
|
|
|
return;
|
|
|
|
}
|
2019-03-23 05:53:51 +03:00
|
|
|
if (mode == ViewMode::Icon) {
|
2020-05-01 03:09:04 +03:00
|
|
|
set_active_widget(m_icon_view);
|
2019-03-23 05:53:51 +03:00
|
|
|
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
|
|
|
{
|
2020-04-21 22:53:13 +03:00
|
|
|
if (m_path_history.size() && m_path_history.at(m_path_history_position) == path)
|
|
|
|
return;
|
|
|
|
|
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
|
|
|
{
|
2020-04-19 19:21:02 +03:00
|
|
|
if (model().root_path() == path) {
|
|
|
|
model().update();
|
|
|
|
return;
|
|
|
|
}
|
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());
|
|
|
|
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();
|
2020-08-13 17:07:58 +03:00
|
|
|
auto size_index = model.index(index.row(), GUI::FileSystemModel::Column::Size, model.parent_index(index));
|
2020-08-16 17:14:39 +03:00
|
|
|
auto file_size = size_index.data().to_i32();
|
2019-09-12 19:59:13 +03:00
|
|
|
selected_byte_count += file_size;
|
|
|
|
});
|
|
|
|
|
2020-01-28 00:36:06 +03:00
|
|
|
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) {
|
2020-08-17 23:49:52 +03:00
|
|
|
auto& node = this->node(current_view().selection().first());
|
2020-01-28 00:36:06 +03:00
|
|
|
if (!node.symlink_target.is_empty()) {
|
|
|
|
builder.append(" -> ");
|
|
|
|
builder.append(node.symlink_target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_status_message(builder.to_string());
|
2019-09-12 19:59:13 +03:00
|
|
|
}
|
2020-08-12 21:27:23 +03:00
|
|
|
|
|
|
|
void DirectoryView::set_should_show_dotfiles(bool show_dotfiles)
|
|
|
|
{
|
|
|
|
m_model->set_should_show_dotfiles(show_dotfiles);
|
|
|
|
}
|
2020-08-17 20:52:01 +03:00
|
|
|
|
|
|
|
void DirectoryView::launch(const URL&, const LauncherHandler& launcher_handler)
|
|
|
|
{
|
|
|
|
pid_t child;
|
|
|
|
if (launcher_handler.details().launcher_type == Desktop::Launcher::LauncherType::Application) {
|
|
|
|
const char* argv[] = { launcher_handler.details().name.characters(), nullptr };
|
|
|
|
posix_spawn(&child, launcher_handler.details().executable.characters(), nullptr, nullptr, const_cast<char**>(argv), environ);
|
|
|
|
if (disown(child) < 0)
|
|
|
|
perror("disown");
|
|
|
|
} else {
|
|
|
|
for (auto& path : selected_file_paths()) {
|
|
|
|
const char* argv[] = { launcher_handler.details().name.characters(), path.characters(), nullptr };
|
|
|
|
posix_spawn(&child, launcher_handler.details().executable.characters(), nullptr, nullptr, const_cast<char**>(argv), environ);
|
|
|
|
if (disown(child) < 0)
|
|
|
|
perror("disown");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> DirectoryView::selected_file_paths() const
|
|
|
|
{
|
|
|
|
Vector<String> paths;
|
|
|
|
auto& view = current_view();
|
|
|
|
auto& model = *view.model();
|
|
|
|
view.selection().for_each_index([&](const GUI::ModelIndex& index) {
|
|
|
|
auto parent_index = model.parent_index(index);
|
|
|
|
auto name_index = model.index(index.row(), GUI::FileSystemModel::Column::Name, parent_index);
|
|
|
|
auto path = name_index.data(GUI::ModelRole::Custom).to_string();
|
|
|
|
paths.append(path);
|
|
|
|
});
|
|
|
|
return paths;
|
|
|
|
}
|
2020-08-17 21:06:21 +03:00
|
|
|
|
2020-09-13 22:38:44 +03:00
|
|
|
void DirectoryView::do_delete(bool should_confirm)
|
|
|
|
{
|
|
|
|
auto paths = selected_file_paths();
|
|
|
|
ASSERT(!paths.is_empty());
|
|
|
|
FileUtils::delete_paths(paths, should_confirm, window());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectoryView::handle_selection_change()
|
|
|
|
{
|
|
|
|
update_statusbar();
|
|
|
|
|
|
|
|
bool can_delete = !current_view().selection().is_empty() && access(path().characters(), W_OK) == 0;
|
|
|
|
m_delete_action->set_enabled(can_delete);
|
|
|
|
m_force_delete_action->set_enabled(can_delete);
|
|
|
|
|
|
|
|
if (on_selection_change)
|
|
|
|
on_selection_change(*m_table_view);
|
|
|
|
}
|
|
|
|
|
2020-08-17 21:06:21 +03:00
|
|
|
void DirectoryView::setup_actions()
|
|
|
|
{
|
|
|
|
m_mkdir_action = GUI::Action::create("New directory...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"), [&](const GUI::Action&) {
|
|
|
|
String value;
|
|
|
|
if (GUI::InputBox::show(value, window(), "Enter name:", "New directory") == GUI::InputBox::ExecOK && !value.is_empty()) {
|
|
|
|
auto new_dir_path = LexicalPath::canonicalized_path(
|
|
|
|
String::format("%s/%s",
|
|
|
|
path().characters(),
|
|
|
|
value.characters()));
|
|
|
|
int rc = mkdir(new_dir_path.characters(), 0777);
|
|
|
|
if (rc < 0) {
|
|
|
|
auto saved_errno = errno;
|
|
|
|
GUI::MessageBox::show(window(), String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(saved_errno)), "Error", GUI::MessageBox::Type::Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-08-17 21:10:19 +03:00
|
|
|
|
|
|
|
m_touch_action = GUI::Action::create("New file...", { Mod_Ctrl | Mod_Shift, Key_F }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GUI::Action&) {
|
|
|
|
String value;
|
|
|
|
if (GUI::InputBox::show(value, window(), "Enter name:", "New file") == GUI::InputBox::ExecOK && !value.is_empty()) {
|
|
|
|
auto new_file_path = LexicalPath::canonicalized_path(
|
|
|
|
String::format("%s/%s",
|
|
|
|
path().characters(),
|
|
|
|
value.characters()));
|
|
|
|
struct stat st;
|
|
|
|
int rc = stat(new_file_path.characters(), &st);
|
|
|
|
if ((rc < 0 && errno != ENOENT)) {
|
|
|
|
auto saved_errno = errno;
|
|
|
|
GUI::MessageBox::show(window(), String::format("stat(\"%s\") failed: %s", new_file_path.characters(), strerror(saved_errno)), "Error", GUI::MessageBox::Type::Error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rc == 0) {
|
|
|
|
GUI::MessageBox::show(window(), String::format("%s: Already exists", new_file_path.characters()), "Error", GUI::MessageBox::Type::Error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int fd = creat(new_file_path.characters(), 0666);
|
|
|
|
if (fd < 0) {
|
|
|
|
auto saved_errno = errno;
|
|
|
|
GUI::MessageBox::show(window(), String::format("creat(\"%s\") failed: %s", new_file_path.characters(), strerror(saved_errno)), "Error", GUI::MessageBox::Type::Error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rc = close(fd);
|
|
|
|
ASSERT(rc >= 0);
|
|
|
|
}
|
|
|
|
});
|
2020-09-13 15:58:23 +03:00
|
|
|
|
|
|
|
m_open_terminal_action = GUI::Action::create("Open Terminal here...", Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"), [&](auto&) {
|
|
|
|
posix_spawn_file_actions_t spawn_actions;
|
|
|
|
posix_spawn_file_actions_init(&spawn_actions);
|
|
|
|
posix_spawn_file_actions_addchdir(&spawn_actions, path().characters());
|
|
|
|
pid_t pid;
|
|
|
|
const char* argv[] = { "Terminal", nullptr };
|
|
|
|
if ((errno = posix_spawn(&pid, "/bin/Terminal", &spawn_actions, nullptr, const_cast<char**>(argv), environ))) {
|
|
|
|
perror("posix_spawn");
|
|
|
|
} else {
|
|
|
|
if (disown(pid) < 0)
|
|
|
|
perror("disown");
|
|
|
|
}
|
|
|
|
posix_spawn_file_actions_destroy(&spawn_actions);
|
|
|
|
});
|
2020-09-13 22:38:44 +03:00
|
|
|
|
|
|
|
m_delete_action = GUI::CommonActions::make_delete_action([this](auto&) { do_delete(true); }, window());
|
|
|
|
|
|
|
|
m_force_delete_action = GUI::Action::create(
|
|
|
|
"Delete without confirmation", { Mod_Shift, Key_Delete },
|
|
|
|
[this](auto&) { do_delete(false); },
|
|
|
|
window());
|
2020-08-17 21:06:21 +03:00
|
|
|
}
|
2020-09-17 15:30:00 +03:00
|
|
|
|
|
|
|
void DirectoryView::handle_drop(const GUI::ModelIndex& index, const GUI::DropEvent& event)
|
|
|
|
{
|
|
|
|
if (!event.mime_data().has_urls())
|
|
|
|
return;
|
|
|
|
auto urls = event.mime_data().urls();
|
|
|
|
if (urls.is_empty()) {
|
|
|
|
dbg() << "No files to drop";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& target_node = node(index);
|
|
|
|
if (!target_node.is_directory())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto& url_to_copy : urls) {
|
|
|
|
if (!url_to_copy.is_valid() || url_to_copy.path() == target_node.full_path())
|
|
|
|
continue;
|
|
|
|
auto new_path = String::format("%s/%s",
|
|
|
|
target_node.full_path().characters(),
|
|
|
|
LexicalPath(url_to_copy.path()).basename().characters());
|
|
|
|
|
|
|
|
if (url_to_copy.path() == new_path)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!FileUtils::copy_file_or_directory(url_to_copy.path(), new_path)) {
|
|
|
|
auto error_message = String::format("Could not copy %s into %s.",
|
|
|
|
url_to_copy.to_string().characters(),
|
|
|
|
new_path.characters());
|
|
|
|
GUI::MessageBox::show(window(), error_message, "File Manager", GUI::MessageBox::Type::Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-09-17 16:26:00 +03:00
|
|
|
|
|
|
|
}
|