Skip to content

Commit 075eb41

Browse files
authored
Add files via upload
1 parent d4c066f commit 075eb41

File tree

1 file changed

+107
-87
lines changed

1 file changed

+107
-87
lines changed

main.py

Lines changed: 107 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def load_services(self):
198198

199199
# For user filter, show all user services
200200
if self.current_filter == "user":
201-
cmd = ["systemctl", "--user", "list-units", "--type=service", "--all", "--no-pager", "--plain"] # Added --all flag
201+
cmd = ["flatpak-spawn", "--host", "systemctl", "--user", "list-units", "--type=service", "--all", "--no-pager", "--plain"]
202202
output = subprocess.run(
203203
cmd,
204204
capture_output=True,
@@ -208,9 +208,9 @@ def load_services(self):
208208
else:
209209
# For all other filters, combine system and user services
210210
# Get system services
211-
system_cmd = ["systemctl", "list-units", "--type=service"]
211+
system_cmd = ["flatpak-spawn", "--host", "systemctl", "list-units", "--type=service"]
212212
if self.current_filter == "all":
213-
system_cmd.append("--all") # Show all units including inactive ones
213+
system_cmd.append("--all")
214214
elif self.current_filter == "failed":
215215
system_cmd.extend(["--state=failed"])
216216
elif self.current_filter == "running":
@@ -227,9 +227,9 @@ def load_services(self):
227227
).stdout
228228

229229
# Get user services
230-
user_cmd = ["systemctl", "--user", "list-units", "--type=service"]
230+
user_cmd = ["flatpak-spawn", "--host", "systemctl", "--user", "list-units", "--type=service"]
231231
if self.current_filter == "all":
232-
user_cmd.append("--all") # Show all units including inactive ones
232+
user_cmd.append("--all")
233233
elif self.current_filter == "failed":
234234
user_cmd.extend(["--state=failed"])
235235
elif self.current_filter == "running":
@@ -403,11 +403,9 @@ def run_systemctl_command(self, command, service_name):
403403

404404
# Build command based on service type
405405
if is_user_service:
406-
cmd = ["systemctl", "--user", command, service_name]
406+
cmd = ["flatpak-spawn", "--host", "systemctl", "--user", command, service_name]
407407
else:
408-
cmd = ["systemctl", command, service_name]
409-
if not self.is_root:
410-
cmd.insert(0, "pkexec")
408+
cmd = ["flatpak-spawn", "--host", "pkexec", "systemctl", command, service_name]
411409

412410
subprocess.run(cmd, check=True)
413411

@@ -463,45 +461,39 @@ def on_edit_service(self, button, service_name):
463461

464462
# Build the edit command based on service type
465463
if is_user_service:
466-
edit_cmd = f"systemctl --user edit {service_name}"
464+
edit_cmd = f"systemctl --user edit {service_name}; read -p 'Press Enter to close...'"
467465
else:
468-
edit_cmd = f"pkexec systemctl edit {service_name}"
469-
470-
# Try different terminal emulators in order of preference
471-
terminals = [
472-
["gnome-terminal", "--", "bash", "-c", f"{edit_cmd}"],
473-
["xfce4-terminal", "-e", f"bash -c '{edit_cmd}'"],
474-
["konsole", "-e", f"bash -c '{edit_cmd}'"],
475-
["x-terminal-emulator", "-e", f"bash -c '{edit_cmd}'"]
476-
]
477-
478-
for terminal_cmd in terminals:
479-
try:
480-
subprocess.run(["which", terminal_cmd[0]], check=True, capture_output=True)
481-
GLib.spawn_async(
482-
argv=terminal_cmd,
483-
flags=GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD,
484-
child_setup=None,
485-
user_data=None
486-
)
487-
488-
# Use a callback to restore expanded state after edit
489-
def restore_expanded_state():
490-
if was_expanded:
491-
for child in self.list_box.observe_children():
492-
if child.get_title() == service_name[:-8]:
493-
child.set_expanded(True)
494-
break
495-
return False
496-
497-
GLib.timeout_add(1000, restore_expanded_state)
498-
return
499-
500-
except subprocess.CalledProcessError:
501-
continue
502-
503-
self.show_error_dialog("No suitable terminal emulator found. Please install gnome-terminal, xfce4-terminal, or konsole.")
504-
466+
edit_cmd = f"pkexec systemctl edit {service_name}; read -p 'Press Enter to close...'"
467+
468+
terminal = self.get_terminal_command()
469+
if terminal is None:
470+
self.show_error_dialog("No suitable terminal emulator found. Please install gnome-terminal, xfce4-terminal, or konsole.")
471+
return
472+
473+
# Build the complete command
474+
cmd = ['flatpak-spawn', '--host']
475+
cmd.append(terminal['binary'])
476+
cmd.extend(terminal['args'])
477+
cmd.append(edit_cmd)
478+
479+
GLib.spawn_async(
480+
argv=cmd,
481+
flags=GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD,
482+
child_setup=None,
483+
user_data=None
484+
)
485+
486+
# Use a callback to restore expanded state after edit
487+
def restore_expanded_state():
488+
if was_expanded:
489+
for child in self.list_box.observe_children():
490+
if child.get_title() == service_name[:-8]:
491+
child.set_expanded(True)
492+
break
493+
return False
494+
495+
GLib.timeout_add(1000, restore_expanded_state)
496+
505497
except GLib.Error as e:
506498
self.show_error_dialog(f"Failed to edit service: {e.message}")
507499

