Class List Method List File List

# Module: Yast::MailWjInclude

Defined in:
  ../../src/include/mail/wj.rb

## Instance Method Summary (collapse)

- - (Object) EditTable(action, data, keys, editEntryP, table_widget)

  A generic handler for editing tables.

- - (Object) evaluate_fp(fp)

  Evaluate a function pointer, working around interpreter limitations.

- - (Object) initialize_mail_wj(include_target)
- - (Object) makeItems(data, keys)

  An item list.

- - (Object) RadioButtonVBox(g_id, rbs)

  Make a RadioButtonGroup containing Left aligned widgets in a VBox that has VSpacing (0.2) as top/bottom padding.

- - (Object) WJ_GetWidget(id)

  Changes widget value using “get” (useful for widgets with choices).

- - (Object) WJ_MakeHelp(items)

  Make a help string by concatenating individual widget helps and other strings.

- - (Object) WJ_MakeWidget(id)

  Constructs a widget term.

- - (Object) WJ_Set(widgets)

  Call the “set” functions for a list of widgets to commit the UI values to a module.

- - (Object) WJ_Set_Missing(id)

  Set a variable acording to widget value This is an error reporting fallback if the real function was not defined.

- - (Object) WJ_Validate(widgets)

  Call the validation functions for a list of widgets.

- - (Object) WJ_Validate_True(id)

  A helper for WJ_Validate.

## Instance Method Details

### - (`Object`) EditTable(action, data, keys, editEntryP, table_widget)

A generic handler for editing tables. The current item of table_widget makeItems (new_data, keys) is used to fill table_widget

Parameters:

