~ubuntu-branches/ubuntu/trusty/uck/trusty

« back to all changes in this revision

Viewing changes to libraries/gui.sh

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2010-07-17 20:49:25 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100717204925-2lxuvgt36co3xroh
Tags: 2.2.1-0ubuntu1
* New upstream release.
* Bump Standards.
* Suggests on brasero | k3b | burn.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# along with UCK.  If not, see <http://www.gnu.org/licenses/>.                    #
19
19
###################################################################################
20
20
 
21
 
function find_dialog()
22
 
{
23
 
        if [ ! -z "$DISPLAY" ] ; then
24
 
                DIALOG=`which kdialog`
25
 
 
26
 
                if [ ! -z "$DIALOG" ]; then
27
 
                        DIALOG_TYPE=kdialog
28
 
                else
29
 
                        DIALOG=`which Xdialog`
30
 
 
31
 
                        if [ ! -z "$DIALOG" ]; then
32
 
                                DIALOG_TYPE=dialog
33
 
                        fi
34
 
                fi
35
 
 
36
 
                if [ -z "$DIALOG" ]; then
37
 
                        DIALOG=`which zenity`
38
 
 
39
 
                        if [ ! -z "$DIALOG" ]; then
40
 
                                DIALOG_TYPE=zenity
41
 
                        fi
42
 
                fi
43
 
        fi
44
 
 
45
 
        if [ -z "$DIALOG" ]; then
46
 
                DIALOG=`which dialog`
47
 
 
48
 
                if [ ! -z "$DIALOG" ]; then
49
 
                        DIALOG_TYPE=dialog
50
 
                fi
51
 
        fi
52
 
 
53
 
        if [ -z $DIALOG ]; then
54
 
                failure "You need kdialog, Xdialog, dialog or zenity application to run this script, please install it using 'apt-get install packagename' where packagename is 'kdebase-bin' for kdialog, 'xdialog' for dialog, 'dialog' for dialog. If you are using text-mode, you need to install dialog."
55
 
        fi
56
 
}
57
 
 
58
 
function dialog_menu()
59
 
{
60
 
        DESCRIPTION="$1"
61
 
        shift
62
 
 
63
 
        declare -a PARAMS
64
 
 
65
 
        if [ "$DIALOG_TYPE" = "zenity" ]; then
66
 
                declare -i i=0
67
 
                for v; do
68
 
                        PARAMS[$i]="$v"
69
 
                        i+=1
70
 
                done
71
 
                $DIALOG --list --text "$DESCRIPTION" --column "" "${PARAMS[@]}" --width=500 --height=400
72
 
        else
73
 
                if [ "$DIALOG_TYPE" = "kdialog" ] ; then
74
 
                        declare -i i=0
75
 
                        for v; do
76
 
                                PARAMS[$i]="$v"
77
 
                                i+=1
78
 
                                PARAMS[$i]="$v" #yes, 2 times as kdialog requires key and value
79
 
                                i+=1
80
 
                        done
81
 
                        $DIALOG --menu "$DESCRIPTION" "${PARAMS[@]}"
82
 
                else
83
 
                        declare -i i=0
84
 
                        for v; do
85
 
                                PARAMS[$i]="$v"
86
 
                                i+=1
87
 
                                PARAMS[$i]="Language"
88
 
                                i+=1
89
 
                        done
90
 
                        $DIALOG --stdout --menu "$DESCRIPTION" 20 30 10 "${PARAMS[@]}"
91
 
                fi
92
 
        fi
93
 
}
94
 
 
95
 
function dialog_multi_choice()
96
 
{
97
 
        DESCRIPTION="$1"
98
 
        shift
99
 
 
100
 
        if [ "$DIALOG_TYPE" = "zenity" ]; then
101
 
                for i; do
102
 
                        PARAMS="$PARAMS $i $i"
103
 
                done
104
 
                $DIALOG --separator $'\n' --list --checklist --multiple --text "$DESCRIPTION" --column "" --column ""  $PARAMS --width=500 --height=400
105
 
        else
106
 
                if [ "$DIALOG_TYPE" = "kdialog" ] ; then
107
 
                        for i; do
108
 
                                PARAMS="$PARAMS $i $i 0"
109
 
                        done
110
 
                        $DIALOG --separate-output --checklist "$DESCRIPTION" $PARAMS
111
 
                else
112
 
                        for i; do
113
 
                                PARAMS="$PARAMS $i Language 0"
114
 
                        done
115
 
                        $DIALOG --stdout --separate-output --checklist "$DESCRIPTION" 20 30 10 $PARAMS
116
 
                fi
117
 
        fi
118
 
 
119
 
        RESULT=$?
120
 
        return $RESULT
121
 
}
122
 
 
123
 
