archweather / build/lib/archweather/autostart.pymain
▤ build/lib/archweather/autostart.py1227 bytes
"""XDG/Plasma autostart integration without root privileges."""
from __future__ import annotations

import os
from pathlib import Path

ENTRY = "archweather-tray.desktop"
OLD_ENTRY = "archweather.desktop"


def xdg_data_home():
    return Path(os.environ.get("XDG_DATA_HOME") or Path.home() / ".local/share")


def xdg_config_home():
    return Path(os.environ.get("XDG_CONFIG_HOME") or Path.home() / ".config")


def desktop_entry():
    return xdg_data_home() / "applications" / ENTRY


def autostart_entry():
    return xdg_config_home() / "autostart" / ENTRY


def enabled():
    target = autostart_entry()
    return target.exists() and "Hidden=true" not in target.read_text(encoding="utf-8")


def enable():
    source = desktop_entry()
    if not source.is_file():
        raise FileNotFoundError("Plasma-starter niet geïnstalleerd: voer ./install.sh uit")
    target = autostart_entry()
    target.parent.mkdir(parents=True, exist_ok=True)
    target.write_text(source.read_text(encoding="utf-8"), encoding="utf-8")
    (target.parent / OLD_ENTRY).unlink(missing_ok=True)
    return target


def disable():
    for entry in (ENTRY, OLD_ENTRY):
        (xdg_config_home() / "autostart" / entry).unlink(missing_ok=True)