- action (`Symbol`) —

  `add,`edit or `delete

- data (`Array<Hash>`) —

  the data edited using the table

- keys (`Array<String>`) —

  keys of respective columns

- editEntry —

  a function to edit an entry: gets the current entry and list of othe entries as parameters

- table_widget (`Object`) —

  id of the table (usually a symbol)

Returns:

- the edited data

    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    # File '../../src/include/mail/wj.rb', line 66

    def EditTable(action, data, keys, editEntryP, table_widget)
      data = deep_copy(data)
      keys = deep_copy(keys)
      editEntryP = deep_copy(editEntryP)
      table_widget = deep_copy(table_widget)
      new_data = nil
      entryno = Convert.to_integer(
        UI.QueryWidget(Id(table_widget), :CurrentItem)
      )
      touched = false

      editEntry = deep_copy(editEntryP) #FIXME

      if action == :add
        entry = editEntry.call({}, data)
        if Ops.greater_than(Builtins.size(entry), 0)
          new_data = Builtins.add(data, entry)
          touched = true
        else
          new_data = deep_copy(data)
        end
      elsif action == :edit
        # edit known fields, preserve unknown fields
        old_entry = Ops.get(data, entryno, {})
        entry = editEntry.call(old_entry, Builtins.remove(data, entryno))
        if Ops.greater_than(Builtins.size(entry), 0)
          i = 0
          new_data = Builtins.maplist(data) do |e|
            i = Ops.add(i, 1)
            Ops.subtract(i, 1) == entryno ? Builtins.union(old_entry, entry) : e
          end
          touched = true
        else
          new_data = deep_copy(data)
        end
      elsif action == :delete
        new_data = Builtins.remove(data, entryno)
        touched = true
      else
        Builtins.y2error("Unknown EditTable action %1.", action)
        new_data = deep_copy(data) # be nice
      end

      if touched
        UI.ChangeWidget(Id(table_widget), :Items, makeItems(new_data, keys))
        @edit_touched = true
      end
      deep_copy(new_data)
    end

### - (`Object`) evaluate_fp(fp)

Evaluate a function pointer, working around interpreter limitations

Parameters:

- fp (`Object`) —

  pointer to a functin without parameters returning boolean string symbol or list

Returns:

- its return value as any

    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    # File '../../src/include/mail/wj.rb', line 122

    def evaluate_fp(fp)
      fp = deep_copy(fp)
      if Ops.is(fp, "boolean ()")
        boolean_fp = Convert.convert(fp, :from => "any", :to => "boolean ()")
        return boolean_fp.call
      end
      if Ops.is(fp, "list ()")
        list_fp = Convert.convert(fp, :from => "any", :to => "list ()")
        return list_fp.call
      end
      if Ops.is(fp, "string ()")
        string_fp = Convert.convert(fp, :from => "any", :to => "string ()")
        return string_fp.call
      end
      if Ops.is(fp, "symbol ()")
        symbol_fp = Convert.convert(fp, :from => "any", :to => "symbol ()")
        return symbol_fp.call
      end
      # report as the caller
      Builtins.y2error(1, "evaluate_fp: Unexpected function pointer: %1", fp)
      nil
    end

### - (`Object`) initialize_mail_wj(include_target)

    23
    24
    25
    26
    27
    28
    29
    30
    31
    # File '../../src/include/mail/wj.rb', line 23

    def initialize_mail_wj(include_target)
      Yast.import "UI"

      textdomain "mail" # shoudn't be necessary

      Yast.include include_target, "mail/widgets.rb"

      @edit_touched = false
    end

### - (`Object`) makeItems(data, keys)

Returns an item list

Parameters:

- data (`Array<Hash>`) —

  a list of structs

- keys (`Array<String>`) —

  which members to put in the table

Returns:

- an item list

    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    # File '../../src/include/mail/wj.rb', line 38

    def makeItems(data, keys)
      data = deep_copy(data)
      keys = deep_copy(keys)
      i = 0
      Builtins.maplist(data) do |d|
        t = Item(Id(i))
        i = Ops.add(i, 1)
        Builtins.foreach(keys) do |k|
          t = Builtins.add(t, Ops.get_string(d, k, ""))
        end
        deep_copy(t)
      end
    end

### - (`Object`) RadioButtonVBox(g_id, rbs)

Make a RadioButtonGroup containing Left aligned widgets in a VBox that has VSpacing (0.2) as top/bottom padding.

Parameters:

- g_id (`Symbol`) —

  group id

- rbs (`Array<Yast::Term>`) —

  a list of widgets, usually RadioButtons

Returns:

- widget

    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    # File '../../src/include/mail/wj.rb', line 308

    def RadioButtonVBox(g_id, rbs)
      rbs = deep_copy(rbs)
      spacing = VSpacing(0.2)
      rb_vbox = VBox(spacing)
      Builtins.foreach(rbs) do |rb|
        left_rb = Left(rb)
        rb_vbox = Builtins.add(rb_vbox, left_rb)
      end
      rb_vbox = Builtins.add(rb_vbox, spacing)

      RadioButtonGroup(Id(g_id), rb_vbox)
    end

### - (`Object`) WJ_GetWidget(id)

Changes widget value using “get” (useful for widgets with choices). Uses a global Widgets map. #widget_def

Parameters:

- id (`Symbol`) —

  which widget

    189
    190
    191
    192
    193
    194
    195
    # File '../../src/include/mail/wj.rb', line 189

    def WJ_GetWidget(id)
      # "get" returns string or boolean:
      e = evaluate_fp(Ops.get(@Widgets, [id, "get"]))
      UI.ChangeWidget(Id(id), :Value, e)

      nil
    end

### - (`Object`) WJ_MakeHelp(items)

Make a help string by concatenating individual widget helps and other strings. Uses a global Widgets map. #widget_def

Parameters:

- items (`Array`) —

  an item is
  either a string - use it
  or a symbol - lookup its help string in Widgets

Returns:

- concatenated strings

    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    # File '../../src/include/mail/wj.rb', line 204

    def WJ_MakeHelp(items)
      items = deep_copy(items)
      ret = ""
      Builtins.foreach(items) do |item|
        if !Ops.is_string?(item)
          item = Ops.get_string(@Widgets, [item, "help"], "")
        end
        ret = Ops.add(ret, Convert.to_string(item))
      end
      ret
    end

### - (`Object`) WJ_MakeWidget(id)

Constructs a widget term. Uses a global Widgets map. Keys are symbols (ids), values are

Structure:

    widget_def
        %xfoo: $[
            "widget": `TextEntry (),
            "opt": %xopt (`notify), // optional
            "label": "&Foo",
            // if there are choices, they are used to construct the widget,
            // otherwise get is used
            "choices": Choices_foo          // list ()
            "help": _("&lt;p&gt;Foo!&lt;/p&gt;"), // used by WJ_MakeHelp, optional
            "get": Get_foo,                        // gets value from module to widget
            "set": Set_foo,                        // sets value from widget to module
            // popups an error and returns false if invalid
            "validate": %x`(Validate_foo ()),// optional
            ],

