summaryrefslogtreecommitdiff
path: root/src/gdialog.in
blob: a27c0e9ade76bd0eaeac5b8a9fe437a4e763d305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!@PERL@

# gdialog -> zenity conversion wrapper
#
# by Mike Newman <mikegtn@gnome.org>
#
# This is all, of course, horrible - but it should translate
# most commond gdialog types to zenity equivalents. It will mostly drop
# the pointless and unused (even by gdialog!) size arguments
# but hopefully will translate all the others.
#
# For testing purposes, I've used a couple of the nautilus scripts
# available at http://g-scripts.sourceforge.net - what is sometimes
# unclear is what is a gdialog/zenity translation problem, and what is
# a problem with the original script

my $command = "zenity ";	# the command line we build up to execute
my $element = "";		# current bit of command line
my $argn = 0;			# counter for walking args
my $args = $#ARGV + 1;		# total number of command line arguments
my $separator = 0;		# set if --separate-output is in use

# this just loads the current arg into $element

sub get_arg () { 
	$element = $ARGV[$argn];
}

# walk the command line

ARG: while ($argn < $args) {

	get_arg;
	
# Informational stuff

	if ($element eq "--help" || $element eq "--about") {
	print ( "gdialog is a compatibility wrapper around zenity, " .
		"provided to hopefully\nallow older scripts to run. " .
		"If you are reading this message, you should\n" .
		"probably be using zenity directly\n\n" .
		"type: 'zenity --help' or 'man zenity' for more information\n");
	exit (1);
	}

# Section 1 : Args which gdialog expects BEFORE box options
# --clear, --backtitle have no obvious effect - ignored

	if ($element eq "--title") {
	
		# --title argument is almost analogous in gdialog and
		# zenity - so pass it almost entirely as is
		
		$argn++;
		get_arg;
		$command .= "--title=\"$element\" ";
		
		# keep processing args
		$argn++;
		next ARG;
	}

	if ($element eq "--separate-output") {

		# set the flag to pring list output line by line
		$separator = 1;

		# keep processing args
		$argn++;
		next ARG;
	}

# Section 2 : Box Options and subsequent args
	
	if ($element eq "--msgbox" || $element eq "--infobox") {
	
		# This bit is common to almost all of the dialogs
		# the arg following the dialog type in gdialog is usually
		# equivalent to zenity's --text arg.
		
		$argn++;
		get_arg;
		$command .= "--info --text=\"$element\" ";
		
		# this also happens a lot - gdialog accepted size args
		# for dialog compatability - which it pretty much ignored
		# and we will do the same
 
		$argn+=2;
		last ARG;
	}
	
	if ($element eq "--yesno") {

		# this will silently ignore the gdialog option to set
		# the default button in question dialogs - which is
		# highly hig-norant anyway!
		
		$argn++;
		get_arg;
		$command .= "--question --text=\"$element\" ";
		last ARG;
	}
	
	if ($element eq "--inputbox") {
		$argn++;
		get_arg;
		$command .= "--entry --text=\"$element\" ";
		
		# ignore size elements and maybe there is some
		# default text to initialize the entry with?
		
		$argn+=3;
		get_arg;
		$command .= "--entry-text=\"$element\" ";
		last ARG;
	}
	
	if ($element eq "--textbox") {
		$command .= "--text-info ";
		
		# the arg immediately following the dialog type in
		# gdialog is the filename, so pass this to zenity
		
		$argn++;
		get_arg;
		$command .= "--filename=\"$element\" ";

		# width and height matter for this one, so get them
		# and apply the same multipliers as used in gdialog

		$argn++;
		get_arg;
		$element = $element * 7;
		$command .= "--height=\"$element\" ";
		$argn++;
		get_arg;
		$element = $element * 8;
		$command .= "--width=\"$element\" ";
		last ARG;
	}
	
	if ($element eq "--checklist" || $element eq "--radiolist") {
		$list=$element;
		$argn++;
		get_arg;
		
		# Conveniently, zenity and gdialog use the same names
		# for list types, so pass this to zenity intact along with
		# an untitled column for the check or radio buttons
		# and the 'text' arg as a second column header
		
		$command .= "--list $list --column='' --column $element ";
		
		# should output be line by line?
		if ($separator) {
			$command .= " --separator='\n' ";
		}

		# Skip to the first 'item' arg of the list content
		# bypassing height, width and list-height
		# from here args run [tag] [item] [status] ...

		$argn += 5; 
		
		# Loop over the remainder of the commandline
		# discarding the 'status' and 'tag' args of each item 
		# and using the 'item' for display in our second column
		# also pass a fake NULL argument since zenity can't set
		# the status of a row like gdialog can
		
		while ($argn < $args) {
			get_arg;
			$command .= "NULL $element ";
			$argn += 3;
		}
		last ARG;
	} 
	
	if ($element eq "--menu") {
		$list=$element;
		$argn++;
		get_arg;
		
		# a gdialog --menu is just a one column zenity --list
		# Use the 'text' arg as a second column header
		# FIXME: or should it be the dialog text, or both?
		
		$command .= "--list --column $element ";
			
		# Skip to the first 'item' arg of the list content
		# bypassing height, width and list-height
		# from here args run [tag] [item] ...

		$argn += 5; 
		
		# Loop over the remainder of the commandline
		# discarding the 'tag' args of each item 
		# and using the 'item' for display in our second column
		
		while ($argn < $args) {
			get_arg;
			$command .= "$element ";
			$argn += 2;
		}
		last ARG;
	} 
	
	if ($element eq "--gauge") {
		$argn++;
		get_arg;
		$command .= "--progress --text=\"$element\" ";
		
		# discard the size args as usually, and see if
		# a percentage value was supplied to initialize the
		# dialog
		
		$argn += 3;
		get_arg;
		if ($element) {
			$command .= "--percentage=$element ";
		}
		last ARG;
	}
	
	$argn++;
}

# execute the constructed zenity command line
$command .= " 2>&1"; 
system($command);