Reduce differences with root_skels in contrib.
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / lib / lua / dfui / dfui.lua
1 -- $Id: dfui.lua,v 1.32 2005/04/03 20:46:01 cpressey Exp $
2 -- Wrapper/helper/extra abstractions for DFUI.
3
4 --[[------]]--
5 --[[ DFUI ]]--
6 --[[------]]--
7
8 --
9 -- This is a wrapper object around DFUI.Connection and DFUI.Progress,
10 -- intended to be used as a "UI adapter" for the App object.
11 --
12
13 module("dfui")
14
15 DFUI = require "ldfui"
16 local POSIX = require "posix"
17
18 DFUI.log = function(fmt, ...)
19         print(string.format(fmt, unpack(arg)))
20 end
21
22 DFUI.new = function(tab)
23         local dfui = {}
24         local transport = tab.transport or "tcp"
25         local rendezvous = tab.rendezvous or "9999"
26         local connection
27
28         dfui.start = function(dfui)
29                 connection = DFUI.Connection.new(transport, rendezvous)
30                 if connection:start() == 0 then
31                         connection:stop()
32                         DFUI.log("Could not establish DFUI connection " ..
33                             " on %s:%s", transport, rendezvous)
34                         return false
35                 end
36                 DFUI.log("DFUI connection on %s:%s successfully established",
37                         transport, rendezvous)
38                 return true
39         end
40
41         dfui.stop = function(dfui)
42                 return connection:stop()
43         end
44
45         dfui.present = function(dfui, tab)
46                 return connection:present(tab)
47         end
48
49         --
50         -- Handy dialogs.  (Perhaps a bit too handy?)
51         --
52
53         dfui.inform = function(dfui, msg)
54                 return connection:present({
55                     id = "inform",
56                     name = "Information",
57                     short_desc = msg,
58                     role = "informative",
59                     actions = {
60                         {
61                             id = "ok",
62                             name = "OK"
63                         }
64                     }
65                 })
66         end
67         
68         dfui.confirm = function(dfui, msg)
69                 return connection:present({
70                     id = "confirm",
71                     name = "Are you SURE?",
72                     short_desc = msg,
73                     role = "alert",
74                     actions = {
75                         {
76                             id = "ok",
77                             name = "OK"
78                         },
79                         {
80                             id = "cancel",
81                             name = "Cancel"
82                         }
83                     }
84                 }).action_id == "ok"
85         end
86
87         dfui.select = function(dfui, msg, map)
88                 local action = {}
89                 local consequence = {}
90                 local id_num = 0
91                 local k, v
92         
93                 for k, v in map do
94                         table.insert(action, {
95                             id = tostring(id_num),
96                             name = k
97                         })
98                         consequence[tostring(id_num)] = v
99                         id_num = id_num + 1
100                 end
101
102                 return consequence[connection:present({
103                     id = "select",
104                     name = "Please Select",
105                     short_desc = msg,
106                     role = "informative",
107                     actions = action
108                 }).action_id]
109         end
110
111         dfui.select_file = function(dfui, tab)
112                 local title = tab.title or "Select File"
113                 local short_desc = tab.short_desc or title
114                 local long_desc = tab.long_desc or ""
115                 local cancel_desc = tab.cancel_desc or "Cancel"
116                 local dir = assert(tab.dir)
117                 local ext = tab.ext or nil
118                 local files, i, filename
119
120                 local form = {
121                     id = "select_file",
122                     name = title,
123                     short_desc = short_desc,
124                     long_desc = long_desc,
125                     
126                     role = "menu",
127
128                     actions = {}
129                 }
130
131                 files = POSIX.dir(dir)
132                 table.sort(files)
133                 for i, filename in files do
134                         if not ext or string.find(filename, "%." .. ext .. "$") then
135                                 table.insert(form.actions, {
136                                     id = filename,
137                                     name = filename
138                                 })
139                         end
140                 end
141
142                 table.insert(form.actions, {
143                     id = "cancel",
144                     name = cancel_desc
145                 })
146
147                 return connection:present(form).action_id
148         end
149
150         --
151         -- Constructor within a constructor, here...
152         --
153         dfui.new_progress_bar = function(dfui, tab)
154                 local method = {}
155                 local pr
156                 local title = tab.title or "Working..."
157                 local short_desc = tab.short_desc or title
158                 local long_desc = tab.long_desc or ""
159                 local amount = 0
160
161                 pr = DFUI.Progress.new(connection,
162                     title, short_desc, long_desc, amount)
163
164                 method.start = function(method)
165                         return pr:start()
166                 end
167
168                 method.set_amount = function(method, new_amount)
169                         return pr:set_amount(new_amount)
170                 end
171
172                 method.set_short_desc = function(method, new_short_desc)
173                         return pr:set_short_desc(new_short_desc)
174                 end
175
176                 method.update = function(method)
177                         return pr:update()
178                 end
179
180                 method.stop = function(method)
181                         return pr:stop()
182                 end
183
184                 return method
185         end
186
187         return dfui
188 end
189
190 return DFUI