Files
thinlauncher/launcher/actions.py

71 lines
2.2 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", "--incognito"]
print(f"Launching browser command: {' '.join(cmd)}")
self._run_command(cmd)
def _launch_xfreerdp(self, config):
cmd = [
"xfreerdp",
f"/v:{config['target']}",
f"/u:{config['user']}",
f"/p:{config['password']}",
"/sound",
"/scale-desktop:133",
"/audio-mode:0",
"/f"
]
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}")