Skip to main content

Mountain/IPC/WindServiceHandlers/NativeHost/
MoveItemToTrash.rs

1#![allow(unused_variables, dead_code, unused_imports)]
2
3//! Wire method: `nativeHost:moveItemToTrash`.
4//! Routes deletions to the OS trash bin so they are recoverable.
5//! macOS uses Finder via osascript; Linux prefers `gio trash` then `trash`;
6//! Windows uses PowerShell Shell.Application. Returns `true` on success.
7
8use serde_json::{Value, json};
9
10pub async fn Fn(Arguments:Vec<Value>) -> Result<Value, String> {
11	let Path = Arguments.first().and_then(|V| V.as_str()).unwrap_or("").to_string();
12
13	if Path.is_empty() {
14		return Ok(json!(false));
15	}
16
17	crate::dev_log!("nativehost", "nativeHost:moveItemToTrash path={}", Path);
18
19	let Moved = {
20		#[cfg(target_os = "macos")]
21		{
22			// Pass path via env var so it is never interpolated into AppleScript source.
23			tokio::process::Command::new("osascript")
24				.env("MOVE_TARGET", &Path)
25				.args([
26					"-e",
27					"tell application \"Finder\" to delete POSIX file (system attribute \"MOVE_TARGET\")",
28				])
29				.status()
30				.await
31				.map(|S| S.success())
32				.unwrap_or(false)
33		}
34
35		#[cfg(target_os = "linux")]
36		{
37			let Gio = tokio::process::Command::new("gio")
38				.args(["trash", &Path])
39				.status()
40				.await
41				.map(|S| S.success())
42				.unwrap_or(false);
43
44			if Gio {
45				true
46			} else {
47				tokio::process::Command::new("trash")
48					.arg(&Path)
49					.status()
50					.await
51					.map(|S| S.success())
52					.unwrap_or(false)
53			}
54		}
55
56		#[cfg(target_os = "windows")]
57		{
58			// Pass path via env var so it is never interpolated into PowerShell source.
59			tokio::process::Command::new("powershell.exe")
60				.env("MOVE_TARGET", &Path)
61				.args([
62					"-NoProfile",
63					"-Command",
64					"(new-object -comobject Shell.Application).NameSpace(0xA).MoveHere($env:MOVE_TARGET)",
65				])
66				.status()
67				.await
68				.map(|S| S.success())
69				.unwrap_or(false)
70		}
71
72		#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
73		{
74			false
75		}
76	};
77
78	Ok(json!(Moved))
79}