Class: Yast::WizardHWClass

Inherits:
Module
  • Object
show all
Defined in:
library/cwm/src/modules/WizardHW.rb

Instance Method Summary collapse

Instance Method Details

#_SetSelectedItem(selected) ⇒ Object

Set which item is to be selected


96
97
98
99
100
101
102
103
104
105
106
107
# File 'library/cwm/src/modules/WizardHW.rb', line 96

def _SetSelectedItem(selected)
  if !selected.nil?
    UI.ChangeWidget(Id(:_hw_items), :CurrentItem, selected)
    UI.ChangeWidget(
      Id(:_hw_sum),
      :Value,
      Ops.get(@descriptions, selected, "")
    )
  end

  nil
end

#CreateActionsButton(actions) ⇒ Yast::Term

Create the Push/Menu button for additional actions


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'library/cwm/src/modules/WizardHW.rb', line 179

def CreateActionsButton(actions)
  actions = deep_copy(actions)
  sz = Builtins.size(actions)
  return Empty() if sz == 0

  if sz == 1
    id = Ops.get(actions, [0, 0])
    if id.nil?
      Builtins.y2error("Unknown ID for button: %1", Ops.get(actions, 0))
      id = "nil"
    end
    return PushButton(Id(id), GetActionLabel(Ops.get(actions, 0, [])))
  end
  items = Builtins.maplist(actions) do |i|
    id = Ops.get(i, 0)
    if id.nil?
      Builtins.y2error("Unknown ID for button: %1", Ops.get(actions, 0))
      id = "nil"
    end
    Item(Id(id), GetActionLabel(i))
  end  # menu button

  MenuButton(_("&Other"), items)
end

#CreateHWDialog(title, help, headers, actions) ⇒ Object

Note:

This is a stable API function

Create the Hardware Wizard dialog Draw the dialog below the widgets (next to other buttons)


363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'library/cwm/src/modules/WizardHW.rb', line 363

def CreateHWDialog(title, help, headers, actions)
  headers = deep_copy(headers)
  actions = deep_copy(actions)  # reinitialize internal variables

  @current_items = []
  @descriptions = {}
  @last_event = {}
  @get_item_descr_callback = nil
  @action_callback = fun_ref(
    method(:SimpleStoreReturnValue),
    "symbol (string, map)"
  )

  # now create the dialog
  widget_descr = CreateWidget(headers, actions)
  Ops.set(widget_descr, "help", help) # to suppress error in log
  w = CWM.CreateWidgets(["wizard_hw"],  "wizard_hw" => widget_descr)
  contents = Ops.get_term(w, [0, "widget"], VBox())
  Wizard.SetContents(title, contents, help, false, true)

  nil
end

#CreateRichTextDescription(title, properties) ⇒ String

Note:

This is a stable API function

Create rich text description of a device. It can be used for WizardHW::SetContents function for formatting richtext device descriptions


468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'library/cwm/src/modules/WizardHW.rb', line 468

def CreateRichTextDescription(title, properties)
  properties = deep_copy(properties)
  items = ""

  if !properties.nil? && Ops.greater_than(Builtins.size(properties), 0)
    Builtins.foreach(properties) do |prop|
      items = Ops.add(Ops.add(Ops.add(items, "<LI>"), prop), "</LI>")
    end
  end

  ret = ""

  ret = Ops.add(Ops.add("<P><B>", title), "</B></P>") if !title.nil? && title != ""

  ret = Ops.add(Ops.add(Ops.add(ret, "<P><UL>"), items), "</UL></P>") if items != ""

  ret
end

#CreateWidget(headers, actions) ⇒ Object

Note:

This is a stable API function

Create CWM widtet for the hardware settings NOTE: The Init and Handle callbacks must be defined


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
# File 'library/cwm/src/modules/WizardHW.rb', line 213

def CreateWidget(headers, actions)
  headers = deep_copy(headers)
  actions = deep_copy(actions)
  hdr = Header()
  Builtins.foreach(headers) { |hi| hdr = Builtins.add(hdr, hi) }
  item_list = Table(Id(:_hw_items), Opt(:notify, :immediate), hdr)
  buttons = HBox(
    PushButton(Id(:add), Label.AddButton),
    PushButton(Id(:edit), Label.EditButton),
    PushButton(Id(:delete), Label.DeleteButton),
    HStretch(),
    CreateActionsButton(actions)
  )
  item_summary = RichText(Id(:_hw_sum), "")

  contents = VBox(VWeight(3, item_list), VWeight(1, item_summary), buttons)

  handle_events = [:_hw_items, :add, :edit, :delete]
  extra_events = Builtins.maplist(actions) { |i| Ops.get(i, 1) }
  extra_events = Builtins.filter(extra_events) { |i| !i.nil? }
  handle_events = Builtins.merge(handle_events, extra_events)

  ret = {
    "widget"        => :custom,
    "custom_widget" => contents,
    "handle_events" => handle_events
  }

  deep_copy(ret)
