Support permanently deleting files and directories using Shift+Del

Add a confirmation dialog to limit risks of data lost.
This commit is contained in:
Tim Dengel 2024-08-12 05:23:04 +02:00 committed by Gwen Lg
parent 8ced8b0551
commit 1a66d7b184
4 changed files with 97 additions and 1 deletions

View file

@ -490,6 +490,10 @@ pub enum Operation {
NewFolder {
path: PathBuf,
},
/// Permanently delete items, skipping the trash
PermanentlyDelete {
paths: Vec<PathBuf>,
},
Rename {
from: PathBuf,
to: PathBuf,
@ -593,6 +597,7 @@ impl Operation {
name = file_name(path),
parent = parent_name(path)
),
Self::PermanentlyDelete { paths } => fl!("permanently-deleting", items = paths.len()),
Self::Rename { from, to } => {
fl!("renaming", from = file_name(from), to = file_name(to))
}
@ -651,6 +656,7 @@ impl Operation {
name = file_name(path),
parent = parent_name(path)
),
Self::PermanentlyDelete { paths } => fl!("permanently-deleted", items = paths.len()),
Self::Rename { from, to } => fl!("renamed", from = file_name(from), to = file_name(to)),
Self::Restore { items } => fl!("restored", items = items.len()),
Self::SetExecutableAndLaunch { path } => {
@ -669,6 +675,7 @@ impl Operation {
| Self::EmptyTrash
| Self::Extract { .. }
| Self::Move { .. }
| Self::PermanentlyDelete { .. }
| Self::Restore { .. } => true,
Self::NewFile { .. }
| Self::NewFolder { .. }
@ -1054,6 +1061,32 @@ impl Operation {
.await
.map_err(wrap_compio_spawn_error)?
.map_err(OperationError::from_str),
Self::PermanentlyDelete { paths } => {
let total = paths.len();
for (idx, path) in paths.into_iter().enumerate() {
controller.check().await.map_err(OperationError::from_str)?;
controller.set_progress((idx as f32) / (total as f32));
tokio::task::spawn_blocking(|| {
if path.is_symlink() || path.is_file() {
fs::remove_file(path)
} else if path.is_dir() {
fs::remove_dir_all(path)
} else {
Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"File to delete is not symlink, file or directory",
))
}
})
.await
.map_err(OperationError::from_str)?
.map_err(OperationError::from_str)?;
}
Ok(OperationSelection::default())
}
Self::Rename { from, to } => compio::runtime::spawn(async move {
controller.check().await.map_err(OperationError::from_str)?;
compio::fs::rename(&from, &to)