Class: Yast::ViewAnymsgClient

Inherits:
Client
  • Object
show all
Defined in:
library/system/src/clients/view_anymsg.rb

Constant Summary

DEFAULT_FILENAMES =
String

Default list of log files

[
  "/var/log/boot.log",
  "/var/log/messages",
  "/var/log/YaST2/y2log"
].freeze

Instance Method Summary collapse

Instance Method Details

#mainObject



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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'library/system/src/clients/view_anymsg.rb', line 58

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

  Yast.import "CommandLine"
  Yast.import "Directory"
  Yast.import "FileUtils"
  Yast.import "Label"

  @vardir = Directory.vardir

  # Check if the filename list is present
  if !FileUtils.Exists(Ops.add(@vardir, "/filenames"))
    SCR.Execute(
      path(".target.bash"),
      Ops.add(
        Ops.add(
          Ops.add(Ops.add("/bin/cp ", Directory.ydatadir), "/filenames "),
          @vardir
        ),
        "/filenames"
      )
    )
  end

  # get filename list
  @filenames = Convert.to_string(
    SCR.Read(path(".target.string"), Ops.add(@vardir, "/filenames"))
  )

  @filenames ||= ""

  # convert \n separated string to ycp list.

  @all_files = Builtins.splitstring(@filenames, "\n")
  @all_files |= DEFAULT_FILENAMES

  @set_default = false
  @combo_files = []

  # check if default given as argument

  @filename = ""
  if Ops.greater_than(Builtins.size(WFM.Args), 0) &&
      Ops.is_string?(WFM.Args(0))
    @filename = Convert.to_string(WFM.Args(0))
    if @filename != ""
      @combo_files = [Item(Id(@filename), @filename, true)]
      @set_default = true
    end
  end

  # the command line description map
  @cmdline = { "id" => "view_anymsg" }
  return CommandLine.Run(@cmdline) if @filename == "help"

  # build up ComboBox

  Builtins.foreach(@all_files) do |name|
    # empty lines or lines starting with "#" are ignored
    if name != "" && Builtins.substring(name, 0, 1) != "#"
      # the default is either given via WFM::Args() -> filename != ""
      # or by a filename starting with "*"
      if Builtins.substring(name, 0, 1) == "*"
        name = Builtins.substring(name, 1) # strip leading "*"
        if name != @filename # do not add it twice
          @combo_files = Builtins.add(
            @combo_files,
            Item(Id(name), name, !@set_default)
          )
        end
        if !@set_default
          @filename = name if @filename == ""
          @set_default = true
        end
      elsif name != @filename # do not add it twice
        @combo_files = Builtins.add(@combo_files, Item(Id(name), name))
      end
    end
  end

  if !@set_default && @filename != ""
    @all_files = Builtins.add(@all_files, Ops.add("*", @filename))
    @combo_files = Builtins.add(
      @combo_files,
      Item(Id(@filename), @filename)
    )
  end

  # set up dialogue

  UI.OpenDialog(
    Opt(:decorated, :defaultsize),
    VBox(
      HSpacing(70), # force width
      HBox(
        HSpacing(1.0),
        ComboBox(
          Id(:custom_file),
          Opt(:editable, :notify, :hstretch),
          "",
          @combo_files
        ),
        HStretch()
      ),
      VSpacing(0.3),
      VWeight(
        1,
        HBox(
          VSpacing(18), # force height
          HSpacing(0.7),
          LogView(
            Id(:log),
            "",
            3, # height
            0
          ), # number of lines to show
          HSpacing(0.7)
        )
      ),
      VSpacing(0.3),
      PushButton(Id(:ok), Label.OKButton),
      VSpacing(0.3)
    )
  )

  @go_on = true

  # wait until user clicks "OK"
  # check if ComboBox selected and change view accordingly

  while @go_on

    # read file content
    file_content = SCR.Read(path(".target.string"), @filename)

    if file_content
      # remove ANSI color escape sequences
      file_content.remove_ansi_sequences
      # remove remaining ASCII control characters (ASCII 0-31 and 127 (DEL))
      # except new line (LF = 0xa) and carriage return (CR = 0xd)
      file_content.tr!("\u0000-\u0009\u000b\u000c\u000e-\u001f\u007f", "")
    else
      file_content = _("File not found.")
    end

    # Fill the LogView with file content
    UI.ChangeWidget(Id(:log), :Value, file_content)

    heading = Builtins.sformat(_("System Log (%1)"), @filename)
    UI.ChangeWidget(Id(:log), :Label, heading)

    # wait for user input

    @ret = Convert.to_symbol(UI.UserInput)

    # clicked "OK" -> exit

    if @ret == :ok
      @go_on = false
    elsif @ret == :cancel # close window
      UI.CloseDialog
      return true
    elsif @ret == :custom_file
      # adapt to combo box settings

      @new_file = Convert.to_string(
        UI.QueryWidget(Id(:custom_file), :Value)
      )
      @filename = @new_file if !@new_file.nil?
    else
      Builtins.y2milestone("bad UserInput (%1)", @ret)
    end
  end

  # write new list of filenames

  @new_files = []
  @set_default = false

  # re-build list to get new default correct
  Builtins.foreach(@all_files) do |file|
    if Builtins.substring(file, 0, 1) == "*"
      old_default = Builtins.substring(file, 1) # strip leading "*"
      if old_default == @filename # default unchanged
        @new_files = Builtins.add(@new_files, file)
        @set_default = true # new default
      else
        @new_files = Builtins.add(@new_files, old_default)
      end
    elsif file != ""
      if file == @filename # mark new default
        @new_files = Builtins.add(@new_files, Ops.add("*", @filename))
        @set_default = true
      else
        @new_files = Builtins.add(@new_files, file)
      end
    end
  end
  # if we don't have a default by now, it wasn't in the list before
  # so add it here.

  if !@set_default && @filename != ""
    @new_files = Builtins.add(@new_files, Ops.add("*", @filename))
  end

  @new_files = Builtins.toset(@new_files)

  # convert ycp list back to \n separated string

  @filenames = Ops.add(Builtins.mergestring(@new_files, "\n"), "\n")

  SCR.Write(
    path(".target.string"),
    Ops.add(@vardir, "/filenames"),
    @filenames
  )

  UI.CloseDialog

  true
end