use serde::{Deserialize, Serialize};
use super::RemoteActionTarget;
use super::browser::{PROVIDER, RESOURCE};
use crate::api::ActionAddress;
pub const APP: &str = "so.tilo.linux";
pub const TITLE: &str = "Linux apps";
pub const OPEN: &str = "open-linux-app";
pub const MARK: &str = "TILO_HOSTED_APPS";
pub const UP_PROVIDER: &str = "linux";
pub const APP_ARGUMENT: &str = "app";
pub const REFUSED: &str = "refused";
#[must_use]
pub fn target() -> RemoteActionTarget {
RemoteActionTarget {
provider: PROVIDER.to_owned(),
address: ActionAddress::new(RESOURCE, OPEN),
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AppLaunch {
pub desktop_id: String,
}
impl AppLaunch {
pub fn new(desktop_id: &str) -> Result<Self, String> {
let desktop_id = desktop_id.trim();
let desktop_id = desktop_id.strip_suffix(".desktop").unwrap_or(desktop_id);
if desktop_id.is_empty() {
return Err(
"name the Linux app to open: its desktop file's id, such as \
org.gnome.TextEditor"
.to_owned(),
);
}
if desktop_id.len() > 255
|| desktop_id.starts_with(['.', '-'])
|| !desktop_id
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_'))
{
return Err(format!(
"{desktop_id:?} is not a desktop file's id: letters, digits, '.', '-' and '_'"
));
}
Ok(Self {
desktop_id: desktop_id.to_owned(),
})
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct OpenApp {
pub workspace: String,
#[serde(default)]
pub account: String,
pub material: String,
}
#[cfg(test)]
mod tests {
use super::*;
tilo_test_env::scenario! {
fn a_linux_app_is_named_and_launched_by_its_desktop_file;
phase an_app_is_named_by_its_desktop_file_and_nothing_else;
{
assert_eq!(
AppLaunch::new(" org.gnome.TextEditor.desktop ").unwrap(),
AppLaunch {
desktop_id: "org.gnome.TextEditor".into()
}
);
assert_eq!(AppLaunch::new("xterm").unwrap().desktop_id, "xterm");
for refused in [
"",
" ",
"xterm -e sh",
"../xterm",
"-x",
"a;b",
"a/b",
"émacs",
] {
assert!(AppLaunch::new(refused).is_err(), "{refused:?}");
}
}
}
}