use anyhow::{Context, Result};
use std::fs;
use std::path::PathBuf;
pub fn install_desktop_integration() -> Result<()> {
#[cfg(target_os = "linux")]
{
install_desktop_file()?;
install_icon()?;
}
Ok(())
}
#[cfg(target_os = "linux")]
fn install_desktop_file() -> Result<()> {
let home = dirs::home_dir().context("Could not locate home directory")?;
let applications_dir = home.join(".local/share/applications");
fs::create_dir_all(&applications_dir)
.context("Failed to create ~/.local/share/applications")?;
let desktop_file = applications_dir.join("podfetch.desktop");
let desktop_content = r#"[Desktop Entry]
Version=1.0
Type=Application
Name=PodFetch
Comment=CLI/TUI podcast downloader
Icon=podfetch
Exec=podfetch %U
Terminal=true
Categories=Utility;Audio;
Keywords=podcast;downloader;rss;
MimeType=application/rss+xml;
"#;
fs::write(&desktop_file, desktop_content)
.context("Failed to write .desktop file")?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let perms = fs::Permissions::from_mode(0o644);
fs::set_permissions(&desktop_file, perms)
.context("Failed to set permissions on .desktop file")?;
}
log::info!(
"Installed desktop file to {}",
desktop_file.display()
);
if which::which("update-desktop-database").is_ok() {
let _ = std::process::Command::new("update-desktop-database")
.arg(&applications_dir)
.output();
}
Ok(())
}
#[cfg(target_os = "linux")]
fn install_icon() -> Result<()> {
let home = dirs::home_dir().context("Could not locate home directory")?;
let icons_dir = home.join(".local/share/icons/hicolor/256x256/apps");
fs::create_dir_all(&icons_dir).context("Failed to create icon directory")?;
let target_icon = icons_dir.join("podfetch.png");
let exe_path = std::env::current_exe()
.context("Could not determine executable path")?;
let exe_dir = exe_path.parent().context("Could not get executable directory")?;
let possible_icon_paths = vec![
exe_dir.join("icon.png"), exe_dir.parent()
.map(|p| p.join("icon.png")) .unwrap_or_default(),
PathBuf::from("/opt/podfetch/icon.png"), ];
for icon_path in possible_icon_paths {
if icon_path.exists() {
fs::copy(&icon_path, &target_icon)
.context("Failed to copy icon")?;
log::info!(
"Installed icon to {}",
target_icon.display()
);
if which::which("gtk-update-icon-cache").is_ok() {
let cache_dir = home.join(".local/share/icons/hicolor");
let _ = std::process::Command::new("gtk-update-icon-cache")
.arg(&cache_dir)
.output();
}
return Ok(());
}
}
log::warn!("Could not find icon.png for desktop integration");
Ok(())
}
#[allow(dead_code)]
fn get_executable_name() -> String {
std::env::current_exe()
.ok()
.and_then(|p| p.file_name().map(|n| n.to_string_lossy().to_string()))
.unwrap_or_else(|| "podfetch".to_string())
}