Skip to content

Commit

Permalink
Adapt dialog to use new ServiceWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Aug 24, 2018
1 parent 3eb7d5a commit bd1c438
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 170 deletions.
333 changes: 182 additions & 151 deletions src/include/tftp-server/dialogs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# $Id$

require "y2journal"
require "yast2/service_widget"

module Yast
module TftpServerDialogsInclude
Expand All @@ -27,16 +28,17 @@ def initialize_tftp_server_dialogs(include_target)
end

# Check for required packaged to be installed
# @return `abort if aborted and `next otherwise

#
# @return [Symbol] :abort if aborted and :next otherwise
def Packages
return :abort if !Package.InstallAll(TftpServer.required_packages)

:next
end

# Read settings dialog
# @return `abort if aborted and `next otherwise
#
# @return [Symbol] :abort if aborted and :next otherwise
def ReadDialog
ret = true

Expand All @@ -50,193 +52,222 @@ def ReadDialog
end

# Write settings dialog
# @return `abort if aborted and `next otherwise
#
# @return [Symbol] :abort if aborted and :next otherwise
def WriteDialog
ret = TftpServer.Write
ret ? :next : :abort
end

# Main dialog
# @return dialog result
#
# @return [Symbol] dialog result (:next, :cancel, :abort)
def MainDialog
Wizard.SetScreenShotName("tftp-server-1-main")

start = TftpServer.start
# start = TftpServer.start
directory = TftpServer.directory
changed = false

# Tftp-server dialog caption
caption = _("TFTP Server Configuration")

