Installer import into contrib (real import this time)
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / backend / lua / lib / StorageSystemUI.lua
1 -- $Id: StorageSystemUI.lua,v 1.6 2005/04/04 20:50:59 cpressey Exp $
2
3 require "gettext"
4
5 --[[-----------]]--
6 --[[ StorageUI ]]--
7 --[[-----------]]--
8
9 StorageUI = {}
10
11 -- Function to present a form to the user from which they
12 -- can select any disk present in the given StorageDescriptor.
13
14 StorageUI.select_disk = function(tab)
15         local dd
16         local disk_actions = {}
17
18         local sd = tab.sd or error("Need a storage descriptor")
19
20         local add_disk_action = function(tdd)
21                 table.insert(disk_actions,
22                     {
23                         id = tdd:get_name(),
24                         name = tdd:get_desc(),
25                         effect = function()
26                                 return tdd
27                         end
28                     }
29                 )
30         end
31
32         for dd in sd:get_disks() do
33                 add_disk_action(dd)
34         end
35
36         table.insert(disk_actions,
37             {
38                 id = "cancel",
39                 name = tab.cancel_desc or _("Cancel"),
40                 effect = function()
41                     return nil
42                 end
43             }
44         )
45
46         return App.ui:present({
47             id = tab.id or "select_disk",
48             name = tab.name or _("Select a Disk"),
49             short_desc = tab.short_desc or _("Select a disk."),
50             long_desc = tab.long_desc,
51             actions = disk_actions,
52             role = "menu"
53         }).result
54 end
55
56 -- Function to present a form to the user from which they
57 -- can select any partition present in the given DiskDescriptor.
58
59 StorageUI.select_part = function(tab)
60         local pd
61         local part_actions = {}
62
63         local dd = tab.dd or error("Need a disk descriptor")
64
65         local add_part_action = function(tpd)
66                 table.insert(part_actions,
67                     {
68                         id = tostring(tpd:get_number()),
69                         name = tpd:get_desc(),
70                         effect = function()
71                                 return tpd
72                         end
73                     }
74                 )
75         end
76
77         for pd in dd:get_parts() do
78                 add_part_action(pd)
79         end
80
81         table.insert(part_actions,
82             {
83                 id = "cancel",
84                 name = tab.cancel_desc or _("Cancel"),
85                 effect = function()
86                     return nil
87                 end
88             }
89         )
90
91         return App.ui:present({
92             id = tab.id or "select_part",
93             name = tab.name or _("Select a Partition"),
94             short_desc = tab.short_desc or _("Select a partition."),
95             long_desc = tab.long_desc,
96             actions = part_actions,
97             role = "menu"
98         }).result
99 end
100
101 StorageUI.select_packages = function(tab)
102         local datasets_list = {}
103         local pkg, selected, i, dataset
104
105         if not tab.sel_pkgs then
106                 tab.sel_pkgs = {}
107         end
108         for pkg, selected in tab.sel_pkgs do
109                 table.insert(datasets_list, {
110                     selected = (selected and "Y") or "N",
111                     package = pkg
112                 })
113         end
114         table.sort(datasets_list, function(a, b)
115                 return a.package < b.package
116         end)
117
118         local fields_list = {
119                 {
120                     id = "selected",
121                     name = tab.checkbox_name or _("Install?"),
122                     control = "checkbox"
123                 },
124                 {
125                     id = "package",
126                     name = _("Full Name of Package"),
127                     editable = "false"
128                 }
129         }
130
131         local actions_list = {
132                 {
133                     id = "all",
134                     name = tab.all_name or _("Select All")
135                 },
136                 {
137                     id = "none",
138                     name = tab.none_name or _("Select None")
139                 },
140                 {
141                     id = "ok",
142                     name = tab.ok_name or _("Accept these Packages")
143                 },
144                 {
145                     id = "cancel",
146                     name = tab.cancel_name or _("Cancel")
147                 }
148         }
149
150         while true do
151                 local response = App.ui:present({
152                     id = tab.id or "select_packages",
153                     name = tab.name or _("Select Packages"),
154                     short_desc = tab.short_desc or
155                         _("Select the packages you wish to install."),
156                     long_desc = tab.long_desc,
157                     actions = actions_list,
158                     fields = fields_list,
159                     datasets = datasets_list,
160         
161                     multiple = "true"
162                 })
163
164                 datasets_list = response.datasets
165
166                 if response.action_id == "all" then
167                         for i, dataset in datasets_list do
168                                 dataset.selected = "Y"
169                         end
170                 elseif response.action_id == "none" then
171                         for i, dataset in datasets_list do
172                                 dataset.selected = "N"
173                         end
174                 else
175                         local pkg_selected = {}
176                         for i, dataset in datasets_list do
177                                 pkg_selected[dataset.package] = (dataset.selected == "Y")
178                         end
179                         return response.action_id == "ok", pkg_selected
180                 end
181         end
182 end