Class: Y2Network::Widgets::AdditionalAddresses

Inherits:
CWM::CustomWidget
  • Object
show all
Defined in:
src/lib/y2network/widgets/additional_addresses.rb

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ AdditionalAddresses


31
32
33
34
# File 'src/lib/y2network/widgets/additional_addresses.rb', line 31

def initialize(settings)
  textdomain "network"
  @settings = settings
end

Instance Method Details

#contentsObject


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'src/lib/y2network/widgets/additional_addresses.rb', line 36

def contents
  Frame(
    Id(:additional_addresses),
    # Frame label
    _("Additional Addresses"),
    HBox(
      HSpacing(3),
      VBox(
        # :-) this is a small trick to make ncurses in 80x25 happy :-)
        # it rounds spacing up or down to the nearest integer, 0.5 -> 1, 0.49 -> 0
        VSpacing(0.49),
        Table(
          Id(:address_table),
          Opt(:notify),
          Header(
            # Table header label
            _("Address Label"),
            # Table header label
            _("IP Address"),
            # Table header label
            _("Netmask")
          ),
          []
        ),
        Left(
          HBox(
            # PushButton label
            PushButton(Id(:add_address), _("Ad&d")),
            # PushButton label
            PushButton(Id(:edit_address), Opt(:disabled), _("&Edit")),
            # PushButton label
            PushButton(Id(:delete_address), Opt(:disabled), _("De&lete"))
          )
        ),
        VSpacing(0.49)
      ),
      HSpacing(3)
    )
  )
end

#dialog(name, entry) ⇒ Yast::Term

Open a dialog to edit a name-ipaddr-netmask triple. TODO: own class for it


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'src/lib/y2network/widgets/additional_addresses.rb', line 157

def dialog(name, entry)
  label = entry ? entry[:label] : ""
  ip = entry ? entry[:ip] : ""
  id = entry ? entry[:id] : ""
  mask = entry ? "/#{entry[:prefixlen]}" : ""

  Yast::UI.OpenDialog(
    Opt(:decorated),
    VBox(
      HSpacing(1),
      VBox(
        # TextEntry label
        TextEntry(Id(:name), _("&Address Label"), label),
        # TextEntry label
        TextEntry(
          Id(:ipaddr),
          _("&IP Address"),
          ip
        ),
        # TextEntry label
        TextEntry(Id(:netmask), _("Net&mask"), mask)
      ),
      HSpacing(1),
      HBox(
        PushButton(Id(:ok), Opt(:default), Yast::Label.OKButton),
        PushButton(Id(:cancel), Yast::Label.CancelButton)
      )
    )
  )

  Yast::UI.ChangeWidget(
    Id(:name),
    :ValidChars,
    Yast::String.CAlnum
  )
  Yast::UI.ChangeWidget(Id(:ipaddr), :ValidChars, Yast::IP.ValidChars)

  if entry
    Yast::UI.SetFocus(Id(:ipaddr))
  else
    Yast::UI.SetFocus(Id(:name))
  end

  while (ret = Yast::UI.UserInput) == :ok

    res = {}
    val = Yast::UI.QueryWidget(Id(:name), :Value)

    if "#{name}.#{val}" !~ /^[[:alnum:]._:-]{1,15}\z/
      # Popup::Error text
      Yast::Popup.Error(_("Label is too long."))
      Yast::UI.SetFocus(Id(:name))
      next
    end

    res[:label] = val

    ip = Yast::UI.QueryWidget(Id(:ipaddr), :Value)
    if !Yast::IP.Check(ip)
      # Popup::Error text
      Yast::Popup.Error(_("The IP address is invalid."))
      Yast::UI.SetFocus(Id(:ipaddr))
      next
    end
    res[:ip] = ip

    val = Yast::UI.QueryWidget(Id(:netmask), :Value)
    if !valid_prefix_or_netmask(ip, val)
      # Popup::Error text
      Yast::Popup.Error(_("The subnet mask is invalid."))
      Yast::UI.SetFocus(Id(:netmask))
      next
    end

    netmask = ""
    prefixlen = ""
    if val.start_with?("/")
      prefixlen = val[1..-1]
    elsif Yast::Netmask.Check6(val)
      prefixlen = val
    else
      netmask = val
    end

    res[:prefixlen] = if prefixlen.empty?
      IPAddr.new("#{netmask}/#{netmask}")
    else
      prefixlen
    end
    res[:prefixlen] = prefixlen
    res[:id] = id

    break
  end

  Yast::UI.CloseDialog
  return nil if ret != :ok

  res
