greatly improved the rdiff wizard. added default option to menu in easydialog.
[matthijs/upstream/backupninja.git] / handlers / easydialog.sh
1 #!/bin/bash
2
3 # copyright 2002 lmoore@tump.com under the terms of the GNU LGPL.
4
5 # whiptail has trouble being called in the foo=$(whiptail ...) fashion for
6 # some reason.  this is very annoying.  this means that we need to use
7 # temporary files to store the answers from the input and list based boxes
8 # and then read the answers into a REPLY variable.  that just really
9 # stinks, oh well, that's what you get when you have a weak link
10 # implementation...
11 #
12 # inputBox and passwordBox could be refactored to use a common function
13
14 test -z "$WIDTH" && WIDTH=0
15 test -z "$HEIGHT" && HEIGHT=0
16 BACKTITLE=""
17 DIALOG=dialog
18 HELP=
19
20 setApplicationTitle() {
21     BACKTITLE=$*
22 }
23
24 setHelp() {
25   HELP="$@"
26 }
27
28 setDimension() {
29     WIDTH=$1
30     HEIGHT=$2
31 }
32
33 booleanBox() {
34     $DIALOG --backtitle "$BACKTITLE" --title "$1" \
35         --yesno "$2" $HEIGHT $WIDTH
36 }
37
38 msgBox() {
39     $DIALOG --backtitle "$BACKTITLE" --title "$1" \
40         --msgbox "$2" $HEIGHT $WIDTH
41 }
42
43 gaugeBox() {
44     $DIALOG --backtitle "$BACKTITLE" --title "$1" \
45         --gauge "$2" $HEIGHT $WIDTH 0
46 }
47
48 inputBox() {
49     local temp=$(mktemp -t) || exit 1
50     trap "rm -f $temp" 0
51     REPLY=
52     $DIALOG --backtitle "$BACKTITLE" --title "$1" \
53         --inputbox "$2" $HEIGHT $WIDTH "$3" 2> $temp
54     local status=$?
55     [ $status = 0 ] && REPLY=$(cat $temp)
56     rm -f $temp
57     return $status
58 }
59
60 # Xdialog and {dialog,whiptail} use different mechanism to "qoute" the
61 # values from a checklist.  {dialog,whiptail} uses standard double quoting
62 # while Xdialog uses a "/" as the separator.  the slash is arguably better,
63 # but the double quoting is more standard.  anyway, this function can be
64 # overridden to allow a derived implementation to change it's quoting
65 # mechanism to the standard double-quoting one.  it receives two
66 # arguements, the file that has the data and the box type.
67 _listReplyHook() {
68     cat $1
69 }
70
71 # this is the base implementation of all the list based boxes, it works
72 # out nicely that way.  the real function just passes it's arguments to
73 # this function with an extra argument specifying the actual box that
74 # needs to be rendered.
75 _genericListBox() {
76     local box=$1
77     shift 1
78     local title=$1
79     local text=$2
80     shift 2
81     local temp=$(mktemp -t) || exit 1
82     trap "rm -f $temp" 0
83     REPLY=
84     $DIALOG $HELP $_DEFAULT --backtitle "$BACKTITLE" --title "$title" \
85         $box "$text" $HEIGHT $WIDTH 10 \
86         "$@" 2> $temp
87     local status=$?
88     [ $status = 0 ] && REPLY=$(_listReplyHook $temp $box)
89     rm -f $temp
90     _DEFAULT=
91     return $status
92 }
93
94 setDefault() {
95   _DEFAULT="--default-item $1"
96 }
97
98 menuBox() {
99     _genericListBox --menu "$@"
100 }
101
102 menuBoxHelp() {
103         HELP="--item-help"
104         _genericListBox --menu "$@"
105         status=$?
106         HELP=
107         return $status
108 }
109
110 menuBoxHelpFile() {
111         HELP="--help-button"
112         _genericListBox --menu "$@"
113         status=$?
114         HELP=
115         return $status
116 }
117
118
119 checkBox() {
120     _genericListBox --checklist "$@"
121 }
122
123 radioBox() {
124     _genericListBox --radiolist "$@"
125 }
126
127 textBox() {
128     $DIALOG --backtitle "$BACKTITLE" --title "$1" --textbox "$2" $HEIGHT $WIDTH
129 }
130
131 passwordBox() {
132     local temp=$(mktemp -t) || exit 1
133     trap "rm -f $temp" 0
134     REPLY=
135     $DIALOG --backtitle "$BACKTITLE" --title "$1" \
136         --passwordbox "$2" $HEIGHT $WIDTH 2> $temp
137     local status=$?
138     [ $status = 0 ] && REPLY=$(cat $temp)
139     rm -f $temp
140     return $status
141 }
142
143 _form_gap=2
144 startForm() {
145    _form_title=$1
146    _form_items=0
147    _form_labels=
148    _form_text=
149 }
150
151 formItem() {
152    _form_labels[$_form_items]=$1
153    _form_text[$_form_items]=$2
154    let "_form_items += 1"
155 }
156     
157 displayForm() {
158    local temp=$(mktemp -t) || exit 1
159    
160    max_length=0
161    for ((i=0; i < ${#_form_labels[@]} ; i++)); do
162       label=${_form_labels[$i]}
163       length=`expr length $label`
164       if [ $length -gt $max_length ]; then
165          max_length=$length
166       fi
167    done
168    let "max_length += 2"
169     
170    local form=
171    local xpos=1
172    (
173       echo -n -e "--form '$_form_title' 0 0 20"
174       for ((i=0; i < $_form_items ; i++)); do
175         label=${_form_labels[$i]}
176         text=${_form_text[$i]}
177 #        if [ "$text" == "" ]; then
178 #           text='_empty_'
179 #        fi
180         echo -n -e "$form $label $xpos 1 '$text' $xpos $max_length 30 30"
181         let "xpos += _form_gap"
182       done
183    ) | xargs $DIALOG 2> $temp
184    local status=$?
185    [ $status = 0 ] && REPLY=`cat $temp`
186    rm -f $temp
187    return $status
188 }