~ubuntu-branches/ubuntu/hoary/zenity/hoary

« back to all changes in this revision

Viewing changes to src/gdialog

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-03-07 17:03:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050307170331-j0667q5o1ni6308d
Tags: upstream-2.10.0
ImportĀ upstreamĀ versionĀ 2.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
# gdialog -> zenity conversion wrapper
 
4
#
 
5
# by Mike Newman <mikegtn@gnome.org>
 
6
#
 
7
# This is all, of course, horrible - but it should translate
 
8
# most commond gdialog types to zenity equivalents. It will mostly drop
 
9
# the pointless and unused (even by gdialog!) size arguments
 
10
# but hopefully will translate all the others.
 
11
#
 
12
# For testing purposes, I've used a couple of the nautilus scripts
 
13
# available at http://g-scripts.sourceforge.net - what is sometimes
 
14
# unclear is what is a gdialog/zenity translation problem, and what is
 
15
# a problem with the original script
 
16
 
 
17
my @command = ("zenity");       # the command line we build up to execute
 
18
my $element = "";               # current bit of command line
 
19
my $argn = 0;                   # counter for walking args
 
20
my $args = $#ARGV + 1;          # total number of command line arguments
 
21
my $separator = 0;              # set if --separate-output is in use
 
22
 
 
23
 
 
24
# Additon by: Kevin C. Krinke (kck) <kckrinke@opendoorsoftware.com>
 
25
#
 
26
# gdialog itself supports both the X-Windows interface as well as a console
 
27
# interface. Here's a fix to use regular dialog when appropriate.
 
28
# This should probably be a more advanced test of some sort, but I don't know
 
29
# of any other easy way of detecting and X-Windows environment. If someone does
 
30
# know better, please let me know. So for now this works: "no DISPLAY; no X".
 
31
 
 
32
unless (defined $ENV{'DISPLAY'} && length($ENV{'DISPLAY'})) {
 
33
 
 
34
        # reset the command string
 
35
 
 
36
        @command = ();
 
37
 
 
38
        # examine all the available/default paths
 
39
 
 
40
        my $PATHS = ($ENV{'PATH'}||'/bin:/usr/bin:/usr/local/bin:/opt/bin');
 
41
 
 
42
      BIN: foreach my $PATH (split(/\:/,$PATHS)) {
 
43
 
 
44
                if (-x $PATH."/gdialog.real") {
 
45
 
 
46
                        # Some GNU/Linux distributions divert binaries when
 
47
                        # other packages are installed. If this exists, chances
 
48
                        # are it's the real gdialog and not the Zenity wrapper.
 
49
                        # gdialog has full support for the Console medium and
 
50
                        # as such is the preference over using the "regular"
 
51
                        # dialog interface.
 
52
 
 
53
                        @command = ($PATH."/gdialog.real");
 
54
                        last BIN;
 
55
 
 
56
                } elsif (-x $PATH."/dialog") {
 
57
 
 
58
                        # change the command and skip ahead!
 
59
 
 
60
                        @command = ($PATH."/dialog");
 
61
                        last BIN;
 
62
 
 
63
                }
 
64
 
 
65
 
 
66
        }
 
67
 
 
68
        unless (@command) {
 
69
 
 
70
                # we didn't find the dialog binary, exit(254) with a message
 
71
                # to STDERR.
 
72
 
 
73
                print STDERR "missing DISPLAY and a console dialog could".
 
74
                             " not be found.\n";
 
75
 
 
76
                # exit code 254 is used because 255, 1, 2, 3 are used by Zenity
 
77
                # and cDialog. This error, is a very _bad_ error so it's semi-
 
78
                # non-standard at 254.
 
79
 
 
80
                exit(254);
 
81
 
 
82
        }
 
83
 
 
84
        # all is well if we've made it this far
 
85
 
 
86
        # so join the arguments double-quoting things so that proper shell
 
87
        # notation is saved.
 
88
 
 
89
        push @command, @ARGV;
 
90
 
 
91
        # and fork the process
 
92
 
 
93
        exec(@command);
 
94
 
 
95
}
 
96
 
 
97
# Got DISPLAY, has X continue as normal...
 
98
# End Addtition by: KCK
 
99
 
 
100
# this just loads the current arg into $element
 
101
 
 
102
sub get_arg () { 
 
103
        $element = $ARGV[$argn];
 
104
}
 
105
 
 
106
# walk the command line
 
