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

Parameters:

  • selected (String)

    string the item that is should be marked as selected



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

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

Parameters:

  • actions (Array<Array>)

    a list of the actions

Returns:

  • (Yast::Term)

    the widget



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

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)

Parameters:

  • title (String)

    string the dialog title

  • help (String)

    string the help for the dialog

  • headers (Array<String>)

    a list of the table headers

  • actions (Array<Array>)

    a list of actions to be offered via additional button next to Add/Edit/Delete button. Each item is a two-item-list, where the first item is the event ID and the second item is the label of the entry of the menu button. If there is only one entry, menu button is replaced by push button. If empty (or not specifued), nothing is shown.



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'library/cwm/src/modules/WizardHW.rb', line 368

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

Parameters:

  • title (String)

    header - usually device name

  • properties (Array<String>)

    important properties of the device which should be displayed in the overview dialog

Returns:

  • (String)

    rich text string



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'library/cwm/src/modules/WizardHW.rb', line 473

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 = ""

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

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

  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

Parameters:

  • headers (Array<String>)

    a list of headers of the table

  • actions (Array<Array>)

    a list of additionaly offered actions, see CreateHWDialog function for details

Returns:

  • a map a widget for CWM



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

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

Parameters:

  • action (Array)

    a list describing the action

Returns:

  • (String)

    the label of the action



171
172
173
174
175
176
177
178
# File 'library/cwm/src/modules/WizardHW.rb', line 171

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

#Handle(_key, event) ⇒ Symbol

Handle function of the widget Used when using the callback interface

Parameters:

  • key (String)

    strnig the widget key

  • key (String)

    strnig the widget key

  • event (Hash)

    map event to be handled

Returns:

  • (Symbol)

    for wizard sequencer or nil



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

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

Parameters:

  • key (String)

    strnig the widget key



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

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



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

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

Parameters:

  • settings (Hash)

    a map containing all the settings: $[

    "action_callback" : symbol(string,map<string,any>) -- callback to handle
       all events which aren't handled internally, first parameter is
       the ID of the selected item, second is the event. If not set,
       events which are symbols are returned to wizard sequencer

    “set_items_callback” : void() – callback to set the items to be displayed.

    Should called WizardHW::SetContents instead of direct widgets
    modification, as it stores the settings also internally. This callback
    must be set.

    “set_initial_item_callback” : void() – callback to set the selected item

    when dialog initialized. Should call the function
    WizardHW::SetSelectedItem instead of manual widget modification.
    If not set, the first item is selected.

    “item_descr_callback” : string(string) – callback to get rich text

    description of the item if it is intended to be dynamical.
    if not set, static description set via "set_items_callback" is
    used.

    “actions” : list<list> – a list of actions to be offered

    via additional button next to Add/Edit/Delete button. Each item is
    a two-item-list, where the first item is the event ID and the second
    item is the label of the entry of the menu button. If there is only
    one entry, menu button is replaced by push button. If empty
    (or not specifued), nothing is shown.

    “title” : string – the dialog title, must be specified “help” : string – the help for the dialog, must be specifed “headers” : list<string> – a list of the table headers, must be specified “next_button” : string – label for the “Next” button. To hide it, set to

    nil. If not specified, "Next" is used.

    “back_button” : string – label for the “Back” button. To hide it, set to

    nil. If not specified, "Back" is used.

    “abort_button” : string – label for the “Abort” button. To hide it, set to

    nil. If not specified, "Abort" is used.

    ]

Returns:

  • (Symbol)

    for wizard sequencer



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
347
348
349
350
351
# File 'library/cwm/src/modules/WizardHW.rb', line 286

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

Returns:

  • id of the selected item



403
404
405
# File 'library/cwm/src/modules/WizardHW.rb', line 403

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

Parameters:

  • items (Array<Hash{String => Object>})

    a list of maps, one item per item in the dialog, with keys “id” : string = the identification of the device, “rich_descr” : string = RichText description of the device “table_descr” : list<string> = fields of the table



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'library/cwm/src/modules/WizardHW.rb', line 422

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.

Parameters:

  • descr (String)

    rich text description



410
411
412
413
414
# File 'library/cwm/src/modules/WizardHW.rb', line 410

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

Parameters:

  • selected (String)

    string the item that is should be marked as selected



394
395
396
397
398
# File 'library/cwm/src/modules/WizardHW.rb', line 394

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

Parameters:

  • selected (String)

    string the ID of the currently selected item

  • event (Hash)

    a map of the current item

Returns:

  • always a non-nil symbol (needed just to finish event loop



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

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

#StoreCurrentItems(items) ⇒ Object

internal functions



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

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.

Returns:

  • a list of strings



500
501
502
503
504
505
506
507
508
509
# File 'library/cwm/src/modules/WizardHW.rb', line 500

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

Returns:

  • a map with keys: “event” : any = event as returned from UI::UserInoput () “selected” : string = ID of the selected item in the list box



460
461
462
463
464
# File 'library/cwm/src/modules/WizardHW.rb', line 460

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

Returns:

  • a map with keys: “event” : map = event as returned from UI::WaitForEvent (), “selected” : string = ID of the selected item in the list box



446
447
448
449
450
451
452
453
# File 'library/cwm/src/modules/WizardHW.rb', line 446

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