Class: Y2Network::Dialogs::WirelessWepKeys::WEPKeys

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

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ WEPKeys


139
140
141
142
# File 'src/lib/y2network/dialogs/wireless_wep_keys.rb', line 139

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

Instance Method Details

#contentsObject


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'src/lib/y2network/dialogs/wireless_wep_keys.rb', line 144

def contents
  VBox(
    Table(
      Id(:wep_keys_table),
      Header(
        # Table header label
        # Abbreviation of Number
        _("No."),
        # Table header label
        _("Key"),
        # Table header label
        Center(_("Default"))
      )
    ),
    HBox(
      # PushButton label
      PushButton(Id(:wep_keys_add), Yast::Label.AddButton),
      # PushButton label
      PushButton(Id(:wep_keys_edit), Yast::Label.EditButton),
      # PushButton label
      PushButton(Id(:wep_keys_delete), Yast::Label.DeleteButton),
      # PushButton label
      PushButton(Id(:wep_keys_default), _("&Set as Default"))
    )
  )
end

#dialog(value = nil) ⇒ String?

Open a dialog to add/edit a key. TODO: own class for it


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'src/lib/y2network/dialogs/wireless_wep_keys.rb', line 242

def dialog(value = nil)
  value ||= ""
  Yast::UI.OpenDialog(
    Opt(:decorated),
    VBox(
      HSpacing(1),
      # TextEntry label
      TextEntry(Id(:key), _("&WEP Key"), value),
      HSpacing(1),
      HBox(
        PushButton(Id(:ok), Opt(:default), Yast::Label.OKButton),
        PushButton(Id(:cancel), Yast::Label.CancelButton)
      )
    )
  )

  Yast::UI.SetFocus(Id(:key))
  val = nil
  while (ret = Yast::UI.UserInput) == :ok
    val = Yast::UI.QueryWidget(Id(:key), :Value)
    break if valid_key?(val)
    # Popup::Error text
    Yast::Popup.Error(
      _(
        "The WEP key is not valid. WEP key can be specified either directly in hex " \
          "digits, with or without dashes, or in the key's ASCII representation " \
          "(prefix s: ), or as a passphrase which will be hashed (prefix h: )."
      )
    )
  end

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

  val
end

#handle(event) ⇒ Symbol?


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
# File 'src/lib/y2network/dialogs/wireless_wep_keys.rb', line 203

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

  cur = Yast::UI.QueryWidget(Id(:wep_keys_table), :CurrentItem).to_i
  case event["ID"]
  when :wep_keys_edit
    key = dialog(@settings.keys[cur])
    if key
      @settings.keys[cur] = key
      refresh_table(cur)
    end
  when :wep_keys_add
    key = dialog
    if key
      # replace first nil value
      index = @settings.keys.index(nil)
      if index
        @settings.keys[index] = key
      else
        @settings.keys << key
      end
      log.info "new keys #{@settings.keys.inspect}"
      refresh_table(@settings.keys.compact.size - 1)
    end
  when :wep_keys_delete
    @settings.keys.delete_at(cur)
    refresh_table(0)
  when :wep_keys_default
    @settings.default_key = cur
    refresh_table(cur)
  end

  nil
end

#initObject

TODO: help text which explain format of WEP keys


173
174
175
# File 'src/lib/y2network/dialogs/wireless_wep_keys.rb', line 173

def init
  refresh_table(0)
end

#refresh_table(selected_index) ⇒ Object


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'src/lib/y2network/dialogs/wireless_wep_keys.rb', line 177

def refresh_table(selected_index)
  @settings.keys ||= [] # TODO: should be fixed by proper initialize of settings object
  table_items = @settings.keys.each_with_index.map do |key, i|
    next unless key
    Item(Id(i), i.to_s, key, i == @settings.default_key ? "X" : "")
  end

  compact_keys = @settings.keys.compact

  Yast::UI.ChangeWidget(Id(:wep_keys_table), :Items, table_items.compact)
  [:wep_keys_delete, :wep_keys_edit, :wep_keys_default].each do |k|
    Yast::UI.ChangeWidget(
      Id(k),
      :Enabled,
      !compact_keys.empty?
    )
  end
  Yast::UI.ChangeWidget(
    Id(:wep_keys_add),
    :Enabled,
    compact_keys.size < 4 # only 4 keys are possible
  )
  Yast::UI.ChangeWidget(Id(:wep_keys_table), :CurrentItem, selected_index)
end

#valid_key?(_key) ⇒ Boolean


279
280
281
282
# File 'src/lib/y2network/dialogs/wireless_wep_keys.rb', line 279

def valid_key?(_key)
  # TODO: write validation
  true
end