diff options
author | Jonathan Archer <jon@jonnyarcher.com> | 2020-04-22 13:30:02 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-23 11:22:32 +0200 |
commit | 5457020d4e41bf5e2a2ab0de6a6ef2dbed4d777f (patch) | |
tree | c7301bc47ded2ebe547b34ab42f5cc12eaa18c51 /Applications | |
parent | d02c02cebeab0b145c1077084078b2dd9bc14550 (diff) | |
download | serenity-5457020d4e41bf5e2a2ab0de6a6ef2dbed4d777f.zip |
Desktop: File creation from the context menu
Kinda hackish, but it does work.
Diffstat (limited to 'Applications')
-rw-r--r-- | Applications/FileManager/main.cpp | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/Applications/FileManager/main.cpp b/Applications/FileManager/main.cpp index 0939b3a3a8..c474ec57c2 100644 --- a/Applications/FileManager/main.cpp +++ b/Applications/FileManager/main.cpp @@ -173,7 +173,35 @@ int run_in_desktop_mode(RefPtr<Core::ConfigFile> config, String initial_location } }); + auto touch_action = GUI::Action::create("New file...", {}, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GUI::Action&) { + auto input_box = GUI::InputBox::construct("Enter name:", "New file", window); + if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) { + auto new_file_path = canonicalized_path( + String::format("%s/%s", + model->root_path().characters(), + input_box->text_value().characters())); + struct stat st; + int rc = stat(new_file_path.characters(), &st); + if ((rc < 0 && errno != ENOENT)) { + GUI::MessageBox::show(String::format("stat(\"%s\") failed: %s", new_file_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window); + return; + } + if (rc == 0) { + GUI::MessageBox::show(String::format("%s: Already exists", new_file_path.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window); + return; + } + int fd = creat(new_file_path.characters(), 0666); + if (fd < 0) { + GUI::MessageBox::show(String::format("creat(\"%s\") failed: %s", new_file_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window); + return; + } + rc = close(fd); + assert(rc >= 0); + } + }); + desktop_view_context_menu->add_action(mkdir_action); + desktop_view_context_menu->add_action(touch_action); item_view.on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) { if (!index.is_valid()) @@ -774,4 +802,4 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio }; return GUI::Application::the().exec(); -} +}
\ No newline at end of file |