-
Notifications
You must be signed in to change notification settings - Fork 22
/
device_map_dialog.rb
235 lines (202 loc) · 5.42 KB
/
device_map_dialog.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
require "yast"
require "y2storage"
Yast.import "BootStorage"
Yast.import "Label"
Yast.import "Popup"
Yast.import "Mode"
require "bootloader/device_map"
module Bootloader
# Represents dialog for modification of device map
class DeviceMapDialog
include Yast::UIShortcuts
include Yast::I18n
def self.run(device_map)
new(device_map).run
end
def initialize(device_map)
@device_map = device_map
end
def run
textdomain "bootloader"
return unless create_dialog
begin
return controller_loop
ensure
close_dialog
end
end
private
def create_dialog
res = Yast::UI.OpenDialog dialog_content
return false unless res
refresh_buttons
true
end
def close_dialog
Yast::UI.CloseDialog
end
def controller_loop
loop do
input = Yast::UI.UserInput
pos = selected_disk_index
case input
when :ok
if disks.empty?
Yast::Popup.Error(_("Device map must contain at least one device"))
next
end
store_order
return :back # we just go back to original dialog
when :cancel
return :back
when :up, :down, :delete, :add
pos = handle_buttons(input, pos)
when :disks
refresh_buttons
next
else
raise "Unknown action #{input}"
end
refresh_disks
Yast::UI.ChangeWidget(Id(:disks), :CurrentItem, disks[pos])
refresh_buttons
end
end
def handle_buttons(action, pos)
case action
when :up
disks.insert(pos - 1, disks.delete_at(pos))
pos -= 1
when :down
disks.insert(pos + 1, disks.delete_at(pos))
pos += 1
when :delete
disks.delete_at(pos)
pos = pos == disks.size ? pos - 1 : pos
when :add
if Yast::Mode.config || Yast::Mode.auto
disks_to_add = add_device_popup_ay_mode
else
disks_to_add = add_devices_popup
end
disks.concat disks_to_add
pos = disks.size - 1
end
pos
end
def dialog_content
VBox(
headings,
VSpacing(1),
contents,
VSpacing(1),
ending_buttons
)
end
def headings
Heading(_("Disk order settings"))
end
def contents
HBox(
VBox(
SelectionBox(Id(:disks), Opt(:notify), _("D&isks"), disks),
# make dialog reasonable big, but increace mainly selection box which contain device names
HSpacing(60)
),
action_buttons
)
end
def action_buttons
VBox(
PushButton(Id(:add), Opt(:key_F3), Yast::Label.AddButton),
PushButton(Id(:delete), Opt(:key_F5), Yast::Label.DeleteButton),
VSpacing(1),
PushButton(Id(:up), Opt(:hstretch), Yast::Label.UpButton),
PushButton(Id(:down), Opt(:hstretch), Yast::Label.DownButton),
VStretch()
)
end
def ending_buttons
ButtonBox(
PushButton(Id(:ok), Yast::Label.OKButton),
PushButton(Id(:cancel), Yast::Label.CancelButton)
)
end
def disks
@disks ||= @device_map.disks_order
end
def refresh_disks
Yast::UI.ChangeWidget(Id(:disks), :Items, disks)
end
def available_devices
staging = Y2Storage::StorageManager.instance.staging
staging.disk_devices.map(&:name)
end
def store_order
Yast::BootStorage.assign_mbr_disk_by_name(disks.first)
@device_map.clear_mapping
disks.each_with_index do |disk, index|
@device_map.add_mapping("hd#{index}", disk)
end
end
def add_device_popup_ay_mode
popup = VBox(
VSpacing(1),
# textentry header
InputField(Id(:devname), Opt(:hstretch), _("&Device")),
VSpacing(1),
ButtonBox(
PushButton(Id(:ok), Opt(:key_F10, :default), Yast::Label.OKButton),
PushButton(Id(:cancel), Opt(:key_F8), Yast::Label.CancelButton)
),
VSpacing(1)
)
Yast::UI.OpenDialog(popup)
Yast::UI.SetFocus(:devname)
pushed = Yast::UI.UserInput
new_dev = Yast::UI.QueryWidget(Id(:devname), :Value)
Yast::UI.CloseDialog
pushed == :ok ? [new_dev] : []
end
def add_devices_popup
devices = available_devices - disks
popup = VBox(
MultiSelectionBox(
Id(:devnames),
"&Devices:",
devices
),
ending_buttons,
VSpacing(1)
)
Yast::UI.OpenDialog(popup)
Yast::UI.SetFocus(:devnames)
pushed = Yast::UI.UserInput
new_devs = Yast::UI.QueryWidget(Id(:devnames), :SelectedItems)
Yast::UI.CloseDialog
pushed == :ok ? new_devs : []
end
def selected_disk_index
disks.index(Yast::UI.QueryWidget(Id(:disks), :CurrentItem))
end
def refresh_buttons
pos = selected_disk_index
if !pos # nothing selected
disk_to_select = disks.first
# there is no disks
if !disk_to_select
up_down_enablement(false, false)
return
end
Yast::UI.ChangeWidget(Id(:disks), :CurrentItem, disk_to_select)
refresh_buttons
return
end
up_down_enablement(pos > 0, pos < (disks.size - 1))
end
def up_down_enablement(up, down)
Yast::UI.ChangeWidget(Id(:up), :Enabled, up)
Yast::UI.ChangeWidget(Id(:down), :Enabled, down)
end
end
end