From 2134df1a5bbc8d86f9f180ce6655d7d4ed34b1e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20D=C3=ADaz?= Date: Tue, 31 Jul 2018 09:06:45 +0100 Subject: [PATCH] Use CWM::ServiceWidget instead ServiceStatus --- src/include/dhcp-server/dialogs.rb | 70 ++++++++++++++++---------- src/include/dhcp-server/widgets.rb | 81 ++++++++---------------------- 2 files changed, 65 insertions(+), 86 deletions(-) diff --git a/src/include/dhcp-server/dialogs.rb b/src/include/dhcp-server/dialogs.rb index 20f8f8b..03238e2 100644 --- a/src/include/dhcp-server/dialogs.rb +++ b/src/include/dhcp-server/dialogs.rb @@ -12,6 +12,8 @@ # Input and output routines. module Yast module DhcpServerDialogsInclude + include Yast::Logger + def initialize_dhcp_server_dialogs(include_target) textdomain "dhcp-server" @@ -47,44 +49,58 @@ def ReadDialog end # Write settings dialog - # @return `abort if aborted and `next otherwise + # + # @return [Symbol] :next whether configuration is saved successfully + # :back if user decided to change settings + # :abort otherwise def WriteDialog - Builtins.y2milestone("Running write dialog") - Wizard.RestoreHelp(Ops.get(@HELPS, "write", "")) - ret = DhcpServer.Write - if ret && restart_after_writing? - # Restart only if it's already running - DhcpServerUI.service.try_restart - end - # yes-no popup - if !ret && - Popup.YesNo( - _("Saving the configuration failed. Change the settings?") - ) - return :back - end - ret ? :next : :abort + log.info("Running write dialog") + + help_text = @HELPS.fetch("write") { "" } + Wizard.RestoreHelp(help_text) + + return :next if write_settings + return :back if Popup.YesNo(_("Saving the configuration failed. Change the settings?")) + :abort end # Write settings without quitting def SaveAndRestart(event) - return nil unless CWM.validate_current_widgets(event) - CWM.save_current_widgets(event) + return nil unless validate_and_save_widgets(event) + + help_text = @HELPS.fetch("write") { "" } Wizard.CreateDialog - Wizard.RestoreHelp(Ops.get(@HELPS, "write", "")) - ret = DhcpServer.Write - if ret - # Restart only if it's already running - DhcpServerUI.service.try_restart if restart_after_writing? - else - Report.Error(_("Saving the configuration failed")) - end - UI.CloseDialog + Wizard.RestoreHelp(help_text) + Report.Error(_("Saving the configuration failed")) unless write_settings + Wizard.CloseDialog + + service_widget.refresh nil end + # Write DHCP server settings and save the service + # + # NOTE: currently, the DhpcServer is a Perl module, reason why the write of + # settings is being performed in two separate steps. + # + # @return [Boolean] true if settings are saved successfully; false otherwise + def write_settings + DhcpServer.Write && dhcp_service.save + end + + # Validates and saves CWM widgets + # + # @param [Hash] event map that triggered saving + def validate_and_save_widgets(event) + return false unless CWM.validate_current_widgets(event) + + CWM.save_current_widgets(event) + + true + end + # Run main dialog # @return [Symbol] for wizard sequencer def OldMainDialog diff --git a/src/include/dhcp-server/widgets.rb b/src/include/dhcp-server/widgets.rb index c3b2632..01af21d 100644 --- a/src/include/dhcp-server/widgets.rb +++ b/src/include/dhcp-server/widgets.rb @@ -7,7 +7,9 @@ # Authors: Jiri Srain require "yast" -require "ui/service_status" +require "yast2/system_service" +require "cwm" +require "cwm/service_widget" # Representation of the configuration of dhcp-server. # Input and output routines. @@ -27,22 +29,13 @@ def initialize_dhcp_server_widgets(include_target) Yast.import "TablePopup" Yast.import "SuSEFirewall" Yast.import "Mode" - Yast.import "DhcpServerUI" end - # Widget to handle the status of the service + # Returns the service for DHCP # - # @return [::UI::ServiceStatus] nil if the service is not found (dhcp server - # not installed) - def status_widget - return @status_widget unless @status_widget.nil? - - service = DhcpServerUI.service - if service - @status_widget = ::UI::ServiceStatus.new(service, reload_flag_label: :restart) - else - nil - end + # @return [Yast2::SystemService] status service + def dhcp_service + @service ||= Yast2::SystemService.find(DhcpServer.ServiceName) end # Function for deleting entry from section @@ -855,47 +848,15 @@ def AllSettingsButtonHandle(key, event) # Handle function for the 'Apply' button def handle_apply(_key, event) - event_id = event["ID"] - if event_id == "apply" - SaveAndRestart(event) - end - nil - end - - def init_service_status(_key) - # If UI::ServiceStatus is used, do not let DnsServer manage the service - # status, let the user decide - DhcpServer.SetWriteOnly(true) - nil - end - - # Handle function for the ServiceStatus widget - def handle_service_status(_key, event) - event_id = event["ID"] - if status_widget.handle_input(event_id) == :enabled_flag - DhcpServer.SetModified - end - nil - end + SaveAndRestart(event) if event["ID"] == "apply" - # Store settings of the widget - # @param [String] id string widget id - # @param [Hash] event map event that caused storing process - def store_service_status(_key, _event) - DhcpServer.SetStartService(status_widget.enabled_flag?) nil end - # Checks if the service must be restarted after saving - # @return [Boolean] - def restart_after_writing? - # If ServiceStatus is used, DhcpServer must be set to write-only - DhcpServer.GetWriteOnly() && status_widget && status_widget.reload_flag? - end - # Initialize widgets + # # Create description map and copy it into appropriate variable of the - # DhcpServer module + # DhcpServer module def InitWidgets options = [ "option subnet-mask", @@ -1322,20 +1283,22 @@ def InitWidgets nil end + def service_widget + @service_widget ||= + begin + widget = ::CWM::ServiceWidget.new(dhcp_service) + widget.widget_id = "service_status" + widget + end + end + # lazy initialization of the service status widget # it needs the "dhcp-server" package already installed in the system # otherwise it crashes def InitServiceWidget - return if @widgets["service_status"] - - @widgets["service_status"] = { - "widget" => :custom, - "custom_widget" => status_widget.widget, - "help" => status_widget.help, - "init" => fun_ref(method(:init_service_status), "void (string)"), - "handle" => fun_ref(method(:handle_service_status), "symbol (string, map)"), - "store" => fun_ref(method(:store_service_status), "void (string, map)") - } + return if @widgets[service_widget.widget_id] + + @widgets[service_widget.widget_id] = service_widget.cwm_definition end end end