diff --git a/arca-setup/src/engine/system.rs b/arca-setup/src/engine/system.rs index 8633ae1..e122327 100644 --- a/arca-setup/src/engine/system.rs +++ b/arca-setup/src/engine/system.rs @@ -189,10 +189,77 @@ pub fn register_associations(dir: &Path) -> Res<()> { r"Software\RegisteredApplications", "Arca", r"Software\Arca\Capabilities", + )?; + own_zip() +} + +/// An archive with nothing in it: the end-of-central-directory record alone. +const EMPTY_ZIP: [u8; 22] = [ + 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +]; + +const ZIP_CLASS: &str = r"Software\Classes\.zip"; +/// Where the type .zip had before Arca took it, so that uninstalling hands +/// it back. Kept outside `Software\Arca`, which is removed first. +const ZIP_BEFORE: &str = r"Software\Classes\Arca.zip.previous"; + +/// The Explorer's New menu has one entry per extension, named after the type +/// the extension belongs to, so Arca's entry needs `.zip` to be Arca's. Only +/// for this user: the machine-wide type, usually Windows' or another +/// archiver's, stays as it is and comes back when Arca goes. +fn own_zip() -> Res<()> { + let current = hkcu() + .open_subkey(ZIP_CLASS) + .and_then(|key| key.get_value::("")) + .ok(); + if current.as_deref() != Some("Arca.zip") { + delete_tree(ZIP_BEFORE)?; + if let Some(before) = current.filter(|before| !before.is_empty()) { + set(ZIP_BEFORE, "", &before)?; + } else { + set(ZIP_BEFORE, "", "")?; + } + } + let name = crate::i18n::Lang::from_system().strings().zip_type; + set(r"Software\Classes\Arca.zip", "", name)?; + set(ZIP_CLASS, "", "Arca.zip")?; + let (key, _) = hkcu() + .create_subkey(format!(r"{ZIP_CLASS}\Arca.zip\ShellNew")) + .map_err(ctx("registry key .zip\\Arca.zip\\ShellNew"))?; + key.set_raw_value( + "Data", + &RegValue { + bytes: EMPTY_ZIP.to_vec().into(), + vtype: REG_BINARY, + }, ) + .map_err(ctx("registry value ShellNew\\Data")) +} + +/// Hands `.zip` back to whatever had it before `own_zip`, or to the +/// machine-wide type when this user had none of their own. +fn give_back_zip() -> Res<()> { + delete_tree(&format!(r"{ZIP_CLASS}\Arca.zip"))?; + let ours = hkcu() + .open_subkey(ZIP_CLASS) + .and_then(|key| key.get_value::("")) + .is_ok_and(|current| current == "Arca.zip"); + if ours { + let before = hkcu() + .open_subkey(ZIP_BEFORE) + .and_then(|key| key.get_value::("")) + .ok() + .filter(|before| !before.is_empty()); + match before { + Some(before) => set(ZIP_CLASS, "", &before)?, + None => delete_value(ZIP_CLASS, "")?, + } + } + delete_tree(ZIP_BEFORE) } pub fn unregister_associations() -> Res<()> { + give_back_zip()?; for (extension, progid, _) in ASSOCIATIONS { delete_tree(&format!(r"Software\Classes\{progid}"))?; delete_value( diff --git a/arca-setup/src/i18n.rs b/arca-setup/src/i18n.rs index 1630fd5..d47bca2 100644 --- a/arca-setup/src/i18n.rs +++ b/arca-setup/src/i18n.rs @@ -41,6 +41,10 @@ pub struct Strings { pub option_menu_hint: &'static str, pub option_assoc: &'static str, pub option_assoc_hint: &'static str, + /// What a .zip is called in the Explorer once Arca owns the type, and + /// the name of its entry under New. + #[cfg_attr(not(windows), allow(dead_code))] + pub zip_type: &'static str, pub option_path: &'static str, pub option_path_hint: &'static str, pub location: &'static str, @@ -80,7 +84,8 @@ static ES: Strings = Strings { option_menu: "Menú contextual del Explorador", option_menu_hint: "Comprimir y extraer con el botón derecho", option_assoc: "Abrir .zip, .7z, .tar, .gz y .tgz con Arca", - option_assoc_hint: "Arca aparece en Aplicaciones predeterminadas", + option_assoc_hint: "Arca aparece en Aplicaciones predeterminadas y en Nuevo", + zip_type: "Archivo ZIP de Arca", option_path: "Añadir el comando arca al PATH", option_path_hint: "Usar Arca desde la terminal", location: "Carpeta de instalación", @@ -130,7 +135,8 @@ static EN: Strings = Strings { option_menu: "Explorer context menu", option_menu_hint: "Compress and extract with a right click", option_assoc: "Open .zip, .7z, .tar, .gz and .tgz with Arca", - option_assoc_hint: "Arca shows up in Default apps", + option_assoc_hint: "Arca shows up in Default apps and under New", + zip_type: "Arca ZIP archive", option_path: "Add the arca command to PATH", option_path_hint: "Use Arca from the terminal", location: "Install location",