-
Notifications
You must be signed in to change notification settings - Fork 12
/
services_table.rb
366 lines (319 loc) · 11 KB
/
services_table.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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
280
281
282
283
284
285
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# encoding: utf-8
# Copyright (c) [2018] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require "yast"
require "services-manager/widgets/base"
Yast.import "ServicesManager"
module Y2ServicesManager
module Widgets
class ServicesTable < Base
extend Yast::I18n
# Systemd states and substates might change. Use the following script to check
# whether new states are not considered yet:
#
# https://github.com/yast/yast-services-manager/blob/check_systemd_states/systemd_status_check.rb
TRANSLATIONS = {
service_state: {
"activating" => N_("Activating"),
"active" => N_("Active"),
"deactivating" => N_("Deactivating"),
"failed" => N_("Failed"),
"inactive" => N_("Inactive"),
"maintenance" => N_("Maintenance"),
"reloading" => N_("Reloading")
},
service_substate: {
"auto-restart" => N_("Auto-restart"),
"cleaning" => N_("Cleaning"),
"condition" => N_("Condition"),
"dead" => N_("Dead"),
"exited" => N_("Exited"),
"failed" => N_("Failed"),
"final-sigkill" => N_("Final-sigkill"),
"final-sigterm" => N_("Final-sigterm"),
"final-watchdog" => N_("Final-watchdog"),
"reload" => N_("Reload"),
"running" => N_("Running"),
"start" => N_("Start"),
"start-post" => N_("Start-post"),
"start-pre" => N_("Start-pre"),
"stop" => N_("Stop"),
"stop-post" => N_("Stop-post"),
"stop-sigkill" => N_("Stop-sigkill"),
"stop-sigterm" => N_("Stop-sigterm"),
"stop-watchdog" => N_("Stop-watchdog")
}
}
private_constant :TRANSLATIONS
# Constructor
#
# @example
# ServicesTable.new(services_names: ["tftp", "cups"])
#
# @param id [Symbol] widget id
# @param services_names [Array<String>] name of services to show
def initialize(id: nil, services_names: [])
textdomain "services-manager"
super(id: id)
@services_names = services_names
end
# @return [Yast::Term]
def widget
@table ||= Table(id, Opt(:immediate), header, items)
end
# Sets focus on the table
def focus
UI.SetFocus(id)
end
# Refreshes the content of the table
#
# The table will refresh its content with the given services names. In case that
# no services names are given, it will show the same services again.
#
# @param services_names [Array<String>, nil]
def refresh(services_names: nil)
@services_names = services_names if services_names
UI.ChangeWidget(id, :Items, items)
focus
end
# Refreshes the row of a specific service
#
# @param service_name [String]
def refresh_row(service_name)
refresh_start_mode_value(service_name)
refresh_state_value(service_name)
focus
end
# Name of the service of the currently selected row
#
# @return [String]
def selected_service_name
UI.QueryWidget(id, :CurrentItem)
end
# Service object of the currently selected row
#
# @return [Yast2::SystemService, nil] nil if the service is not found
def selected_service
service(selected_service_name)
end
# Help text
#
# @return [String]
def help
# TRANSLATORS: help text to explain the columns of the services table
_(
"<h2>The Services Table</h2>" \
"<p>This table contains <b>all services</b> with the following information about each service:</p>" \
"<b>Service</b> shows the name of the service." \
"<br />" \
"<b>Start</b> shows the start mode of the service:" \
"<ul>" \
"<li>On Boot: the service will be automatically started after booting the system.</li>" \
"<li>On Demand: the service will be automatically started when needed.</li>" \
"<li>Manually: the service will not be automatically started.</li>" \
"</ul>" \
"<b>State</b> shows the state and substate of the service." \
"<br />" \
"<b>Description</b> shows the description of the service." \
"<br />" \
"<br />" \
"Note: edited values are marked with '(*)'."
)
end
private
# @return [Array<String>] services shown in the table
attr_reader :services_names
# Default widget id
#
# @see Base#default_id
#
# @return [Yast::Term]
def default_id
Id(:services_table)
end
# Table header
#
# @return [Yast::Term]
def header
Header(
*columns.map { |c| send("#{c}_title") }
)
end
# Content of the table
#
# @return [Array<Yast::Term>]
def items
services_names.sort_by { |s| s.downcase }.map { |s| Item(*values_for(s)) }
end
# Values to show in the table for a specific service
#
# @param service_name [String]
# @return [Array<Yast::Term, String>]
def values_for(service_name)
[row_id(service_name)] + columns.map { |c| send("#{c}_value", service_name) }
end
# Columns to show in the table
#
# @return [Array<Symbol>]
def columns
[:name, :start_mode, :state, :description]
end
# Title for name column
#
# @return [String]
def name_title
_("Service")
end
# Title for start_mode column
#
# @return [String]
def start_mode_title
_("Start")
end
# Title for state column
#
# @return [String]
def state_title
_("State")
end
# Title for description column
#
# @return [String]
def description_title
_("Description")
end
# Id for a table row of a service
#
# @param service_name [String]
# @return [Yast::Term]
def row_id(service_name)
Id(service_name)
end
# Value for the name column of a service
#
# @param service_name [String]
# @return [String]
def name_value(service_name)
max_width = max_column_width(:name)
return service_name if service_name.size < max_width
service_name[0..(max_width - 3)] + "..."
end
# Value for the start_mode column of a service
#
# @note The value contains a special mark when it has been edited by the user,
# see {#highlight_value}.
#
# @param service_name [String]
# @return [String]
def start_mode_value(service_name)
value = ServicesManagerService.start_mode_to_human_for(service_name)
value = highlight_value(value) if service(service_name).changed?(:start_mode)
value
end
# Value for the state column of a service
#
# By default it shows the current service status, but if the user starts or stops
# the service, then it shows the fixed text "Active" or "Inactive", see {#current_state_value}
# and {#future_state_value}.
#
# @note The value contains a special mark when it has been edited by the user,
# see {#highlight_value}.
#
# @param service_name [String]
# @return [String]
def state_value(service_name)
service = service(service_name)
return current_state_value(service) unless service.changed?(:active)
future_state_value(service)
end
# Value for the description column of a service
#
# @param service_name [String]
# @return [String]
def description_value(service_name)
ServicesManagerService.description(service_name) || ""
end
# Text for the current state of the service
#
# @param service [Yast2::SystemService]
# @return [String]
def current_state_value(service)
state = TRANSLATIONS[:service_state][service.state]
substate = TRANSLATIONS[:service_substate][service.substate]
return _(state) unless substate
# TRANSLATORS: state of a service, as showed by systemctl (e.g., "Active (Running)").
# %{state} is replaced by the service state (e.g. "Active", "Inactive", etc) and
# %{substate} is replaced by the service substate (e.g., "Start", "Stop", "Exited", etc).
format(_("%{state} (%{substate})"), state: _(state), substate: _(substate))
end
# Text for the future state of the service
#
# @note It contains a special mark, see {#highlight_value}.
#
# @param service [Yast2::SystemService]
# @return [String]
def future_state_value(service)
value = service.active? ? _("Active") : _("Inactive")
highlight_value(value)
end
# Adds a special mark to highlight the value (e.g., when the value has been edited)
#
# @param value [String]
# @return [String]
def highlight_value(value)
"(*) " + value
end
# Service object
#
# @param service_name [String]
# @return [Yast2::SystemService, nil] nil if the service is not found
def service(service_name)
ServicesManagerService.find(service_name)
end
# Updates the value for the start_mode column of a service
#
# @param service_name [String]
def refresh_start_mode_value(service_name)
UI.ChangeWidget(id, Cell(service_name, 1), start_mode_value(service_name))
end
# Updates the value for the state column of a service
#
# @param service_name [String]
def refresh_state_value(service_name)
UI.ChangeWidget(id, Cell(service_name, 2), state_value(service_name))
end
# Max width of a column
#
# In general there is no limitation for any column. Only name column has
# a limited width.
#
# @param column [Symbol]
# @return [Integer]
def max_column_width(column)
return nil if column != :name
# use 60 for other elements in table we want to display, see bsc#993826
display_width - 60
end
# @return [Integer]
def display_width
UI.GetDisplayInfo["Width"] || 80
end
end
end
end