end

#GetActionLabel(action) ⇒ String

Get the description label for the action


169
170
171
172
173
174
# File 'library/cwm/src/modules/WizardHW.rb', line 169

def GetActionLabel(action)
  action = deep_copy(action)
  fallback = ""
  fallback = Ops.get_string(action, 0, "") if Ops.is_string?(Ops.get(action, 0))
  Ops.get_string(action, 1, fallback)
end

#Handle(_key, event) ⇒ Symbol

Handle function of the widget Used when using the callback interface


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'library/cwm/src/modules/WizardHW.rb', line 133

def Handle(_key, event)
  event = deep_copy(event)
  @last_event = deep_copy(event)
  current = Convert.to_string(UI.QueryWidget(Id(:_hw_items), :CurrentItem))
  if Ops.get(event, "ID") == :_hw_items
    descr = if @get_item_descr_callback.nil?
      Ops.get(@descriptions, current, "")
    else
      @get_item_descr_callback.call(current)
    end
    UI.ChangeWidget(Id(:_hw_sum), :Value, descr)
    return nil
  end

  return @action_callback.call(current, event) unless @action_callback.nil?

  ret = Ops.get(event, "ID")

  Ops.is_symbol?(ret) ? Convert.to_symbol(ret) : nil
end

#Init(_key) ⇒ Object

Init function of the widget Used when using the callback interface


112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'library/cwm/src/modules/WizardHW.rb', line 112

def Init(_key)
  if !@set_items_callback.nil?
    @set_items_callback.call
  else
    Builtins.y2warning("No initialization callback")
  end
  if !@select_initial_item_callback.nil?
    @select_initial_item_callback.call
  else
    _SetSelectedItem(Ops.get_string(@current_items, [0, "id"]))
  end

  nil
end

#mainObject


33
34
35
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
76
77
78
79
# File 'library/cwm/src/modules/WizardHW.rb', line 33

def main
  Yast.import "UI"
  textdomain "base"

  Yast.import "CWM"
  Yast.import "Label"
  Yast.import "Report"
  Yast.import "Popup"
  Yast.import "Wizard"

  # local store

  # List of items in the currently displayed dialog
  @current_items = []

  # Map of rich text descriptions for all items
  # Contained info can be reachable through current_items, this is for
  # faster access
  @descriptions = {}

  # The last handled UI event
  @last_event = {}

  # The return value to be returned by WizardHW::WaitForEvent ()
  @dialog_ret = nil

  # callbacks

  # Callback
  # Perform an action (when an event which is not handled internally occurred)
  @action_callback = nil

  # Callback
  # Get rich text description of an item.
  # This can be used to set it dynamically
  @get_item_descr_callback = nil

  # Callback
  # Set all the items
  # Should call the SetContents function of this module
  @set_items_callback = nil

  # Callback
  # Select the initial item
  # If not set, the first is selected
  @select_initial_item_callback = nil
end

#RunHWDialog(settings) ⇒ Symbol

Note:

This is a stable API function

Draw the dialog, handle all its events via callbacks


283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'library/cwm/src/modules/WizardHW.rb', line 283

def RunHWDialog(settings)
  settings = deep_copy(settings)  # reinitialize internal variables

  @current_items = []
  @descriptions = {}
  @last_event = {}

  # callbacks
  @action_callback = Convert.convert(
    Ops.get(settings, "action_callback"),
    from: "any",
    to:   "symbol (string, map)"
  )
  @get_item_descr_callback = Convert.convert(
    Ops.get(settings, "item_descr_callback"),
    from: "any",
    to:   "string (string)"
  )
  @set_items_callback = Convert.convert(
    Ops.get(settings, "set_items_callback"),
    from: "any",
    to:   "void ()"
  )
  @select_initial_item_callback = Convert.convert(
    Ops.get(settings, "set_initial_item_callback"),
    from: "any",
    to:   "void ()"
  )

  # other variables
  actions = Ops.get_list(settings, "actions", [])
  headers = Ops.get_list(settings, "headers", [])
  title = Ops.get_string(settings, "title", "")
  help = Ops.get_string(settings, "help", "HELP")

  # adapt the widget description map
  widget = CreateWidget(headers, actions)
  widget = Builtins.remove(widget, "handle_events")
  Ops.set(widget, "help", help)
  Ops.set(widget, "init", fun_ref(method(:Init), "void (string)"))
  Ops.set(
    widget,
    "handle",
    fun_ref(method(:Handle), "symbol (string, map)")
  )
  widget_descr = { "wizard_hw" => widget }

  # now run the dialog via CWM with handler set
  CWM.ShowAndRun(
    "widget_descr" => widget_descr,
    "widget_names" => ["wizard_hw"],
    "contents"     => VBox("wizard_hw"),
    "caption"      => title,
    "abort_button" => Ops.get_string(settings, "abort_button") do
      Label.AbortButton
    end,
    "back_button"  => Ops.get_string(settings, "back_button") do
      Label.BackButton
    end,
    "next_button"  => Ops.get_string(settings, "next_button") do
      Label.NextButton
    end
  )
