system: add shutdown and restart host

This commit is contained in:
alfrix 2022-08-17 16:44:44 -03:00 committed by Alfredo Monclus
parent ea5c5afa5e
commit c1f17b154c

View File

@ -1,9 +1,9 @@
import gi
import logging
import os
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Pango, GLib
from datetime import datetime
from ks_includes.screen_panel import ScreenPanel
@ -18,6 +18,7 @@ ALLOWED_SERVICES = ["KlipperScreen", "MoonCord", "klipper", "moonraker", "webcam
class SystemPanel(ScreenPanel):
def __init__(self, screen, title, back=True):
super().__init__(screen, title, back)
self.refresh = None
self.update_status = None
self.update_dialog = None
self.update_prog = None
@ -35,12 +36,10 @@ class SystemPanel(ScreenPanel):
self.refresh.set_vexpand(False)
reboot = self._gtk.ButtonImage('refresh', _('System\nRestart'), 'color3')
reboot.connect("clicked", self._screen._confirm_send_action,
_("Are you sure you wish to reboot the system?"), "machine.reboot")
reboot.connect("clicked", self.reboot_poweroff, "reboot")
reboot.set_vexpand(False)
shutdown = self._gtk.ButtonImage('shutdown', _('System\nShutdown'), 'color4')
shutdown.connect("clicked", self._screen._confirm_send_action,
_("Are you sure you wish to shutdown the system?"), "machine.shutdown")
shutdown.connect("clicked", self.reboot_poweroff, "poweroff")
shutdown.set_vexpand(False)
scroll = self._gtk.ScrolledWindow()
@ -377,3 +376,35 @@ class SystemPanel(ScreenPanel):
def _autoscroll(self, *args):
adj = self.labels['update_scroll'].get_vadjustment()
adj.set_value(adj.get_upper() - adj.get_page_size())
def reboot_poweroff(self, widget, method):
scroll = self._gtk.ScrolledWindow()
scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
vbox.set_halign(Gtk.Align.CENTER)
vbox.set_valign(Gtk.Align.CENTER)
if method == "reboot":
label = Gtk.Label(label=_("Are you sure you wish to reboot the system?"))
else:
label = Gtk.Label(label=_("Are you sure you wish to shutdown the system?"))
vbox.add(label)
scroll.add(vbox)
buttons = [
{"name": _("Host"), "response": Gtk.ResponseType.OK},
{"name": _("Machine"), "response": Gtk.ResponseType.APPLY},
{"name": _("Cancel"), "response": Gtk.ResponseType.CANCEL}
]
self._gtk.Dialog(self._screen, buttons, scroll, self.reboot_poweroff_confirm, method)
def reboot_poweroff_confirm(self, widget, response_id, method):
if response_id == Gtk.ResponseType.OK:
if method == "reboot":
os.system("systemctl reboot")
else:
os.system("systemctl poweroff")
elif response_id == Gtk.ResponseType.APPLY:
if method == "reboot":
self._screen._ws.send_method("machine.reboot")
else:
self._screen._ws.send_method("machine.shutdown")
widget.destroy()