forked from StreamController/StreamController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autostart.py
101 lines (80 loc) · 3.2 KB
/
autostart.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
Author: Core447
Year: 2024
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This programm comes with ABSOLUTELY NO WARRANTY!
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import shutil
import gi
gi.require_version("Xdp", "1.0")
from gi.repository import Xdp
from loguru import logger as log
def is_flatpak():
return os.path.isfile('/.flatpak-info')
log.catch
def setup_autostart(enable: bool = True):
if enable:
if is_flatpak():
setup_autostart_flatpak(True)
else:
setup_autostart_desktop_entry(True)
else:
setup_autostart_flatpak(False)
setup_autostart_desktop_entry(False)
def setup_autostart_flatpak(enable: bool = True):
"""
Use portal to autostart for Flatpak
Documentation:
https://libportal.org/method.Portal.request_background.html
https://libportal.org/method.Portal.request_background_finish.html
https://docs.flatpak.org/de/latest/portal-api-reference.html#gdbus-org.freedesktop.portal.Background
"""
def request_background_callback(portal, result, user_data):
try:
success = portal.request_background_finish(result)
except:
success = False
log.info(f"request_background success={success}")
if not success:
setup_autostart_desktop_entry()
xdp = Xdp.Portal.new()
try:
flag = Xdp.BackgroundFlags.AUTOSTART if enable else Xdp.BackgroundFlags.ACTIVATABLE
# Request Autostart
xdp.request_background(
None, # parent
"Autostart StreamController", # reason
["/app/bin/launch.sh", "-b"], # commandline
flag,
None, # cancellable
request_background_callback,
None, # user_data
)
except:
log.error(f"request_background failed")
setup_autostart_desktop_entry(enable)
def setup_autostart_desktop_entry(enable: bool = True):
log.info("Setting up autostart using desktop entry")
xdg_config_home = os.path.join(os.environ.get("HOME"), ".config")
AUTOSTART_DIR = os.path.join(xdg_config_home, "autostart")
AUTOSTART_DESKTOP_PATH = os.path.join(AUTOSTART_DIR, "StreamController.desktop")
if enable:
try:
os.makedirs(os.path.dirname(AUTOSTART_DESKTOP_PATH), exist_ok=True)
shutil.copyfile(os.path.join("flatpak", "autostart.desktop"), AUTOSTART_DESKTOP_PATH)
log.info(f"Autostart set up at: {AUTOSTART_DESKTOP_PATH}")
except Exception as e:
log.error(f"Failed to set up autostart at: {AUTOSTART_DESKTOP_PATH} with error: {e}")
else:
if os.path.exists(AUTOSTART_DESKTOP_PATH):
try:
os.remove(AUTOSTART_DESKTOP_PATH)
log.info(f"Autostart removed from: {AUTOSTART_DESKTOP_PATH}")
except Exception as e:
log.error(f"Failed to remove autostart from: {AUTOSTART_DESKTOP_PATH} with error: {e}")