Skip to content

Commit

Permalink
Merge baefc0e into e8575ea
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Feb 26, 2021
2 parents e8575ea + baefc0e commit 4d33ace
Show file tree
Hide file tree
Showing 33 changed files with 1,756 additions and 399 deletions.
97 changes: 97 additions & 0 deletions src/lib/y2network/bitrate.rb
@@ -0,0 +1,97 @@
# Copyright (c) [2021] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

module Y2Network
# Represents a bitrate
#
# This class is not as generic as Y2Storage::DiskSize and it is used to support
# parsing bitrates information from `iwlist`. However, it could be extended
# in the future if needed.
class Bitrate
include Comparable

# Exception when trying to parse a string as a bitrate
class ParseError < StandardError; end

PARSE_REGEXP = /\A(\d+(?:.\d+)?)\ *(\w+)?/.freeze
private_constant :PARSE_REGEXP

UNITS = ["b", "kb", "Mb", "Gb"].freeze
private_constant :UNITS

class << self
# Parses a string and converts the value to a string
#
# @example Parsing Mb/s
# bitrate = Bitrate.parse("54 Mb/s")
# bitrate.to_i #=> 54000000
# bitrate.to_s #=> "54 Mb/s"
#
# @param str [String] String to parse
# @return [Bitrate]
# @raise ParseError
def parse(str)
match = PARSE_REGEXP.match(str)
raise ParseError unless match

number, unit = match.captures
unit ||= "b"
power = UNITS.index(unit)
raise ParseError unless power

new(number.to_i * (1000**power))
end
end

# @param bits [Integer] Bits
def initialize(bits)
@bits = bits
end

# @return [Integer] Return the bits
def to_i
@bits
end

# Returns the string representation of the bitrate
#
# It automatically selects the unit to use depending on the bitrate value.
#
# @return [String] String representation
def to_s
power = (0..UNITS.length - 1).to_a.reverse.find do |u|
to_i > (1000**u)
end

units = UNITS[power]
number = @bits.to_f / (1000**power)
number_str = number.to_s.sub(".0", "") # strip insignificant zeroes

"#{number_str} #{units}/s"
end

# Compare two bitrates
#
# @param other [Bitrate]
# @return [Integer]
def <=>(other)
to_i <=> other.to_i
end
end
end
2 changes: 1 addition & 1 deletion src/lib/y2network/connection_config/wireless.rb
Expand Up @@ -32,7 +32,7 @@ class Wireless < Base
attr_accessor :essid
# @return [String] Network ID
attr_accessor :nwid
# @return [Symbol] Authorization mode (:open, :shared, :psk, :eap)
# @return [Symbol] Authorization mode (:open, :shared, :psk, :eap)
attr_accessor :auth_mode
# FIXME: Consider moving keys to different classes.
# @return [String] WPA preshared key
Expand Down
10 changes: 8 additions & 2 deletions src/lib/y2network/dialogs/wireless_expert_settings.rb
Expand Up @@ -19,8 +19,8 @@

require "yast"
require "cwm/dialog"
require "y2network/widgets/wireless"
require "y2network/widgets/wireless_expert"
require "y2network/widgets/wireless_mode"