end

#SelectedItemObject

Note:

This is a stable API function

Return the id of the currently selected item in the table


398
399
400
# File 'library/cwm/src/modules/WizardHW.rb', line 398

def SelectedItem
  Convert.to_string(UI.QueryWidget(Id(:_hw_items), :CurrentItem))
end

#SetContents(items) ⇒ Object

Note:

This is a stable API function

Set the information about hardware


417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'library/cwm/src/modules/WizardHW.rb', line 417

def SetContents(items)
  items = deep_copy(items)
  term_items = Builtins.maplist(items) do |i|
    t = Item(Id(Ops.get_string(i, "id", "")))
    Builtins.foreach(Ops.get_list(i, "table_descr", [])) do |l|
      t = Builtins.add(t, l)
    end
    deep_copy(t)
  end
  UI.ChangeWidget(Id(:_hw_items), :Items, term_items)
  StoreCurrentItems(items)
  enabled = Ops.greater_than(Builtins.size(items), 0)
  UI.ChangeWidget(Id(:edit), :Enabled, enabled)
  UI.ChangeWidget(Id(:delete), :Enabled, enabled)
  SetSelectedItem(Ops.get_string(items, [0, "id"], "")) if enabled

  nil
end

#SetRichDescription(descr) ⇒ Object

Note:

This is a stable API function

Set the rich text description.


405
406
407
408
409
# File 'library/cwm/src/modules/WizardHW.rb', line 405

def SetRichDescription(descr)
  UI.ChangeWidget(Id(:_hw_sum), :Value, descr)

  nil
end

#SetSelectedItem(selected) ⇒ Object

Note:

This is a stable API function

Set which item is to be selected


389
390
391
392
393
# File 'library/cwm/src/modules/WizardHW.rb', line 389

def SetSelectedItem(selected)
  _SetSelectedItem(selected)

  nil
end

#SimpleStoreReturnValue(selected, event) ⇒ Object

Store the event description in internal variable To be used by WizardHW::WaitForEvent function


88
89
90
91
92
# File 'library/cwm/src/modules/WizardHW.rb', line 88

def SimpleStoreReturnValue(selected, event)
  event = deep_copy(event)
  @dialog_ret = { "event" => event, "selected" => selected }
  :next # anything but nil
end

#StoreCurrentItems(items) ⇒ Object

internal functions


156
157
158
159
160
161
162
163
164
# File 'library/cwm/src/modules/WizardHW.rb', line 156

def StoreCurrentItems(items)
  items = deep_copy(items)
  @current_items = deep_copy(items)
  @descriptions = Builtins.listmap(items) do |i|
    { Ops.get_string(i, "id", "") => Ops.get_string(i, "rich_descr", "") }
  end

  nil
end

#UnconfiguredDeviceObject

Note:

This is a stable API function

Get propertly list of an unconfigured device. Should be used together with device name in CreateRichTextDescription() function.


491
492
493
494
495
496
497
498
499
500
# File 'library/cwm/src/modules/WizardHW.rb', line 491

def UnconfiguredDevice  # translators: message for hardware configuration without any configured
  # device

  [
    _("The device is not configured"),
    # translators: message for hardware configuration without any configured
    # device
    _("Press <B>Edit</B> to configure")
  ]
end

#UserInputObject

Note:

This is a stable API function

Wait for event from the event


455
456
457
458
459
# File 'library/cwm/src/modules/WizardHW.rb', line 455

def UserInput
  ret = WaitForEvent()
  Ops.set(ret, "event", Ops.get(ret, ["event", "ID"]))
  deep_copy(ret)
end

#WaitForEventObject

Note:

This is a stable API function

Wait for event from the event


441
442
443
444
445
446
447
448
# File 'library/cwm/src/modules/WizardHW.rb', line 441

def WaitForEvent
  event = nil
  while event.nil?
    event = UI.WaitForEvent
    event = nil if Handle("wizard_hw", event).nil?
  end
  deep_copy(@dialog_ret)
end