end

#handle(event) ⇒ Symbol?


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'src/lib/y2network/widgets/additional_addresses.rb', line 121

def handle(event)
  return nil if event["EventReason"] != "Activated"

  cur = Yast::UI.QueryWidget(Id(:address_table), :CurrentItem).to_i
  case event["ID"]
  when :edit_address
    item = dialog(@settings.name, @settings.aliases[cur])
    if item
      @settings.aliases[cur] = item
      refresh_table
      Yast::UI.ChangeWidget(Id(:address_table), :CurrentItem, cur)
    end
  when :add_address
    item = dialog(@settings.name, nil)
    if item
      @settings.aliases << item
      refresh_table
      Yast::UI.ChangeWidget(
        Id(:address_table),
        :CurrentItem,
        @settings.aliases.size - 1
      )
    end
  when :delete_address
    @settings.aliases.delete_at(cur)
    refresh_table
  end

  nil
end

#helpObject


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'src/lib/y2network/widgets/additional_addresses.rb', line 77

def help
  _(
    "<p><b><big>Additional Addresses</big></b></p>\n<p>Configure additional addresses of an interface in this table.</p>\n"
  ) +
    # Aliases dialog help 2/4
    _(
      "<p>Enter an <b>IPv4 Address Label</b>, an <b>IP Address</b>, and\nthe <b>Netmask</b>.</p>"
    ) +
    # Aliases dialog help 3/4
    _(
      "<p><b>IPv4 Address Label</b>, formerly known as Alias Name, is optional and legacy. The total\n" \
      "length of interface name (inclusive of the colon and label) is\n" \
      "limited to 15 characters. The obsolete ifconfig utility truncates it after 9 characters.</p>"
    ) +
    # Aliases dialog help 3/4, #83766
    _(
      "<p>Do not include the interface name in the label. For example, enter <b>foo</b> instead of <b>eth0:foo</b>.</p>"
    )
end

#initObject


97
98
99
# File 'src/lib/y2network/widgets/additional_addresses.rb', line 97

def init
  refresh_table
end

#refresh_tableObject


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'src/lib/y2network/widgets/additional_addresses.rb', line 101

def refresh_table
  table_items = @settings.aliases.each_with_index.map do |data, i|

    Item(Id(i), data[:label], data[:ip], "/#{data[:prefixlen]}")
  end

  Yast::UI.ChangeWidget(Id(:address_table), :Items, table_items)
  Yast::UI.ChangeWidget(
    Id(:edit_address),
    :Enabled,
    !@settings.aliases.empty?
  )
  Yast::UI.ChangeWidget(
    Id(:delete_address),
    :Enabled,
    !@settings.aliases.empty?
  )
end

#valid_prefix_or_netmask(ip, mask) ⇒ Object


258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'src/lib/y2network/widgets/additional_addresses.rb', line 258

def valid_prefix_or_netmask(ip, mask)
  valid_mask = false
  mask = mask[1..-1] if mask.start_with?("/")

  if Yast::IP.Check4(ip) && (Yast::Netmask.Check4(mask) || Yast::Netmask.CheckPrefix4(mask))
    valid_mask = true
  elsif Yast::IP.Check6(ip) && Yast::Netmask.Check6(mask)
    valid_mask = true
  else
    log.warn "IP address #{ip} and mask #{mask} is not valid"
  end
  valid_mask
end