45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
"""Desktop entry + x-scheme-handler/nxm registration (user-level only)."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import shlex
|
|
import shutil
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
from .. import APP_ID
|
|
from .util import atomic_write_text
|
|
|
|
DESKTOP_NAME = f"{APP_ID}.desktop"
|
|
|
|
|
|
def desktop_entry(exec_path: str) -> str:
|
|
return (
|
|
"[Desktop Entry]\n"
|
|
"Type=Application\n"
|
|
"Name=MH Wilds Mod Manager\n"
|
|
"Comment=Manage mods for Monster Hunter Wilds\n"
|
|
f"Exec={shlex.quote(exec_path)} %u\n"
|
|
"Icon=applications-games\n"
|
|
"Terminal=false\n"
|
|
"Categories=Game;Utility;\n"
|
|
"MimeType=x-scheme-handler/nxm;\n"
|
|
"StartupNotify=true\n"
|
|
)
|
|
|
|
|
|
def applications_dir() -> Path:
|
|
base = Path(os.environ.get("XDG_DATA_HOME") or Path.home() / ".local/share")
|
|
return base / "applications"
|
|
|
|
|
|
def register_nxm_handler(exec_path: str) -> Path:
|
|
"""Write the .desktop file and make it the default nxm:// handler. Returns the file path."""
|
|
target = applications_dir() / DESKTOP_NAME
|
|
atomic_write_text(target, desktop_entry(exec_path), 0o644)
|
|
for cmd in (["update-desktop-database", str(target.parent)],
|
|
["xdg-mime", "default", DESKTOP_NAME, "x-scheme-handler/nxm"]):
|
|
if shutil.which(cmd[0]):
|
|
subprocess.run(cmd, check=False, capture_output=True, timeout=30)
|
|
return target
|