• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yast / yast-yast2 / 6416808488

05 Oct 2023 08:51AM UTC coverage: 41.872% (+0.008%) from 41.864%
6416808488

Pull #1292

github

mvidner
version + changelog
Pull Request #1292: Fix case-insensitive finding of LICENSE.*.TXT (bsc#1215698)

7 of 7 new or added lines in 1 file covered. (100.0%)

12412 of 29643 relevant lines covered (41.87%)

10.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/library/cwm/src/modules/DialogTree.rb
1
# ***************************************************************************
2
#
3
# Copyright (c) 2002 - 2012 Novell, Inc.
4
# All Rights Reserved.
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of version 2 of the GNU General Public License as
8
# published by the Free Software Foundation.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, contact Novell, Inc.
17
#
18
# To contact Novell about this file by physical or electronic mail,
19
# you may find current contact information at www.novell.com
20
#
21
# ***************************************************************************
22
# File:  modules/DialogTree.ycp
23
# Package:  Common widget manipulation
24
# Summary:  Routines for handling the dialog with tree on the left side
25
# Authors:  Jiri Srain <jsrain@suse.cz>
26
#
27
# $Id$
28
#
29
require "yast"
×
30

31
module Yast
×
32
  class DialogTreeClass < Module
×
33
    def main
×
34
      Yast.import "UI"
×
35
      textdomain "base"
×
36

37
      Yast.import "CWM"
×
38
      Yast.import "Label"
×
39
      Yast.import "Wizard"
×
40

41
      # local data
42

43
      # Currently selected item in the tree
44
      @selected_screen = nil
×
45

46
      # Previously selected item in the tree
47
      @previous_screen = nil
×
48
    end
×
49

50
    # Restore the previously selected dialog after clicking another item not
51
    # causing dialog change due to validation failed
52
    def RestoreSelectedDialog
×
53
      @selected_screen = @previous_screen
×
54
      if UI.WidgetExists(Id(:wizardTree))
×
55
        UI.ChangeWidget(Id(:wizardTree), :CurrentItem, @selected_screen)
×
56
      else
×
57
        UI.WizardCommand(term(:SelectTreeItem, @selected_screen))
×
58
      end
×
59

60
      nil
×
61
    end
×
62

63
    # virtual DialogTree widget
64

65
    # Init function of virtual DialogTree widget
66
    # @param [String] key string widget key
67
    def DialogTreeInit(_key)
×
68
      if UI.WidgetExists(Id(:wizardTree))
×
69
        UI.ChangeWidget(Id(:wizardTree), :CurrentItem, @selected_screen)
×
70
        UI.SetFocus(Id(:wizardTree))
×
71
      else
×
72
        UI.WizardCommand(term(:SelectTreeItem, @selected_screen))
×
73
      end
×
74

75
      nil
×
76
    end
×
77

78
    # Handle function of virtual DialogTree widget
79
    # @param [String] key string widget key
80
    # @param [Hash] event map event that caused handler call
81
    # @return [Symbol] for wizard sequencer or nil
82
    def DialogTreeHandle(_key, event)
×
83
      event = deep_copy(event)
×
84
      ret = Ops.get(event, "ID")
×
85

86
      ret = Convert.to_string(UI.QueryWidget(Id(:wizardTree), :CurrentItem)) if ret == :wizardTree
×
87
      @previous_screen = @selected_screen
×
88
      @selected_screen = Convert.to_string(ret)
×
89
      :_cwm_internal_tree_handle
×
90
    end
×
91

92
    # Get the map of the virtal left tree widget
93
    # @param [Array<String>] ids a list of widget ids of all tree items
94
    # @return [Hash] tree of the widget
95
    def GetVirtualDialogTreeWidget(ids)
×
96
      ids = deep_copy(ids)
×
97
      handle_events = deep_copy(ids)
×
98
      handle_events = Builtins.add(handle_events, :wizardTree)
×
99
      {
×
100
        "init"          => fun_ref(method(:DialogTreeInit), "void (string)"),
×
101
        "handle_events" => handle_events,
×
102
        "handle"        => fun_ref(
×
103
          method(:DialogTreeHandle),
×
104
          "symbol (string, map)"
×
105
        )
×
106
      }
×
107
    end
×
108

109
    # internal functions
110

111
    # Draw the screen related to the particular tree item
112
    # @param [Hash{String => Object}] current_screen a map describing the current screen
113
    # @param [Hash <String, Hash{String => Object>}] widget_descr a map describing all widgets that may be present in the
114
    #  screen
115
    # extra_widget a map of the additional widget to be added at the end
116
    #  of the list of widgets
117
    # @return a list of preprocessed widgets that appear in this dialog
118
    def DrawScreen(current_screen, widget_descr, extra_widget, set_focus)
×
119
      current_screen = deep_copy(current_screen)
