File Coverage

blib/lib/UI/Dialog/Backend/GDialog.pm
Criterion Covered Total %
statement 48 225 21.3
branch 5 64 7.8
condition 19 128 14.8
subroutine 8 19 42.1
pod 10 11 90.9
total 90 447 20.1


line stmt bran cond sub pod time code
1             package UI::Dialog::Backend::GDialog;
2             ###############################################################################
3             # Copyright (C) 2015 Kevin C. Krinke
4             #
5             # This library is free software; you can redistribute it and/or
6             # modify it under the terms of the GNU Lesser General Public
7             # License as published by the Free Software Foundation; either
8             # version 2.1 of the License, or (at your option) any later version.
9             #
10             # This library is distributed in the hope that it will be useful,
11             # but WITHOUT ANY WARRANTY; without even the implied warranty of
12             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13             # Lesser General Public License for more details.
14             #
15             # You should have received a copy of the GNU Lesser General Public
16             # License along with this library; if not, write to the Free Software
17             # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18             ###############################################################################
19 1     1   21779 use 5.006;
  1         5  
20 1     1   7 use strict;
  1         2  
  1         30  
21 1     1   734 use FileHandle;
  1         12176  
  1         6  
22 1     1   409 use Carp;
  1         1  
  1         51  
23 1     1   667 use UI::Dialog::Backend;
  1         3  
  1         47  
