diff options
author | Andreas Kling <kling@serenityos.org> | 2020-08-17 20:06:21 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-17 21:34:14 +0200 |
commit | be48f58d41a20802fc8a665b8e353a37ecce6a34 (patch) | |
tree | 4f557a1342b364f46c4b1ba06f326d36009ef63d /Applications/FileManager | |
parent | 0ffd319bdaa8ba93b74701f4b9a44e8567d2aadc (diff) | |
download | serenity-be48f58d41a20802fc8a665b8e353a37ecce6a34.zip |
FileManager: Move "create new directory" action into DirectoryView
This makes it available in both desktop and windowed mode. :^)
Diffstat (limited to 'Applications/FileManager')
-rw-r--r-- | Applications/FileManager/DirectoryView.cpp | 28 | ||||
-rw-r--r-- | Applications/FileManager/DirectoryView.h | 7 | ||||
-rw-r--r-- | Applications/FileManager/main.cpp | 34 |
3 files changed, 42 insertions, 27 deletions
diff --git a/Applications/FileManager/DirectoryView.cpp b/Applications/FileManager/DirectoryView.cpp index 056305d57b..4cd9393264 100644 --- a/Applications/FileManager/DirectoryView.cpp +++ b/Applications/FileManager/DirectoryView.cpp @@ -25,9 +25,11 @@ */ #include "DirectoryView.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> @@ -127,6 +129,8 @@ DirectoryView::DirectoryView(Mode mode) set_active_widget(nullptr); set_content_margins({ 2, 2, 2, 2 }); + setup_actions(); + setup_model(); setup_icon_view(); @@ -161,8 +165,12 @@ void DirectoryView::setup_model() 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); + if (on_path_change) - on_path_change(model().root_path()); + on_path_change(model().root_path(), can_write_in_path); }; m_model->register_client(*this); @@ -429,3 +437,21 @@ Vector<String> DirectoryView::selected_file_paths() const }); return paths; } + +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); + } + } + }); +} diff --git a/Applications/FileManager/DirectoryView.h b/Applications/FileManager/DirectoryView.h index 19ef54b12e..6967f25f38 100644 --- a/Applications/FileManager/DirectoryView.h +++ b/Applications/FileManager/DirectoryView.h @@ -79,7 +79,7 @@ public: void launch(const AK::URL&, const LauncherHandler&); - Function<void(const StringView&)> on_path_change; + Function<void(const StringView& path, bool can_write_in_path)> on_path_change; Function<void(GUI::AbstractView&)> on_selection_change; Function<void(const GUI::ModelIndex&, const GUI::ContextMenuEvent&)> on_context_menu_request; Function<void(const GUI::ModelIndex&, const GUI::DropEvent&)> on_drop; @@ -134,6 +134,8 @@ public: Vector<String> selected_file_paths() const; + GUI::Action& mkdir_action() { return *m_mkdir_action; } + private: explicit DirectoryView(Mode); const GUI::FileSystemModel& model() const { return *m_model; } @@ -141,6 +143,7 @@ private: // ^GUI::ModelClient virtual void model_did_update(unsigned) override; + void setup_actions(); void setup_model(); void setup_icon_view(); void setup_columns_view(); @@ -164,4 +167,6 @@ private: RefPtr<GUI::TableView> m_table_view; RefPtr<GUI::IconView> m_icon_view; RefPtr<GUI::ColumnsView> m_columns_view; + + RefPtr<GUI::Action> m_mkdir_action; }; diff --git a/Applications/FileManager/main.cpp b/Applications/FileManager/main.cpp index 676c18d872..18c0b2f9af 100644 --- a/Applications/FileManager/main.cpp +++ b/Applications/FileManager/main.cpp @@ -175,12 +175,14 @@ int run_in_desktop_mode(RefPtr<Core::ConfigFile> config) Desktop::Launcher::open(URL::create_with_file_protocol("/bin/DisplaySettings")); }); + desktop_view_context_menu->add_action(directory_view.mkdir_action()); + #if 0 - desktop_view_context_menu->add_action(mkdir_action); desktop_view_context_menu->add_action(touch_action); - desktop_view_context_menu->add_separator(); #endif + desktop_view_context_menu->add_separator(); + desktop_view_context_menu->add_action(file_manager_action); desktop_view_context_menu->add_separator(); desktop_view_context_menu->add_action(display_properties_action); @@ -292,22 +294,6 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio directory_view.open_parent_directory(); }); - auto 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", - directory_view.path().characters(), - value.characters())); - int rc = mkdir(new_dir_path.characters(), 0777); - if (rc < 0) { - GUI::MessageBox::show(window, String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error); - } else { - refresh_tree_view(); - } - } - }); - auto 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()) { @@ -600,7 +586,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio auto menubar = GUI::MenuBar::construct(); auto& app_menu = menubar->add_menu("File Manager"); - app_menu.add_action(mkdir_action); + app_menu.add_action(directory_view.mkdir_action()); app_menu.add_action(touch_action); app_menu.add_action(copy_action); app_menu.add_action(paste_action); @@ -646,7 +632,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio main_toolbar.add_action(go_home_action); main_toolbar.add_separator(); - main_toolbar.add_action(mkdir_action); + main_toolbar.add_action(directory_view.mkdir_action()); main_toolbar.add_action(touch_action); main_toolbar.add_action(copy_action); main_toolbar.add_action(paste_action); @@ -658,7 +644,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio main_toolbar.add_action(*view_as_table_action); main_toolbar.add_action(*view_as_columns_action); - directory_view.on_path_change = [&](const String& new_path) { + directory_view.on_path_change = [&](const String& new_path, bool can_write_in_path) { const Gfx::Bitmap* icon = nullptr; if (new_path == Core::StandardPaths::home_directory()) icon = &home_directory_icon(); @@ -683,8 +669,6 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio return; } - auto can_write_in_path = access(new_path.characters(), W_OK) == 0; - mkdir_action->set_enabled(can_write_in_path); touch_action->set_enabled(can_write_in_path); paste_action->set_enabled(can_write_in_path && GUI::Clipboard::the().type() == "text/uri-list"); go_forward_action->set_enabled(directory_view.path_history_position() < directory_view.path_history_size() - 1); @@ -728,7 +712,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio directory_context_menu->add_separator(); directory_context_menu->add_action(properties_action); - directory_view_context_menu->add_action(mkdir_action); + directory_view_context_menu->add_action(directory_view.mkdir_action()); directory_view_context_menu->add_action(touch_action); directory_view_context_menu->add_action(paste_action); directory_view_context_menu->add_action(open_terminal_action); @@ -741,7 +725,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio tree_view_directory_context_menu->add_separator(); tree_view_directory_context_menu->add_action(properties_action); tree_view_directory_context_menu->add_separator(); - tree_view_directory_context_menu->add_action(mkdir_action); + tree_view_directory_context_menu->add_action(directory_view.mkdir_action()); tree_view_directory_context_menu->add_action(touch_action); RefPtr<GUI::Menu> file_context_menu; |