use std::collections::HashSet;
use std::path::PathBuf;
use once_cell::sync::Lazy;
use plist::Value;
use sysinfo::System;
pub static APPLEBOOKS_VERSION: Lazy<String> = Lazy::new(|| {
let path: PathBuf = [
"/",
"System",
"Applications",
"Books.app",
"Contents",
"Info.plist",
]
.iter()
.collect();
let Ok(value) = Value::from_file(path) else {
log::warn!("could not determine Apple Books version");
return "v?".to_owned();
};
let version_short = value
.as_dictionary()
.and_then(|d| d.get("CFBundleShortVersionString"))
.and_then(plist::Value::as_string)
.unwrap_or_else(|| {
log::warn!("could not determine 'CFBundleShortVersionString'");
"?"
});
let version_long = value
.as_dictionary()
.and_then(|d| d.get("CFBundleVersion"))
.and_then(plist::Value::as_string)
.unwrap_or_else(|| {
log::warn!("could not determine 'CFBundleVersion'");
"?"
});
format!("v{version_short}-{version_long}")
});
#[must_use]
pub fn applebooks_is_running() -> bool {
let process_names: HashSet<String> = System::new_all()
.processes()
.values()
.map(|process| process.name().to_string_lossy())
.map(String::from)
.collect();
!super::defaults::APPLEBOOKS_NAMES.is_disjoint(&process_names)
}