a0495a7846499499f2f3899d9886116c534aa044
[matthijs/upstream/backupninja.git] / lib / easydialog.in
1 #!@BASH@
2 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3
4 # copyright 2002 lmoore@tump.com under the terms of the GNU LGPL.
5 # additions 2005 collective@riseup.net
6
7 # whiptail has trouble being called in the foo=$(whiptail ...) fashion for
8 # some reason.  this is very annoying.  this means that we need to use
9 # temporary files to store the answers from the input and list based boxes
10 # and then read the answers into a REPLY variable.  that just really
11 # stinks, oh well, that's what you get when you have a weak link
12 # implementation...
13 #
14 # inputBox and passwordBox could be refactored to use a common function
15
16 test -z "$WIDTH" && WIDTH=0
17 test -z "$HEIGHT" && HEIGHT=0
18 BACKTITLE=""
19 DIALOG=dialog
20 HELP=
21
22 setApplicationTitle() {
23    BACKTITLE=$*
24 }
25
26 setHelp() {
27    HELP="$@"
28 }
29
30 setDimension() {
31    WIDTH=$1
32    HEIGHT=$2
33 }
34
35 booleanBox() {
36    $DIALOG --backtitle "$BACKTITLE" --title "$1" \
37       `[ "$3" == no ] && echo '--defaultno'` --yesno "$2" $HEIGHT $WIDTH
38 }
39
40 msgBox() {
41    $DIALOG --backtitle "$BACKTITLE" --title "$1" \
42       --msgbox "$2" $HEIGHT $WIDTH
43 }
44
45 gaugeBox() {
46    $DIALOG --backtitle "$BACKTITLE" --title "$1" \
47       --gauge "$2" $HEIGHT $WIDTH 0
48 }
49
50 inputBox() {
51    local temp=$(@MKTEMP@ -t backupninja.XXXXXX) || exit 1
52    trap "rm -f $temp" 0
53    REPLY=
54    $DIALOG --backtitle "$BACKTITLE" --title "$1" \
55       --inputbox "$2" $HEIGHT $WIDTH "$3" 2> $temp
56    local status=$?
57    [ $status = 0 ] && REPLY=$(cat $temp)
58    rm -f $temp
59    return $status
60 }
61
62 # Xdialog and {dialog,whiptail} use different mechanism to "qoute" the
63 # values from a checklist.  {dialog,whiptail} uses standard double quoting
64 # while Xdialog uses a "/" as the separator.  the slash is arguably better,
65 # but the double quoting is more standard.  anyway, this function can be
66 # overridden to allow a derived implementation to change it's quoting
67 # mechanism to the standard double-quoting one.  it receives two
68 # arguements, the file that has the data and the box type.
69 _listReplyHook() {
70    cat $1
71 }
72
73 # this is the base implementation of all the list based boxes, it works
74 # out nicely that way.  the real function just passes it's arguments to
75 # this function with an extra argument specifying the actual box that
76 # needs to be rendered.
77 _genericListBox() {
78    local box=$1
79    shift 1
80    local title=$1
81    local text=$2
82    shift 2
83    local temp=$(@MKTEMP@ -t backupninja.XXXXXX) || exit 1
84    trap "rm -f $temp" 0
85    REPLY=
86    $DIALOG $HELP $_DEFAULT --backtitle "$BACKTITLE" --title "$title" \
87       $box "$text" $HEIGHT $WIDTH 10 \
88       "$@" 2> $temp
89    local status=$?
90    [ $status = 0 ] && REPLY=$(_listReplyHook $temp $box)
91    rm -f $temp
92    _DEFAULT=
93    return $status
94 }
95
96 setDefault() {
97    _DEFAULT="--default-item $1"
98 }
99
100 menuBox() {
101    _genericListBox --menu "$@"
102 }
103
104 ## a menu box with additional help info displayed
105 ## at the bottom of the window when an item is selected
106 menuBoxHelp() {
107    HELP="--item-help"
108    _genericListBox --menu "$@"
109    status=$?
110    HELP=
111    return $status
112 }
113
114 ## a menu box with an addition button 'help'
115 menuBoxHelpFile() {
116    HELP="--help-button"
117    _genericListBox --menu "$@"
118    status=$?
119    HELP=
120    return $status
121 }
122
123 checkBox() {
124    _genericListBox --checklist "$@"
125 }
126
127 radioBox() {
128    _genericListBox --radiolist "$@"
129 }
130
131 textBox() {
132    $DIALOG --backtitle "$BACKTITLE" --title "$1" --textbox "$2" $HEIGHT $WIDTH
133 }
134
135 passwordBox() {
136    local temp=$(@MKTEMP@ -t backupninja.XXXXXX) || exit 1
137    trap "rm -f $temp" 0
138    REPLY=
139    $DIALOG --backtitle "$BACKTITLE" --title "$1" \
140       --passwordbox "$2" $HEIGHT $WIDTH 2> $temp
141    local status=$?
142    [ $status = 0 ] && REPLY=$(cat $temp)
143    rm -f $temp
144    return $status
145 }
146
147
148 #########################################################
149 ## begin-item-display style lists
150 ##
151 ## these lists are built by calling fuctions multiple times.
152 ## this can make it easier to build your list in a loop
153 ##
154
155 listBegin() {
156    _menu_title=$1
157    _menu_msg=$2
158    _menu_items=0
159    _menu_text=
160    _menu_labels=
161    _menu_status=
162 }
163
164 listItem() {
165    _menu_labels[$_menu_items]=$1
166    _menu_text[$_menu_items]=$2
167    _menu_status[$_menu_items]=$3 # available only for checklist
168    let "_menu_items += 1"
169 }
170
171
172 ##
173 ## takes one of:
174 ## menu, checklist, radiolist
175 ##
176 listDisplay() {
177    boxtype=$1
178    local temp=$(@MKTEMP@ -t backupninja.XXXXXX) || exit 1
179    trap "rm -f $temp" 0
180
181    local label
182    local text
183    local status
184    (
185       echo -ne " $HELP $_DEFAULT "
186       echo -ne " --backtitle '$BACKTITLE' "
187       echo -ne " --title '$_menu_title' "
188       echo -ne " --$boxtype '$_menu_msg' "
189       echo -ne " $HEIGHT $WIDTH 10 "
190       for ((i=0; i < $_menu_items ; i++)); do
191          label=${_menu_labels[$i]}
192          text=${_menu_text[$i]}
193          status=${_menu_status[$i]}
194          echo -ne " $label '$text' $status "
195       done
196    ) | xargs $DIALOG 2> $temp
197
198    local status=$?
199    REPLY=""
200    [ $status = 0 ] && REPLY=`cat $temp`
201    rm -f $temp
202    _DEFAULT=
203    return $status
204 }
205
206 ####################################################
207 ## FORM
208
209 _form_gap=2
210 formBegin() {
211    _form_title=$1
212    _form_items=0
213    _form_labels=
214    _form_text=
215 }
216
217 formItem() {
218    _form_labels[$_form_items]=$1
219    _form_text[$_form_items]=$2
220    let "_form_items += 1"
221 }
222
223 formDisplay() {
224    local temp=$(@MKTEMP@ -t backupninja.XXXXXX) || exit 1
225
226    max_length=0
227    for ((i=0; i < ${#_form_labels[@]} ; i++)); do
228       label=${_form_labels[$i]}
229       length=`expr length $label`
230       if [ $length -gt $max_length ]; then
231          max_length=$length
232       fi
233    done
234    let "max_length += 2"
235
236    local xpos=1
237    (
238       echo -n -e "--form '$_form_title' 0 0 20"
239       for ((i=0; i < $_form_items ; i++)); do
240          label=${_form_labels[$i]}
241          text=${_form_text[$i]}
242          echo -n -e " $label $xpos 1 '$text' $xpos $max_length 30 30"
243          let "xpos += _form_gap"
244       done
245    ) | xargs $DIALOG 2> $temp
246    local status=$?
247
248    ##
249    ## the exit status is meaningless, it is always 0.
250    ## i can't figure out how to get the exit status of dialog
251    ## if we do "dialog `arg code`" or "dialog $args", then the quotes
252    ## get messed up and dialog won't run.
253    ## if we do "(arg code) | xargs dialog", then the exit status is
254    ## swallowed by xargs. xargs should return different exit status
255    ## depending on the exit status of the command run, but i have
256    ## never been able to get that to work.
257    ##
258
259    REPLY=
260    if [ $status = 0 ]; then
261       IFS=$''
262       REPLY=`cat $temp`
263       IFS=$' \t\n'
264    fi
265    rm -f $temp
266    return $status
267 }