124 lines
3.7 KiB
Rust
124 lines
3.7 KiB
Rust
//! Login autostart integration.
|
|
//!
|
|
//! Linux uses an XDG autostart entry, Windows a script in `shell:startup`.
|
|
//! Both use a stable file name and only ever touch that one file.
|
|
|
|
/// Stable name of our XDG autostart entry.
|
|
#[cfg(target_os = "linux")]
|
|
pub const LINUX_FILE: &str = "dev.moorly.Moorly.desktop";
|
|
/// Stable name of our Windows startup script.
|
|
#[cfg(windows)]
|
|
pub const WINDOWS_FILE: &str = "Moorly.cmd";
|
|
|
|
#[cfg(target_os = "linux")]
|
|
fn entry_path() -> Option<std::path::PathBuf> {
|
|
let config = std::env::var_os("XDG_CONFIG_HOME")
|
|
.map(std::path::PathBuf::from)
|
|
.or_else(|| {
|
|
std::env::var_os("HOME").map(|home| std::path::PathBuf::from(home).join(".config"))
|
|
})?;
|
|
|
|
Some(config.join("autostart").join(LINUX_FILE))
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
fn entry_path() -> Option<std::path::PathBuf> {
|
|
let appdata = std::env::var_os("APPDATA").map(std::path::PathBuf::from)?;
|
|
|
|
Some(
|
|
appdata
|
|
.join("Microsoft")
|
|
.join("Windows")
|
|
.join("Start Menu")
|
|
.join("Programs")
|
|
.join("Startup")
|
|
.join(WINDOWS_FILE),
|
|
)
|
|
}
|
|
|
|
#[cfg(not(any(target_os = "linux", windows)))]
|
|
fn entry_path() -> Option<std::path::PathBuf> {
|
|
None
|
|
}
|
|
|
|
/// Whether our autostart entry exists.
|
|
pub fn enabled() -> bool {
|
|
entry_path().is_some_and(|path| path.is_file())
|
|
}
|
|
|
|
/// Creates or refreshes our autostart entry.
|
|
pub fn enable() -> Result<(), String> {
|
|
let path = entry_path()
|
|
.ok_or_else(|| "Autostart at login is only supported on Linux and Windows.".to_owned())?;
|
|
let executable = std::env::current_exe()
|
|
.map_err(|error| format!("Could not find the Moorly executable: {error}"))?;
|
|
|
|
if let Some(dir) = path.parent() {
|
|
std::fs::create_dir_all(dir)
|
|
.map_err(|error| format!("Could not create the autostart folder: {error}"))?;
|
|
}
|
|
|
|
std::fs::write(&path, entry_contents(&executable))
|
|
.map_err(|error| format!("Could not write the autostart entry: {error}"))?;
|
|
|
|
#[cfg(target_os = "linux")]
|
|
crate::icons::refresh_linux_caches();
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Removes our autostart entry, leaving every other entry untouched.
|
|
pub fn disable() -> Result<(), String> {
|
|
let Some(path) = entry_path() else {
|
|
return Ok(());
|
|
};
|
|
|
|
match std::fs::remove_file(&path) {
|
|
Ok(()) => Ok(()),
|
|
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
|
|
Err(error) => Err(format!("Could not remove the autostart entry: {error}")),
|
|
}
|
|
}
|
|
|
|
#[cfg(target_os = "linux")]
|
|
fn entry_contents(executable: &std::path::Path) -> String {
|
|
let executable = executable.display().to_string();
|
|
let executable = executable.replace('\\', "\\\\").replace('"', "\\\"");
|
|
|
|
format!(
|
|
"[Desktop Entry]\n\
|
|
Type=Application\n\
|
|
Name=Moorly\n\
|
|
Comment=SSH and terminal client\n\
|
|
Exec=\"{executable}\"\n\
|
|
Icon=dev.moorly.Moorly\n\
|
|
Terminal=false\n\
|
|
Categories=Network;Utility;TerminalEmulator;\n\
|
|
StartupNotify=true\n\
|
|
StartupWMClass=Moorly\n\
|
|
X-Moorly-Generated=true\n"
|
|
)
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
fn entry_contents(executable: &std::path::Path) -> String {
|
|
format!("@echo off\r\nstart \"\" \"{}\"\r\n", executable.display())
|
|
}
|
|
|
|
#[cfg(not(any(target_os = "linux", windows)))]
|
|
fn entry_contents(_executable: &std::path::Path) -> String {
|
|
String::new()
|
|
}
|
|
|
|
#[cfg(all(test, target_os = "linux"))]
|
|
mod tests {
|
|
#[test]
|
|
fn entry_is_marked_as_ours() {
|
|
let entry = super::entry_contents(std::path::Path::new("/usr/bin/moorly"));
|
|
|
|
assert!(entry.contains("Exec=\"/usr/bin/moorly\""));
|
|
assert!(entry.contains("Icon=dev.moorly.Moorly"));
|
|
assert!(entry.contains("X-Moorly-Generated=true"));
|
|
}
|
|
}
|