24              
25             BEGIN {
26 1     1   8 use vars qw( $VERSION @ISA );
  1         2  
  1         64  
27 1     1   12 @ISA = qw( UI::Dialog::Backend );
28 1         2503 $VERSION = '1.11';
29             }
30              
31             #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
32             #: Constructor Method
33             #:
34              
35             sub new {
36 1     1 1 831 my $proto = shift();
37 1   33     8 my $class = ref($proto) || $proto;
38 1 50       7 my $cfg = ((ref($_[0]) eq "HASH") ? $_[0] : (@_) ? { @_ } : {});
    50          
39 1         3 my $self = {};
40 1         3 bless($self, $class);
41 1         10 $self->{'_state'} = {};
42 1         3 $self->{'_opts'} = {};
43              
44             #: Dynamic path discovery...
45 1         3 my $CFG_PATH = $cfg->{'PATH'};
46 1 50       6 if ($CFG_PATH) {
    50          
47 0 0       0 if (ref($CFG_PATH) eq "ARRAY") {
    0          
    0          
48 0         0 $self->{'PATHS'} = $CFG_PATH;
49             }
50             elsif ($CFG_PATH =~ m!:!) {
51 0         0 $self->{'PATHS'} = [ split(/:/,$CFG_PATH) ];
52             }
53             elsif (-d $CFG_PATH) {
54 0         0 $self->{'PATHS'} = [ $CFG_PATH ];
55             }
56             }
57             elsif ($ENV{'PATH'}) {
58 1         8 $self->{'PATHS'} = [ split(/:/,$ENV{'PATH'}) ];
59             }
60             else {
61 0         0 $self->{'PATHS'} = '';
62             }
63              
64 1   50     30 $self->{'_opts'}->{'literal'} = $cfg->{'literal'} || 0;
65 1   50     9 $self->{'_opts'}->{'callbacks'} = $cfg->{'callbacks'} || undef();
66 1   50     7 $self->{'_opts'}->{'debug'} = $cfg->{'debug'} || undef();
67 1   50     7 $self->{'_opts'}->{'title'} = $cfg->{'title'} || undef();
68 1   50     8 $self->{'_opts'}->{'backtitle'} = $cfg->{'backtitle'} || undef();
69 1   50     15 $self->{'_opts'}->{'width'} = $cfg->{'width'} || 65;
70 1   50     6 $self->{'_opts'}->{'height'} = $cfg->{'height'} || 10;
71 1   50     8 $self->{'_opts'}->{'percentage'} = $cfg->{'percentage'} || 1;
72 1   50     12 $self->{'_opts'}->{'bin'} ||= $self->_find_bin('gdialog.real') || $self->_find_bin('gdialog') || '/usr/bin/gdialog';
      33        
73 1   50     8 $self->{'_opts'}->{'autoclear'} = $cfg->{'autoclear'} || 0;
74 1   50     8 $self->{'_opts'}->{'clearbefore'} = $cfg->{'clearbefore'} || 0;
75 1   50     6 $self->{'_opts'}->{'clearafter'} = $cfg->{'clearafter'} || 0;
76 1   50     13 $self->{'_opts'}->{'beepbin'} = $cfg->{'beepbin'} || $self->_find_bin('beep') || '/usr/bin/beep';
77 1   50     12 $self->{'_opts'}->{'beepbefore'} = $cfg->{'beepbefore'} || 0;
78 1   50     6 $self->{'_opts'}->{'beepafter'} = $cfg->{'beepafter'} || 0;
79 1   50     7 $self->{'_opts'}->{'timeout'} = $cfg->{'timeout'} || 0;
80 1   50     6 $self->{'_opts'}->{'wait'} = $cfg->{'wait'} || 0;
81 1 50       14 unless (-x $self->{'_opts'}->{'bin'}) {
82 1         348 croak("the gdialog binary could not be found at: ".$self->{'_opts'}->{'bin'});
83             }
84              
85 0   0       $self->{'_opts'}->{'trust-input'} = $cfg->{'trust-input'} || 0;
86              
87 0 0         $self->{'test_mode'} = $cfg->{'test_mode'} if exists $cfg->{'test_mode'};
88 0           $self->{'test_mode_result'} = '';
89              
90 0           return($self);
91             }
92              
93             #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
94             #: Private Methods
95             #:
96              
97             my $SIG_CODE = {};
98             sub _del_gauge {
99 0     0     my $CODE = $SIG_CODE->{$$};
100 0 0         unless (not ref($CODE)) {
101 0           delete($CODE->{'_GAUGE'});
102 0           $CODE->rv('1');
103 0           $CODE->rs('null');
104 0           $CODE->ra('null');
105 0           $SIG_CODE->{$$} = "";
106             }
107             }
108              
109             sub append_format_base {
110 0     0 0   my ($self,$args,$fmt) = @_;
111 0           return $fmt;
112             }
113              
114              
115             #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
116             #: Public Methods
117             #:
118              
119             #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
120             #: Ask a binary question (Yes/No)
121             sub yesno {
122 0     0 1   my $self = shift();
123 0   0       my $caller = (caller(1))[3] || 'main';
124 0 0 0       $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
125 0 0 0       if ($_[0] && $_[0] eq 'caller') {
126 0           shift(); $caller = shift();
  0            
127             }
128 0           my $args = $self->_pre($caller,@_);
129              
130 0           my $fmt = $self->prepare_format($args);
131 0           $fmt = $self->append_format_base($args,$fmt);
132 0           $fmt = $self->append_format($fmt,'--yesno {{text}} {{height}} {{width}}');
133             my $command = $self->prepare_command
134             ( $args, $fmt,
135 0           text => $self->make_kvt($args,$args->{'text'}),
136             );
137              
138 0           my $rv = $self->command_state($command);
139 0   0       $self->rv($rv||'null');
140 0           $self->ra('null');
141 0           my $this_rv;
142 0 0 0       if ($rv && $rv >= 1) {
143 0           $self->ra("NO");
144 0           $self->rs("NO");
145 0           $this_rv = 0;
146             }
147             else {
148 0           $self->ra("YES");
149 0           $self->rs("YES");
150 0           $this_rv = 1;
151             }
152 0           $self->_post($args);
153 0           return($this_rv);
154             }
155              
156             #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
157             #: Text entry
158             sub inputbox {
159 0     0 1   my $self = shift();
160 0   0       my $caller = (caller(1))[3] || 'main';
161 0 0 0       $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
162 0 0 0       if ($_[0] && $_[0] eq 'caller') {
163 0           shift(); $caller = shift();
  0            
164             }
165 0           my $args = $self->_pre($caller,@_);
166              
167 0           my $fmt = $self->prepare_format($args);
168 0           $fmt = $self->append_format_base($args,$fmt);
169 0           $fmt = $self->append_format($fmt,'--inputbox');
170 0           $fmt = $self->append_format($fmt,'{{text}} {{height}} {{width}} {{entry}}');
171             my $command = $self->prepare_command
172             ( $args, $fmt,
173             text => $self->make_kvt($args,$args->{'text'}),
174 0   0       entry => $self->make_kvl($args,($args->{'init'}||$args->{'entry'})),
175             );
176              
177 0           my ($rv,$text) = $self->command_string($command);
178 0   0       $self->rv($rv||'null');
179 0           $self->ra('null');
180 0           my $this_rv;
181 0 0 0       if ($rv && $rv >= 1) {
182 0           $self->rs('null');
183 0           $this_rv = 0;
184             }
185             else {
186 0           $self->ra($text);
187 0           $self->rs($text);
188 0           $this_rv = $text;
189             }
190 0           $self->_post($args);
191 0           return($this_rv);
192             }
193             #: password boxes aren't supported by gdialog
194             sub password {
195 0     0 1   my $self = shift();
196 0           $self->msgbox(text=> 'GDialog does not support passwords at all, '.
197             'you will see the text as you type in the next dialog.' );
198 0   0       return($self->inputbox('caller',((caller(1))[3]||'main'),@_));
199             }
200              
201             #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
202             #: Text box
203             sub msgbox {
204 0     0 1   my $self = shift();
205 0   0       my $caller = (caller(1))[3] || 'main';
206 0 0 0       $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
207 0 0 0       if ($_[0] && $_[0] eq 'caller') {
208 0           shift(); $caller = shift();
  0            
209             }
210 0           my $args = $self->_pre($caller,@_);
211              
212 0           my $fmt = $self->prepare_format($args);
213 0           $fmt = $self->append_format_base($args,$fmt);
214 0           $fmt = $self->append_format($fmt,'--scrolltext');
215 0 0         if ($args->{'infobox'}) {
216 0           $fmt = $self->append_format($fmt,'--infobox');
217             }
218             else {
219 0           $fmt = $self->append_format($fmt,'--msgbox');
220             }
221 0           $fmt = $self->append_format($fmt,'{{text}} {{height}} {{width}}');
222             my $command = $self->prepare_command
223             ( $args, $fmt,
224 0           text => $self->make_kvt($args,$args->{'text'}),
225             );
226              
227 0           my $rv = $self->command_state($command);
228 0   0       $self->rv($rv||'null');
229 0           $self->ra('null');
230 0           $self->rs('null');
231 0           my $this_rv;
232 0 0 0       if ($rv && $rv >= 1) {
233 0           $this_rv = 0;
234             }
235             else {
236 0           $this_rv = 1;
237             }
238 0           $self->_post($args);
239 0           return($this_rv);
240             }
241             sub infobox {
242 0     0 1   my $self = shift();
243 0   0       return($self->msgbox('caller',((caller(1))[3]||'main'),@_,'infobox',1));
244             }
245              
246             #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
247             #: File box
248             sub textbox {
249 0     0 1   my $self = shift();
250 0   0       my $caller = (caller(1))[3] || 'main';
251 0 0 0       $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
252 0 0 0       if ($_[0] && $_[0] eq 'caller') {
253 0           shift(); $caller = shift();
  0            
254             }
255 0           my $args = $self->_pre($caller,@_);
256              
257 0           my $fmt = $self->prepare_format($args);
258 0           $fmt = $self->append_format_base($args,$fmt);
259 0           $fmt = $self->append_format($fmt,'--textbox');
260 0           $fmt = $self->append_format($fmt,'{{path}} {{height}} {{width}}');
261             my $command = $self->prepare_command
262             ( $args, $fmt,
263 0   0       path => $self->make_kvl($args,($args->{'path'}||'.')),
264             );
265              
266 0           my ($rv,$text) = $self->command_string($command);
267 0   0       $self->rv($rv||'null');
268 0           $self->ra('null');
269 0           my $this_rv;
270 0 0 0       if ($rv && $rv >= 1) {
271 0           $self->rs('null');
272 0           $this_rv = 0;
273             }
274             else {
275 0           $self->ra($text);
276 0           $self->rs($text);
277 0           $this_rv = $text;
278             }
279 0           $self->_post($args);
280 0           return($this_rv);
281             }
282              
283             #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
284             #: a simple menu list
285             sub menu {
286 0     0 1   my $self = shift();
287 0   0       my $caller = (caller(1))[3] || 'main';
288 0 0 0       $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
289 0 0 0       if ($_[0] && $_[0] eq 'caller') {
290 0           shift(); $caller = shift();
  0            
291             }
292 0           my $args = $self->_pre($caller,@_);
293              
294 0   0       $args->{'listheight'} ||= $args->{'menuheight'};
295              
296 0           my $fmt = $self->prepare_format($args);
297 0           $fmt = $self->append_format_base($args,$fmt);
298 0           $fmt = $self->append_format($fmt,'--menu');
299 0           $fmt = $self->append_format($fmt,'{{text}} {{height}} {{width}} {{listheight}} {{list}}');
300             my $command = $self->prepare_command
301             ( $args, $fmt,
302 0           text => $self->make_kvt($args,$args->{'text'}),
303             );
304              
305 0           my ($rv,$selected) = $self->command_string($command);
306 0   0       $self->rv($rv||'null');
307 0           $self->ra('null');
308 0           my $this_rv;
309 0 0 0       if ($rv && $rv >= 1) {
310 0           $self->rs('null');
311 0           $this_rv = 0;
312             }
313             else {
314 0           $self->ra($selected);
315 0           $self->rs($selected);
316 0           $this_rv = $selected;
317             }
318 0           $self->_post($args);
319 0           return($this_rv);
320             }
321              
322             #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
323             #: a check list
324             sub checklist {
325 0     0 1   my $self = shift();
326 0   0       my $caller = (caller(1))[3] || 'main';
327 0 0 0       $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
328 0 0 0       if ($_[0] && $_[0] eq 'caller') {
329 0           shift(); $caller = shift();
  0            
330             }
331 0           my $args = $self->_pre($caller,@_);
332              
333             $args->{'listheight'} = $args->{'menuheight'}
334 0 0         if exists $args->{'menuheight'};
335              
336 0           my $fmt = $self->prepare_format($args);
337 0           $fmt = $self->append_format_base($args,$fmt);
338 0           $fmt = $self->append_format($fmt,'--separate-output');
339 0 0         if ($args->{radiolist} == 1) {
340 0           $fmt = $self->append_format($fmt,'--radiolist');
341             }
342             else {
343 0           $fmt = $self->append_format($fmt,'--checklist');
344             }
345 0           $fmt = $self->append_format($fmt,'{{text}} {{height}} {{width}} {{listheight}} {{list}}');
346             my $command = $self->prepare_command
347             ( $args, $fmt,
348             text => $self->make_kvt($args,$args->{'text'}),
349 0           listheight => $self->make_kvl($args,$args->{'listheight'})
350             );
351              
352 0           my ($rv,$selected) = $self->command_array($command);
353 0   0       $self->rv($rv||'null');
354 0           $self->rs('null');
355 0           my $this_rv;
356 0 0 0       if ($rv && $rv >= 1) {
357 0           $self->ra('null');
358 0           $this_rv = 0;
359             }
360             else {
361 0           $self->rs(join("\n",@$selected));
362 0           $self->ra(@$selected);
363 0           $this_rv = $selected;
364             }
365 0           $self->_post($args);
366 0 0         return($this_rv) unless ref($this_rv) eq "ARRAY";
367 0           return(@{$this_rv});
  0            
368             }
369             #: a radio button list
370             sub radiolist {
371 0     0 1   my $self = shift();
372 0   0       return($self->checklist('caller',((caller(1))[3]||'main'),@_,'radiolist',1));
373             }
374              
375             1;
376