Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,756 additions
and
399 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.