function dialog_line_input()
124
 
{
125
 
        DESCRIPTION="$1"
126
 
        INITIAL_VALUE="$2"
127
 
 
128
 
        if [ "$DIALOG_TYPE" = "zenity" ] ; then
129
 
                $DIALOG --entry --text "$DESCRIPTION" --entry-text "$INITIAL_VALUE"
130
 
        else
131
 
                if [ "$DIALOG_TYPE" = "kdialog" ] ; then
132
 
                        $DIALOG --inputbox "$DESCRIPTION" "$INITIAL_VALUE"
133
 
                else
134
 
                        $DIALOG --stdout --inputbox "$DESCRIPTION" 20 30 "$INITIAL_VALUE"
135
 
                fi
136
 
        fi
137
 
 
138
 
        RESULT=$?
139
 
        return $RESULT
140
 
}
141
 
 
142
 
function dialog_choose_file()
143
 
{
144
 
        TITLE="$1"
145
 
 
146
 
        if [ "$DIALOG_TYPE" = "zenity" ] ; then
147
 
                $DIALOG --title "$TITLE" --file-selection "`pwd`/"
148
 
        else
149
 
                if [ "$DIALOG_TYPE" = "kdialog" ] ; then
150
 
                        $DIALOG --title "$TITLE" --getopenfilename "`pwd`/"
151
 
                else
152
 
                        $DIALOG --stdout --title "$TITLE" --fselect "`pwd`/" 10 70
153
 
                fi
154
 
        fi
155
 
}
156
 
 
157
 
function dialog_msgbox()
158
 
{
159
 
        TITLE="$1"
160
 
        TEXT="$2"
161
 
 
162
 
        if [ "$DIALOG_TYPE" = "zenity" ]; then
163
 
                echo -n "$TEXT" | $DIALOG --title "$TITLE" --text-info --width=500 --height=400
164
 
        else
165
 
                $DIALOG --title "$TITLE" --msgbox "$TEXT" 20 80
166
 
        fi
167
 
}
168
 
 
169
 
function dialog_question()
170
 
{
171
 
        TITLE="$1"
172
 
        TEXT="$2"
173
 
 
174
 
        if [ "$DIALOG_TYPE" = "zenity" ]; then
175
 
                $DIALOG --title "$TITLE" --question --text "$TEXT"
176
 
        else
177
 
                $DIALOG --title "$TITLE" --yesno "$TEXT" 20 80
178
 
        fi
179
 
}
180
 
 
181
 
find_dialog
 
 
b'\\ No newline at end of file'
 
21
# DESCRIPTION:
 
22
#       This shell library module provides user interface funtionality.
 
23
#
 
24
#       It looks for and uses the best possible UI for shell procedures
 
25
#       and falls back to simple builtin functionality if none is found.
 
26
#
 
27
#       This module has been written so it works in many shells, e.g.:
 
28
#               bash, dash, ash, busybox, zsh, ksh
 
29
#       have been tested. Minimum requirement is a shell that can handle
 
30
#       functions and your typical selection of standard commands.
 
31
#
 
32
#       For similar work in other areas see:
 
33
#               http://sourceforge.net/projects/ui-dialog/
 
34
#
 
35
# PROVIDES:
 
36
#       find_dialog() -- determine dialog system to use
 
37
#       dialog_menu(descr, c1, c2, ...) -- solicit a single choice from the user
 
38
#       dialog_multi_choice(descr, c1, c2, ...) -- solicit multiple choices
 
39
#       dialog_line_input(descr, init) -- solicit a single line of input
 
40
#       dialog_choose_file(descr) -- select a file
 
41
#       dialog_msgbox(title, message) -- display a message and wait for input
 
42
#       dialog_question(title, question) -- display a yes/no question
 
43
#
 
44
# Some notes on cross-shell portability:
 
45
#       - "which" is a zsh builtin that does not work the way we need it. So
 
46
#         we use /usr/bin/which instead.
 
47
#       - Using command substitution within a test expression does not work
 
48
#         quite right in some shells. Using intermediate varaibles instead.
 
