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
use crate::AutoLaunch;
use std::fs;
use std::io::{Result, Write};
use std::path::PathBuf;
impl AutoLaunch<'_> {
pub fn new<'a>(app_name: &'a str, app_path: &'a str, hidden: bool) -> AutoLaunch<'a> {
AutoLaunch::<'a> {
app_name,
app_path,
hidden,
}
}
pub fn enable(&self) -> Result<()> {
let hidden = if self.hidden { " --hidden" } else { "" };
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, hidden
);
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)
} else {
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")
}