Class: Yast::WizardHWClass

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) _SetSelectedItem(selected)

Set which item is to be selected

Parameters:

  • selected (String)

    string the item that is should be marked as selected



102
103
104
105
106
107
108
109
110
111
112
113
# File '../../src/modules/WizardHW.rb', line 102

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

- (Yast::Term) CreateActionsButton(actions)

Create the Push/Menu button for additional actions

Parameters:

  • actions (Array<Array>)

    a list of the actions

Returns:

  • (Yast::Term)

    the widget



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File '../../src/modules/WizardHW.rb', line 193

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

- (Object) CreateHWDialog(title, help, headers, actions)

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.



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File '../../src/modules/WizardHW.rb', line 387

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

- (String) CreateRichTextDescription(title, properties)

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



493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File '../../src/modules/WizardHW.rb', line 493

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

- (Object) CreateWidget(headers, actions)

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



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
257
258
# File '../../src/modules/WizardHW.rb', line 229

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

- (String) GetActionLabel(action)

Get the description label for the action

Parameters:

  • action (Array)

    a list describing the action

Returns:

  • (String)

    the label of the action



181
182
183
184
185
186
187
188
# File '../../src/modules/WizardHW.rb', line 181

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

- (Symbol) Handle(key, event)

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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File '../../src/modules/WizardHW.rb', line 139

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
      descr = Ops.get(@descriptions, current, "")
    else
      descr = @get_item_descr_callback.call(current)
    end
    UI.ChangeWidget(Id(:_hw_sum), :Value, descr)
    return nil
  end
  if @action_callback == nil
    ret = Ops.get(event, "ID")
    if Ops.is_symbol?(ret)
      return Convert.to_symbol(ret)
    else
      return nil
    end
  else
    return @action_callback.call(current, event)
  end
end

- (Object) Init(key)

Init function of the widget Used when using the callback interface

Parameters:

  • key (String)

    strnig the widget key



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File '../../src/modules/WizardHW.rb', line 118

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

- (Object) main



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
82
83
84
85
# File '../../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

- (Symbol) RunHWDialog(settings)

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



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
352
353
354
355
356
357
358
359
360
361
362
363
364
# File '../../src/modules/WizardHW.rb', line 299

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

- (Object) SelectedItem

Note:

This is a stable API function

Return the id of the currently selected item in the table

Returns:

  • id of the selected item



423
424
425
# File '../../src/modules/WizardHW.rb', line 423

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

- (Object) SetContents(items)

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



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File '../../src/modules/WizardHW.rb', line 442

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

- (Object) SetRichDescription(descr)

Note:

This is a stable API function

Set the rich text description.

Parameters:

  • descr (String)

    rich text description



430
431
432
433
434
# File '../../src/modules/WizardHW.rb', line 430

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

  nil
end

- (Object) SetSelectedItem(selected)

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



413
414
415
416
417
# File '../../src/modules/WizardHW.rb', line 413

def SetSelectedItem(selected)
  _SetSelectedItem(selected)

  nil
end

- (Object) SimpleStoreReturnValue(selected, event)

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



94
95
96
97
98
# File '../../src/modules/WizardHW.rb', line 94

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

- (Object) StoreCurrentItems(items)

internal functions



168
169
170
171
172
173
174
175
176
# File '../../src/modules/WizardHW.rb', line 168

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

- (Object) UnconfiguredDevice

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



521
522
523
524
525
526
527
528
529
530
# File '../../src/modules/WizardHW.rb', line 521

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

- (Object) UserInput

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



480
481
482
483
484
# File '../../src/modules/WizardHW.rb', line 480

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

- (Object) WaitForEvent

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



466
467
468
469
470
471
472
473
# File '../../src/modules/WizardHW.rb', line 466

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