| 1 | //! Open a `.ppd` from the command line or the file manager. |
| 2 | //! |
| 3 | //! The manifest name stays `character.ppd`. Windows records the open command |
| 4 | //! for the current user the first time the program starts. Linux packages |
| 5 | //! install the shared MIME XML and a desktop file with `%f`. |
| 6 | |
| 7 | use std::path::{Component, Path, PathBuf}; |
| 8 | |
| 9 | use crate::project::MANIFEST_NAME; |
| 10 | |
| 11 | pub const MIME_TYPE: &str = "application/x-paperdoll-project"; |
| 12 | pub const PROG_ID: &str = "Paperdoll.Project"; |
| 13 | |
| 14 | pub fn file_open_command(exe: &Path) -> String { |
| 15 | format!("\"{}\" \"%1\"", exe.display()) |
| 16 | } |
| 17 | |
| 18 | pub fn resolve_project_argument(base: &Path, arg: &str) -> Result<PathBuf, String> { |
| 19 | if arg.is_empty() || arg.chars().any(|ch| ch == '\0') { |
| 20 | return Err("het projectpad is leeg of bevat een leeg teken".to_string()); |
| 21 | } |
| 22 | let joined = if Path::new(arg).is_absolute() { |
| 23 | PathBuf::from(arg) |
| 24 | } else { |
| 25 | base.join(arg) |
| 26 | }; |
| 27 | let full = normalize(&joined); |
| 28 | let candidate = if full.is_dir() { |
| 29 | full.join(MANIFEST_NAME) |
| 30 | } else { |
| 31 | full |
| 32 | }; |
| 33 | if candidate.extension().and_then(|ext| ext.to_str()) != Some("ppd") { |
| 34 | return Err("open een .ppd-bestand of een projectmap met character.ppd".to_string()); |
| 35 | } |
| 36 | if !candidate.is_file() { |
| 37 | return Err(format!("geen projectbestand op {}", candidate.display())); |
| 38 | } |
| 39 | Ok(candidate) |
| 40 | } |
| 41 | |
| 42 | fn normalize(path: &Path) -> PathBuf { |
| 43 | let mut out = PathBuf::new(); |
| 44 | for component in path.components() { |
| 45 | match component { |
| 46 | Component::CurDir => {} |
| 47 | Component::ParentDir => { |
| 48 | out.pop(); |
| 49 | } |
| 50 | other => out.push(other.as_os_str()), |
| 51 | } |
| 52 | } |
| 53 | out |
| 54 | } |
| 55 | |
| 56 | /// Writes the per-user open-with command. A missing write is reported and |
| 57 | /// does not stop the window. |
| 58 | #[cfg(windows)] |
| 59 | pub fn ensure_user_file_association(exe: &Path) -> Result<(), String> { |
| 60 | use winreg::enums::*; |
| 61 | use winreg::RegKey; |
| 62 | let hkcu = RegKey::predef(HKEY_CURRENT_USER); |
| 63 | let (extension, _) = hkcu |
| 64 | .create_subkey("Software\\Classes\\.ppd") |
| 65 | .map_err(|err| err.to_string())?; |
| 66 | let (open_with, _) = extension |
| 67 | .create_subkey("OpenWithProgids") |
| 68 | .map_err(|err| err.to_string())?; |
| 69 | open_with |
| 70 | .set_value(PROG_ID, &String::new()) |
| 71 | .map_err(|err| err.to_string())?; |
| 72 | extension |
| 73 | .set_value("Content Type", &MIME_TYPE) |
| 74 | .map_err(|err| err.to_string())?; |
| 75 | let (prog, _) = hkcu |
| 76 | .create_subkey(format!("Software\\Classes\\{PROG_ID}")) |
| 77 | .map_err(|err| err.to_string())?; |
| 78 | prog.set_value("", &"Paperdoll-project") |
| 79 | .map_err(|err| err.to_string())?; |
| 80 | let (icon, _) = prog |
| 81 | .create_subkey("DefaultIcon") |
| 82 | .map_err(|err| err.to_string())?; |
| 83 | icon.set_value("", &format!("\"{}\",0", exe.display())) |
| 84 | .map_err(|err| err.to_string())?; |
| 85 | let (command, _) = hkcu |
| 86 | .create_subkey(format!( |
| 87 | "Software\\Classes\\{PROG_ID}\\shell\\open\\command" |
| 88 | )) |
| 89 | .map_err(|err| err.to_string())?; |
| 90 | command |
| 91 | .set_value("", &file_open_command(exe)) |
| 92 | .map_err(|err| err.to_string())?; |
| 93 | Ok(()) |
| 94 | } |
| 95 | |
| 96 | /// Register an AppImage or standalone Linux binary for the current user. |
| 97 | /// Do not change the user's default application for an existing MIME type. |
| 98 | #[cfg(target_os = "linux")] |
| 99 | pub fn install_linux_integration(exe: &Path) -> Result<(), String> { |
| 100 | let data = std::env::var_os("XDG_DATA_HOME") |
| 101 | .map(PathBuf::from) |
| 102 | .or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".local/share"))) |
| 103 | .ok_or("HOME of XDG_DATA_HOME ontbreekt")?; |
| 104 | write_linux_integration(&data, exe)?; |
| 105 | for (command, path) in [ |
| 106 | ("update-mime-database", data.join("mime")), |
| 107 | ("update-desktop-database", data.join("applications")), |
| 108 | ] { |
| 109 | let status = std::process::Command::new(command) |
| 110 | .arg(path) |
| 111 | .status() |
| 112 | .map_err(|e| format!("{command}: {e}"))?; |
| 113 | if !status.success() { |
| 114 | return Err(format!("{command} is mislukt")); |
| 115 | } |
| 116 | } |
| 117 | Ok(()) |
| 118 | } |
| 119 | |
| 120 | pub fn linux_desktop_entry(exe: &Path) -> Result<String, String> { |
| 121 | let path = exe.to_str().ok_or("het uitvoerbare pad moet UTF-8 zijn")?; |
| 122 | if !exe.is_absolute() || path.chars().any(|ch| ch.is_control()) { |
| 123 | return Err("kies een absoluut pad zonder stuurtekens".into()); |
| 124 | } |
| 125 | // Desktop Exec quoting has an extra escape layer for reserved characters. |
| 126 | let escaped = path |
| 127 | .replace('\\', "\\\\\\\\") |
| 128 | .replace('"', "\\\\\\\"") |
| 129 | .replace('`', "\\\\`") |
| 130 | .replace('$', "\\\\$") |
| 131 | .replace('%', "%%"); |
| 132 | Ok(format!("[Desktop Entry]\nType=Application\nName=Paperdoll Renderer\nExec=\"{escaped}\" %f\nIcon=paperdoll-renderer\nStartupWMClass=paperdoll-renderer\nTerminal=false\nCategories=Graphics;3DGraphics;\nMimeType={MIME_TYPE};\n")) |
| 133 | } |
| 134 | |
| 135 | pub fn write_linux_integration(data: &Path, exe: &Path) -> Result<(), String> { |
| 136 | let desktop = linux_desktop_entry(exe)?; |
| 137 | let files: [(&str, &[u8]); 3] = [ |
| 138 | ( |
| 139 | "applications/paperdoll-renderer.desktop", |
| 140 | desktop.as_bytes(), |
| 141 | ), |
| 142 | ( |
| 143 | "mime/packages/paperdoll-project.xml", |
| 144 | include_bytes!("../packaging/linux/paperdoll-project.xml"), |
| 145 | ), |
| 146 | ( |
| 147 | "icons/hicolor/128x128/apps/paperdoll-renderer.png", |
| 148 | include_bytes!("../packaging/linux/hicolor/128x128/apps/paperdoll-renderer.png"), |
| 149 | ), |
| 150 | ]; |
| 151 | for (relative, bytes) in files { |
| 152 | let path = data.join(relative); |
| 153 | std::fs::create_dir_all(path.parent().unwrap()).map_err(|e| e.to_string())?; |
| 154 | std::fs::write(path, bytes).map_err(|e| e.to_string())?; |
| 155 | } |
| 156 | Ok(()) |
| 157 | } |
| 158 | |
| 159 | #[cfg(test)] |
| 160 | mod tests { |
| 161 | use super::*; |
| 162 | use std::fs; |
| 163 | |
| 164 | #[test] |
| 165 | #[cfg(unix)] |
| 166 | fn desktop_entry_keeps_spaces_and_does_not_set_a_default() { |
| 167 | let entry = |
| 168 | linux_desktop_entry(Path::new("/home/test/My Tools/Paperdoll.AppImage")).unwrap(); |
| 169 | assert!(entry.contains("Exec=\"/home/test/My Tools/Paperdoll.AppImage\" %f")); |
| 170 | assert!(entry.contains("MimeType=application/x-paperdoll-project;")); |
| 171 | assert!(linux_desktop_entry(Path::new("/bad\npath")).is_err()); |
| 172 | assert!(linux_desktop_entry(Path::new("relative")).is_err()); |
| 173 | let quoted = linux_desktop_entry(Path::new("/test/100% ready.AppImage")).unwrap(); |
| 174 | assert!(quoted.contains("100%% ready")); |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn open_command_quotes_the_executable_and_the_file() { |
| 179 | let command = file_open_command(Path::new(r"C:\Program Files\paperdoll-renderer.exe")); |
| 180 | assert!(command.contains(r"C:\Program Files\paperdoll-renderer.exe")); |
| 181 | assert!(command.ends_with("\"%1\"")); |
| 182 | assert!(command.starts_with('"')); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn project_argument_accepts_spaces_relative_and_absolute_ppd() { |
| 187 | let root = std::env::temp_dir().join(format!("paperdoll open {}", std::process::id())); |
| 188 | let _ = fs::remove_dir_all(&root); |
| 189 | fs::create_dir_all(root.join("sub")).unwrap(); |
| 190 | let manifest = root.join(MANIFEST_NAME); |
| 191 | fs::write(&manifest, b"{}").unwrap(); |
| 192 | let relative = resolve_project_argument( |
| 193 | root.parent().unwrap(), |
| 194 | root.file_name().unwrap().to_str().unwrap(), |
| 195 | ) |
| 196 | .unwrap(); |
| 197 | assert_eq!(relative, manifest); |
| 198 | let direct = resolve_project_argument(&root, "character.ppd").unwrap(); |
| 199 | assert_eq!(direct, manifest); |
| 200 | let absolute = |
| 201 | resolve_project_argument(Path::new("/"), manifest.to_str().unwrap()).unwrap(); |
| 202 | assert_eq!(absolute, manifest); |
| 203 | let missing = resolve_project_argument(&root, "nope.ppd"); |
| 204 | assert!(missing.is_err()); |
| 205 | let model = resolve_project_argument(&root, "body.glb"); |
| 206 | assert!(model.is_err()); |
| 207 | let _ = fs::remove_dir_all(&root); |
| 208 | } |
| 209 | } |