123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523 |
- /*
- * 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.
- */
- #include "DirectoryView.h"
- #include "FileUtils.h"
- #include <AK/LexicalPath.h>
- #include <AK/NumberFormat.h>
- #include <AK/StringBuilder.h>
- #include <LibCore/StandardPaths.h>
- #include <LibGUI/InputBox.h>
- #include <LibGUI/MessageBox.h>
- #include <LibGUI/SortingProxyModel.h>
- #include <serenity.h>
- #include <spawn.h>
- #include <stdio.h>
- #include <unistd.h>
- 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)
- {
- // 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];
- }
- // 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));
- }
- 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 = this->node(index);
- auto path = node.full_path();
- struct stat st;
- if (stat(path.characters(), &st) < 0) {
- perror("stat");
- return;
- }
- if (S_ISDIR(st.st_mode)) {
- if (is_desktop()) {
- Desktop::Launcher::open(URL::create_with_file_protocol(path));
- return;
- }
- open(path);
- return;
- }
- auto url = URL::create_with_file_protocol(path);
- auto launcher_handlers = get_launch_handlers(url);
- auto default_launcher = get_default_launch_handler(launcher_handlers);
- if (default_launcher) {
- launch(url, *default_launcher);
- } else {
- auto error_message = String::format("Could not open %s", path.characters());
- GUI::MessageBox::show(window(), error_message, "File Manager", GUI::MessageBox::Type::Error);
- }
- }
- DirectoryView::DirectoryView(Mode mode)
- : m_mode(mode)
- , m_model(GUI::FileSystemModel::create())
- , m_sorting_model(GUI::SortingProxyModel::create(m_model))
- {
- set_active_widget(nullptr);
- set_content_margins({ 2, 2, 2, 2 });
- setup_actions();
- setup_model();
- setup_icon_view();
- if (mode != Mode::Desktop) {
- setup_columns_view();
- setup_table_view();
- }
- set_view_mode(ViewMode::Icon);
- }
- const GUI::FileSystemModel::Node& DirectoryView::node(const GUI::ModelIndex& index) const
- {
- return model().node(m_sorting_model->map_to_source(index));
- }
- void DirectoryView::setup_model()
- {
- m_model->set_root_path(Core::StandardPaths::desktop_directory());
- 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;
- if (on_error)
- on_error(error, error_string, quit);
- };
- m_model->on_complete = [this] {
- if (m_table_view)
- m_table_view->selection().clear();
- if (m_icon_view)
- m_icon_view->selection().clear();
- add_path_to_history(model().root_path());
- bool can_write_in_path = access(model().root_path().characters(), W_OK) == 0;
- m_mkdir_action->set_enabled(can_write_in_path);
- m_touch_action->set_enabled(can_write_in_path);
- if (on_path_change)
- on_path_change(model().root_path(), can_write_in_path);
- };
- m_model->register_client(*this);
- m_model->on_thumbnail_progress = [this](int done, int total) {
- if (on_thumbnail_progress)
- on_thumbnail_progress(done, total);
- };
- }
- 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);
- }
- m_icon_view->set_model(m_sorting_model);
- m_icon_view->set_model_column(GUI::FileSystemModel::Column::Name);
- m_icon_view->on_activation = [&](auto& index) {
- handle_activation(index);
- };
- m_icon_view->on_selection_change = [this] {
- handle_selection_change();
- };
- m_icon_view->on_context_menu_request = [this](auto& index, auto& event) {
- if (on_context_menu_request)
- on_context_menu_request(index, event);
- };
- m_icon_view->on_drop = [this](auto& index, auto& event) {
- if (on_drop)
- on_drop(index, event);
- };
- }
- 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);
- m_columns_view->on_activation = [&](auto& index) {
- handle_activation(index);
- };
- m_columns_view->on_selection_change = [this] {
- handle_selection_change();
- };
- m_columns_view->on_context_menu_request = [this](auto& index, auto& event) {
- if (on_context_menu_request)
- on_context_menu_request(index, event);
- };
- m_columns_view->on_drop = [this](auto& index, auto& event) {
- if (on_drop)
- on_drop(index, event);
- };
- }
- 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);
- m_table_view->on_activation = [&](auto& index) {
- handle_activation(index);
- };
- m_table_view->on_selection_change = [this] {
- handle_selection_change();
- };
- m_table_view->on_context_menu_request = [this](auto& index, auto& event) {
- if (on_context_menu_request)
- on_context_menu_request(index, event);
- };
- m_table_view->on_drop = [this](auto& index, auto& event) {
- if (on_drop)
- on_drop(index, event);
- };
- }
- DirectoryView::~DirectoryView()
- {
- m_model->unregister_client(*this);
- }
- void DirectoryView::model_did_update(unsigned flags)
- {
- if (flags & GUI::Model::UpdateFlag::InvalidateAllIndexes) {
- for_each_view_implementation([](auto& view) {
- view.selection().clear();
- });
- }
- update_statusbar();
- }
- void DirectoryView::set_view_mode(ViewMode mode)
- {
- if (m_view_mode == mode)
- return;
- m_view_mode = mode;
- update();
- if (mode == ViewMode::Table) {
- 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_icon_view);
- return;
- }
- ASSERT_NOT_REACHED();
- }
- void DirectoryView::add_path_to_history(const StringView& path)
- {
- if (m_path_history.size() && m_path_history.at(m_path_history_position) == path)
- return;
- 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)
- {
- if (model().root_path() == path) {
- model().update();
- return;
- }
- 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());
- 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.index(index.row(), GUI::FileSystemModel::Column::Size, model.parent_index(index));
- auto file_size = size_index.data().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& node = this->node(current_view().selection().first());
- if (!node.symlink_target.is_empty()) {
- builder.append(" -> ");
- builder.append(node.symlink_target);
- }
- }
- set_status_message(builder.to_string());
- }
- void DirectoryView::set_should_show_dotfiles(bool show_dotfiles)
- {
- m_model->set_should_show_dotfiles(show_dotfiles);
- }
- 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;
- }
- 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);
- }
- 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);
- }
- }
- });
- 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);
- }
- });
- 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);
- });
- 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());
- }
|