Parameters:

- id (`Symbol`) —

  which widget

Returns:

- a widget term

    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    # File '../../src/include/mail/wj.rb', line 167

    def WJ_MakeWidget(id)
      w_def = Ops.get_map(@Widgets, id, {})
      widget = Ops.get_term(w_def, "widget", CheckBox())
      return nil if widget == nil
      widget = Builtins.add(widget, Id(id))
      if Builtins.haskey(w_def, "opt")
        widget = Builtins.add(widget, Ops.get_term(w_def, "opt", Empty()))
      end
      widget = Builtins.add(widget, Ops.get_string(w_def, "label", "LABEL"))
      # if choices are defined (combo boxes ...), use them
      # otherwise set the value directly
      # TODO: verified only for TextEntry. Password, CheckBox, ComboBox

      # "choices" returns list, "get" returns string, symbol or boolean
      init = evaluate_fp(Ops.get(w_def, "choices", Ops.get(w_def, "get")))
      return Builtins.add(widget, init) if init != nil
      deep_copy(widget)
    end

### - (`Object`) WJ_Set(widgets)

Call the “set” functions for a list of widgets to commit the UI values to a module. Uses a global Widgets map. #widget_def

Parameters:

- widgets (`Array`) —

  which widgets to commit

    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    # File '../../src/include/mail/wj.rb', line 276

    def WJ_Set(widgets)
      widgets = deep_copy(widgets)
      Builtins.foreach(widgets) do |id|
        if Ops.is_symbol?(id)
          id_sym = Convert.to_symbol(id)

          set_it = Convert.convert(
            Ops.get(
              @Widgets,
              [id_sym, "set"],
              fun_ref(method(:WJ_Set_Missing), "void (symbol)")
            ),
            :from => "any",
            :to   => "void (symbol)"
          )
          set_it.call(id_sym)
        end
      end

      nil
    end

### - (`Object`) WJ_Set_Missing(id)

Set a variable acording to widget value This is an error reporting fallback if the real function was not defined

Parameters:

- id (`Symbol`) —

  widget id

    265
    266
    267
    268
    269
    270
    # File '../../src/include/mail/wj.rb', line 265

    def WJ_Set_Missing(id)
      # report as the caller
      Builtins.y2error(1, "WJ: Missing Set definition for %1", id)

      nil
    end

### - (`Object`) WJ_Validate(widgets)

Call the validation functions for a list of widgets. Widgets with an unspecified function are assumed to be valid. Uses a global Widgets map. #widget_def

Parameters:

- widgets (`Array`) —

  which widgets to validate

Returns:

- true if all widgets are ok

    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
    257
    258
    259
    260
    # File '../../src/include/mail/wj.rb', line 228

    def WJ_Validate(widgets)
      widgets = deep_copy(widgets)
      ok = true
      Builtins.foreach(widgets) do |id|
        if ok
          if Ops.is_symbol?(id)
            id_sym = Convert.to_symbol(id)
            w_def = Ops.get_map(@Widgets, id)
            if w_def == nil
              # report as the caller
              Builtins.y2error(1, "WJ: Missing widget definition for %1", id)
              ok = false
            else
              validate = Convert.convert(
                Ops.get(
                  w_def,
                  "validate",
                  fun_ref(method(:WJ_Validate_True), "boolean (symbol)")
                ),
                :from => "any",
                :to   => "boolean (symbol)"
              )
              ok = validate.call(id_sym)
            end
          else
            ok = false
          end
        else
          next #break
        end
      end
      ok
    end

### - (`Object`) WJ_Validate_True(id)

A helper for WJ_Validate.

Parameters:

- id (`Symbol`) —

  a widget id

Returns:

- always true

    219
    220
    221
    # File '../../src/include/mail/wj.rb', line 219

    def WJ_Validate_True(id)
      true
    end