Implement autostart querying upon application start
This commit is contained in:
1 parent
5ade35d6a5
commit
14a9154366
5 files changed
+83
-42
No files matched your search
@@ -33,10 +33,16 @@ from cache import ImageCache
|
||||
from exceptions import MinerException, ExitRequest
|
||||
from utils import resource_path, set_root_icon, webopen, Game, _T
|
||||
from constants import (
|
||||
SELF_PATH, OUTPUT_FORMATTER, WS_TOPICS_LIMIT, MAX_WEBSOCKETS, WINDOW_TITLE, State
|
||||
SELF_PATH,
|
||||
WINDOW_TITLE,
|
||||
LOGGING_LEVELS,
|
||||
MAX_WEBSOCKETS,
|
||||
WS_TOPICS_LIMIT,
|
||||
OUTPUT_FORMATTER,
|
||||
State,
|
||||
)
|
||||
if sys.platform == "win32":
|
||||
from registry import RegistryKey, ValueType
|
||||
from registry import RegistryKey, ValueType, ValueNotFound
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -1492,7 +1498,7 @@ class SettingsPanel:
|
||||
self._vars: _SettingsVars = {
|
||||
"proxy": StringVar(master, str(self._settings.proxy)),
|
||||
"tray": IntVar(master, self._settings.autostart_tray),
|
||||
"autostart": IntVar(master, self._settings.autostart),
|
||||
"autostart": IntVar(master, 0),
|
||||
"priority_only": IntVar(master, self._settings.priority_only),
|
||||
"tray_notifications": IntVar(master, self._settings.tray_notifications),
|
||||
}
|
||||
@@ -1647,6 +1653,8 @@ class SettingsPanel:
|
||||
command=self._twitch.state_change(State.INVENTORY_FETCH),
|
||||
).grid(column=1, row=0)
|
||||
|
||||
self._vars["autostart"].set(self._query_autostart())
|
||||
|
||||
def clear_selection(self) -> None:
|
||||
self._priority_list.selection_clear(0, "end")
|
||||
self._exclude_list.selection_clear(0, "end")
|
||||
@@ -1654,45 +1662,79 @@ class SettingsPanel:
|
||||
def update_notifications(self) -> None:
|
||||
self._settings.tray_notifications = bool(self._vars["tray_notifications"].get())
|
||||
|
||||
def _get_autostart_path(self, tray: bool) -> str:
|
||||
self_path = f'"{SELF_PATH.resolve()!s}"'
|
||||
if tray:
|
||||
self_path += " --tray"
|
||||
return self_path
|
||||
def _get_self_path(self) -> str:
|
||||
# NOTE: we need double quotes in case the path contains spaces
|
||||
return f'"{SELF_PATH.resolve()!s}"'
|
||||
|
||||
def _get_autostart_path(self) -> str:
|
||||
flags: list[str] = [''] # this will add a space between self path and flags
|
||||
# if non-zero, include the current logging level as well
|
||||
if self._settings.logging_level > 0:
|
||||
for lvl_idx, lvl_value in LOGGING_LEVELS.items():
|
||||
if lvl_value == self._settings.logging_level:
|
||||
flags.append(f"-{'v' * lvl_idx}")
|
||||
break
|
||||
if self._vars["tray"].get():
|
||||
flags.append("--tray")
|
||||
return self._get_self_path() + ' '.join(flags)
|
||||
|
||||
def _get_linux_autostart_filepath(self) -> Path:
|
||||
autostart_folder: Path = Path("~/.config/autostart").expanduser()
|
||||
if (config_home := os.environ.get("XDG_CONFIG_HOME")) is not None:
|
||||
config_autostart: Path = Path(config_home, "autostart").expanduser()
|
||||
if config_autostart.exists():
|
||||
autostart_folder = config_autostart
|
||||
return autostart_folder / f"{self.AUTOSTART_NAME}.desktop"
|
||||
|
||||
def _query_autostart(self) -> bool:
|
||||
if sys.platform == "win32":
|
||||
with RegistryKey(self.AUTOSTART_KEY, read_only=True) as key:
|
||||
try:
|
||||
value_type, value = key.get(self.AUTOSTART_NAME)
|
||||
except ValueNotFound:
|
||||
return False
|
||||
if (
|
||||
value_type is not ValueType.REG_SZ
|
||||
or self._get_self_path() not in value
|
||||
):
|
||||
# TODO: Consider deleting the old value to avoid autostart errors
|
||||
return False
|
||||
return True
|
||||
elif sys.platform == "linux":
|
||||
autostart_file: Path = self._get_linux_autostart_filepath()
|
||||
if not autostart_file.exists():
|
||||
return False
|
||||
with autostart_file.open('r', encoding="utf8") as file:
|
||||
# TODO: Consider deleting the old file to avoid autostart errors
|
||||
return self._get_self_path() not in file.read()
|
||||
|
||||
def update_autostart(self) -> None:
|
||||
enabled = bool(self._vars["autostart"].get())
|
||||
tray = bool(self._vars["tray"].get())
|
||||
self._settings.autostart = enabled
|
||||
self._settings.autostart_tray = tray
|
||||
self._settings.autostart_tray = bool(self._vars["tray"].get())
|
||||
if sys.platform == "win32":
|
||||
if enabled:
|
||||
# NOTE: we need double quotes in case the path contains spaces
|
||||
autostart_path = self._get_autostart_path(tray)
|
||||
with RegistryKey(self.AUTOSTART_KEY) as key:
|
||||
key.set(self.AUTOSTART_NAME, ValueType.REG_SZ, autostart_path)
|
||||
key.set(
|
||||
self.AUTOSTART_NAME,
|
||||
ValueType.REG_SZ,
|
||||
self._get_autostart_path(),
|
||||
)
|
||||
else:
|
||||
with RegistryKey(self.AUTOSTART_KEY) as key:
|
||||
key.delete(self.AUTOSTART_NAME, silent=True)
|
||||
elif sys.platform == "linux":
|
||||
autostart_folder: Path = Path("~/.config/autostart").expanduser()
|
||||
if (config_home := os.environ.get("XDG_CONFIG_HOME")) is not None:
|
||||
config_autostart: Path = Path(config_home, "autostart").expanduser()
|
||||
if config_autostart.exists():
|
||||
autostart_folder = config_autostart
|
||||
autostart_file: Path = autostart_folder / f"{self.AUTOSTART_NAME}.desktop"
|
||||
autostart_file: Path = self._get_linux_autostart_filepath()
|
||||
if enabled:
|
||||
autostart_path = self._get_autostart_path(tray)
|
||||
file_contents = dedent(
|
||||
file_contents: str = dedent(
|
||||
f"""
|
||||
[Desktop Entry]
|
||||
Type=Application
|
||||
Name=Twitch Drops Miner
|
||||
Description=Mine timed drops on Twitch
|
||||
Exec=sh -c '{autostart_path}'
|
||||
Exec=sh -c '{self._get_autostart_path()}'
|
||||
"""
|
||||
)
|
||||
with autostart_file.open("w", encoding="utf8") as file:
|
||||
with autostart_file.open('w', encoding="utf8") as file:
|
||||
file.write(file_contents)
|
||||
else:
|
||||
autostart_file.unlink(missing_ok=True)
|
||||
|
||||
Reference in new issue
Block a user