Skip to content

Commit

Permalink
Move the logic to (un)select package to a controller
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Jan 31, 2020
1 parent 2acddb8 commit e8c9c9b
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 260 deletions.
140 changes: 140 additions & 0 deletions src/lib/registration/controllers/package_search.rb
Expand Up @@ -21,11 +21,22 @@
require "registration/package_search"

Yast.import "Popup"
Yast.import "HTML"

module Registration
module Controllers
# Implements the actions and keeps the state for the package search feature
class PackageSearch
include Yast::I18n

# @return [Array<RemotePackage>] List of selected packages
attr_reader :selected_packages

# Constructor
def initialize
@selected_packages = []
end

# Returns the list of the current search
#
# @return [Array<RemotePackage>] List of found packages
Expand All @@ -43,6 +54,135 @@ def search(text)
pkg.select! if selected_package_ids.include?(pkg.id)
end
end

# Selects/unselects the current package for installation
#
# It does nothing if the package is already installed.
def toggle_package(package)
return if package.installed?

if package.selected?
unselect_package(package)
else
select_package(package)
end
end

private

# Selects the current package for installation
#
# If required, it selects the corresponding addon
#
# @param package [RemotePackage] Package to select
def select_package(package)
addon = package.addon
select_addon(addon) if addon
set_package_as_selected(package) if addon.nil? || addon.selected? || addon.registered?
end

# Unselects the current package for installation
#
# If not needed, unselects the corresponding addon
#
# @param package [RemotePackage] Package to unselect
#
# @see #unselect_addon
# @see #unselect_package!
def unselect_package(package)
unset_package_as_selected(package)
unselect_addon(package.addon) if package.addon
end

# Selects the given addon if needed
#
# @param addon [Addon] Addon to select
def select_addon(addon)
return if addon.registered? || addon.selected? || addon.auto_selected?
addon.selected if enable_addon?(addon)
end

# Unselects the given addon if required
#
# @param addon [Addon] Addon to unselect
def unselect_addon(addon)
return if addon.registered? || needed_addon?(addon)
addon.unselected if disable_addon?(addon)
end

# Sets the package as selected
#
# Marks the package as selected and adds it to the list of selected packages.
#
# @param package [RemotePackage] Package to add
def set_package_as_selected(package)
package.select!
selected_packages << package
end

# Unsets the package as selected
#
# Marks the package as not selected and removes it from the list of selected packages.
#
# @param package [RemotePackage] Package to remove
def unset_package_as_selected(package)
package.unselect!
selected_packages.delete(package)
end

# Asks the user to enable the addon
#
# @param addon [Addon] Addon to ask about
def enable_addon?(addon)
description = Yast::HTML.Para(
format(
_("The selected package is provided by the '%{name}', " \
"which is not enabled on this system yet."),
name: addon.name
)
)

unselected_deps = addon.dependencies.reject { |d| d.selected? || d.registered? }
if !unselected_deps.empty?
description << Yast::HTML.Para(
format(
_("Additionally, '%{name}' depends on the following modules/extensions:"),
name: addon.name
)
)
description << Yast::HTML.List(unselected_deps.map(&:name))
end
# TRANSLATORS: 'it' and 'them' refers to the modules/extensions to enable
question = n_(
"Do you want to enable it?", "Do you want to enable them?", unselected_deps.size + 1
)
yes_no_popup(description + question)
end

# Asks the user to disable the addon
#
# @param addon [Addon] Addon to ask about
def disable_addon?(addon)
message = format(
_("The '%{name}' is not needed anymore.\n" \
"Do you want to unselect it?"),
name: addon.name
)
yes_no_popup(message)
end

# Determines whether the addon is still needed
def needed_addon?(addon)
selected_packages.any? { |pkg| pkg.addon == addon }
end

# Asks a yes/no question
#
# @return [Boolean] true if the answer is affirmative; false otherwise
def yes_no_popup(message)
ret = Yast2::Popup.show(message, richtext: true, buttons: :yes_no)
ret == :yes
end
end
end
end
128 changes: 2 additions & 126 deletions src/lib/registration/widgets/package_search.rb
Expand Up @@ -39,9 +39,6 @@ module Widgets
class PackageSearch < CWM::CustomWidget
include Yast::Logger