# firewall widget using CWM
fw_settings = {
"services" => ["tftp"],
"display_details" => true
}
fw_cwm_widget = CWMFirewallInterfaces.CreateOpenFirewallWidget(
fw_settings
Wizard.SetContentsButtons(
caption,
contents,
help,
Label.BackButton,
Label.OKButton
)
Wizard.HideBackButton
Wizard.SetAbortButton(:abort, Label.CancelButton)

# dialog help text
help_text = _("<p><big><b>Configuring a TFTP Server</b></big></p>")
# dialog help text
help_text = Ops.add(
help_text,
_(
"<p>Use this to enable a server for TFTP (trivial file transfer protocol). The server will be started using xinetd.</p>"
)
)
# enlighten newbies, #102946
# dialog help text
help_text = Ops.add(
help_text,
_("<p>Note that TFTP and FTP are not the same.</p>")
)
# dialog help text
help_text = Ops.add(
help_text,
_(
"<p><b>Boot Image Directory</b>:\n" +
"Specify the directory where served files are located. The usual value is\n" +
"<tt>/tftpboot</tt>. The directory will be created if it does not exist. \n" +
"The server uses this as its root directory (using the <tt>-s</tt> option).</p>\n"
)
)
help_text = Ops.add(help_text, Ops.get_string(fw_cwm_widget, "help", ""))
# Initialize the widget (set the current value)
CWMFirewallInterfaces.OpenFirewallInit(firewall_widget, "")

UI.ChangeWidget(Id(:viewlog), :Enabled, !Mode.config)

result = handle_events

Wizard.RestoreScreenShotName
result
end

private

contents = HVSquash(
# Dialog contents
#
# @return [Yast::Term]
def contents
HVSquash(
VBox(
RadioButtonGroup(
Id(:rbg),
VBox(
Left(
RadioButton(
Id(:tftpno),
Opt(:notify),
# Radio button label, disable TFTP server
_("&Disable"),
!start
)
),
Left(
RadioButton(
Id(:tftpyes),
Opt(:notify),
# Radio button label, disable TFTP server
_("&Enable"),
start
)
)
)
),
service_widget.content,
VSpacing(1),
TextAndButton(
# Text entry label
HBox(
# Directory where served files (usually boot images) reside
TextEntry(Id(:directory), _("&Boot Image Directory"), directory),
# push button label
# select a directory from the filesystem
PushButton(Id(:browse), _("Bro&wse..."))
Bottom(TextEntry(Id(:directory), _("&Boot Image Directory"), TftpServer.directory)),
HSpacing(0.5),
# Select a directory from the filesystem
Bottom(PushButton(Id(:browse), _("Bro&wse...")))
),
VSpacing(1),
Ops.get_term(fw_cwm_widget, "custom_widget", Empty()),
firewall_widget["custom_widget"] || Empty(),
VSpacing(2),
# push button label
# display a log file
# Display a log file
PushButton(Id(:viewlog), _("&View Log"))
)
)
end

Wizard.SetContentsButtons(
caption,
contents,
help_text,
Label.BackButton,
Label.OKButton
)
Wizard.HideBackButton
Wizard.SetAbortButton(:abort, Label.CancelButton)

# initialize the widget (set the current value)
CWMFirewallInterfaces.OpenFirewallInit(fw_cwm_widget, "")

UI.ChangeWidget(Id(:viewlog), :Enabled, !Mode.config)
event = nil
ret = nil
begin
UI.ChangeWidget(Id(:directory), :Enabled, start)
UI.ChangeWidget(Id(:browse), :Enabled, start)
# Handles dialog events
#
# @return [Symbol] :next, :cancel, :abort
def handle_events
input = nil

loop do
event = UI.WaitForEvent
ret = Ops.get(event, "ID")
ret = :abort if ret == :cancel

# handle the events, enable/disable the button, show the popup if button clicked
CWMFirewallInterfaces.OpenFirewallHandle(fw_cwm_widget, "", event)

start = UI.QueryWidget(Id(:rbg), :CurrentButton) == :tftpyes
directory = Convert.to_string(UI.QueryWidget(Id(:directory), :Value))

# discard the difference in disabled fields:
# directory is only considered if start is on
changed = CWMFirewallInterfaces.OpenFirewallModified("") ||
start != TftpServer.start || # "" because method doesn't use parameter at all, nice :(
start && directory != TftpServer.directory

if ret == :browse
directory = UI.AskForExistingDirectory(
directory != "" ? directory : "/",
""
)
UI.ChangeWidget(Id(:directory), :Value, directory) if directory != nil
elsif ret == :viewlog
# show both service and socket logs for current boot
query = Y2Journal::Query.new(interval: "0", filters: { "unit" => ["tftp.service", "tftp.socket"] })
Y2Journal::EntriesDialog.new(query: query).run
end
input = event["ID"]

# validity checks
if ret == :next && start
if CheckDirectorySyntax(directory)
#ok, say that it will be created
if !Mode.config &&
Ops.less_than(SCR.Read(path(".target.size"), directory), 0)
# the dir does not exist
ret = Popup.YesNo(Message.DirectoryDoesNotExistCreate(directory)) ? ret : nil
end
else
UI.SetFocus(Id(:directory))
# error popup
Popup.Error(
_(
"The directory must start with a slash (/)\nand must not contain spaces."
)
)
ret = nil
# Handle the events, enable/disable the button, show the popup if button clicked
CWMFirewallInterfaces.OpenFirewallHandle(firewall_widget, "", event)

case input
when :browse
ask_directory
when :viewlog
show_log
when :next
if check_directory
# Grab current settings, store them to SuSEFirewall::
CWMFirewallInterfaces.OpenFirewallStore(firewall_widget, "", event)
save_service
break
end
when :cancel, :abort
break if Popup.ReallyAbort(changes?)
end
end until ret == :next ||
(ret == :back || ret == :abort) && (!changed || Popup.ReallyAbort(true))
end

input
end

# Help text
#
# @return [String]
def help
_("<p><big><b>Configuring a TFTP Server</b></big></p>") +
_("<p>Use this to enable a server for TFTP (trivial file transfer protocol). The server will be started using xinetd.</p>") +
_("<p>Note that TFTP and FTP are not the same.</p>") +
_(
"<p><b>Boot Image Directory</b>:\n" +
"Specify the directory where served files are located. The usual value is\n" +
"<tt>/tftpboot</tt>. The directory will be created if it does not exist. \n" +
"The server uses this as its root directory (using the <tt>-s</tt> option).</p>\n"
) +
firewall_widget["help"] || ""
end

# Widget to define state and start mode of the service
#
# @return [Yast2::ServiceWidget]
def service_widget
@service_widget ||= Yast2::ServiceWidget.new(TftpServer.service)
end

if ret == :next
# grab current settings, store them to SuSEFirewall::
CWMFirewallInterfaces.OpenFirewallStore(fw_cwm_widget, "", event)
# Firewall widget using CWM
#
# @return [Hash] see CWMFirewallInterfaces.CreateOpenFirewallWidget
def firewall_widget
@firewall_widget ||= CWMFirewallInterfaces.CreateOpenFirewallWidget(
"services" => ["tftp"],
"display_details" => true
)
end

TftpServer.start = start
TftpServer.directory = directory if start
# Value of the input field to indicate the Boot Image Directory
#
# @return [String]
def directory
UI.QueryWidget(Id(:directory), :Value)
end

# Opens a dialog to ask for the directory
#
# @note The input field is updated with the selected directory.
def ask_directory
search_path = directory.empty? ? "/" : directory

directory = UI.AskForExistingDirectory(search_path, "")
UI.ChangeWidget(Id(:directory), :Value, directory)
end

# Asks whether to create the directory (usefull when the directory does not exist)
#
# @return [Boolean]
def ask_create_directory
Popup.YesNo(Message.DirectoryDoesNotExistCreate(directory))
end

# Checks whether the given path is valid, and if so, it asks for creating the directory
# when it does not exist yet
#
# @return [Boolean] true when the given path is valid and exists (or should be created);
# false otherwise.
def check_directory
if !valid_directory?
show_directory_error
false
elsif !exist_directory?
ask_create_directory
else
true
end
end

Wizard.RestoreScreenShotName
Convert.to_symbol(ret)
# Checks whether the given directory path is valid
#
# @return [Boolean]
def valid_directory?
directory.start_with?("/") && !directory.match?(/[ \t]/)
end
def TextAndButton(text, button)
text = deep_copy(text)
button = deep_copy(button)
HBox(Bottom(text), HSpacing(0.5), Bottom(button))

# Checks whether the given directory path already exists
#
# @return [Boolean]
def exist_directory?
return true if Mode.config

SCR.Read(path(".target.size"), directory) >= 0
end
def CheckDirectorySyntax(dir)
Builtins.substring(dir, 0, 1) == "/" &&
Builtins.filterchars(" \t", dir) == ""

# Opens a popup to indicate the error when the given directory path is not valid
def show_directory_error
message = _("The directory must start with a slash (/)\nand must not contain spaces.")

Popup.Error(message)
end

# Shows both service and socket logs since current boot
def show_log
query = Y2Journal::Query.new(interval: "0", filters: { "unit" => ["tftp.service", "tftp.socket"] })
Y2Journal::EntriesDialog.new(query: query).run
end

# Whether something has been edited
#
# @note Changes in the Service Widget are not taken into account.
#
# @return [Boolean]
def changes?
CWMFirewallInterfaces.OpenFirewallModified("") || directory != TftpServer.directory
end

# Saves the service changes
def save_service
service_widget.store
TftpServer.start = TftpServer.service.active?
TftpServer.directory = directory
end
end
end
Loading

0 comments on commit bd1c438

Please sign in to comment.