-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautostart.py
More file actions
69 lines (59 loc) · 2.44 KB
/
Copy pathautostart.py
File metadata and controls
69 lines (59 loc) · 2.44 KB
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
"""Toggle whether LinuxPop launches at login.
Writes / removes ~/.config/autostart/linuxpop.desktop. Honoured by every
freedesktop-spec desktop environment (Cinnamon, GNOME, KDE Plasma, XFCE,
MATE, Budgie, etc.).
Used from the Settings dialog (Settings -> Activation -> Start at login)
so the user doesn't have to know where the file lives or edit it by hand.
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
AUTOSTART_DIR = Path(os.path.expanduser("~/.config/autostart"))
AUTOSTART_FILE = AUTOSTART_DIR / "linuxpop.desktop"
def _desktop_file_contents(exec_path: str) -> str:
return (
"[Desktop Entry]\n"
"Type=Application\n"
"Name=LinuxPop\n"
"Comment=PopClip-inspired floating action popup\n"
f"Exec={exec_path}\n"
"Icon=linuxpop\n"
"Terminal=false\n"
"Categories=Utility;\n"
"X-GNOME-Autostart-enabled=true\n"
"StartupNotify=false\n"
)
def is_enabled() -> bool:
"""True if the autostart .desktop file exists."""
return AUTOSTART_FILE.is_file()
def set_enabled(enabled: bool, main_py: Path | None = None) -> bool:
"""Create or remove the autostart .desktop file. Returns True on
success. `main_py` defaults to the currently-running main.py path
so a user who runs LinuxPop from a non-standard checkout still gets
their copy on autostart, not /usr/bin/linuxpop."""
try:
if enabled:
AUTOSTART_DIR.mkdir(parents=True, exist_ok=True)
if main_py is None:
# Resolve to the actual main.py we were invoked from
main_py = Path(sys.argv[0]).resolve()
if main_py.name != "main.py":
main_py = Path(__file__).resolve().parent / "main.py"
# Quote the path so checkouts in directories with spaces
# (e.g. ~/My Code/linuxpop/) don't produce a broken Exec line
# that the DE silently fails to launch.
from shlex import quote as _q
exec_line = f"/usr/bin/python3 {_q(str(main_py))}"
AUTOSTART_FILE.write_text(
_desktop_file_contents(exec_line),
encoding="utf-8",
)
# Make it executable so older DEs that check x-bit honour it
AUTOSTART_FILE.chmod(0o755)
else:
if AUTOSTART_FILE.is_file():
AUTOSTART_FILE.unlink()
return True
except OSError:
return False