File Coverage

blib/lib/UI/Dialog/Backend/KDialog.pm
Criterion Covered Total %
statement 52 224 23.2
branch 5 74 6.7
condition 19 144 13.1
subroutine 9 32 28.1
pod 19 24 79.1
total 104 498 20.8


line stmt bran cond sub pod time code
1             package UI::Dialog::Backend::KDialog;
2             ###############################################################################
3             # Copyright (C) 2004-2016 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 2     2   13171 use 5.006;
  2         4  
20 2     2   6 use strict;
  2         3  
  2         28  
21 2     2   6 use warnings;
  2         1  
  2         39  
22 2     2   6 use Carp;
  2         3  
  2         131  
23 2     2   9 use Cwd qw( abs_path );
  2         2  
  2         69  
24 2     2   762 use UI::Dialog::Backend;
  2         3  
  2         66  
25              
26             BEGIN {
27 2     2   10 use vars qw( $VERSION @ISA );
  2         2  
  2         90  
28 2     2   13 @ISA = qw( UI::Dialog::Backend );
29 2         3628 $VERSION = '1.21';
30             }
31              
32             #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
33             #: Constructor Method
34             #:
35              
36             sub new {
37 1     1 1 326 my $proto = shift();
38 1   33     6 my $class = ref($proto) || $proto;
39 1 50       5 my $cfg = ((ref($_[0]) eq "HASH") ? $_[0] : (@_) ? { @_ } : {});
    50          
40 1         1 my $self = {};
41 1         2 bless($self, $class);
42 1         6 $self->{'_state'} = {};
43 1         2 $self->{'_opts'} = {};
44              
45             #: Dynamic path discovery...
46 1         1 my $CFG_PATH = $cfg->{'PATH'};
47 1 50       4 if ($CFG_PATH) {
    50          
48 0 0       0 if (ref($CFG_PATH) eq "ARRAY") {
    0          
    0          
49 0         0 $self->{'PATHS'} = $CFG_PATH;
50             }
51             elsif ($CFG_PATH =~ m!:!) {
52 0         0 $self->{'PATHS'} = [ split(/:/,$CFG_PATH) ];
53             }
54             elsif (-d $CFG_PATH) {
55 0         0 $self->{'PATHS'} = [ $CFG_PATH ];
56             }
57             }
58             elsif ($ENV{'PATH'}) {
59 1         6 $self->{'PATHS'} = [ split(/:/,$ENV{'PATH'}) ];
60             }
61             else {
62 0         0 $self->{'PATHS'} = '';
63             }
64              
65 1   50     5 $self->{'_opts'}->{'literal'} = $cfg->{'literal'} || 0;
66 1   50     6 $self->{'_opts'}->{'callbacks'} = $cfg->{'callbacks'} || undef();
67 1   50     4 $self->{'_opts'}->{'debug'} = $cfg->{'debug'} || undef();
68 1   50     4 $self->{'_opts'}->{'caption'} = $cfg->{'caption'} || undef();
69 1   50     5 $self->{'_opts'}->{'icon'} = $cfg->{'icon'} || undef();
70 1   50     4 $self->{'_opts'}->{'miniicon'} = $cfg->{'miniicon'} || undef();
71 1   50     4 $self->{'_opts'}->{'title'} = $cfg->{'title'} || undef();
72 1   50     4 $self->{'_opts'}->{'width'} = $cfg->{'width'} || 65;
73 1   50     4 $self->{'_opts'}->{'height'} = $cfg->{'height'} || 10;
74 1   33     6 $self->{'_opts'}->{'bin'} ||= $self->_find_bin('kdialog');
75 1   50     6 $self->{'_opts'}->{'autoclear'} = $cfg->{'autoclear'} || 0;
76 1   50     4 $self->{'_opts'}->{'clearbefore'} = $cfg->{'clearbefore'} || 0;
77 1   50     6 $self->{'_opts'}->{'clearafter'} = $cfg->{'clearafter'} || 0;
78 1   50     9 $self->{'_opts'}->{'beepbin'} = $cfg->{'beepbin'} || $self->_find_bin('beep') || '/usr/bin/beep';
79 1   50     4 $self->{'_opts'}->{'beepbefore'} = $cfg->{'beepbefore'} || 0;
80 1   50     9 $self->{'_opts'}->{'beepafter'} = $cfg->{'beepafter'} || 0;
81 1   50     4 $self->{'_opts'}->{'timeout'} = $cfg->{'timeout'} || 0;
82 1   50     4 $self->{'_opts'}->{'wait'} = $cfg->{'wait'} || 0;
83 1 50       5 unless (-x $self->{'_opts'}->{'bin'}) {
84 1         159 croak("the kdialog binary could not be found at: ".$self->{'_opts'}->{'bin'});
85             }
86              
87 0   0       $self->{'_opts'}->{'trust-input'} = $cfg->{'trust-input'} || 0;
88              
89 0 0         $self->{'test_mode'} = $cfg->{'test_mode'} if exists $cfg->{'test_mode'};
90 0           $self->{'test_mode_result'} = '';
91              
92 0           return($self);
93             }
94              
95             #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
96             #: Private Methods
97             #:
98              
99             sub append_format_base {
100 0     0 0   my ($self,$args,$fmt) = @_;
101 0           $fmt = $self->append_format_check($args,$fmt,'caption','--caption {{caption}}');
102 0           $fmt = $self->append_format_check($args,$fmt,'icon','--icon {{icon}}');
103 0           $fmt = $self->append_format_check($args,$fmt,'miniicon','--miniicon {{miniicon}}');
104 0 0         if ($self->{'_opts'}->{'force-no-separate-output'}) {
105 0           delete $args->{'separate-output'};
106             }
107             else {
108 0           $fmt = $self->append_format_check($args,$fmt,"separate-output","--separate-output");
109             }
110 0           return $fmt;
111             }
112              
113             #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
114             #: Public Methods
115             #:
116              
117              
118             #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
119             #: Ask a binary question (Yes/No)
120             sub yesno {
121 0     0 1   my $self = shift();
122 0   0       my $caller = (caller(1))[3] || 'main';
123 0 0 0       $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
124 0 0 0       if ($_[0] && $_[0] eq 'caller') {
125 0           shift(); $caller = shift();
  0            
126             }
127 0           my $args = $self->_pre($caller,@_);
128              
129 0   0       $args->{'yesno'} ||= "yesno";
130              
131 0           my $fmt = $self->prepare_format($args);
132 0           $fmt = $self->append_format_base($args,$fmt);
133 0           $fmt = $self->append_format($fmt,'--'.$args->{'yesno'}.' {{text}} {{height}} {{width}}');
134             my $command = $self->prepare_command
135             ( $args, $fmt,
136 0           text => $self->make_kvt($args,$args->{'text'}),
137             );
138              
139 0           my $rv = $self->command_state($command);
140 0 0 0       if ($rv && $rv >= 1) {
141 0           $self->ra("NO");
142 0           $self->rs("NO");
143 0           $self->rv($rv);
144             } else {
145 0           $self->ra("YES");
146 0           $self->rs("YES");
147 0           $self->rv('null');
148             }
149 0           $self->_post($args);
150 0 0         return($rv == 0 ? 1 : 0);
151             }
152             sub yesnocancel {
153 0     0 1   my $self = shift();
154 0   0       return($self->yesno('caller',((caller(1))[3]||'main'),@_,'yesno','yesnocancel'));
155             }
156             sub warningyesno {
157 0     0 1   my $self = shift();
158 0   0       return($self->yesno('caller',((caller(1))[3]||'main'),@_,'yesno','warningyesno'));
159             }
160             sub warningyesnocancel {
161 0     0 1   my $self = shift();
162 0   0       return($self->yesno('caller',((caller(1))[3]||'main'),@_,'yesno','warningyesnocancel'));
163             }
164             #: Broken documented "feature"
165             # sub warningcontinuecancel {
166             # my $self = shift();
167             # return($self->yesno(@_,'yesno','warningcontinuecancel'));
168             # }
169             sub noyes {
170 0     0 0   my $self = shift();
171 0   0       return($self->yesno('caller',((caller(1))[3]||'main'),@_));
172             }
173              
174             #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
175             #: Text entry
176             sub inputbox {
177 0     0 1   my $self = shift();
178 0   0       my $caller = (caller(1))[3] || 'main';
179 0 0 0       $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
180 0 0 0       if ($_[0] && $_[0] eq 'caller') {
181 0           shift(); $caller = shift();
  0            
182             }
183 0           my $args = $self->_pre($caller,@_);
184              
185 0   0       $args->{'inputbox'} ||= 'inputbox';
186              
187 0           my $fmt = $self->prepare_format($args);
188 0           $fmt = $self->append_format_base($args,$fmt);
189 0           $fmt = $self->append_format($fmt,'--'.$args->{'inputbox'}.' {{text}} {{entry}}');
190             my $command = $self->prepare_command
191             ( $args, $fmt,
192             text => $self->make_kvt($args,$args->{'text'}),
193 0   0       entry => $self->make_kvl($args,($args->{'init'}||$args->{'entry'})),
194             );
195              
196 0           my ($rv,$text) = $self->command_string($command);
197 0           $self->_post($args);
198 0 0         return($rv == 0 ? $text : 0);
199             }
200             sub password {
201 0     0 1   my $self = shift();
202 0   0       return($self->inputbox('caller',((caller(1))[3]||'main'),@_,'inputbox','password'));
203             }
204              
205             #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
206             #: Text box
207             sub msgbox {
208 0     0 1   my $self = shift();
209 0   0       my $caller = (caller(1))[3] || 'main';
210 0 0 0       $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
211 0 0 0       if ($_[0] && $_[0] eq 'caller') {
212 0           shift(); $caller = shift();
  0            
213             }
214 0           my $args = $self->_pre($caller,@_);
215              
216 0   0       $args->{'msgbox'} ||= 'msgbox';
217              
218 0           my $fmt = $self->prepare_format($args);
219 0           $fmt = $self->append_format_base($args,$fmt);
220 0           $fmt = $self->append_format($fmt,'--'.$args->{'msgbox'}.' {{text}}');
221             my $command = $self->prepare_command
222             ( $args, $fmt,
223 0           text => $self->make_kvt($args,$args->{'text'}),
224             );
225              
226 0           my $rv = $self->command_state($command);
227 0           $self->_post($args);
228 0 0         return($rv == 0 ? 1 : 0);
229             }
230             sub error {
231 0     0 0   my $self = shift();
232 0   0       return($self->msgbox('caller',((caller(1))[3]||'main'),@_,'msgbox','error'));
233             }
234             sub sorry {
235 0     0 0   my $self = shift();
236 0   0       return($self->msgbox('caller',((caller(1))[3]||'main'),@_,'msgbox','sorry'));
237             }
238             sub infobox {
239 0     0 0   my $self = shift();
240 0   0       return($self->msgbox('caller',((caller(1))[3]||'main'),@_,'msgbox','msgbox'));
241             }
242              
243             #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
244             #: File box
245             sub textbox {
246 0     0 1   my $self = shift();
247 0   0       my $caller = (caller(1))[3] || 'main';
248 0 0 0       $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
249 0 0 0       if ($_[0] && $_[0] eq 'caller') {
250 0           shift(); $caller = shift();
  0            
251             }
252 0           my $args = $self->_pre($caller,@_);
253              
254 0           my $fmt = $self->prepare_format($args);
255 0           $fmt = $self->append_format_base($args,$fmt);
256 0           $fmt = $self->append_format($fmt,'--textbox');
257 0           $fmt = $self->append_format($fmt,'{{path}} {{height}} {{width}}');
258             my $command = $self->prepare_command
259             ( $args, $fmt,
260 0   0       path => $self->make_kvl($args,($args->{'filename'}||$args->{'path'}||'.')),
261             );
262              
263 0           my ($rv,$text) = $self->command_string($command);
264 0           $self->_post($args);
265 0 0         return($rv == 0 ? 1 : 0);
266             }
267              
268             #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
269             #: a simple menu
270             sub menu {
271 0     0 1   my $self = shift();
272 0   0       my $caller = (caller(1))[3] || 'main';
273 0 0 0       $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
274 0 0 0       if ($_[0] && $_[0] eq 'caller') {
275 0           shift(); $caller = shift();
  0            
276             }
277 0           my $args = $self->_pre($caller,@_);
278              
279 0           my $fmt = $self->prepare_format($args);
280 0           $fmt = $self->append_format_base($args,$fmt);
281 0           $fmt = $self->append_format($fmt,'--separate-output');
282 0           $fmt = $self->append_format($fmt,'--menu');
283 0           $fmt = $self->append_format($fmt,'{{text}} {{list}}');
284             my $command = $self->prepare_command
285             ( $args, $fmt,
286 0           text => $self->make_kvt($args,$args->{'text'}),
287             );
288              
289 0           my ($rv,$selected) = $self->command_string($command);
290 0           $self->_post($args);
291 0 0         return($rv == 0 ? $selected : 0);
292             }
293              
294             #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
295             #: a check list
296             sub checklist {
297 0     0 1   my $self = shift();
298 0   0       my $caller = (caller(1))[3] || 'main';
299 0 0 0       $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
300 0 0 0       if ($_[0] && $_[0] eq 'caller') {
301 0           shift(); $caller = shift();
  0            
302             }
303 0           my $args = $self->_pre($caller,@_);
304              
305             $args->{'listheight'} = $args->{'menuheight'}
306 0 0         if exists $args->{'menuheight'};
307              
308 0           my $fmt = $self->prepare_format($args);
309 0           $fmt = $self->append_format_base($args,$fmt);
310 0   0       $args->{radiolist} ||= 0;
311 0 0         if ($args->{radiolist}) {
312 0           $fmt = $self->append_format($fmt,'--radiolist');
313             }
314             else {
315 0           $fmt = $self->append_format($fmt,'--separate-output');
316 0           $fmt = $self->append_format($fmt,'--checklist');
317             }
318 0           $fmt = $self->append_format($fmt,'{{text}} {{list}}');
319             my $command = $self->prepare_command
320             ( $args, $fmt,
321 0           text => $self->make_kvt($args,$args->{'text'}),
322             );
323              
324 0 0         if ($args->{radiolist}) {
325 0           my ($rv,$selected) = $self->command_string($command);
326 0 0         return($rv == 0 ? $selected : 0);
327             }
328 0           my ($rv,$selected) = $self->command_array($command);
329 0 0         return($rv == 0 ? @{$selected} : 0);
  0            
330             }
331             #: a radio button list
332             sub radiolist {
333 0     0 1   my $self = shift();
334 0   0       return($self->checklist('caller',((caller(1))[3]||'main'),@_,'radiolist',1));
335             }
336              
337              
338             #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
339             #: file select
340             sub fselect {
341 0     0 1   my $self = shift();
342 0   0       my $caller = (caller(1))[3] || 'main';
343 0 0 0       $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
344 0 0 0       if ($_[0] && $_[0] eq 'caller') {
345 0           shift(); $caller = shift();
  0            
346             }
347 0           my $args = $self->_pre($caller,@_);
348              
349 0   0       $args->{'fselect'} ||= 'getopenfilename';
350              
351 0           my $fmt = $self->prepare_format($args);
352 0           $fmt = $self->append_format_base($args,$fmt);
353 0           $fmt = $self->append_format($fmt,'--separate-output');
354 0           $fmt = $self->append_format($fmt,'--'.$args->{'fselect'});
355 0 0         if ($args->{'getexistingdirectory'}) {
356 0           $fmt = $self->append_format($fmt,'{{path}}');
357             } else {
358 0           $fmt = $self->append_format($fmt,'{{path}} {{filter}}');
359             }
360             my $command = $self->prepare_command
361             ( $args, $fmt,
362             path => $self->make_kvl($args,($args->{'path'}||abs_path())),
363 0   0       filter => $self->make_kvl($args,($args->{'filter'}||'*'))
      0        
364             );
365              
366 0           my ($rv,$selected) = $self->command_string($command);
367 0           $self->_post($args);
368 0 0         return($rv == 0 ? $selected : 0);
369             }
370             sub getopenfilename {
371 0     0 1   my $self = shift();
372 0   0       return($self->fselect('caller',((caller(1))[3]||'main'),@_,'fselect','getopenfilename'));
373             }
374             sub getsavefilename {
375 0     0 1   my $self = shift();
376 0   0       return($self->fselect('caller',((caller(1))[3]||'main'),@_,'fselect','getsavefilename'));
377             }
378             sub getopenurl {
379 0     0 1   my $self = shift();
380 0   0       return($self->fselect('caller',((caller(1))[3]||'main'),@_,'fselect','getopenurl'));
381             }
382             sub getsaveurl {
383 0     0 1   my $self = shift();
384 0   0       return($self->fselect('caller',((caller(1))[3]||'main'),@_,'fselect','getsaveurl'));
385             }
386              
387             #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
388             #: directory select
389             sub dselect {
390 0     0 1   my $self = shift();
391 0   0       return($self->fselect('caller',((caller(1))[3]||'main'),@_,'fselect','getexistingdirectory'));
392             }
393             sub getexistingdirectory {
394 0     0 1   my $self = shift();
395 0   0       return($self->fselect('caller',((caller(1))[3]||'main'),@_,'fselect','getexistingdirectory'));
396             }
397              
398              
399             1;