module Y2Network
module Dialogs
Expand All @@ -44,11 +44,13 @@ def contents
VBox(
VSpacing(0.5),
Frame(
_("Wireless Expert Settings"),
_("Expert Settings"),
HBox(
HSpacing(2),
VBox(
VSpacing(1),
mode_widget,
VSpacing(0.2),
channel_widget, # TODO: channel only when mode is master or adhoc
VSpacing(0.2),
bitrate_widget,
Expand Down Expand Up @@ -104,6 +106,10 @@ def should_open_dialog?

private

def mode_widget
@mode_widget ||= Y2Network::Widgets::WirelessMode.new(@settings)
end

def channel_widget
@channel_widget ||= Y2Network::Widgets::WirelessChannel.new(@settings)
end
Expand Down
137 changes: 137 additions & 0 deletions src/lib/y2network/dialogs/wireless_networks.rb
@@ -0,0 +1,137 @@
# Copyright (c) [2021] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "yast2/feedback"
require "y2network/widgets/wireless_networks"
require "y2network/wireless_network"
require "cwm/popup"

Yast.import "Label"

module Y2Network
module Dialogs
# Button that runs an custom action when it is clicked
class CallbackButton < ::CWM::PushButton
# @param label [String] Button label
# @param block [Proc] Action to run
def initialize(label, &block)
@label = label
@block = block
end

# @see CWM::AbstractWidget
def label
@label
end

# @see CWM::CustomWidget
def handle
@block.call

nil
end
end

# This widget displays a list of wireless networks and allows the user to select one
#
# @example Returning the ESSID of the selected network
# WirelessNetworks.new("wlo1").run #=> "sample_essid"
class WirelessNetworks < CWM::Popup
attr_reader :interface

# Constructor
#
# @param builder [InterfaceConfigBuilder]
def initialize(builder)
textdomain "network"
@builder = builder
end

# @see CWM::AbstractWidget
def title
_("Available Wireless Networks")
end

# @see CWM::CustomWidget
def contents
VBox(
MinSize(70, 10, networks_table),
refresh_button
)
end

# Runs the dialog and returns the selected network instance
#
# If the user presses the 'Cancel' button, it returns `nil`.
#
# @return [WirelessNetwork] Network or `nil` if the dialog was canceled
def run
networks_table.update(find_networks)
(super == :ok) ? networks_table.selected : nil
end

private

# Returns the label for the 'Accept' button
#
# @return [String]
def ok_button_label
Yast::Label.SelectButton
end

# Refresh button
#
# @return [Yast::Term]
def refresh_button
CallbackButton.new(_("Refresh")) { networks_table.update(find_networks(false)) }
end

# Embedded wireless networks table
#
# @return [Y2Network::Widgets::WirelessNetworks] Wireless networks table widget
def networks_table
@networks_table ||= Y2Network::Widgets::WirelessNetworks.new(@builder)
end

# Scans for wireless networks
#
# @param cache [Boolean] Use the cached values if available
# @return [Array<WirelessNetwork>] List of found wireless networks
# @see Y2Network::WirelessNetwork.all
def find_networks(cache = true)
found_networks = nil
Yast2::Feedback.show(
_("Scanning for wireless networks..."), headline: _("Scanning network")
) do
found_networks = Y2Network::WirelessNetwork.all(@builder.interface.name, cache: cache)
log.info("Found networks: #{found_networks.map(&:essid)}")
end

found_networks
end

# Returns the dialogs button
#
# @return [Array<Yast::Term>]
def buttons
[ok_button, cancel_button]
end
end
end
end
Expand Up @@ -61,9 +61,10 @@ def wireless_keys
end

BACKWARD_MAPPING = {
"wpa-eap": :eap,
"wpa-psk": :psk,
shared: :sharedkey
"wpa-eap": :eap,
"wpa-psk": :psk,
shared: :sharedkey,
"no-encryption": :none
}.freeze
# Transform old backwards compatible values to unified ones.
#
Expand Down
20 changes: 18 additions & 2 deletions src/lib/y2network/wicked/connection_config_writers/wireless.rb
Expand Up @@ -50,7 +50,7 @@ def update_file(conn)
# @see #write_psk_auth_settings
# @see #write_shared_auth_settings
def write_auth_settings(conn)
file.wireless_auth_mode = conn.auth_mode
file.wireless_auth_mode = auth_mode_from_conn(conn)
meth = "write_#{conn.auth_mode || :open}_auth_settings".to_sym
send(meth, conn) if respond_to?(meth, true)
end
Expand Down Expand Up @@ -85,7 +85,7 @@ def write_psk_auth_settings(conn)
def write_wep_auth_settings(conn)
return if (conn.keys || []).compact.all?(&:empty?)

file.wireless_keys = conn.keys
file.wireless_keys = file_keys(conn)
file.wireless_key_length = conn.key_length
file.wireless_default_key = conn.default_key
end
Expand All @@ -99,6 +99,22 @@ def write_open_auth_settings(conn)
def write_shared_auth_settings(conn)
write_wep_auth_settings(conn)
end

# @param conn [Y2Network::ConnectionConfig::Base] Configuration to write
def auth_mode_from_conn(conn)
return "no-encryption" if conn.auth_mode.to_sym == :none

conn.auth_mode
end

# Convenience method to obtain the map of wireless keys in the file
# format
#
# @param conn [Y2Network::ConnectionConfig::Base] Configuration to write
# @return [Hash<Integer, String>] indexed wireless wep keys
def file_keys(conn)
conn.keys.each_with_index.with_object({}) { |(k, i), h| h["_#{i}"] = k }
end
end
end
end
Expand Down

0 comments on commit 4d33ace

Please sign in to comment.