@@ -542,8 +534,8 @@ def filter_services(self, row):
542534
return show_by_search and show_by_status
543535

544536
def show_error_dialog(self, message):
545-
dialog = Adw.MessageDialog.new(
546-
transient_for=self,
537+
dialog = Adw.MessageDialog(
538+
parent=self,
547539
heading="Error",
548540
body=message
549541
)
@@ -606,56 +598,84 @@ def on_show_status(self, button, service_name):
606598
service_name = f"{service_name}.service"
607599

608600
# Build the status command based on service type
609-
status_cmd = f"systemctl {'--user ' if is_user_service else ''}status {service_name}"
610-
611-
# Try different terminal emulators in order of preference
612-
terminals = [
613-
["gnome-terminal", "--", "bash", "-c", f"{status_cmd}; read -p 'Press Enter to close...'"],
614-
["xfce4-terminal", "-e", f"bash -c '{status_cmd}; read -p \"Press Enter to close...\"'"],
615-
["konsole", "-e", f"bash -c '{status_cmd}; read -p \"Press Enter to close...\"'"],
616-
["x-terminal-emulator", "-e", f"bash -c '{status_cmd}; read -p \"Press Enter to close...\"'"]
617-
]
618-
619-
for terminal_cmd in terminals:
620-
try:
621-
subprocess.run(["which", terminal_cmd[0]], check=True, capture_output=True)
622-
GLib.spawn_async(
623-
argv=terminal_cmd,
624-
flags=GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD,
625-
child_setup=None,
626-
user_data=None
627-
)
628-
629-
# Use a callback to restore expanded state after showing status
630-
def restore_expanded_state():
631-
if was_expanded:
632-
for child in self.list_box.observe_children():
633-
if child.get_title() == service_name[:-8]:
634-
child.set_expanded(True)
635-
break
636-
return False
637-
638-
GLib.timeout_add(1000, restore_expanded_state)
639-
return
640-
641-
except subprocess.CalledProcessError:
642-
continue
643-
644-
self.show_error_dialog("No suitable terminal emulator found. Please install gnome-terminal, xfce4-terminal, or konsole.")
645-
601+
status_cmd = f"systemctl {'--user ' if is_user_service else ''}status {service_name}; read -p 'Press Enter to close...'"
602+
603+
terminal = self.get_terminal_command()
604+
if terminal is None:
605+
self.show_error_dialog("No suitable terminal emulator found. Please install gnome-terminal, xfce4-terminal, or konsole.")
606+
return
607+
608+
# Build the complete command
609+
cmd = ['flatpak-spawn', '--host']
610+
cmd.append(terminal['binary'])
611+
cmd.extend(terminal['args'])
612+
cmd.append(status_cmd)
613+
614+
GLib.spawn_async(
615+
argv=cmd,
616+
flags=GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD,
617+
child_setup=None,
618+
user_data=None
619+
)
620+
621+
# Use a callback to restore expanded state after showing status
622+
def restore_expanded_state():
623+
if was_expanded:
624+
for child in self.list_box.observe_children():
625+
if child.get_title() == service_name[:-8]:
626+
child.set_expanded(True)
627+
break
628+
return False
629+
630+
GLib.timeout_add(1000, restore_expanded_state)
631+
646632
except GLib.Error as e:
647633
self.show_error_dialog(f"Failed to show service status: {e.message}")
648634

649635
def check_if_user_service(self, service_name):
650636
"""Helper method to check if a service is a user service"""
651637
try:
652-
check_cmd = ["systemctl", "--user", "list-units", "--type=service", "--all", "--no-pager", "--plain"]
638+
check_cmd = ["flatpak-spawn", "--host", "systemctl", "--user", "list-units", "--type=service", "--all", "--no-pager", "--plain"]
653639
result = subprocess.run(check_cmd, capture_output=True, text=True, check=True)
654640
# Check if the service name appears in the user services list
655641
return any(service_name in line for line in result.stdout.splitlines())
656642
except subprocess.CalledProcessError:
657643
return False
658644

645+
def get_terminal_command(self):
646+
"""Helper function to find an available terminal emulator"""
647+
terminals = [
648+
{
649+
'binary': 'gnome-terminal',
650+
'args': ['--', 'bash', '-c']
651+
},
652+
{
653+
'binary': 'xfce4-terminal',
654+
'args': ['-e', 'bash -c']
655+
},
656+
{
657+
'binary': 'konsole',
658+
'args': ['-e', 'bash -c']
659+
},
660+
{
661+
'binary': 'x-terminal-emulator',
662+
'args': ['-e', 'bash -c']
663+
}
664+
]
665+
666+
for terminal in terminals:
667+
try:
668+
# Use flatpak-spawn to check if terminal exists on host
669+
subprocess.run(
670+
['flatpak-spawn', '--host', 'which', terminal['binary']],
671+
check=True,
672+
capture_output=True
673+
)
674+
return terminal
675+
except subprocess.CalledProcessError:
676+
continue
677+
return None
678+
659679
class SystemdManagerApp(Adw.Application):
660680
def __init__(self):
661681
super().__init__(application_id="org.mfat.systemdpilot",

0 commit comments

Comments
 (0)