107
 
 
108
ARG: while ($argn < $args) {
 
109
 
 
110
        get_arg;
 
111
        
 
112
# Informational stuff
 
113
 
 
114
        if ($element eq "--help" || $element eq "--about") {
 
115
        print ( "gdialog is a compatibility wrapper around zenity, " .
 
116
                "provided to hopefully\nallow older scripts to run. " .
 
117
                "If you are reading this message, you should\n" .
 
118
                "probably be using zenity directly\n\n" .
 
119
                "type: 'zenity --help' or 'man zenity' for more information\n");
 
120
        exit (1);
 
121
        }
 
122
 
 
123
# Section 1 : Args which gdialog expects BEFORE box options
 
124
# --clear, --backtitle have no obvious effect - ignored
 
125
 
 
126
        if ($element eq "--title") {
 
127
        
 
128
                # --title argument is almost analogous in gdialog and
 
129
                # zenity - so pass it almost entirely as is
 
130
                
 
131
                $argn++;
 
132
                get_arg;
 
133
                push @command, "--title=$element";
 
134
                
 
135
                # keep processing args
 
136
                $argn++;
 
137
                next ARG;
 
138
        }
 
139
 
 
140
        if ($element eq "--separate-output") {
 
141
 
 
142
                # set the flag to pring list output line by line
 
143
                $separator = 1;
 
144
 
 
145
                # keep processing args
 
146
                $argn++;
 
147
                next ARG;
 
148
        }
 
149
 
 
150
# Section 2 : Box Options and subsequent args
 
151
        
 
152
        if ($element eq "--msgbox" || $element eq "--infobox") {
 
153
        
 
154
                # This bit is common to almost all of the dialogs
 
155
                # the arg following the dialog type in gdialog is usually
 
156
                # equivalent to zenity's --text arg.
 
157
                
 
158
                $argn++;
 
159
                get_arg;
 
160
                push @command, "--info", "--text=$element";
 
161
                
 
162
                # this also happens a lot - gdialog accepted size args
 
163
                # for dialog compatability - which it pretty much ignored
 
164
                # and we will do the same
 
165
 
 
166
                $argn+=2;
 
167
                last ARG;
 
168
        }
 
169
        
 
170
        if ($element eq "--yesno") {
 
171
 
 
172
                # this will silently ignore the gdialog option to set
 
173
                # the default button in question dialogs - which is
 
174
                # highly hig-norant anyway!
 
175
                
 
176
                $argn++;
 
177
                get_arg;
 
178
                push @command, "--question", "--text=$element";
 
179
                last ARG;
 
180
        }
 
181
        
 
182
        if ($element eq "--inputbox") {
 
183
                $argn++;
 
184
                get_arg;
 
185
                push @command, "--entry", "--text=$element";
 
186
                
 
187
                # ignore size elements and maybe there is some
 
188
                # default text to initialize the entry with?
 
189
                
 
190
                $argn+=3;
 
191
                get_arg;
 
192
                push @command, "--entry-text=$element";
 
193
                last ARG;
 
194
        }
 
195
        
 
196
        if ($element eq "--textbox") {
 
197
                push @command, "--text-info";
 
198
                
 
199
                # the arg immediately following the dialog type in
 
200
                # gdialog is the filename, so pass this to zenity
 
201
                
 
202
                $argn++;
 
203
                get_arg;
 
204
                push @command, "--filename=$element";
 
205
 
 
206
                # width and height matter for this one, so get them
 
207
                # and apply the same multipliers as used in gdialog
 
208
 
 
209
                $argn++;
 
210
                get_arg;
 
211
                $element = $element * 7;
 
212
                push @command, "--height=$element";
 
213
                $argn++;
 
214
                get_arg;
 
215
                $element = $element * 8;
 
216
                push @command, "--width=$element";
 
217
                last ARG;
 
218
        }
 
219
        
 
220
        if ($element eq "--checklist" || $element eq "--radiolist") {
 
221
                $list=$element;
 
222
                $argn++;
 
223
                get_arg;
 
224
                
 
225
                # Conveniently, zenity and gdialog use the same names
 
226
                # for list types, so pass this to zenity intact along with
 
227
                # an untitled column for the check or radio buttons
 
228
                # and the 'text' arg as a second column header
 
229
                
 
230
                push @command, "--list", $list, "--column=''", "--column=''", "--column", $element;
 
231
 
 
232
                # should output be line by line?
 
233
                if ($separator) {
 
234
                        push @command, "--separator=\n";
 
235
                }
 
236
 
 
237
                # Skip to the first 'item' arg of the list content
 
238
                # bypassing height, width and list-height
 
239
                # from here args run [tag] [item] [status] ...
 
240
 
 
241
                $argn += 4; 
 
242
                
 
243
                # Loop over the remainder of the commandline
 
244
                # discarding the 'status' args of each item
 
245
                # and using the 'item' for display in our second column
 
246
                # also pass a fake NULL argument since zenity can't set
 
247
                # the status of a row like gdialog can
 
248
                
 
249
                while ($argn < $args) {
 
250
                        get_arg;
 
251
                        push @command, "NULL", $element;
 
252
                        $argn += 1;
 
253
                        get_arg;
 
254
                        push @command, $element;
 
255
                        $argn += 2;
 
256
                }
 
257
                last ARG;
 
258
        } 
 
259
        
 
260
        if ($element eq "--menu") {
 
261
                $list=$element;
 
262
                $argn++;
 
263
                get_arg;
 
264
                
 
265
                # a gdialog --menu is just a two column zenity --list
 
266
                # Leave the first column blank (not provided)
 
267
                # Use the 'text' arg as a second column header
 
268
                # FIXME: or should it be the dialog text, or both?
 
269
                
 
270
                push @command, "--list", "--column", "", "--column", $element;
 
271
                        
 
272
                # Skip to the first 'item' arg of the list content
 
273
                # after using height, width and bypassing list-height
 
274
                # from here args run [tag] [item] ...
 
275
 
 
276
                $argn += 1;
 
277
                
 
278
                get_arg;
 
279
                # Height and width in characters to be displayed, so adjust
 
280
                # cdialog uses 6 height for non-list, zenity uses ~24 pixels
 
281
                # per list entry (default font), and 103 pixels for non-list
 
282
                # This appears to be almost exact
 
283
                $element = $element*24 - 35;
 
284
                push @command, "--height", $element;
 
285
                
 
286
                $argn += 1;
 
287
                get_arg;
 
288
                # cdialog uses 6 width for non-list, zenity uses ~7 pixels
 
289
                # per character (default font), and 22 pixels for non-list
 
290
                # This is not exact, but close enough
 
291
                $element = $element*7 - 20;
 
292
                push @command, "--width", $element;
 
293
                
 
294
                $argn += 2; 
 
295
                
 
296
                # Loop over the remainder of the commandline
 
297
                # keeping 'tag' args of each item (required to return)
 
298
                # and using the 'item' for display in our second column
 
299
                
 
300
                while ($argn < $args) {
 
301
                        get_arg;
 
302
                        push @command, $element;
 
303
                        $argn += 1;
 
304
                }
 
305
                last ARG;
 
306
        } 
 
307
        
 
308
        if ($element eq "--gauge") {
 
309
                $argn++;
 
310
                get_arg;
 
311
                push @command, "--progress", "--text=$element";
 
312
                
 
313
                # discard the size args as usually, and see if
 
314
                # a percentage value was supplied to initialize the
 
315
                # dialog
 
316
                
 
317
                $argn += 3;
 
318
                get_arg;
 
319
                if ($element) {
 
320
                        push @command, "--percentage=$element";
 
321
                }
 
322
                last ARG;
 
323
        }
 
324
        
 
325
        $argn++;
 
326
}
 
327
 
 
328
# save STDOUT and STDERR
 
329
open(ORG_STDOUT, ">&STDOUT");
 
330
open(ORG_STDERR, ">&STDERR");
 
331
 
 
332
# redirect STDERR to /dev/null (GTK messages ie: 
 
333
#  (zenity:637): Gtk-WARNING **: Unable to locate theme engine in module_path: "mist",)
 
334
open(STDERR, ">/dev/null");
 
335
 
 
336
# redirect STDOUT to STDERR (gdialog direct output to STDERR by default)
 
337
open(STDOUT, ">&ORG_STDERR");
 
338
 
 
339
# execute the constructed zenity command line
 
340
 
 
341
# perl doc: The return value of system() is the exit status of the
 
342
#program as returned by the wait() call. To get the actual exit value
 
343
# divide by 256.
 
344
 
 
345
my $return = system(@command)/256;
 
346
 
 
347
# restore STDOUT and STDERR
 
348
open(STDOUT, ">&ORG_STDOUT");
 
349
open(STDERR, ">&ORG_STDERR");
 
350
close(ORG_STDOUT);
 
351
close(ORG_STDERR);
 
352
 
 
353
exit $return;