×
120
      widget_descr = deep_copy(widget_descr)
×
121
      extra_widget = deep_copy(extra_widget)
×
122
      widget_names = Ops.get_list(current_screen, "widget_names", [])
×
123
      contents = Ops.get_term(current_screen, "contents", VBox())
×
124
      caption = Ops.get_string(current_screen, "caption", "")
×
125

126
      w = CWM.CreateWidgets(widget_names, widget_descr)
×
127
      help = CWM.MergeHelps(w)
×
128
      contents = CWM.PrepareDialog(contents, w)
×
129
      Wizard.SetContentsFocus(caption, contents, help, true, true, set_focus)
×
130

131
      # add virtual widget
132
      w = Builtins.add(w, extra_widget)
×
133

134
      # return widgets of the dialog for further usage
135
      deep_copy(w)
×
136
    end
×
137

138
    # Draw the dialog with the flat tree (only single level of the tree entries)
139
    # @param [Array<String>] ids_order a list of IDs in the same order as they are expected to be
140
    #  in the left menu
141
    # @param [Hash <String, Hash{String => Object>}] screens map of all screens (key is screen ID, value is screen
142
    #  description map
143
    def ShowFlat(ids_order, screens)
×
144
      ids_order = deep_copy(ids_order)
×
145
      screens = deep_copy(screens)
×
146
      Wizard.OpenTreeNextBackDialog
×
147
      tree = []
×
148
      Builtins.foreach(ids_order) do |i|
×
149
        tree = Wizard.AddTreeItem(
×
150
          tree,
×
151
          "",
×
152
          Ops.get_string(
×
153
            screens,
×
154
            [i, "tree_item_label"],
×
155
            Ops.get_string(screens, [i, "caption"], "")
×
156
          ),
×
157
          i
×
158
        )
×
159
      end
×
160
      Wizard.CreateTree(tree, "")
×
161

162
      nil
×
163
    end
×
164

165
    # Draw the dialog with multi-level tree
166
    # @param [list <map> ()] tree_handler a callback to a function that creates the tree using
167
    #  Wizard::AddTreeItem and returns the resulting tree
168
    def ShowTree(tree_handler)
×
169
      tree_handler = deep_copy(tree_handler)
×
170
      Wizard.OpenTreeNextBackDialog
×
171
      tree = tree_handler.call
×
172
      Wizard.CreateTree(tree, "")
×
173

174
      nil
×
175
    end
×
176

177
    # Adjust buttons at the bottom of the dialog
178
    # @param [Hash{String => String}] buttons a map with keys "abort_button", "back_button" and
179
    #  "next_button" adn values labels of appropriate buttons
180
    def AdjustButtons(buttons)
×
181
      buttons = deep_copy(buttons)
×
182
      CWM.AdjustButtons(
×
183
        Ops.get(buttons, "next_button") { Label.NextButton },
×
184
        Ops.get(buttons, "back_button") { Label.BackButton },
×
185
        Ops.get(buttons, "abort_button") { Label.AbortButton },
×
186
        Label.HelpButton
×
187
      )
×
188

189
      nil
×
190
    end
×
191

192
    # Adjust buttons at the bottom of the dialog
193
    # @param [Hash{String => Object}] buttons a map with keys "abort_button", "back_button" and
194
    #  "next_button" adn values labels of appropriate buttons, other keys
195
    #  with values of other types are possible
196
    def AdjustButtonsAny(buttons)
×
197
      buttons = deep_copy(buttons)
×
198
      buttons2 = Convert.convert(
×
199
        Builtins.filter(buttons) { |k, _v| Builtins.issubstring(k, "_button") },
×
200
        from: "map <string, any>",
×
201
        to:   "map <string, string>"
×
202
      )
×
203
      AdjustButtons(buttons2)
×
204

205
      nil
×
206
    end
×
207

208
    # Generic function to create dialog and handle it's events.
209
    # Run the event loop over the dialog with the left tree.
210
    # @param setttings a map of settings of the dialog
211
    # <pre>
212
    # "screens" : map<string,map<string,any>> of all screens
213
    #             (key is screen ID, value is screen description map)
214
    # "widget_descr" : map<string,map<string,any>> description map of all widgets
215
    # "initial_screen" : string the id of the screen that should be displayed
216
    #                    as the first
217
    # "fallback" : map<any,any> initialize/save/handle fallbacks if not specified
218
    #              with the widgets, to be passed to CWM
219
    # </pre>
220
    # @return [Symbol] wizard sequencer symbol
221
    def Run(settings)
×
222
      settings = deep_copy(settings)
×
223
      screens = Ops.get_map(settings, "screens", {})
×
224
      widget_descr = Ops.get_map(settings, "widget_descr", {})
×
225
      initial_screen = Ops.get_string(settings, "initial_screen", "")
×
226
      functions = Ops.get_map(settings, "functions", {})