# @return [Array<String>] List of selected packages
attr_reader :selected_packages

# Constructor
def initialize(controller = ::Registration::Controllers::PackageSearch.new)
textdomain "registration"
Expand Down Expand Up @@ -158,130 +155,22 @@ def find_current_package
end

# Selects/unselects the current package for installation
#
# It does nothing if the package is already installed.
def toggle_package
package = find_current_package
return if package.nil? || package.installed?

if package.selected?
unselect_package(package)
else
select_package(package)
end
return if package.nil?

controller.toggle_package(package)
packages_table.update_item(package)
update_details
end

# Selects the current package for installation
#
# If required, it selects the corresponding addon
#
# @param package [RemotePackage] Package to select
def select_package(package)
addon = package.addon
select_addon(addon) if addon
set_package_as_selected(package) if addon.nil? || addon.selected? || addon.registered?
end

# Unselects the current package for installation
#
# If not needed, unselects the corresponding addon
#
# @param package [RemotePackage] Package to unselect
#
# @see #unselect_addon
# @see #unselect_package!
def unselect_package(package)
unset_package_as_selected(package)
unselect_addon(package.addon) if package.addon
end

# Selects the given addon if needed
#
# @param addon [Addon] Addon to select
def select_addon(addon)
return if addon.registered? || addon.selected? || addon.auto_selected?
addon.selected if enable_addon?(addon)
end

# Unselects the given addon if required
#
# @param addon [Addon] Addon to unselect
def unselect_addon(addon)
return if addon.registered? || needed_addon?(addon)
addon.unselected if disable_addon?(addon)
end

# Sets the package as selected
#
# Marks the package as selected and adds it to the list of selected packages.
#
# @param package [RemotePackage] Package to add
def set_package_as_selected(package)
package.select!
selected_packages << package
end

# Unsets the package as selected
#
# Marks the package as not selected and removes it from the list of selected packages.
#
# @param package [RemotePackage] Package to remove
def unset_package_as_selected(package)
package.unselect!
selected_packages.delete(package)
end

# Updates the package details widget
def update_details
# FIXME: remove the content if the current package is nil
current_package = find_current_package
package_details.update(current_package) if current_package
end

# Asks the user to enable the addon
#
# @param addon [Addon] Addon to ask about
def enable_addon?(addon)
description = Yast::HTML.Para(
format(
_("The selected package is provided by the '%{name}', " \
"which is not enabled on this system yet."),
name: addon.name
)
)

unselected_deps = addon.dependencies.reject { |d| d.selected? || d.registered? }
if !unselected_deps.empty?
description << Yast::HTML.Para(
format(
_("Additionally, '%{name}' depends on the following modules/extensions:"),
name: addon.name
)
)
description << Yast::HTML.List(unselected_deps.map(&:name))
end
# TRANSLATORS: 'it' and 'them' refers to the modules/extensions to enable
question = n_(
"Do you want to enable it?", "Do you want to enable them?", unselected_deps.size + 1
)
yes_no_popup(description + question)
end

# Asks the user to disable the addon
#
# @param addon [Addon] Addon to ask about
def disable_addon?(addon)
message = format(
_("The '%{name}' is not needed anymore.\n" \
"Do you want to unselect it?"),
name: addon.name
)
yes_no_popup(message)
end

MINIMAL_SEARCH_TEXT_SIZE = 2

# Determines whether the search text is valid or not
Expand All @@ -298,19 +187,6 @@ def valid_search_text?(text)
)
false
end

# Determines whether the addon is still needed
def needed_addon?(addon)
selected_packages.any? { |pkg| pkg.addon == addon }
end

# Asks a yes/no question
#
# @return [Boolean] true if the answer is affirmative; false otherwise
def yes_no_popup(message)
ret = Yast2::Popup.show(message, richtext: true, buttons: :yes_no)
ret == :yes
end
end
end
end

0 comments on commit e8c9c9b

Please sign in to comment.