49
#       - Expressions are evaluated by expr and arrays are handled via eval.
 
50
 
 
51
# _dialog_type is set when the module is loaded and defines the type
 
52
# of user interface. Possible values (in order of preference) are:
 
53
#               kdialog         -- use kdialog
 
54
#               zenity          -- use zenity
 
55
#               dialog          -- use dialog
 
56
#               builtin         -- use shell-only builtins
 
57
_dialog_type=builtin
 
58
 
 
59
# find_dialog -- find the best type of dialog that is usable
 
60
find_dialog() {
 
61
        if [ -n "$DISPLAY" ]; then
 
62
                # Check availability of X windows based dialogs
 
63
                kdialog=`/usr/bin/which kdialog`
 
64
                zenity=`/usr/bin/which zenity`
 
65
                if [ -n "$kdialog" ]; then
 
66
                        _dialog_type=kdialog
 
67
                elif [ -n "$zenity" ]; then
 
68
                        _dialog_type=zenity
 
69
                fi
 
70
        fi
 
71
 
 
72
        if [ -z "$DISPLAY" -o $_dialog_type = builtin ]; then
 
73
                # None of the X windows based dialogs found/usable.
 
74
                dialog=`/usr/bin/which dialog`
 
75
                if [ -n "$dialog" ]; then
 
76
                        _dialog_type=dialog
 
77
                fi
 
78
        fi
 
79
}
 
80
 
 
81
# dialog_menu -- solicit a single choice from the user
 
82
#       Parameters: descr choice [choice [...]]
 
83
#       Returns: selected choice on stdout
 
84
#       Status: 0 for success, 1 for failure
 
85
dialog_menu() {
 
86
        descr="$1"; shift
 
87
 
 
88
        case $_dialog_type in
 
89
        kdialog)
 
90
                # Create key/value pairs for argument list
 
91
                i=0; args=
 
92
                while [ -n "$1" ]; do
 
93
                        i=`expr $i + 1`
 
94
                        args="$args $i '$1'"; shift
 
95
                done
 
96
                eval set -- "$args"
 
97
 
 
98
                choice=`kdialog --menu "$descr" "$@"`
 
99
 
 
100
                # Convert key returned by dialog to value
 
101
                if expr "$choice" : '[1-9][0-9]*$' >/dev/null; then
 
102
                        eval echo \$`expr $choice \* 2`
 
103
                        return 0
 
104
                fi
 
105
                return 1
 
106
                ;;
 
107
 
 
108
        zenity)
 
109
                zenity --list --width=500 --height=400 \
 
110
                        --column "$descr" "$@"
 
111
                return $?
 
112
                ;;
 
113
 
 
114
        dialog)
 
115
                # Create key/value pairs for argument list
 
116
                i=0; args=
 
117
                while [ -n "$1" ]; do
 
118
                        i=`expr $i + 1`
 
119
                        args="$args $i '$1'"; shift
 
120
                done
 
121
                eval set -- "$args"
 
122
 
 
123
                choice=`dialog --stdout --menu "$descr" 20 30 10 "$@"`
 
124
 
 
125
                # Convert key returned by dialog to value
 
126
                if expr "$choice" : '[1-9][0-9]*$' >/dev/null; then
 
127
                        eval echo \$`expr $choice \* 2`
 
128
                        return 0
 
129
                fi
 
130
                return 1
 
131
                ;;
 
132
 
 
133
        builtin)
 
134
                echo >&2
 
135
                echo "$descr" >&2
 
136
                i=0
 
137
                while [ $i -lt $# ]; do
 
138
                        i=`expr $i + 1`
 
139
                        eval echo "$i\) \$$i" >&2
 
140
                done
 
141
                echo -n "Your choice (1..$#)> " >&2
 
142
                read choice
 
143
 
 
144
                # Convert key returned by dialog to value
 
145
                if expr "$choice" : '[1-9][0-9]*$' >/dev/null; then
 
146
                        eval echo \$$choice
 
147
                        return 0
 
148
                fi
 
149
                return 1
 
150
                ;;
 
151
 
 
152
        *)      echo "$0: internal error: fall through case" >&2; exit 100
 
153
                ;;
 
154
        esac
 
155
}
 
156
 
 
157
# dialog_multi_choice -- solicit multiple choices from the user
 
158
#       Parameters: descr choice [choice [...]]
 
159
#       Returns: selected choices on stdout
 