×
227

228
      initial_screen = "" if initial_screen.nil?
×
229
      if initial_screen == ""
×
230
        Builtins.foreach(screens) do |k, _v|
×
231
          initial_screen = k if initial_screen == ""
×
232
        end
×
233
      end
×
234

235
      @selected_screen = initial_screen
×
236
      ids = Builtins.maplist(screens) { |k, _v| k }
×
237
      extra_widget = GetVirtualDialogTreeWidget(ids)
×
238

239
      w = DrawScreen(
×
240
        Ops.get(screens, @selected_screen, {}),
×
241
        widget_descr,
×
242
        extra_widget,
×
243
        true
×
244
      )
×
245

246
      ret = nil
×
247
      while ret.nil?
×
248
        CWM.SetValidationFailedHandler(
×
249
          fun_ref(method(:RestoreSelectedDialog), "void ()")
×
250
        )
×
251
        ret = CWM.Run(w, functions)
×
252
        CWM.SetValidationFailedHandler(nil)
×
253
        # switching scrren, dialog was validated and stored
254
        next if ret != :_cwm_internal_tree_handle
×
255

256
        toEval = Convert.convert(
×
257
          Ops.get(screens, [@selected_screen, "init"]),
×
258
          from: "any",
×
259
          to:   "symbol (string)"
×
260
        )
×
261
        tab_init = nil
×
262
        tab_init = toEval.call(@selected_screen) if !toEval.nil?
×
263
        if tab_init.nil? # everything OK
×
264
          w = DrawScreen(
×
265
            Ops.get(screens, @selected_screen, {}),
×
266
            widget_descr,
×
267
            extra_widget,
×
268
            false
×
269
          )
×
270
          ret = nil
×
271
        elsif tab_init == :refuse_display # do not display this screen
×
272
          @selected_screen = @previous_screen
×
273
          ret = nil # exit dialog
×
274
        else
×
275
          ret = tab_init
×
276
        end
×
277
      end
×
278
      ret
×
279
    end
×
280

281
    # Run the event loop over the dialog with the left tree. After finished, run
282
    #  UI::CloseDialog
283
    # @param setttings a map of settings of the dialog. See @Run for possible keys
284
    # @return [Symbol] wizard sequencer symbol
285
    def RunAndHide(settings)
×
286
      settings = deep_copy(settings)
×
287
      Run(settings)
×
288
    ensure
×
289
      UI.CloseDialog
×
290
    end
×
291

292
    # Display the dialog and run its event loop
293
    # @param setttings a map of settings of the dialog
294
    # <pre>
295
    # "ids_order" : list<string> of IDs in the same order as they are expected
296
    #               to be in the left menu. Not used if "tree_creator" is defined
297
    # "tree_creator" : list<map>() a callback to a function that creates
298
    #                  the tree using Wizard::AddTreeItem and returns the
299
    #                  resulting tree
300
    # "back_button" : string label of the back button (optional)
301
    # "next_button" : string label of the next button (optional)
302
    # "abort_button" : string label of the abort button (optional)
303
    # See @RunAndHide for other possible keys in the map
304
    # </pre>
305
    # @return [Symbol] wizard sequencer symbol
306
    def ShowAndRun(settings)
×
307
      settings = deep_copy(settings)
×
308
      ids_order = Ops.get_list(settings, "ids_order", [])
×
309
      screens = Ops.get_map(settings, "screens", {})
×
310
      tree_handler = Convert.convert(
×
311
        Ops.get(settings, "tree_creator"),
×
312
        from: "any",
×
313
        to:   "list <map> ()"
×
314
      )
×
315

316
      if tree_handler.nil?
×
317
        ShowFlat(ids_order, screens)
×
318
        if Ops.get_string(settings, "initial_screen", "") == ""
×
319
          Builtins.find(ids_order) do |s|
×
320
            Ops.set(settings, "initial_screen", s)
×
321
            true
×
322
          end
×
323
        end
×
324
      else
×
325
        ShowTree(tree_handler)
×
326
      end
×
327
      AdjustButtonsAny(settings)
×
328
      RunAndHide(settings)
×
329
    end
×
330

331
    publish function: :ShowFlat, type: "void (list <string>, map <string, map <string, any>>)"
×
332
    publish function: :ShowTree, type: "void (list <map> ())"
×
333
    publish function: :AdjustButtons, type: "void (map <string, string>)"
×
334
    publish function: :AdjustButtonsAny, type: "void (map <string, any>)"
×
335
    publish function: :Run, type: "symbol (map <string, any>)"
×
336
    publish function: :RunAndHide, type: "symbol (map <string, any>)"
×
337
    publish function: :ShowAndRun, type: "symbol (map <string, any>)"
×
338
  end
×
339

340
  DialogTree = DialogTreeClass.new
×
341
  DialogTree.main
×
342
end
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc