Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.
feat(linux): Send to from Nemo and Thunar, and folders everywhere
Beefe64d7688dd9
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ede51ba..c82ccef 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -152,6 +152,10 @@ has a Stop button. Choosing another### Changed+- **Send to your phone from more Linux file managers.** Right-click files+ or folders in Nemo (Cinnamon) or Thunar (XFCE) as well as GNOME Files,+ Dolphin and Caja, and folders now work in all of them+ ([#49](https://github.com/thtbee/nectarlink/issues/49)).- **Setup on Windows asks about the desktop shortcut.** Untick **Add adesktop shortcut** to skip it. Updates leave it as you had it, so oneyou deleted no longer comes backdiff --git a/desktop/app/src/linux/shortcut.rs b/desktop/app/src/linux/shortcut.rsindex e344081..73f5d37 100644--- a/desktop/app/src/linux/shortcut.rs+++ b/desktop/app/src/linux/shortcut.rs@@ -3,12 +3,15 @@//// `crate::send_to` reconciles per-device `<Name>.lnk` files in `send_to_dir()`.// On Linux we store those canonical `.lnk` metadata files inside-// `~/.local/share/nectarlink/send-to/` and mirror them into the three major Linux-// file managers:+// `~/.local/share/nectarlink/send-to/` and mirror them into the file managers'+// right-click menus:// 1. GNOME Files (Nautilus): `~/.local/share/nautilus/scripts/Send to <Name> (NectarLink)`// 2. KDE Dolphin (KIO ServiceMenus): `~/.local/share/kio/servicemenus/nectarlink-send-<id>.desktop`-// 3. Cinnamon Nemo / XFCE Thunar (File Manager Actions):+// 3. MATE Caja (with caja-actions; File Manager Actions):// `~/.local/share/file-manager/actions/nectarlink-send-<id>.desktop`+// 4. Cinnamon Nemo: `~/.local/share/nemo/actions/nectarlink-send-<id>.nemo_action`+// 5. XFCE Thunar: our `<action>` entries in `~/.config/Thunar/uca.xml`, next to+// the user's own custom actions (only when Thunar's folder exists).#![allow(dead_code)]@@ -175,22 +178,33 @@ pub fn recent_dir() -> Option<PathBuf> {}/// Synchronize all NectarLink `.lnk` entries in `send_to_dir` into the user's-/// file-manager context menus (Nautilus scripts, KDE KIO ServiceMenus, Nemo/Thunar actions).+/// file-manager context menus (see the top of this file).pub fn sync_file_manager_menus(send_to_dir: &Path) -> Result<()> {let Some(data_home) = xdg_data_home() else {return Ok(());};let entries = collect_shortcuts(send_to_dir)?;- write_file_manager_integrations(&data_home, &entries)+ write_file_manager_integrations(&data_home, &entries)?;+ if let Some(config) = xdg_config_home() {+ write_thunar_actions(&config, &entries)?;+ }+ Ok(())}-/// Remove all NectarLink file-manager context menu integrations (Nautilus scripts,-/// KDE Dolphin KIO ServiceMenus, and Nemo/Thunar File Manager Actions).+/// Remove all NectarLink file-manager context menu entries.pub fn remove_all_file_manager_menus() -> Result<()> {let Some(data_home) = xdg_data_home() else {return Ok(());};- write_file_manager_integrations(&data_home, &[])+ write_file_manager_integrations(&data_home, &[])?;+ if let Some(config) = xdg_config_home() {+ write_thunar_actions(&config, &[])?;+ }+ Ok(())+}++fn xdg_config_home() -> Option<PathBuf> {+ dirs::config_dir().filter(|p| p.is_absolute())}fn collect_shortcuts(send_to_dir: &Path) -> Result<Vec<(PathBuf, Shortcut)>> {@@ -244,8 +258,8 @@ fn format_menu_label(path: &Path) -> String {}}-/// Write Nautilus, KDE Dolphin KIO ServiceMenu, and Nemo/Thunar file-manager action files-/// under `root_data_dir` (e.g. `~/.local/share`) and prune stale NectarLink entries.+/// Write the Nautilus, KDE Dolphin, Caja and Nemo entries under `root_data_dir`+/// (e.g. `~/.local/share`) and prune stale NectarLink entries.pub fn write_file_manager_integrations(root_data_dir: &Path,shortcuts: &[(PathBuf, Shortcut)],@@ -253,15 +267,18 @@ pub fn write_file_manager_integrations(let nautilus_dir = root_data_dir.join("nautilus/scripts");let kio_dir = root_data_dir.join("kio/servicemenus");let actions_dir = root_data_dir.join("file-manager/actions");+ let nemo_dir = root_data_dir.join("nemo/actions");if !shortcuts.is_empty() {std::fs::create_dir_all(&nautilus_dir)?;std::fs::create_dir_all(&kio_dir)?;std::fs::create_dir_all(&actions_dir)?;+ std::fs::create_dir_all(&nemo_dir)?;}let mut desired_nautilus = std::collections::HashSet::new();let mut desired_desktop = std::collections::HashSet::new();+ let mut desired_nemo = std::collections::HashSet::new();for (lnk_path, sc) in shortcuts {let label = format_menu_label(lnk_path);@@ -296,7 +313,7 @@ pub fn write_file_manager_integrations(let kio_content = format!("[Desktop Entry]\n\Type=Service\n\- MimeType=all/allfiles;\n\+ MimeType=all/all;\n\Actions=NectarLinkSend_{dev_id};\n\X-KDE-Priority=TopLevel\n\\n\@@ -307,7 +324,7 @@ pub fn write_file_manager_integrations();write_file_nofollow(&kio_path, &kio_content, 0o755)?;- // 3. Nemo / Thunar (DES-EMA File Manager Action)+ // 3. Caja (DES-EMA File Manager Action)let action_path = actions_dir.join(&desktop_filename);let action_content = format!("[Desktop Entry]\n\@@ -318,11 +335,27 @@ pub fn write_file_manager_integrations(Profiles=profile-zero;\n\\n\[X-Action-Profile profile-zero]\n\- MimeTypes=all/allfiles;\n\+ MimeTypes=all/all;\n\Exec=\"{exec_target}\" {args} %F\n\Name=Default profile\n",);write_file_nofollow(&action_path, &action_content, 0o644)?;++ // 4. Nemo. Its Name takes %-codes, so the phone's name loses any '%'.+ let nemo_filename = format!("nectarlink-send-{dev_id}.nemo_action");+ desired_nemo.insert(nemo_filename.clone());+ let nemo_label = label.replace('%', "");+ let nemo_content = format!(+ "[Nemo Action]\n\+ Name={nemo_label}\n\+ Comment={comment}\n\+ Exec=\"{exec_target}\" {args} %F\n\+ Icon-Name=nectarlink\n\+ Selection=notnone\n\+ Extensions=any;\n\+ Quote=double\n"+ );+ write_file_nofollow(&nemo_dir.join(&nemo_filename), &nemo_content, 0o644)?;}// Prune stale Nautilus scripts generated by NectarLink@@ -338,6 +371,20 @@ pub fn write_file_manager_integrations(}}+ // Prune stale Nemo actions.+ if let Ok(rd) = std::fs::read_dir(&nemo_dir) {+ for entry in rd.flatten() {+ let p = entry.path();+ if let Some(name) = p.file_name().and_then(|s| s.to_str())+ && name.starts_with("nectarlink-")+ && name.ends_with(".nemo_action")+ && !desired_nemo.contains(name)+ {+ let _ = std::fs::remove_file(&p);+ }+ }+ }+// Prune stale KDE ServiceMenus and File Manager Actions prefixed with `nectarlink-`for dir in [&kio_dir, &actions_dir] {if let Ok(rd) = std::fs::read_dir(dir) {@@ -357,6 +404,100 @@ pub fn write_file_manager_integrations(Ok(())}+/// Thunar keeps every custom action in one file, `<config>/Thunar/uca.xml`,+/// with the user's own. Replaces NectarLink's entries there (`unique-id`+/// `nectarlink-send-…`) with `shortcuts`, keeping everything else as it was.+/// Nothing is created for a PC without Thunar, and a file that doesn't look+/// like Thunar's is left alone.+pub fn write_thunar_actions(config_dir: &Path, shortcuts: &[(PathBuf, Shortcut)]) -> Result<()> {+ let thunar_dir = config_dir.join("Thunar");+ if !thunar_dir.is_dir() {+ return Ok(());+ }+ let path = thunar_dir.join("uca.xml");+ let existing = match std::fs::read_to_string(&path) {+ Ok(text) => Some(text),+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,+ Err(e) => return Err(e),+ };+ if existing.is_none() && shortcuts.is_empty() {+ return Ok(());+ }+ let base = existing+ .clone()+ .unwrap_or_else(|| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<actions>\n</actions>\n".to_string());+ let Some(updated) = thunar_uca_with(&base, shortcuts) else {+ return Ok(());+ };+ if existing.as_deref() == Some(updated.as_str()) {+ return Ok(());+ }+ // Written beside it and renamed over it, so Thunar never reads half a file.+ let temp = thunar_dir.join(".uca.xml.nectarlink");+ write_file_nofollow(&temp, &updated, 0o644)?;+ std::fs::rename(&temp, &path)+}++/// `uca` without NectarLink's actions, plus one for each of `shortcuts`;+/// `None` when it has no closing `</actions>` to put them before.+fn thunar_uca_with(uca: &str, shortcuts: &[(PathBuf, Shortcut)]) -> Option<String> {+ let mut kept = String::with_capacity(uca.len());+ let mut rest = uca;+ while let Some(start) = rest.find("<action>") {+ let end = start + rest[start..].find("</action>")? + "</action>".len();+ if rest[start..end].contains("<unique-id>nectarlink-send-") {+ // Dropped with the indentation before it and the line break after it.+ kept.push_str(rest[..start].trim_end_matches([' ', '\t']));+ rest = rest[end..].strip_prefix('\n').unwrap_or(&rest[end..]);+ } else {+ kept.push_str(&rest[..end]);+ rest = &rest[end..];+ }+ }+ kept.push_str(rest);++ let close = kept.rfind("</actions>")?;+ let mut ours = String::new();+ for (lnk_path, sc) in shortcuts {+ let label = xml_escape(&format_menu_label(lnk_path));+ let dev_id = extract_device_id(&sc.arguments);+ let command = xml_escape(&format!(+ "'{}' {} %F",+ escape_posix_sh_single_quoted(&sc.target.to_string_lossy()),+ sanitize_exec_args(&sc.arguments)+ ));+ let comment = xml_escape(&sanitize_single_line(&sc.description));+ ours.push_str(&format!(+ "<action>\n\+ \t<icon>nectarlink</icon>\n\+ \t<name>{label}</name>\n\+ \t<submenu></submenu>\n\+ \t<unique-id>nectarlink-send-{dev_id}</unique-id>\n\+ \t<command>{command}</command>\n\+ \t<description>{comment}</description>\n\+ \t<range></range>\n\+ \t<patterns>*</patterns>\n\+ \t<directories/>\n\+ \t<audio-files/>\n\+ \t<image-files/>\n\+ \t<other-files/>\n\+ \t<text-files/>\n\+ \t<video-files/>\n\+ </action>\n"+ ));+ }+ let (head, tail) = kept.split_at(close);+ Some(format!("{head}{ours}{tail}"))+}++fn xml_escape(s: &str) -> String {+ s.replace('&', "&")+ .replace('<', "<")+ .replace('>', ">")+ .replace('"', """)+ .replace('\'', "'")+}+#[cfg(test)]mod tests {use super::*;@@ -440,4 +581,67 @@ mod tests {assert!(!kio_p1.exists());assert!(!act_p1.exists());}++ #[test]+ fn nemo_actions_are_written_and_pruned() {+ let tmp = tempfile::tempdir().unwrap();+ let sc = Shortcut {+ target: PathBuf::from("/usr/bin/nectarlink-desktop"),+ arguments: "--send-to pixel9".to_string(),+ description: "Send to Pixel 9".to_string(),+ };+ let lnk = tmp.path().join("100% Pixel.lnk");+ write_file_manager_integrations(tmp.path(), &[(lnk, sc)]).unwrap();+ let nemo = tmp.path().join("nemo/actions/nectarlink-send-pixel9.nemo_action");+ let text = std::fs::read_to_string(&nemo).unwrap();+ assert!(text.starts_with("[Nemo Action]\n"));+ assert!(text.contains("\nName=Send to 100 Pixel (NectarLink)\n"), "{text}");+ assert!(text.contains("\nExec=\"/usr/bin/nectarlink-desktop\" --send-to pixel9 %F\n"));+ assert!(text.contains("\nExtensions=any;\n"));+ write_file_manager_integrations(tmp.path(), &[]).unwrap();+ assert!(!nemo.exists());+ }++ #[test]+ fn thunar_actions_keep_the_users_own() {+ let tmp = tempfile::tempdir().unwrap();+ let sc = Shortcut {+ target: PathBuf::from("/opt/user's bin/nectarlink-desktop"),+ arguments: "--send-to pixel9".to_string(),+ description: "Send to Pixel & co".to_string(),+ };+ let lnk = tmp.path().join("Pixel <9>.lnk");++ // No Thunar on this PC: nothing is created.+ write_thunar_actions(tmp.path(), &[(lnk.clone(), sc.clone())]).unwrap();+ assert!(!tmp.path().join("Thunar").exists());++ let dir = tmp.path().join("Thunar");+ std::fs::create_dir_all(&dir).unwrap();+ let uca = dir.join("uca.xml");+ let users = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<actions>\n<action>\n\t<icon>utilities-terminal</icon>\n\t<name>Open Terminal Here</name>\n\t<unique-id>1-1</unique-id>\n\t<command>exo-open --working-directory %f --launch TerminalEmulator</command>\n\t<directories/>\n</action>\n</actions>\n";+ std::fs::write(&uca, users).unwrap();++ write_thunar_actions(tmp.path(), &[(lnk.clone(), sc.clone())]).unwrap();+ let text = std::fs::read_to_string(&uca).unwrap();+ assert!(text.contains("<name>Open Terminal Here</name>"));+ assert!(text.contains("<name>Send to Pixel <9> (NectarLink)</name>"), "{text}");+ assert!(text.contains("<unique-id>nectarlink-send-pixel9</unique-id>"));+ assert!(text.contains(+ "<command>'/opt/user'\\''s bin/nectarlink-desktop' --send-to pixel9 %F</command>"+ ));+ assert!(text.contains("<description>Send to Pixel & co</description>"));+ assert!(text.trim_end().ends_with("</actions>"));++ // Writing again changes nothing; removing ours gives back the user's file.+ write_thunar_actions(tmp.path(), &[(lnk, sc)]).unwrap();+ assert_eq!(std::fs::read_to_string(&uca).unwrap(), text);+ write_thunar_actions(tmp.path(), &[]).unwrap();+ assert_eq!(std::fs::read_to_string(&uca).unwrap(), users);++ // A file that isn't Thunar's is left alone.+ std::fs::write(&uca, "not xml").unwrap();+ write_thunar_actions(tmp.path(), &[]).unwrap();+ assert_eq!(std::fs::read_to_string(&uca).unwrap(), "not xml");+ }}diff --git a/docs/guide/files-and-transfers.md b/docs/guide/files-and-transfers.mdindex 9413d81..f02363f 100644--- a/docs/guide/files-and-transfers.md+++ b/docs/guide/files-and-transfers.md@@ -26,6 +26,7 @@ You can send files or entire folders from your PC using any of these entry point2. **Flick to send**: Drag a file toward the screen edge where your `Shelf` sits with a quick flick gesture. A 3-second **Undo** toast appears before the transfer begins.3. **Home screen button**: Click **Send files** on the PC Home screen and pick files or folders.4. **File Explorer `Send to` menu**: Right-click any file or folder in Windows File Explorer, choose **Send to**, and click your phone's name.+ On Linux, right-click files or folders and choose **Send to *your phone* (NectarLink)**: under **Scripts** in GNOME Files, and in the menu itself in Dolphin, Nemo, Thunar and Caja (Caja needs the `caja-actions` package).5. **Command Palette or CLI**: Press `Ctrl+Alt+Space` and choose **Send files to phone**, or run `nectarlink send <path>` in a terminal.Received files on your phone are saved to `Download/Nectarlink` (preserving subfolder structure when you send a folder).@@ -100,7 +101,7 @@ NectarLink includes a built-in LocalSend v2.1 client and server so you can excha## What the Options Do-- **File Explorer Send to** (`Settings › Files & Sync` on PC, default: **On**): Adds paired phones to the right-click `Send to` menu in Windows File Explorer.+- **File Explorer Send to** (**File manager Send to** on Linux; `Settings › Files & Sync` on PC, default: **On**): Adds paired phones to the right-click `Send to` menu in Windows File Explorer, or to the right-click menu of GNOME Files, Dolphin, Nemo, Thunar and Caja on Linux.- **Your PC in your pocket** (`Settings › Files & Sync` on PC, default: **Off**): Allows paired phones with storage permission to browse your PC's user folders (`Downloads`, `Documents`, `Desktop`, `Pictures`).- **LocalSend on local network** (`Settings › Files & Sync` on PC and `Settings › Features & Data › LocalSend` on Android, default: **Off**): Enables un-paired LAN file sharing with LocalSend devices.- **Synced folders** (`Settings › Files & Sync` on PC): Manages one-way and two-way folder synchronization rules.