Skip to content

Commit

Permalink
Merge pull request #770 from openSUSE/pattern_modify_dbus
Browse files Browse the repository at this point in the history
Pattern modify dbus
  • Loading branch information
lslezak authored Sep 26, 2023
2 parents 392e7d5 + 2f08e60 commit 2b3feba
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
27 changes: 27 additions & 0 deletions service/lib/agama/dbus/software/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def initialize(backend, logger)
register_callbacks
register_progress_callbacks
register_service_status_callbacks
@selected_patterns = {}
end

SOFTWARE_INTERFACE = "org.opensuse.Agama.Software1"
Expand All @@ -61,6 +62,13 @@ def initialize(backend, logger)

dbus_reader :selected_base_product, "s"

# documented way to be able to write to patterns and trigger signal
attr_writer :selected_patterns

# selected patterns is hash with pattern name as id and 0 for user selected and
# 1 for auto selected. Can be extended in future e.g. for mandatory patterns
dbus_attr_reader :selected_patterns, "a{sy}"

dbus_method :SelectProduct, "in ProductID:s" do |product_id|
old_product_id = backend.product

Expand Down Expand Up @@ -93,6 +101,10 @@ def initialize(backend, logger)
]
end

dbus_method(:AddPattern, "in id:s") { |p| backend.add_pattern(p) }
dbus_method(:RemovePattern, "in id:s") { |p| backend.remove_pattern(p) }
dbus_method(:SetUserPatterns, "in ids:as") { |ids| backend.user_patterns = ids }

dbus_method :ProvisionsSelected, "in Provisions:as, out Result:ab" do |provisions|
[provisions.map { |p| backend.provision_selected?(p) }]
end
Expand Down Expand Up @@ -163,6 +175,21 @@ def register_callbacks
nm_client.on_connection_changed do |connected|
probe if connected
end

backend.on_selected_patterns_change do
self.selected_patterns = compute_patterns
end
end

USER_SELECTED_PATTERN = 0
AUTO_SELECTED_PATTERN = 1
def compute_patterns
patterns = {}
user_selected, auto_selected = backend.selected_patterns
user_selected.each { |p| patterns[p] = USER_SELECTED_PATTERN }
auto_selected.each { |p| patterns[p] = AUTO_SELECTED_PATTERN }

patterns
end
end
end
Expand Down
53 changes: 53 additions & 0 deletions service/lib/agama/software/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def initialize(config, logger)
end
@repositories = RepositoriesManager.new
on_progress_change { logger.info progress.to_s }
# patterns selected by user
@user_patterns = []
@selected_patterns_change_callbacks = []
end

def select_product(name)
Expand Down Expand Up @@ -193,6 +196,52 @@ def patterns(filtered)
patterns
end

def add_pattern(id)
# TODO: error handling
res = Yast::Pkg.ResolvableInstall(id, :pattern)
logger.info "Adding pattern #{res.inspect}"
@user_patterns << id

res = Yast::Pkg.PkgSolve(unused = true)
logger.info "Solver run #{res.inspect}"
selected_patterns_changed
end

def remove_pattern(id)
# TODO: error handling
res = Yast::Pkg.ResolvableNeutral(id, :pattern, force = false)
logger.info "Removing pattern #{res.inspect}"
@user_patterns.delete(id)

res = Yast::Pkg.PkgSolve(unused = true)
logger.info "Solver run #{res.inspect}"
selected_patterns_changed
end

def user_patterns=(ids)
@user_patterns.each { |p| Yast::Pkg.ResolvableNeutral(p, :pattern, force = false) }
@user_patterns = ids
@user_patterns.each { |p| Yast::Pkg.ResolvableInstall(p, :pattern) }
logger.info "Setting patterns to #{res.inspect}"

res = Yast::Pkg.PkgSolve(unused = true)
logger.info "Solver run #{res.inspect}"
selected_patterns_changed
end

# @return [Array<Array<String>,Array<String>] returns pair of arrays where the first one
# is user selected pattern ids and in other is auto selected ones
def selected_patterns
patterns = Y2Packager::Resolvable.find(kind: :pattern, status: :selected)
patterns.map!(&:name)

patterns.partition { |p| @user_patterns.include?(p) }
end

def on_selected_patterns_change(&block)
@selected_patterns_change_callbacks << block
end

# Determines whether a package is installed
#
# @param name [String] Package name
Expand Down Expand Up @@ -293,6 +342,10 @@ def select_resolvables
proposal.set_resolvables("agama", :package, optional_packages,
optional: true)
end

def selected_patterns_changed
@selected_patterns_change_callbacks.each(&:call)
end
end
end
end

0 comments on commit 2b3feba

Please sign in to comment.