use std::io;
use std::path::{Path, PathBuf};
use std::process::Command;
#[cfg(target_os = "windows")]
use std::process::Stdio;
use crate::handler;
const APP_DISPLAY_NAME: &str = "Open as HTML";
#[cfg(target_os = "linux")]
const DESKTOP_FILE_NAME: &str = "mdo.desktop";
#[cfg(target_os = "linux")]
const SETUP_DESKTOP_FILE_NAME: &str = "mdo-setup.desktop";
#[cfg(target_os = "windows")]
const WINDOWS_ICON_BYTES: &[u8] = include_bytes!("../assets/mdo.ico");
#[cfg(target_os = "linux")]
const SVG_ICON: &str = r##"<svg xmlns="http://www.w3.org/2000/svg" width="256" height="256" viewBox="0 0 256 256">
<rect width="256" height="256" rx="48" fill="#f7f7f7"/>
<text x="128" y="151"
text-anchor="middle"
dominant-baseline="middle"
font-family="Segoe UI Symbol, Noto Sans Symbols 2, Noto Sans Symbols, DejaVu Sans, sans-serif"
font-size="176"
font-weight="700"
fill="#1e66e2">â“‚</text>
</svg>
"##;
#[cfg(any(target_os = "linux", target_os = "windows"))]
use std::fs;
#[cfg(target_os = "linux")]
pub fn install(set_default: bool) -> io::Result<()> {
let exe = std::env::current_exe()?;
let data_home = xdg_data_home()?;
let plan = linux_install_plan(&data_home, &exe, set_default);
let report = apply_linux_install_plan(&plan)?;
print_linux_install_report(&report);
Ok(())
}
#[cfg(target_os = "linux")]
#[derive(Clone, Debug, PartialEq, Eq)]
struct LinuxInstallPlan {
desktop_file: PathBuf,
desktop_entry: String,
icon_file: PathBuf,
set_default: bool,
default_mimes: &'static [&'static str],
legacy_cleanup_root: Option<PathBuf>,
}
#[cfg(target_os = "linux")]
#[derive(Clone, Debug, PartialEq, Eq)]
struct LinuxInstallReport {
desktop_file: PathBuf,
icon_file: PathBuf,
default: LinuxDefaultOutcome,
}
#[cfg(target_os = "linux")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum LinuxDefaultOutcome {
Applied,
NeedsManualHint,
NotRequested,
}
#[cfg(target_os = "linux")]
fn linux_install_plan(data_home: &Path, current_exe: &Path, set_default: bool) -> LinuxInstallPlan {
LinuxInstallPlan {
desktop_file: data_home.join("applications").join(DESKTOP_FILE_NAME),
desktop_entry: linux_desktop_entry(current_exe),
icon_file: data_home
.join("icons")
.join("hicolor")
.join("scalable")
.join("apps")
.join("mdo.svg"),
set_default,
default_mimes: &["text/markdown", "text/x-markdown"],
legacy_cleanup_root: Some(data_home.to_path_buf()),
}
}
#[cfg(target_os = "linux")]
fn apply_linux_install_plan(plan: &LinuxInstallPlan) -> io::Result<LinuxInstallReport> {
if let Some(applications) = plan.desktop_file.parent() {
fs::create_dir_all(applications)?;
}
if let Some(icon_dir) = plan.icon_file.parent() {
fs::create_dir_all(icon_dir)?;
}
fs::write(&plan.icon_file, SVG_ICON)?;
fs::write(&plan.desktop_file, &plan.desktop_entry)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(&plan.desktop_file, fs::Permissions::from_mode(0o644))?;
}
if let Some(applications) = plan.desktop_file.parent() {
run_optional("update-desktop-database", &[applications.as_os_str()]);
}
let default = if plan.set_default {
let mut ok = false;
for mime in plan.default_mimes {
ok |= run_optional_status(
"xdg-mime",
&[
std::ffi::OsStr::new("default"),
std::ffi::OsStr::new(DESKTOP_FILE_NAME),
std::ffi::OsStr::new(*mime),
],
);
}
if ok {
LinuxDefaultOutcome::Applied
} else {
LinuxDefaultOutcome::NeedsManualHint
}
} else {
LinuxDefaultOutcome::NotRequested
};
if let Some(root) = &plan.legacy_cleanup_root {
remove_legacy_nautilus_scripts(root)?;
}
Ok(LinuxInstallReport {
desktop_file: plan.desktop_file.clone(),
icon_file: plan.icon_file.clone(),
default,
})
}
#[cfg(target_os = "linux")]
fn print_linux_install_report(report: &LinuxInstallReport) {
println!("Installed desktop entry: {}", report.desktop_file.display());
println!("Installed icon: {}", report.icon_file.display());
match report.default {
LinuxDefaultOutcome::Applied => {
println!("{APP_DISPLAY_NAME} is now the default Markdown handler.");
}
LinuxDefaultOutcome::NeedsManualHint => {
println!(
"Could not set {APP_DISPLAY_NAME} as the default automatically (is xdg-mime installed?)."
);
println!("To set it manually, run: xdg-mime default {DESKTOP_FILE_NAME} text/markdown");
println!("(and the same for text/x-markdown)");
}
LinuxDefaultOutcome::NotRequested => {
println!(
"{APP_DISPLAY_NAME} is available from Open With without changing your default Markdown handler."
);
}
}
}
#[cfg(target_os = "windows")]
pub fn install(_set_default: bool) -> io::Result<()> {
let current_exe = std::env::current_exe()?;
let handler_selection = windows_handler_for(¤t_exe);
let command = windows_registry_command(&handler_selection.path, handler_selection.is_wrapper);
let icon_file = install_windows_icon()?;
let icon_ref = format!("\"{}\",0", icon_file.display());
let current_exe_command = windows_registry_command(¤t_exe, false);
let plan = install_windows_plan(
handler_selection.is_wrapper,
&command,
¤t_exe_command,
&icon_ref,
);
apply_registry_plan(&plan)?;
println!("Using handler: {}", handler_selection.path.display());
println!("Using icon: {}", icon_file.display());
if !handler_selection.is_wrapper {
println!(
"Note: mdo-open.exe was not found next to mdo.exe, so Explorer will launch mdo.exe directly."
);
}
println!("{APP_DISPLAY_NAME} is registered for Markdown files.");
println!("To make it the default, use Open with -> Choose another app -> {APP_DISPLAY_NAME} -> Always.");
Ok(())
}
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
pub fn install(_set_default: bool) -> io::Result<()> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"built-in file-manager installation is currently supported on Windows and Linux",
))
}
#[cfg(target_os = "linux")]
pub fn uninstall() -> io::Result<()> {
let data_home = xdg_data_home()?;
let desktop_dir = data_home.join("applications");
remove_linux_handler_files(&data_home)?;
for mimeapps in mimeapps_paths(&data_home) {
remove_desktop_from_mimeapps(&mimeapps)?;
}
run_optional("update-desktop-database", &[desktop_dir.as_os_str()]);
println!("Done.");
Ok(())
}
#[cfg(target_os = "windows")]
pub fn uninstall() -> io::Result<()> {
apply_registry_plan(&uninstall_windows_plan())?;
let _ = remove_windows_icon();
println!("Done.");
println!(
"If {APP_DISPLAY_NAME} was set as the default handler for .md, Windows will prompt you to pick a new default the next time you open a .md file."
);
Ok(())
}
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
pub fn uninstall() -> io::Result<()> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"built-in file-manager uninstallation is currently supported on Windows and Linux",
))
}
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
pub fn integration_installed() -> bool {
false
}
#[cfg(target_os = "linux")]
pub fn integration_installed() -> bool {
xdg_data_home()
.map(|data_home| linux_integration_installed_at(&data_home))
.unwrap_or(false)
}
#[cfg(target_os = "windows")]
pub fn integration_installed() -> bool {
reg_key_exists(WINDOWS_OWNED_PROGID_COMMAND_KEY)
}
#[cfg(target_os = "linux")]
pub fn install_linux_setup_launcher_for_exe(setup_exe: &Path) -> io::Result<PathBuf> {
let data_home = xdg_data_home()?;
let desktop_file = write_linux_setup_launcher(setup_exe, &data_home)?;
let desktop_dir = data_home.join("applications");
run_optional("update-desktop-database", &[desktop_dir.as_os_str()]);
Ok(desktop_file)
}
#[cfg(target_os = "linux")]
fn write_linux_setup_launcher(setup_exe: &Path, data_home: &Path) -> io::Result<PathBuf> {
let desktop_dir = data_home.join("applications");
let desktop_file = desktop_dir.join(SETUP_DESKTOP_FILE_NAME);
fs::create_dir_all(&desktop_dir)?;
fs::write(&desktop_file, linux_setup_desktop_entry(setup_exe))?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(&desktop_file, fs::Permissions::from_mode(0o644))?;
}
Ok(desktop_file)
}
#[cfg(target_os = "linux")]
fn linux_integration_installed_at(data_home: &Path) -> bool {
data_home
.join("applications")
.join(DESKTOP_FILE_NAME)
.is_file()
}
#[cfg(target_os = "linux")]
fn remove_linux_handler_files(data_home: &Path) -> io::Result<()> {
let desktop_file = data_home.join("applications").join(DESKTOP_FILE_NAME);
let icon_file = data_home
.join("icons")
.join("hicolor")
.join("scalable")
.join("apps")
.join("mdo.svg");
remove_file_if_present(&desktop_file)?;
remove_file_if_present(&icon_file)?;
remove_legacy_nautilus_scripts(data_home)
}
#[cfg(target_os = "windows")]
const WINDOWS_OWNED_PROGID_COMMAND_KEY: &str = r"HKCU\Software\Classes\mdo.md\shell\open\command";
#[cfg(target_os = "windows")]
fn reg_key_exists(key: &str) -> bool {
Command::new("reg")
.args(["query", key, "/ve"])
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.map(|status| status.success())
.unwrap_or(false)
}
#[cfg(target_os = "linux")]
fn xdg_data_home() -> io::Result<PathBuf> {
if let Some(value) = std::env::var_os("XDG_DATA_HOME") {
return Ok(PathBuf::from(value));
}
let home = std::env::var_os("HOME").ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
"HOME is not set; cannot locate ~/.local/share",
)
})?;
Ok(PathBuf::from(home).join(".local").join("share"))
}
#[cfg(target_os = "linux")]
fn linux_desktop_entry(exe: &Path) -> String {
let quoted_exe = quote_desktop_exec_arg(&exe.to_string_lossy());
let open_flags = handler::OPEN_FLAGS.join(" ");
format!(
"[Desktop Entry]\n\
Type=Application\n\
Name={APP_DISPLAY_NAME}\n\
GenericName=Markdown HTML Opener\n\
Comment=Open Markdown as HTML in the default browser\n\
Exec={quoted_exe} {open_flags} %f\n\
Icon=mdo\n\
Terminal=false\n\
NoDisplay=true\n\
MimeType=text/markdown;text/x-markdown;\n\
Categories=Utility;TextTools;\n\
StartupNotify=false\n"
)
}
#[cfg(target_os = "linux")]
fn linux_setup_desktop_entry(setup_exe: &Path) -> String {
let quoted_exe = quote_desktop_exec_arg(&setup_exe.to_string_lossy());
format!(
"[Desktop Entry]\n\
Type=Application\n\
Name=mdo Setup\n\
GenericName=Markdown Opener Setup\n\
Comment=Set up mdo and open the first-run tour\n\
Exec={quoted_exe}\n\
Icon=utilities-terminal\n\
Terminal=false\n\
NoDisplay=false\n\
Categories=Utility;TextTools;\n\
StartupNotify=false\n"
)
}
#[cfg(target_os = "linux")]
fn quote_desktop_exec_arg(value: &str) -> String {
let mut quoted = String::with_capacity(value.len() + 2);
quoted.push('"');
for ch in value.chars() {
match ch {
'\\' | '"' | '$' | '`' => {
quoted.push('\\');
quoted.push(ch);
}
_ => quoted.push(ch),
}
}
quoted.push('"');
quoted
}
#[cfg(target_os = "linux")]
fn run_optional(program: &str, args: &[&std::ffi::OsStr]) {
let _ = run_optional_status(program, args);
}
#[cfg(target_os = "linux")]
fn run_optional_status(program: &str, args: &[&std::ffi::OsStr]) -> bool {
Command::new(program)
.args(args)
.status()
.map(|status| status.success())
.unwrap_or(false)
}
#[cfg(target_os = "linux")]
fn remove_legacy_nautilus_scripts(data_home: &Path) -> io::Result<()> {
let nautilus_dir = data_home.join("nautilus").join("scripts");
for name in ["Open as HTML", "Preview with mdo", "Render with mdo"] {
remove_file_if_present(&nautilus_dir.join(name))?;
}
Ok(())
}
#[cfg(target_os = "linux")]
fn remove_file_if_present(path: &Path) -> io::Result<()> {
match fs::remove_file(path) {
Ok(()) => {
println!("Removed: {}", path.display());
Ok(())
}
Err(e) if e.kind() == io::ErrorKind::NotFound => {
println!("Skip : {} (not present)", path.display());
Ok(())
}
Err(e) => Err(e),
}
}
#[cfg(target_os = "linux")]
fn mimeapps_paths(data_home: &Path) -> Vec<PathBuf> {
let mut paths = Vec::new();
if let Some(config_home) = std::env::var_os("XDG_CONFIG_HOME") {
paths.push(PathBuf::from(config_home).join("mimeapps.list"));
} else if let Some(home) = std::env::var_os("HOME") {
paths.push(PathBuf::from(home).join(".config").join("mimeapps.list"));
}
paths.push(data_home.join("applications").join("mimeapps.list"));
paths
}
#[cfg(target_os = "linux")]
fn remove_desktop_from_mimeapps(path: &Path) -> io::Result<()> {
let contents = match fs::read_to_string(path) {
Ok(contents) => contents,
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(()),
Err(e) => return Err(e),
};
let updated = strip_mdo_from_mimeapps(&contents);
if updated != contents {
fs::write(path, updated)?;
println!("Updated: {}", path.display());
}
Ok(())
}
#[cfg(target_os = "linux")]
fn strip_mdo_from_mimeapps(contents: &str) -> String {
let mut section = "";
let mut out = Vec::new();
for line in contents.lines() {
match line {
"[Default Applications]" => {
section = "default";
out.push(line.to_string());
continue;
}
"[Added Associations]" => {
section = "added";
out.push(line.to_string());
continue;
}
_ if line.starts_with('[') => {
section = "";
out.push(line.to_string());
continue;
}
_ => {}
}
if section == "default"
&& (line == "text/markdown=mdo.desktop" || line == "text/x-markdown=mdo.desktop")
{
continue;
}
if section == "added"
&& (line.starts_with("text/markdown=") || line.starts_with("text/x-markdown="))
{
if let Some((mime, apps)) = line.split_once('=') {
let apps = apps
.split(';')
.filter(|app| !app.is_empty() && *app != DESKTOP_FILE_NAME)
.collect::<Vec<_>>();
if apps.is_empty() {
continue;
}
out.push(format!("{mime}={};", apps.join(";")));
continue;
}
}
out.push(line.to_string());
}
let mut result = out.join("\n");
if contents.ends_with('\n') {
result.push('\n');
}
result
}
#[cfg(target_os = "windows")]
struct WindowsHandler {
path: PathBuf,
is_wrapper: bool,
}
#[cfg(target_os = "windows")]
fn windows_handler_for(current_exe: &Path) -> WindowsHandler {
let sibling_wrapper = handler::sibling_binary(current_exe, "mdo-open.exe").ok();
match sibling_wrapper {
Some(path) => WindowsHandler {
path,
is_wrapper: true,
},
None => WindowsHandler {
path: current_exe.to_path_buf(),
is_wrapper: false,
},
}
}
#[cfg(target_os = "windows")]
fn windows_registry_command(handler_path: &Path, is_wrapper: bool) -> String {
if is_wrapper {
format!("\"{}\" \"%1\"", handler_path.display())
} else {
format!(
"\"{}\" {} \"%1\"",
handler_path.display(),
handler::OPEN_FLAGS.join(" ")
)
}
}
#[cfg(target_os = "windows")]
#[derive(Debug, Clone, PartialEq, Eq)]
struct RegistryOp {
action: RegistryAction,
best_effort: bool,
}
#[cfg(target_os = "windows")]
#[derive(Debug, Clone, PartialEq, Eq)]
enum RegistryAction {
SetDefault {
key: String,
value: String,
},
SetValue {
key: String,
name: String,
value: String,
},
DeleteKey {
key: String,
},
DeleteValue {
key: String,
name: String,
},
}
#[cfg(target_os = "windows")]
impl RegistryOp {
fn required(action: RegistryAction) -> Self {
Self {
action,
best_effort: false,
}
}
fn best_effort(action: RegistryAction) -> Self {
Self {
action,
best_effort: true,
}
}
}
#[cfg(target_os = "windows")]
fn application_registration_ops(exe_name: &str, command: &str, icon_ref: &str) -> Vec<RegistryOp> {
let key = format!(r"HKCU\Software\Classes\Applications\{exe_name}");
vec![
RegistryOp::required(RegistryAction::SetValue {
key: key.clone(),
name: "FriendlyAppName".to_string(),
value: APP_DISPLAY_NAME.to_string(),
}),
RegistryOp::required(RegistryAction::SetValue {
key: key.clone(),
name: "ApplicationName".to_string(),
value: APP_DISPLAY_NAME.to_string(),
}),
RegistryOp::required(RegistryAction::SetDefault {
key: format!(r"{key}\DefaultIcon"),
value: icon_ref.to_string(),
}),
RegistryOp::required(RegistryAction::SetDefault {
key: format!(r"{key}\shell\open\command"),
value: command.to_string(),
}),
RegistryOp::required(RegistryAction::SetValue {
key: format!(r"{key}\SupportedTypes"),
name: ".md".to_string(),
value: String::new(),
}),
]
}
#[cfg(target_os = "windows")]
fn install_windows_plan(
is_wrapper: bool,
command: &str,
current_exe_command: &str,
icon_ref: &str,
) -> Vec<RegistryOp> {
let mut ops = application_registration_ops("mdo.exe", current_exe_command, icon_ref);
if is_wrapper {
ops.extend(application_registration_ops(
"mdo-open.exe",
command,
icon_ref,
));
}
ops.push(RegistryOp::required(RegistryAction::SetValue {
key: r"HKCU\Software\Classes\.md\OpenWithProgids".to_string(),
name: "mdo.md".to_string(),
value: String::new(),
}));
ops.push(RegistryOp::required(RegistryAction::SetDefault {
key: r"HKCU\Software\Classes\mdo.md".to_string(),
value: "Markdown document (Open as HTML)".to_string(),
}));
ops.push(RegistryOp::required(RegistryAction::SetDefault {
key: WINDOWS_OWNED_PROGID_COMMAND_KEY.to_string(),
value: command.to_string(),
}));
ops.push(RegistryOp::required(RegistryAction::SetDefault {
key: r"HKCU\Software\Classes\mdo.md\DefaultIcon".to_string(),
value: icon_ref.to_string(),
}));
for old_verb in [
r"HKCU\Software\Classes\SystemFileAssociations\.md\shell\Preview with mdo",
r"HKCU\Software\Classes\SystemFileAssociations\.md\shell\Render with mdo",
] {
ops.push(RegistryOp::best_effort(RegistryAction::DeleteKey {
key: old_verb.to_string(),
}));
}
let verb = r"HKCU\Software\Classes\SystemFileAssociations\.md\shell\Open as HTML";
ops.push(RegistryOp::required(RegistryAction::SetDefault {
key: verb.to_string(),
value: APP_DISPLAY_NAME.to_string(),
}));
ops.push(RegistryOp::required(RegistryAction::SetDefault {
key: format!(r"{verb}\command"),
value: command.to_string(),
}));
ops.push(RegistryOp::required(RegistryAction::SetValue {
key: verb.to_string(),
name: "Icon".to_string(),
value: icon_ref.to_string(),
}));
ops
}
#[cfg(target_os = "windows")]
fn uninstall_windows_plan() -> Vec<RegistryOp> {
let mut ops = Vec::new();
for key in [
r"HKCU\Software\Classes\Applications\mdo.exe",
r"HKCU\Software\Classes\Applications\mdo-open.exe",
r"HKCU\Software\Classes\mdo.md",
r"HKCU\Software\Classes\SystemFileAssociations\.md\shell\Open as HTML",
r"HKCU\Software\Classes\SystemFileAssociations\.md\shell\Preview with mdo",
r"HKCU\Software\Classes\SystemFileAssociations\.md\shell\Render with mdo",
r"HKCU\Software\Classes\Applications\md2htmlx.exe",
r"HKCU\Software\Classes\md2htmlx.md",
r"HKCU\Software\Classes\SystemFileAssociations\.md\shell\Render with md2htmlx",
] {
ops.push(RegistryOp::best_effort(RegistryAction::DeleteKey {
key: key.to_string(),
}));
}
ops.push(RegistryOp::best_effort(RegistryAction::DeleteValue {
key: r"HKCU\Software\Classes\.md\OpenWithProgids".to_string(),
name: "mdo.md".to_string(),
}));
ops.push(RegistryOp::best_effort(RegistryAction::DeleteValue {
key: r"HKCU\Software\Classes\.md\OpenWithProgids".to_string(),
name: "md2htmlx.md".to_string(),
}));
ops
}
#[cfg(target_os = "windows")]
fn apply_registry_plan(ops: &[RegistryOp]) -> io::Result<()> {
for op in ops {
let result = apply_registry_action(&op.action);
if !op.best_effort {
result?;
}
}
Ok(())
}
#[cfg(target_os = "windows")]
fn apply_registry_action(action: &RegistryAction) -> io::Result<()> {
match action {
RegistryAction::SetDefault { key, value } => reg_add_default(key, value),
RegistryAction::SetValue { key, name, value } => reg_add_value(key, name, value),
RegistryAction::DeleteKey { key } => reg_delete_key(key),
RegistryAction::DeleteValue { key, name } => reg_delete_value(key, name),
}
}
#[cfg(target_os = "windows")]
fn install_windows_icon() -> io::Result<PathBuf> {
let icon_file = windows_icon_file()?;
if let Some(parent) = icon_file.parent() {
fs::create_dir_all(parent)?;
}
fs::write(&icon_file, WINDOWS_ICON_BYTES)?;
Ok(icon_file)
}
#[cfg(target_os = "windows")]
fn remove_windows_icon() -> io::Result<()> {
let icon_file = match windows_icon_file() {
Ok(path) => path,
Err(_) => return Ok(()),
};
match fs::remove_file(&icon_file) {
Ok(()) => {}
Err(e) if e.kind() == io::ErrorKind::NotFound => {}
Err(e) => return Err(e),
}
if let Some(parent) = icon_file.parent() {
match fs::remove_dir(parent) {
Ok(()) => {}
Err(e)
if matches!(
e.kind(),
io::ErrorKind::NotFound | io::ErrorKind::DirectoryNotEmpty
) => {}
Err(e) => return Err(e),
}
}
Ok(())
}
#[cfg(target_os = "windows")]
fn windows_icon_file() -> io::Result<PathBuf> {
let base = std::env::var_os("LOCALAPPDATA")
.or_else(|| std::env::var_os("APPDATA"))
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
"LOCALAPPDATA and APPDATA are not set; cannot install icon",
)
})?;
Ok(PathBuf::from(base).join("mdo").join("mdo.ico"))
}
#[cfg(target_os = "windows")]
fn reg_add_default(key: &str, value: &str) -> io::Result<()> {
reg_status(
Command::new("reg").args(["add", key, "/ve", "/d", value, "/f"]),
"reg add",
)
}
#[cfg(target_os = "windows")]
fn reg_add_value(key: &str, name: &str, value: &str) -> io::Result<()> {
reg_status(
Command::new("reg").args(["add", key, "/v", name, "/t", "REG_SZ", "/d", value, "/f"]),
"reg add",
)
}
#[cfg(target_os = "windows")]
fn reg_delete_key(key: &str) -> io::Result<()> {
reg_status(
Command::new("reg").args(["delete", key, "/f"]),
"reg delete",
)
}
#[cfg(target_os = "windows")]
fn reg_delete_value(key: &str, name: &str) -> io::Result<()> {
reg_status(
Command::new("reg").args(["delete", key, "/v", name, "/f"]),
"reg delete",
)
}
#[cfg(target_os = "windows")]
fn reg_status(command: &mut Command, action: &str) -> io::Result<()> {
let output = command
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()?;
if output.status.success() {
Ok(())
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
let details = stderr.trim();
let details = if details.is_empty() {
stdout.trim()
} else {
details
};
let suffix = if details.is_empty() {
String::new()
} else {
format!(": {details}")
};
Err(io::Error::other(format!(
"{action} failed with status {}{suffix}",
output.status
)))
}
}
#[cfg(all(test, target_os = "linux"))]
mod linux_tests {
use super::*;
#[test]
fn desktop_exec_arg_escapes_special_chars() {
assert_eq!(
quote_desktop_exec_arg(r#"/tmp/a\b"c$d`e/mdo"#),
r#""/tmp/a\\b\"c\$d\`e/mdo""#
);
}
#[test]
fn linux_install_plan_derives_standard_paths_and_entry_content() {
let data_home = Path::new("/home/u/.local/share");
let exe = Path::new("/opt/my tools/mdo");
let plan = linux_install_plan(data_home, exe, true);
assert_eq!(
plan.desktop_file,
data_home.join("applications").join(DESKTOP_FILE_NAME)
);
assert_eq!(
plan.icon_file,
data_home
.join("icons")
.join("hicolor")
.join("scalable")
.join("apps")
.join("mdo.svg")
);
assert!(plan
.desktop_entry
.contains("Exec=\"/opt/my tools/mdo\" --open %f\n"));
assert!(plan.set_default);
assert_eq!(plan.default_mimes, &["text/markdown", "text/x-markdown"]);
assert_eq!(plan.legacy_cleanup_root, Some(data_home.to_path_buf()));
let plan_without_default = linux_install_plan(data_home, exe, false);
assert!(!plan_without_default.set_default);
}
#[test]
fn apply_linux_install_plan_writes_entry_and_icon_and_reports() {
let dir = std::env::temp_dir().join(format!(
"mdo-install-plan-test-{}-{:?}",
std::process::id(),
std::thread::current().id()
));
let exe = dir.join("mdo");
fs::create_dir_all(&dir).unwrap();
let mut plan = linux_install_plan(&dir, &exe, false);
plan.legacy_cleanup_root = None;
let report = apply_linux_install_plan(&plan).expect("apply should write the registration");
assert_eq!(
report.default,
LinuxDefaultOutcome::NotRequested,
"no default was requested"
);
let entry = fs::read_to_string(&report.desktop_file).expect("entry written");
assert!(entry.contains("[Desktop Entry]\n"));
assert!(
entry.contains(&format!("Exec=\"{}\" --open %f\n", exe.display())),
"entry should carry the contract Exec line: {entry}"
);
let icon = fs::read_to_string(&report.icon_file).expect("icon written");
assert!(icon.contains("<svg"));
use std::os::unix::fs::PermissionsExt;
let mode = fs::metadata(&report.desktop_file)
.expect("entry metadata")
.permissions()
.mode();
assert_eq!(mode & 0o777, 0o644, "the entry must stay mode 0644");
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn apply_linux_install_plan_requested_default_without_mimes_needs_manual_hint() {
let dir = std::env::temp_dir().join(format!(
"mdo-install-plan-hint-test-{}-{:?}",
std::process::id(),
std::thread::current().id()
));
let exe = dir.join("mdo");
fs::create_dir_all(&dir).unwrap();
let mut plan = linux_install_plan(&dir, &exe, true);
plan.default_mimes = &[];
plan.legacy_cleanup_root = None;
let report = apply_linux_install_plan(&plan).expect("apply should complete");
assert_eq!(report.default, LinuxDefaultOutcome::NeedsManualHint);
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn setup_launcher_is_visible_and_not_a_file_handler() {
let entry = linux_setup_desktop_entry(Path::new(r#"/opt/mdo tools/a\b"c$d`e/mdo-setup"#));
assert!(entry.contains("Name=mdo Setup\n"));
assert!(entry.contains(r#"Exec="/opt/mdo tools/a\\b\"c\$d\`e/mdo-setup""#));
assert!(entry.contains("Terminal=false\n"));
assert!(entry.contains("NoDisplay=false\n"));
assert!(!entry.contains("MimeType="));
assert!(!entry.contains("%f"));
}
#[test]
fn setup_launcher_has_separate_handler_ownership() {
assert_ne!(SETUP_DESKTOP_FILE_NAME, DESKTOP_FILE_NAME);
let entry = linux_setup_desktop_entry(Path::new("/usr/bin/mdo-setup"));
assert!(!entry.contains("Icon=mdo\n"));
}
#[test]
fn handler_uninstall_preserves_setup_launcher() {
let data_home = std::env::temp_dir().join(format!(
"mdo-setup-launcher-test-{}-{:?}",
std::process::id(),
std::thread::current().id()
));
let applications = data_home.join("applications");
fs::create_dir_all(&applications).unwrap();
fs::write(applications.join(DESKTOP_FILE_NAME), "handler").unwrap();
let setup_file =
write_linux_setup_launcher(Path::new("/opt/mdo/bin/mdo-setup"), &data_home).unwrap();
fs::write(&setup_file, "stale").unwrap();
write_linux_setup_launcher(Path::new("/opt/mdo/bin/mdo-setup"), &data_home).unwrap();
remove_linux_handler_files(&data_home).unwrap();
assert!(!applications.join(DESKTOP_FILE_NAME).exists());
assert!(setup_file.exists());
assert!(fs::read_to_string(&setup_file)
.unwrap()
.contains("Exec=\"/opt/mdo/bin/mdo-setup\"\n"));
fs::remove_dir_all(data_home).unwrap();
}
#[test]
fn owned_state_check_tracks_only_the_handler_desktop_file() {
let data_home = std::env::temp_dir().join(format!(
"mdo-owned-state-test-{}-{:?}",
std::process::id(),
std::thread::current().id()
));
assert!(!linux_integration_installed_at(&data_home));
write_linux_setup_launcher(Path::new("/usr/bin/mdo-setup"), &data_home).unwrap();
assert!(!linux_integration_installed_at(&data_home));
let applications = data_home.join("applications");
fs::write(applications.join(DESKTOP_FILE_NAME), "handler").unwrap();
assert!(linux_integration_installed_at(&data_home));
remove_linux_handler_files(&data_home).unwrap();
assert!(!linux_integration_installed_at(&data_home));
fs::remove_dir_all(data_home).unwrap();
}
#[test]
fn mimeapps_uninstall_removes_only_mdo_entries() {
let input = "\
[Default Applications]\n\
text/markdown=mdo.desktop\n\
text/plain=code.desktop\n\
[Added Associations]\n\
text/markdown=code.desktop;mdo.desktop;other.desktop;\n\
text/x-markdown=mdo.desktop;\n\
image/png=viewer.desktop;\n";
let output = strip_mdo_from_mimeapps(input);
assert_eq!(
output,
"\
[Default Applications]\n\
text/plain=code.desktop\n\
[Added Associations]\n\
text/markdown=code.desktop;other.desktop;\n\
image/png=viewer.desktop;\n"
);
}
#[test]
fn desktop_entry_reflects_the_handler_invocation_contract() {
let entry = linux_desktop_entry(Path::new("/opt/mdo/mdo"));
let expected = format!(
"Exec=\"/opt/mdo/mdo\" {} %f\n",
handler::OPEN_FLAGS.join(" ")
);
assert!(
entry.contains(&expected),
"entry should carry the contract Exec line: {entry}"
);
let setup_entry = linux_setup_desktop_entry(Path::new("/usr/bin/mdo-setup"));
assert!(!setup_entry.contains(handler::OPEN_FLAGS[0]));
}
}
#[cfg(all(test, target_os = "windows"))]
mod windows_tests {
use super::*;
#[test]
fn registry_command_reflects_the_handler_invocation_contract() {
let command = windows_registry_command(Path::new(r"C:\Tools\mdo.exe"), false);
let expected = format!(
r#""C:\Tools\mdo.exe" {} "%1""#,
handler::OPEN_FLAGS.join(" ")
);
assert_eq!(command, expected);
let wrapper = windows_registry_command(Path::new(r"C:\Tools\mdo-open.exe"), true);
assert_eq!(wrapper, r#""C:\Tools\mdo-open.exe" "%1""#);
assert!(!wrapper.contains(handler::OPEN_FLAGS[0]));
}
#[test]
fn registry_command_uses_open_flag_without_wrapper() {
let command = windows_registry_command(Path::new(r"C:\Tools\mdo.exe"), false);
assert_eq!(command, r#""C:\Tools\mdo.exe" --open "%1""#);
}
#[test]
fn registry_command_omits_open_flag_for_wrapper() {
let command = windows_registry_command(Path::new(r"C:\Tools\mdo-open.exe"), true);
assert_eq!(command, r#""C:\Tools\mdo-open.exe" "%1""#);
}
#[test]
fn registry_command_preserves_spaces_in_exe_path() {
let command =
windows_registry_command(Path::new(r"C:\Program Files\mdo tools\mdo.exe"), false);
assert_eq!(
command,
r#""C:\Program Files\mdo tools\mdo.exe" --open "%1""#
);
}
#[test]
fn registry_command_preserves_cmd_metacharacters_in_exe_path() {
let command =
windows_registry_command(Path::new(r"C:\Program Files (x86)\mdo & co^\mdo.exe"), true);
assert_eq!(
command,
r#""C:\Program Files (x86)\mdo & co^\mdo.exe" "%1""#
);
}
#[test]
fn install_plan_registers_wrapper_and_direct_targets_when_wrapper_present() {
let handler_command =
windows_registry_command(Path::new(r"C:\Program Files\mdo & co\mdo-open.exe"), true);
let current_exe_command =
windows_registry_command(Path::new(r"C:\Program Files\mdo & co\mdo.exe"), false);
let icon_ref = r#""C:\Users\me\AppData\Local\mdo\mdo.ico",0"#;
let plan = install_windows_plan(true, &handler_command, ¤t_exe_command, icon_ref);
assert!(
plan.contains(&RegistryOp::required(RegistryAction::SetDefault {
key: r"HKCU\Software\Classes\Applications\mdo.exe\shell\open\command".to_string(),
value: current_exe_command.clone(),
}))
);
assert!(
plan.contains(&RegistryOp::required(RegistryAction::SetDefault {
key: r"HKCU\Software\Classes\Applications\mdo-open.exe\shell\open\command"
.to_string(),
value: handler_command.clone(),
}))
);
assert!(
plan.contains(&RegistryOp::required(RegistryAction::SetDefault {
key: r"HKCU\Software\Classes\mdo.md\shell\open\command".to_string(),
value: handler_command.clone(),
}))
);
assert!(
plan.contains(&RegistryOp::best_effort(RegistryAction::DeleteKey {
key: r"HKCU\Software\Classes\SystemFileAssociations\.md\shell\Preview with mdo"
.to_string(),
}))
);
assert!(
plan.contains(&RegistryOp::best_effort(RegistryAction::DeleteKey {
key: r"HKCU\Software\Classes\SystemFileAssociations\.md\shell\Render with mdo"
.to_string(),
}))
);
let best_effort_count = plan.iter().filter(|op| op.best_effort).count();
assert_eq!(best_effort_count, 2);
}
#[test]
fn install_plan_skips_wrapper_registration_without_wrapper() {
let command = windows_registry_command(Path::new(r"C:\Tools\mdo.exe"), false);
let icon_ref = r#""C:\Tools\mdo.ico",0"#;
let plan = install_windows_plan(false, &command, &command, icon_ref);
assert!(!plan.iter().any(|op| matches!(
&op.action,
RegistryAction::SetDefault { key, .. } | RegistryAction::SetValue { key, .. }
if key.contains("mdo-open.exe")
)));
}
#[test]
fn owned_state_check_queries_a_key_install_writes_and_uninstall_removes() {
let command = windows_registry_command(Path::new(r"C:\Tools\mdo.exe"), false);
let plan = install_windows_plan(false, &command, &command, r#""C:\Tools\mdo.ico",0"#);
assert!(
plan.contains(&RegistryOp::required(RegistryAction::SetDefault {
key: WINDOWS_OWNED_PROGID_COMMAND_KEY.to_string(),
value: command.clone(),
}))
);
assert!(uninstall_windows_plan().iter().any(|op| matches!(
&op.action,
RegistryAction::DeleteKey { key }
if key.as_str() == WINDOWS_OWNED_PROGID_COMMAND_KEY
|| WINDOWS_OWNED_PROGID_COMMAND_KEY.starts_with(&format!(r"{key}\"))
)));
}
#[test]
fn uninstall_plan_is_entirely_best_effort() {
let plan = uninstall_windows_plan();
assert!(!plan.is_empty());
assert!(plan.iter().all(|op| op.best_effort));
assert!(
plan.contains(&RegistryOp::best_effort(RegistryAction::DeleteKey {
key: r"HKCU\Software\Classes\Applications\md2htmlx.exe".to_string(),
}))
);
assert!(
plan.contains(&RegistryOp::best_effort(RegistryAction::DeleteValue {
key: r"HKCU\Software\Classes\.md\OpenWithProgids".to_string(),
name: "md2htmlx.md".to_string(),
}))
);
}
}