160
#       Status: 0 for success, 1 for failure
 
161
dialog_multi_choice() {
 
162
        descr="$1"; shift
 
163
 
 
164
        case $_dialog_type in
 
165
        kdialog)
 
166
                args=
 
167
                for i; do
 
168
                        args="$args '$i' '$i' 0"
 
169
                done
 
170
                eval set -- "$args"
 
171
                kdialog --separate-output --checklist "$descr" "$@"
 
172
                ;;
 
173
 
 
174
        zenity)
 
175
                args=
 
176
                for i; do
 
177
                        args="$args '$i' '$i'"
 
178
                done
 
179
                eval set -- "$args"
 
180
                zenity --separator $'\n' --list --checklist --multiple --width=500 --height=400 \
 
181
                        --text "$descr" --column "" --column ""  "$@"
 
182
                ;;
 
183
 
 
184
        dialog)
 
185
                args=
 
186
                for i; do
 
187
                        args="$args '$i' '' 0"
 
188
                done
 
189
                eval set -- "$args"
 
190
                dialog --stdout --separate-output --checklist "$descr" \
 
191
                        20 30 10 "$@"
 
192
                ;;
 
193
 
 
194
        builtin)
 
195
                selection=
 
196
                while :
 
197
                do
 
198
                        i=0
 
199
                        while [ $i -lt $# ]; do
 
200
                                i=`expr $i + 1`
 
201
                                eval echo "$i\) \$$i" >&2
 
202
                        done
 
203
                        echo "Currently selected: $selection" >&2
 
204
                        echo -n "Your choice (1..$#, 0 or epmty line if done, -1 to abort) > " >&2
 
205
                        read choice
 
206
 
 
207
                        case $choice in
 
208
                        -1)     return 1;;
 
209
                        ""|0)   break;;
 
210
                        esac
 
211
 
 
212
                        # Sanity checks
 
213
                        if expr "$choice" : '[1-9][0-9]*$' >/dev/null; then
 
214
                                : this is what we need
 
215
                        else
 
216
                                continue
 
217
                        fi
 
218
                        [ "$choice" -gt $# ] && continue
 
219
 
 
220
                        # Delete if already in selection
 
221
                        found=0
 
222
                        nselection=
 
223
                        for chosen in $selection
 
224
                        do
 
225
                                if [ $chosen -eq $choice ]; then
 
226
                                        found=1
 
227
                                else
 
228
                                        nselection="$nselection $chosen"
 
229
                                fi
 
230
                        done
 
231
                        selection="$nselection"
 
232
 
 
233
                        # Append if not found
 
234
                        if [ $found -eq 0 ]; then
 
235
                                selection="$selection $choice"
 
236
                        fi
 
237
                done
 
238
 
 
239
                # Create list with choices selected on stdout
 
240
                if [ -n "$selection" ]; then
 
241
                        for choice in $selection; do
 
242
                                eval echo \$$choice
 
243
                        done
 
244
                        return 0
 
245
                fi
 
246
 
 
247
                return 1
 
248
                ;;
 
249
 
 
250
        *)      echo "$0: internal error: fall through case" >&2; exit 100
 
251
                ;;
 
252
        esac
 
253
}
 
254
 
 
255
# dialog_line_input -- negotiate a single line of input from the user
 
256
#       Arguments: descr initial_value
 
257
#       Returns: a single line on stdout
 
258
#       Status: 0 for success, 1 for failure
 
259
dialog_line_input() {
 
260
        descr="$1"; shift
 
261
        initial="$1"; shift
 
262
 
 
263
        case $_dialog_type in
 
264
        kdialog)
 
265
                kdialog --inputbox "$descr" "$initial"
 
266
                ;;
 
267
 
 
268
        zenity)
 
269
                zenity --entry --text "$descr" --entry-text "$initial"
 
270
                ;;
 
271
 
 
272
        dialog)
 
273
                dialog --stdout --inputbox "$descr" 20 30 "$initial"
 
274
                ;;
 
275
 
 
276
        builtin)
 
277
                echo >&2
 
278
                echo "$descr" >&2
 
279
                echo -n "($initial)> " >&2
 
280
                read line
 
281
                case $line in
 
282
                "")     echo "$initial";;
 
283
                *)      echo "$line";;
 
284
                esac
 
285
                ;;
 
286
 
 
287
        *)      echo "$0: internal error: fall through case" >&2; exit 100
 
288
                ;;
 
