ce7d31eefcae6ad7fba7d0e296bbf62914fb8586
[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 --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     return $status
91 }
92
93 menuBox() {
94     _genericListBox --menu "$@"
95 }
96
97 menuBoxHelp() {
98         HELP="--item-help"
99         _genericListBox --menu "$@"
100         status=$?
101         HELP=
102         return $status
103 }
104
105 menuBoxHelpFile() {
106         HELP="--help-button"
107         _genericListBox --menu "$@"
108         status=$?
109         HELP=
110         return $status
111 }
112
113
114 checkBox() {
115     _genericListBox --checklist "$@"
116 }
117
118 radioBox() {
119     _genericListBox --radiolist "$@"
120 }
121
122 textBox() {
123     $DIALOG --backtitle "$BACKTITLE" --title "$1" --textbox "$2" $HEIGHT $WIDTH
124 }
125
126 passwordBox() {
127     local temp=$(mktemp -t) || exit 1
128     trap "rm -f $temp" 0
129     REPLY=
130     $DIALOG --backtitle "$BACKTITLE" --title "$1" \
131         --passwordbox "$2" $HEIGHT $WIDTH 2> $temp
132     local status=$?
133     [ $status = 0 ] && REPLY=$(cat $temp)
134     rm -f $temp
135     return $status
136 }
137
138 _form_gap=2
139 startForm() {
140    _form_title=$1
141    _form_items=0
142    _form_labels=
143    _form_text=
144 }
145
146 formItem() {
147    _form_labels[$_form_items]=$1
148    _form_text[$_form_items]=$2
149    let "_form_items += 1"
150 }
151     
152 displayForm() {
153    local temp=$(mktemp -t) || exit 1
154    
155    max_length=0
156    for ((i=0; i < ${#_form_labels[@]} ; i++)); do
157       label=${_form_labels[$i]}
158       length=`expr length $label`
159       if [ $length -gt $max_length ]; then
160          max_length=$length
161       fi
162    done
163    let "max_length += 2"
164     
165    local form=
166    local xpos=1
167    for ((i=0; i < $_form_items ; i++)); do
168       label=${_form_labels[$i]}
169       text=${_form_text[$i]}
170       if [ "$text" == "" ]; then
171          text='_empty_'
172       fi
173       form=`echo -e "$form $label $xpos 1" $text "$xpos $max_length 30 30"`
174       let "xpos += _form_gap"
175    done
176
177    $DIALOG --form "$_form_title" 0 0 20 $form 2> $temp
178    local status=$?
179    [ $status = 0 ] && REPLY=$(cat $temp)
180    rm -f $temp
181    return $status
182 }