1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crate::AutoLaunch;
use anyhow::Result;
use std::fs;
use std::io::Write;
use std::path::PathBuf;
impl AutoLaunch {
pub fn new(app_name: &str, app_path: &str, args: &[impl AsRef<str>]) -> AutoLaunch {
AutoLaunch {
app_name: app_name.into(),
app_path: app_path.into(),
args: args.iter().map(|s| s.as_ref().to_string()).collect(),
}
}
pub fn enable(&self) -> Result<()> {
let data = format!(
"[Desktop Entry]\n\
Type=Application\n\
Version=1.0\n\
Name={}\n\
Comment={}startup script\n\
Exec={} {}\n\
StartupNotify=false\n\
Terminal=false",
self.app_name,
self.app_name,
self.app_path,
self.args.join(" ")
);
let dir = get_dir();
if !dir.exists() {
fs::create_dir(&dir)?;
}
fs::File::create(self.get_file())?.write(data.as_bytes())?;
Ok(())
}
pub fn disable(&self) -> Result<()> {
let file = self.get_file();
if file.exists() {
fs::remove_file(file)?;
}
Ok(())
}
pub fn is_enabled(&self) -> Result<bool> {
Ok(self.get_file().exists())
}
fn get_file(&self) -> PathBuf {
get_dir().join(format!("{}.desktop", self.app_name))
}
}
fn get_dir() -> PathBuf {
dirs::home_dir().unwrap().join(".config").join("autostart")
}