289
        esac
 
290
 
 
291
        return $?
 
292
}
 
293
 
 
294
# dialog_choose_file -- solicit a single file name from the user
 
295
#       Parameters: descr
 
296
#       Returns: selected file name on stdout
 
297
#       Status: 0 for success, 1 for failure
 
298
dialog_choose_file() {
 
299
        descr="$1"; shift
 
300
 
 
301
        case $_dialog_type in
 
302
        kdialog)
 
303
                kdialog --title "$descr" --getopenfilename "`pwd`/"
 
304
                ;;
 
305
 
 
306
        zenity)
 
307
                zenity --title "$descr" --file-selection "`pwd`/"
 
308
                ;;
 
309
 
 
310
        dialog)
 
311
                dialog --stdout --title "$descr" --fselect "`pwd`/" 10 70
 
312
                ;;
 
313
 
 
314
        builtin) # run in subshell to leave cwd alone for caller
 
315
                ( while :
 
316
                do
 
317
                        echo >&2
 
318
                        echo "$descr" >&2
 
319
                        echo "Current working directory: `pwd`" >&2
 
320
                        ls >&2
 
321
                        echo -n "Please enter file name, empty to abort > " >&2
 
322
                        read answ
 
323
                        if [ -z "$answ" ]; then
 
324
                                return 1
 
325
                        fi
 
326
                        if [ -f "$answ" ]; then
 
327
                                echo "`pwd`/$answ"
 
328
                                return 0
 
329
                        fi
 
330
                        if [ -d "$answ" ]; then
 
331
                                cd "$answ"
 
332
                                continue
 
333
                        fi
 
334
                        echo "That is not the name of a file. Try again..." >&2
 
335
                done )
 
336
                ;;
 
337
 
 
338
        *)      echo "$0: internal error: fall through case" >&2; exit 100
 
339
                ;;
 
340
        esac
 
341
 
 
342
        return $?
 
343
}
 
344
 
 
345
# dialog_msgbox -- Show message and wait for user to acknowledge
 
346
#       Parameters: descr text
 
347
#       Returns: nothing
 
348
#       Status: nothing
 
349
dialog_msgbox() {
 
350
        descr="$1"; shift
 
351
        text="$1"; shift
 
352
 
 
353
        case $_dialog_type in
 
354
        kdialog)
 
355
                kdialog --title "$descr" --msgbox "$text"
 
356
                ;;
 
357
 
 
358
        zenity)
 
359
                echo -n "$text" |
 
360
                zenity --title "$descr" --text-info --width=500 --height=400
 
361
                ;;
 
362
 
 
363
        dialog)
 
364
                dialog --title "$descr" --msgbox "$text" 20 80
 
365
                ;;
 
366
 
 
367
        builtin)
 
368
                echo >&2
 
369
                echo "$descr" >&2
 
370
                echo "$text" >&2
 
371
                echo -n "Hit <RETURN> to continue..." >&2
 
372
                read dummy
 
373
                ;;
 
374
 
 
375
        *)      echo "$0: internal error: fall through case" >&2; exit 100
 
376
                ;;
 
377
        esac
 
378
}
 
379
 
 
380
# dialog_question -- solicit a yes/no answer from the user
 
381
#       Parameters: descr question
 
382
#       Returns: nothing
 
383
#       Status: 0 for success / yes, 1 for failure / no
 
384
dialog_question() {
 
385
        descr="$1"; shift
 
386
        text="$1"; shift
 
387
 
 
388
        case $_dialog_type in
 
389
        kdialog)
 
390
                kdialog --title "$descr" --yesno "$text"
 
391
                ;;
 
392
 
 
393
        zenity)
 
394
                zenity --title "$descr" --question --text "$text"
 
395
                ;;
 
396
 
 
397
        dialog)
 
398
                dialog --title "$descr" --yesno "$text" 20 80
 
399
                ;;
 
400
 
 
401
        builtin)
 
402
                echo >&2
 
403
                echo "$descr" >&2
 
404
                echo -n "$text (y/N)> " >&2
 
405
                read answ
 
406
                case $answ in
 
407
                [Yy]*)  return 0;;
 
408
                *)      return 1;;
 
409
                esac
 
410
                ;;
 
411
 
 
412
        *)      echo "$0: internal error: fall through case" >&2; exit 100
 
413
                ;;
 
414
        esac
 
415
}
 
416
 
 
417
find_dialog