use std::path::Path;
use anyhow::{Context, Result, bail};
use windows::Data::Xml::Dom::XmlDocument;
use windows::UI::Notifications::{ToastNotification, ToastNotificationManager};
use windows::core::HSTRING;
use windows_sys::Win32::Foundation::ERROR_SUCCESS;
use windows_sys::Win32::System::Registry::{HKEY_CURRENT_USER, REG_SZ, RegSetKeyValueW};
const APP_ID: &str = "sne.sne";
fn wide(text: &str) -> Vec<u16> {
text.encode_utf16().chain([0]).collect()
}
fn set_string(key: &str, name: &str, value: &str) -> Result<()> {
let (key, name, value) = (wide(key), wide(name), wide(value));
let status = unsafe {
RegSetKeyValueW(
HKEY_CURRENT_USER,
key.as_ptr(),
name.as_ptr(),
REG_SZ,
value.as_ptr().cast(),
(value.len() * 2) as u32,
)
};
if status != ERROR_SUCCESS {
bail!("could not write to the registry (Windows error {status})");
}
Ok(())
}
fn register(logo: Option<&Path>) -> Result<()> {
let key = format!(r"Software\Classes\AppUserModelId\{APP_ID}");
set_string(&key, "DisplayName", "sne")?;
if let Some(logo) = logo.and_then(Path::to_str) {
set_string(&key, "IconUri", logo)?;
}
Ok(())
}
fn escape(text: &str) -> String {
text.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('"', """)
.replace('\'', "'")
}
fn toast_xml(title: &str, body: &str, url: &str) -> String {
let (title, body, url) = (escape(title), escape(body), escape(url));
format!(
"<toast activationType=\"protocol\" launch=\"{url}\">\
<visual><binding template=\"ToastGeneric\"><text>{title}</text><text>{body}</text></binding></visual>\
<actions><action content=\"See what's new\" activationType=\"protocol\" arguments=\"{url}\"/></actions>\
</toast>"
)
}
pub fn show(title: &str, body: &str, url: &str, logo: Option<&Path>) -> Result<()> {
let _ = register(logo);
let show = || -> windows::core::Result<()> {
let xml = XmlDocument::new()?;
xml.LoadXml(&HSTRING::from(toast_xml(title, body, url)))?;
let toast = ToastNotification::CreateToastNotification(&xml)?;
ToastNotificationManager::CreateToastNotifierWithId(&HSTRING::from(APP_ID))?.Show(&toast)
};
show().context("Windows did not show the notification")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn texts_cannot_change_the_notification() {
let xml = toast_xml("Update available", "sne <1.0> & \"more\"", "https://x/?a=1&b=2");
assert!(xml.contains("<text>sne <1.0> & "more"</text>"));
assert!(xml.contains("launch=\"https://x/?a=1&b=2\""));
let doc = XmlDocument::new().unwrap();
doc.LoadXml(&HSTRING::from(xml)).unwrap();
}
}