Files
thinlauncher/launcher/actions.py

98 lines
2.9 KiB
Python

import subprocess
import shutil
class ActionDispatcher:
def dispatch(self, service_type, config):
print(f"Executing action -> Type: {service_type}")
if service_type == "browser":
self._launch_app_browser(config['target'])
elif service_type == "rdp":
## Refactor required to pass 'config' through to this level
self._launch_xfreerdp(config)
elif service_type == "command":
self._run_command(config['cmd'].split())
elif service_type == "power":
self._handle_power_action(config)
else:
print(f"Service type: {service_type}, not known")
def _launch_app_browser(self, url):
print(f"URL: {url}")
browser_candidates = [
"chromium",
"chromium-browser",
"brave-browser",
"google-chrome",
"microsoft-edge",
"vivaldi"
]
browser_bin = None
for candidate in browser_candidates:
if shutil.which(candidate):
browser_bin = candidate
break
if not browser_bin:
print("Error: No supported chromium based browser found in PATH,")
return
cmd = [
browser_bin,
f"--app={url}",
"--start-maximized",
"--no-first-run",
"--disable-session-crashed-bubble",
]
print(f"Launching browser command: {' '.join(cmd)}")
self._run_command(cmd)
def _launch_xfreerdp(self, config):
rdp_candidates = [
"sdl-freerdp3",
"sdl-freerdp",
# "xfreerdp3",
# "xfreerdp"
]
rdp_bin = None
for candidate in rdp_candidates:
if shutil.which(candidate):
rdp_bin = candidate
break
if not rdp_bin:
print("Error: No supported FreeRDP executable found in PATH,")
return
cmd = [
rdp_bin,
f"/v:{config['target']}",
f"/u:{config['user']}",
f"/p:{config['password']}",
"/sound",
"/audio-mode:0",
"/cert:ignore",
"/f"
]
if config.get('scale_desktop', '') != "":
cmd.append(f"/scale-desktop:{config['scale_desktop']}")
self._run_command(cmd)
def _handle_power_action(self, action):
if action == "shutdown":
print("Initiating system shutdown")
self._run_command(["systemctl", "poweroff"])
elif action == "reboot":
print("Initiating system reboot...")
self._run_command(["systemctl", "reboot"])
else:
print(f"Unknown power action: {action}")
def _run_command(self, cmd):
try:
subprocess.Popen(cmd)
except Exception as e:
print(f"Failed to execute cmd: {cmd}: {e}")