38 lines
1.7 KiB
Python
38 lines
1.7 KiB
Python
"""Launch native desktop notifications without blocking Tk."""
|
|
|
|
import base64
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
from copienator.platform import find_executable
|
|
|
|
|
|
def notify_desktop(title: str, message: str) -> None:
|
|
if sys.platform.startswith("linux"):
|
|
executable = find_executable("notify-send")
|
|
if not executable:
|
|
raise RuntimeError("Installez notify-send (libnotify) pour les notifications de bureau.")
|
|
command = [executable, "--app-name=Copienator", "--", title, message]
|
|
elif sys.platform == "darwin":
|
|
command = ["/usr/bin/osascript", "-e",
|
|
f"display notification {json.dumps(message, ensure_ascii=False)} with title {json.dumps(title, ensure_ascii=False)}"]
|
|
elif os.name == "nt":
|
|
# Encode the script and quote strings as literals; no shell interpolation.
|
|
quote = lambda value: "'" + value.replace("'", "''") + "'"
|
|
script = (
|
|
"Add-Type -AssemblyName System.Windows.Forms;"
|
|
"$notice = New-Object System.Windows.Forms.NotifyIcon;"
|
|
"$notice.Icon = [System.Drawing.SystemIcons]::Information;"
|
|
"$notice.Visible = $true;"
|
|
f"$notice.ShowBalloonTip(10000, {quote(title)}, {quote(message)}, "
|
|
"[System.Windows.Forms.ToolTipIcon]::Info);"
|
|
"Start-Sleep -Seconds 12; $notice.Dispose()"
|
|
)
|
|
command = ["powershell.exe", "-NoProfile", "-NonInteractive", "-WindowStyle", "Hidden",
|
|
"-EncodedCommand", base64.b64encode(script.encode("utf-16-le")).decode("ascii")]
|
|
else:
|
|
raise RuntimeError("Notifications de bureau indisponibles sur ce système.")
|
|
subprocess.Popen(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|