Skip to content

Commit

Permalink
adapt service widget
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger authored and joseivanlopez committed Aug 6, 2018
1 parent 328c46f commit 668f1ea
Showing 1 changed file with 39 additions and 40 deletions.
79 changes: 39 additions & 40 deletions library/systemd/src/lib/yast2/service_widget.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
module Yast2
# Class that represents widget that allows configuration of services.
# It uses to hold configuration {Yast2::ServiceConfiguration}.
# It uses to hold configuration {Yast2::SystemService} or {Yast2::CompoundService}.
# It can work with both and for usage it really depends if it should configure single
# or multiple services
#
# @example usage of widget with workflow with read + propose + show_dialog + write
# class Workflow
# def initialize
# service = Yast::SystemdService.find!("my_service")
# @service_configuration = Yast2::ServiceConfiguration.new(service)
# end
#
# def read
# @service_configuration.read
# @service = Yast::SystemService.find("my_service")
# end
#
# def propose
# service_configuration.action = :restart
# service_configuration.autostart = :on_demand
# @service.action = :restart
# @service.start_mode = :on_demand
# end
#
# def show_dialog
# service_widget = ServiceWidget.new(@service_configuration)
# service_widget = ServiceWidget.new(@service)
# content = VBox(
# ...,
# service_widget.content
Expand All @@ -33,24 +30,24 @@ module Yast2
# end
#
# def write
# service_configuration.write
# @service.save
# end
# end
class ServiceWidget
include Yast::I18n
include Yast::Logger
include Yast::UIShortcuts
# creates new widget instance for given service configuration
# @param service_configuration [Yast2::ServiceConfiguration] configuration holder
def initialize(service_configuration)

# creates new widget instance for given service
# @param service_configuration [Yast2::SystemService,Yast2::CompoundService] service
def initialize(service)
textdomain "base"
@service_configuration = service_configuration
@service = service
end

# gets widget term
# @return <Yast::Term>
def content
# TODO: disabling invalid action and autostart or kick it out?
Frame(
_("Service Configuration"),
VBox(
Expand Down Expand Up @@ -80,44 +77,48 @@ def handle_input(event_id)
# Stores current configuration. Should be called always even when going
# back so configuration is persistent when going again forward.
def store
service.reset # so we start from scratch
store_action
store_autostart
end

private

attr_reader :service_configuration
attr_reader :service

def store_action
action = Yast::UI.QueryWidget(Id(:service_widget_action), :CurrentItem)
return unless action

service_configuration.action = action.to_s.sub(/^service_widget_action_/, "").to_sym
action = action.to_s.sub(/^service_widget_action_/, "").to_sym
return if action == :nothing

service.public_send(action)
end

def store_autostart
autostart = Yast::UI.QueryWidget(Id(:service_widget_autostart), :CurrentItem)
return unless autostart

service_configuration.autostart = autostart.to_s.sub(/^service_widget_autostart_/, "").to_sym
autostart = autostart.to_s.sub(/^service_widget_autostart_/, "").to_sym
return if autostart == :inconsistent

service.start_mode = autostart
end

def status
case service_configuration.status
case service.current_active?
# TRANSLATORS: Status of service
when :active
when true
_("Active")
when :inactive
when false
# TRANSLATORS: Status of service
_("Inactive")
when :inconsistent
# TRANSLATORS: Status of service
_("Partly Active")
when :unknown
# TRANSLATORS: Status of service
_("Unknown")
else
raise "Unknown status #{service_configuration.status.inspect}"
raise "Unknown status #{service.current_active?.inspect}"
end
end

Expand All @@ -130,14 +131,13 @@ def action_widget
end

def action_items
mixed = [:inconsistent, :unknown].include?(service_configuration.status)
current_action = service_configuration.action
current_action = service.action
res = []
res << Item(Id(:service_widget_action_start), _("Start"), current_action == :start) unless service_configuration.status == :active
res << Item(Id(:service_widget_action_stop), _("Stop"), current_action == :stop) unless service_configuration.status == :inactive
res << Item(Id(:service_widget_action_restart), _("Restart"), current_action == :restart) unless service_configuration.status == :inactive
res << Item(Id(:service_widget_action_restart), _("Reload"), current_action == :reload) if service_configuration.status != :inactive && service_configuration.support_reload?
res << Item(Id(:service_widget_action_nothing), _("Keep current state"), current_action == :nothing)
res << Item(Id(:service_widget_action_start), _("Start"), current_action == :start) if service.current_active? != true
res << Item(Id(:service_widget_action_stop), _("Stop"), current_action == :stop) if service.current_active? != false
res << Item(Id(:service_widget_action_restart), _("Restart"), current_action == :restart) if service.current_active? != true
res << Item(Id(:service_widget_action_restart), _("Reload"), current_action == :reload) if service.current_active? != true && service.support_reload?
res << Item(Id(:service_widget_action_nothing), _("Keep current state"), current_action == nil)

res
end
Expand All @@ -151,15 +151,14 @@ def autostart_widget
end

def autostart_items
current_autostart = service_configuration.autostart
keep = [:inconsistent, :unknown].include?(current_autostart)
keep_option = [:inconsistent, :unknown].include?(service_configuration.system_autostart)
current_start_mode = service.start_mode
system_start_mode = service.current_start_mode
res = []

res << Item(Id(:service_widget_autostart_on_boot), _("Start on boot"), current_autostart == :on_boot)
res << Item(Id(:service_widget_autostart_on_demand), _("Start on demand"), current_autostart == :on_demand) if service_configuration.support_on_demand?
res << Item(Id(:service_widget_autostart_manual), _("Do not start"), current_autostart == :manual)
res << Item(Id(:service_widget_autostart_inconsistent), _("Keep current settings"), keep) if keep_option
res << Item(Id(:service_widget_autostart_on_boot), _("Start on boot"), current_start_mode == :on_boot)
res << Item(Id(:service_widget_autostart_on_demand), _("Start on demand"), current_start_mode == :on_demand) if service.support_start_on_demand?
res << Item(Id(:service_widget_autostart_manual), _("Do not start"), current_start_mode == :manual)
res << Item(Id(:service_widget_autostart_inconsistent), _("Keep current settings"), current_start_mode == :inconsistent) if system_start_mode == :inconsistent

res
end
Expand Down

0 comments on commit 668f1ea

Please sign in to comment.