Dialog/Backend.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.5% 29 / 31 CC 1 SCAR 0.4
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Dialog/Backend/Zenity.pm ↑ Summary Dialog/Backend/CDialog.pm »
1 package UI::Dialog::Backend;
2 ###############################################################################
3 # Copyright (C) 2004-2016 Kevin C. Krinke <kevin@krinke.ca>
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 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶use Cwd qw( abs_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use Text::Wrap qw( wrap );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use String::ShellQuote;
Subroutine BEGIN: called CC=1 SCAR=2.6
27 use File::Slurp::Unicode;
28  
29 BEGIN {
30 use vars qw($VERSION);
31 $VERSION = '1.14';
32 }
33  
34 #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
35 #: Constructor Method
36 #:
37  
38 #: not even really necessary as this class is inherited, and the constructor is
39 #: more often than not overridden by the backend inheriting it.
40 sub new {
41 my $proto = shift();
42 my $class = ref($proto) || $proto;
43 my $cfg = ((ref($_[0]) eq "HASH") ? $_[0] : (@_) ? { @_ } : {});
44 my $self = { '_opts' => $cfg };
45 $self->{'test_mode'} = $cfg->{'test_mode'} if exists $cfg->{'test_mode'};
46 $self->{'test_mode_result'} = '';
47 bless($self, $class);
48 return($self);
49 }
50  
51 #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
52 #: Accessory Methods
53 #:
54  
55 #: Return the path to the dialog variant binary
56 sub get_bin {
57 return $_[0]->{'_opts'}{'bin'} if defined $_[0]->{'_opts'}{'bin'};
58 return undef;
59 }
60  
61 #: Provide the API interface to nautilus
62 sub nautilus {
63 my $self = $_[0];
64 my $nautilus = $self->{'_nautilus'} || {};
65 unless (ref($nautilus) eq "UI::Dialog::Backend::Nautilus") {
66 if ($self->_find_bin('nautilus')) {
67 if (eval "require UI::Dialog::Backend::Nautilus; 1") {
68 require UI::Dialog::Backend::Nautilus;
69 $self->{'_nautilus'} = new UI::Dialog::Backend::Nautilus;
70 }
71 }
72 }
73 return($self->{'_nautilus'});
74 }
75  
76 #: Provide the API interface to osd_cat (aka: xosd)
77 sub xosd {
78 my $self = shift();
79 my @args = (@_ %2 == 0) ? (@_) : ();
80 my $xosd = $self->{'_xosd'} || {};
81 unless (ref($xosd) eq "UI::Dialog::Backend::XOSD") {
82 if ($self->_find_bin('osd_cat')) {
83 if (eval "require UI::Dialog::Backend::XOSD; 1") {
84 require UI::Dialog::Backend::XOSD;
85 $self->{'_xosd'} = new UI::Dialog::Backend::XOSD (@args);
86 }
87 }
88 }
89 return($self->{'_xosd'});
90 }
91  
92 #: Provide the API interface to notify-send
93 sub notify_send {
94 my $self = shift();
95 my @args = (@_ %2 == 0) ? (@_) : ();
96 my $notify_send = $self->{'_notify_send'} || {};
97 unless (ref($notify_send) eq "UI::Dialog::Backend::NotifySend") {
98 if ($self->_find_bin('notify-send')) {
99 if (eval "require UI::Dialog::Backend::NotifySend; 1") {
100 require UI::Dialog::Backend::NotifySend;
101 $self->{'_notify_send'} = new UI::Dialog::Backend::NotifySend (@args);
102 }
103 }
104 }
105 return($self->{'_notify_send'});
106 }
107  
108 #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
109 #: State Methods
110 #:
111  
112 #: enable altering of attributes
113 sub attr {
114 my $self = $_[0];
115 my $name = $_[1];
116 unless ($_[2]) {
117 return($self->{'_opts'}->{$name}) unless not $self->{'_opts'}->{$name};
118 return(undef());
119 }
120 if ($_[2] == 0 || $_[2] =~ /^NULL$/i) {
121 $self->{'_opts'}->{$name} = 0;
122 }
123 else {
124 $self->{'_opts'}->{$name} = $_[2];
125 }
126 return($self->{'_opts'}->{$name});
127 }
128  
129 #: return the last response data as an ARRAY
130 sub ra {
131 my $self = shift();
132 my (@argv) = @_;
133 if (@argv) {
134 if (defined $argv[0] && $argv[0] =~ m!^null$!i) {
135 $self->{'_state'}{'ra'} = [];
136 } else {
137 $self->{'_state'}{'ra'} = \@argv;
138 }
139 } else {
140 $self->{'_state'}->{'ra'} ||= [];
141 }
142 return(@{ $self->{'_state'}->{'ra'} });
143 }
144  
145 #: return the last response data as a SCALAR
146 sub rs {
147 my $self = shift();
148 my (@argv) = @_;
149 if (@argv) {
150 if (defined $argv[0] && $argv[0] =~ m!^null$!i) {
151 $self->{'_state'}{'rs'} = '';
152 } else {
153 $self->{'_state'}{'rs'} = $argv[0];
154 }
155 }
156 return($self->{'_state'}->{'rs'});
157 }
158  
159 #: return the last exit code as a SCALAR
160 sub rv {
161 my $self = shift();
162 my (@argv) = @_;
163 if (@argv) {
164 if (defined $argv[0] && $argv[0] =~ m!^null$!i) {
165 $self->{'_state'}{'rv'} = 0;
166 } else {
167 $self->{'_state'}{'rv'} = $argv[0];
168 }
169 }
170 return($self->{'_state'}->{'rv'});
171 }
172  
173 #: report on the state of the last dialog variant execution.
174 sub state {
175 my $self = shift();
176 my $rv = $self->rv() || 0;
177 $self->_debug((join(" | ",(caller())))." > state() > is: ".($rv||'NULL'),2);
178 if ($rv == 1 or $rv == 129) {
179 return("CANCEL");
180 }
181 elsif ($rv == 2) {
182 return("HELP");
183 }
184 elsif ($rv == 3) {
185 return("EXTRA");
186 }
187 elsif ($rv == 254) {
188 return("ERROR");
189 }
190 elsif ($rv == 255) {
191 return("ESC");
192 }
193 elsif (not $rv or $rv =~ /^null$/i) {
194 return("OK");
195 }
196 else {
197 return("UNKNOWN(".$rv.")");
198 }
199 }
200  
201 #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
202 #: Preparation Methods
203 #
204  
205 #: construct a HASHREF for command {{tag}} substitutions
206 sub make_kvt {
207 my ($self,$args,$value) = @_;
208 return
209 {
210 literal => ($args->{'literal'} || 0),
211 width => ($args->{'width'}||'65'),
212 trust => ($args->{'trust-input'} || 0),
213 value => ($value || '')
214 };
215 }
216 sub make_kvl {
217 my ($self,$args,$value) = @_;
218 return
219 {
220 literal => 1,
221 width => ($args->{'width'}||'65'),
222 trust => ($args->{'trust-input'} || 0),
223 value => ($value || '')
224 };
225 }
226  
227 #: Helper method to generate a base format string, accepts additional
228 #: strings which are considered trusted programmer template input.
229 sub prepare_format {
230 my $self = shift(@_);
231 my $args = shift(@_);
232 # start with our binary path
233 my $fmt = $self->{'_opts'}{'bin'};
234 $fmt = $self->append_format_check($args,$fmt,'title','--title {{title}}');
235 return $fmt;
236 }
237  
238 sub append_format {
239 my ($self,$fmt,$value) = @_;
240 if (ref($fmt) eq "SCALAR") {
241 $$fmt .= ' '.$value;
242 }
243 else {
244 $fmt .= ' '.$value;
245 }
246 return $fmt;
247 }
248  
249 #: simple test and if true; append value to format
250 sub append_format_check {
251 my ($self,$args,$fmt,$key,$value) = @_;
252 if (exists $args->{$key} and defined $args->{$key} and $args->{$key}) {
253 $fmt = $self->append_format($fmt,$value);
254 }
255 return $fmt;
256 }
257  
258 sub clean_format {
259 my ($self,$trust,$sref) = @_;
260 unless (ref($sref) eq "SCALAR") {
261 die("Programmer error. clean_format requires a SCALAR ref, found: ".ref($sref));
262 }
263 $$sref =~ s!\x00!!mg; # remove nulls
264 #unless ($trust) {
265 #$$sref =~ s!\`!'!mg;
266 #$$sref =~ s!\$\(!\(!mg;
267 #$$sref =~ s!\$!\\\$!mg;
268 #}
269 #$$sref =~ s!"!\\"!mg; # escape double-quotes
270 return $sref;
271 }
272  
273 #: Given a command string "format" and any key/value replacement pairs,
274 #: construct the exec'able command string.
275 sub prepare_command {
276 my $self = shift(@_);
277 my $args = shift(@_);
278 my $format = shift(@_);
279 my (%rpl_add) = @_;
280 my %rpl = ();
281 foreach my $key (keys %{$args}) {
282 $rpl{$key} = $self->make_kvl($args,$args->{$key}||'');
283 }
284 foreach my $key (keys %rpl_add) {
285 $rpl{$key} = $rpl_add{$key};
286 }
287 foreach my $key (keys %rpl) {
288 my $value = $rpl{$key}->{value}||'';
289 if (ref($value) eq "ARRAY") {
290 #: menu, checklist, radiolist...
291 my $list = '';
292 foreach my $item (@{$value}) {
293 if (ref($item) eq "ARRAY") {
294 # checklist, radiolist...
295 if (@{$item} == 2) {
296 $list .= ' '.shell_quote($item->[0]);
297 $list .= ' '.($item->[1] ? 'on' : 'off');
298 next;
299 }
300 elsif (@{$item} == 3) {
301 $list .= ' '.shell_quote($item->[0]);
302 $list .= ' '.($item->[1] ? 'on' : 'off');
303 $list .= ' '.(shell_quote($item->[2])||1);
304 next;
305 }
306 elsif (@{$item} == 4) {
307 $list .= ' ' . shell_quote($item->[0]);
308 $list .= ' '.($item->[1] ? 'on' : 'off');
309 $list .= ' '.(shell_quote($item->[2])||1);
310 $list .= ' "'.$item->[3].'"';
311 next;
312 }
313 }
314 # menu...
315 $list .= ' '.shell_quote($item);
316 }
317 $format =~ s!\{\{\Q${key}\E\}\}!${list}!mg;
318 } # if (ref($value) eq "ARRAY")
319 elsif ($key eq "list") {
320 # assume this has been manipulated already?
321 $format =~ s!\{\{\Q${key}\E\}\}!${value}!mg;
322 }
323 else {
324 $value ||= '' unless defined $value;
325 $value = "$1" if $value =~ m!^(\d+)$!;
326 if (ref(\$value) eq "SCALAR") {
327 unless ($rpl{$key}->{'trust'}||$rpl{$key}->{literal}) {
328 $value = $self->_organize_text
329 ( $value, $rpl{$key}->{width}, $rpl{$key}->{'trust'} );
330 }
331 $value = shell_quote($value);
332 $format =~ s!\{\{\Q${key}\E\}\}!${value}!mg;
333 }
334 }
335 }
336 return $format;
337 }
338  
339  
340 #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
341 #: Execution Methods
342 #:
343  
344 sub is_unit_test_mode {
345 my ($self) = @_;
346 return 1
347 if ( exists $self->{'test_mode'}
348 &&
349 defined $self->{'test_mode'}
350 &&
351 $self->{'test_mode'}
352 );
353 return 0;
354 }
355 sub get_unit_test_result {
356 my ($self) = @_;
357 return $self->{'test_mode_result'};
358 }
359  
360 #: run command and return the rv and any text output from stderr
361 sub perform_command {
362 my $self = $_[0];
363 my $cmnd = $_[1];
364 if ($self->is_unit_test_mode()) {
365 $self->{'test_mode_result'} = $cmnd;
366 return (0,'test_mode_result');
367 }
368 $self->_debug("perform_command: ".$cmnd.";");
369 my $tmp_stderr = $self->gen_tempfile_name();
370 system($cmnd." 2> ".$tmp_stderr);
371 my $rv = $? >> 8;
372 my $text = read_file($tmp_stderr);
373 unlink($tmp_stderr) if -f $tmp_stderr;
374 $self->_debug("perform_command: stderr=".shell_quote($text),2);
375 return ($rv,$text);
376 }
377  
378 #: execute a simple command (return the exit code only);
379 sub command_state {
380 my $self = $_[0];
381 my $cmnd = $_[1];
382 if ($self->is_unit_test_mode()) {
383 $self->{'test_mode_result'} = $cmnd;
384 return 0;
385 }
386 my ($rv,$text) = $self->perform_command($cmnd);
387 $self->_debug("command_state: rv=".$rv,1);
388 $self->rv($rv);
389 $self->rs('null');
390 $self->ra('null');
391 return($rv);
392 }
393  
394 #: execute a command and return the exit code and one-line SCALAR
395 sub command_string {
396 my $self = $_[0];
397 my $cmnd = $_[1];
398 if ($self->is_unit_test_mode()) {
399 $self->{'test_mode_result'} = $cmnd;
400 return (wantarray) ? (0,'') : '';
401 }
402 my ($rv,$text) = $self->perform_command($cmnd);
403 chomp($text);
404 $self->_debug("command_string: rv=".$rv.", rs=".shell_quote($text),1);
405 $self->rv($rv);
406 $self->rs($text);
407 $self->ra('null');
408 return($text) unless defined wantarray;
409 return (wantarray) ? ($rv,$text) : $text;
410 }
411  
412 #: execute a command and return the exit code and ARRAY of data
413 sub command_array {
414 my $self = $_[0];
415 my $cmnd = $_[1];
416 if ($self->is_unit_test_mode()) {
417 $self->{'test_mode_result'} = $cmnd;
418 return (wantarray) ? (0,[]) : [];
419 }
420 my ($rv,$text) = $self->perform_command($cmnd);
421 $self->_debug("command_array: rv=".$rv.", rs=".shell_quote($text),1);
422 $self->rv($rv);
423 $self->rs($text);
424 # this is so hackish that it may just work
425 my $alt_text = $text;
426 $alt_text =~ s!\r??\n!__\\n!mg; #: replace newlines with "a symbol"
427 my @alt_items = split(/_\\n/,$alt_text); #: split on "part symbol"
428 my @alt_final = ();
429 foreach my $alt_item (@alt_items) {
430 my $i = $alt_item;
431 $i =~ s!_$!!; #: remove the trailing bit of symbol
432 push(@alt_final,$i);
433 }
434 $self->ra(@alt_final); #: final array can now contain blanks
435 return([$self->ra()]) unless defined wantarray and wantarray;
436 return (wantarray) ? ($rv,[$self->ra()]) : [$self->ra()];
437 }
438  
439 #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
440 #: Utility Methods
441 #:
442  
443 #: make some noise
444 sub beep {
445 my $self = $_[0];
446 return($self->_beep(1));
447 }
448  
449 #: Clear terminal screen.
450 sub clear {
451 my $self = $_[0];
452 return($self->_clear(1));
453 }
454  
455 # word-wrap a line
456 sub word_wrap {
457 my $self = shift();
458 my $width = shift() || 65;
459 my $indent = shift() || "";
460 my $sub_indent = shift() || "";
461 $Text::Wrap::columns = $width - 3;
462 my $raw = join("\n",@_);
463 my $string = wrap($indent, $sub_indent, $raw);
464 return(split(m!\n!,$string));
465 }
466  
467 # generate a temporary file name
468 sub gen_tempfile_name {
469 my $self = $_[0];
470 my $template = $self->{'_opts'}->{'tempfiletemplate'} || "UI_Dialog_tempfile_XXXXX";
471 if (eval("require File::Temp; 1")) {
472 use File::Temp qw( tempfile );
473 my ($fh,$filename) = tempfile( UNLINK => 1 ) or croak( "Can't create tempfile: $!" );
474 if (wantarray) {
475 return($fh,$filename);
476 }
477 else {
478 close($fh); # actually required on win32
479 return($filename);
480 }
481 return($fh,$filename);
482 }
483 else {
484 my $mktemp = $self->_find_bin('mktemp');
485 if ($mktemp && -x $mktemp) {
486 chomp(my $tempfile = `$mktemp "$template"`);
487 return($tempfile);
488 }
489 else {
490 #pseudo-random filename coming up!
491 my $tempdir = "/tmp";
492 unless (-d $tempdir) {
493 if (-d "/var/tmp") {
494 $tempdir = "/var/tmp";
495 }
496 else {
497 $tempdir = ".";
498 }
499 }
500 $self->gen_random_string(5);
501 my $tempfile = "UI_Dialog_tempfile_".$self->gen_random_string(5);
502 while (-e $tempdir."/".$tempfile) {
503 $self->gen_random_string(5);
504 $tempfile = "UI_Dialog_tempfile_".$self->gen_random_string(5);
505 }
506 return($tempdir."/".$tempfile);
507 }
508 }
509 }
510  
511 # generate a random string as a (possibly) suitable failover option in the
512 # event that File::Temp is not installed and the 'mktemp' program does not
513 # exist in the path.
514 sub gen_random_string {
515 my $self = $_[0];
516 my $length = $_[1] || 5;
517 my $string = "";
518 my $counter = 0;
519 while ($counter < $length) {
520 # 33 - 127
521 my $num = rand(128);
522 while ($num < 33 or $num > 127) {
523 $num = rand(128);
524 }
525 $string .= chr($num);
526 $counter++;
527 }
528 return($string);
529 }
530  
531 #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
532 #: Widget Wrapping Methods
533 #:
534  
535 #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
536 #: file select
537 sub fselect {
538 my $self = shift();
539 my $caller = (caller(1))[3] || 'main';
540 $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
541 if ($_[0] && $_[0] eq 'caller') {
542 shift(); $caller = shift();
543 }
544 my $args = $self->_pre($caller,@_);
545  
546 $self->rv('NULL');
547 $self->rs('NULL');
548 $self->ra('NULL');
549  
550 $self->_beep($args->{'beepbefore'});
551  
552 my $cwd = abs_path();
553 $args->{'path'} ||= abs_path();
554 my $path = $args->{'path'};
555 if (!$path || $path =~ /^(\.|\.\/)$/) {
556 $path = $cwd;
557 }
558 my $file;
559 my ($menu,$list) = ([],[]);
560 FSEL: while ($self->state() ne "ESC" && $self->state() ne "CANCEL") {
561 my $entries = ($args->{'dselect'}) ? ['[new directory]'] : ['[new file]'];
562 ($menu, $list) = $self->_list_dir($path,$entries);
563 $file = $self->menu(height=>$args->{'height'},width=>$args->{'width'},listheight=>($args->{'listheight'}||$args->{'menuheight'}),
564 title=>$args->{'title'},text=>$path,list=>$menu);
565 if ($self->state() eq "CANCEL") {
566 $self->rv(1);
567 $self->rs('NULL');
568 $self->ra('NULL');
569 last FSEL;
570 }
571 elsif ($file ne "") {
572 if ($list->[($file - 1 || 0)] =~ /^\[(new\sdirectory|new\sfile)\]$/) {
573 my $nfn;
574 while (!$nfn || -e $path."/".$nfn) {
575 $nfn = $self->inputbox(height=>$args->{'height'},width=>$args->{'width'},title=>$args->{'title'},
576 text=>'Enter a name (will have a base directory of: '.$path.')');
577 next FSEL if $self->state() eq "ESC" or $self->state() eq "CANCEL";
578 if (-e $path."/".$nfn) {
579 $self->msgbox(title=>'error',text=>$path."/".$nfn.' already exists! Choose another name please.');
580 }
581 }
582 $file = $path."/".$nfn;
583 $file =~ s!/$!! unless $file =~ m!^/$!;
584 $file =~ s!/\./!/!g; $file =~ s!/+!/!g;
585 last FSEL;
586 }
587 elsif ($list->[($file - 1 || 0)] eq "../") {
588 $path = dirname($path);
589 }
590 elsif ($list->[($file - 1 || 0)] eq "./") {
591 $file = $path;
592 $file =~ s!/$!! unless $file =~ m!^/$!;
593 $file =~ s!/\./!/!g; $file =~ s!/+!/!g;
594 last FSEL;
595 }
596 elsif (-d $path."/".$list->[($file - 1 || 0)]) {
597 $path = $path."/".$list->[($file - 1 || 0)];
598 }
599 elsif (-e $path."/".$list->[($file - 1 || 0)]) {
600 $file = $path."/".$list->[($file - 1 || 0)];
601 $file =~ s!/$!! unless $file =~ m!^/$!;
602 $file =~ s!/\./!/!g; $file =~ s!/+!/!g;
603 last FSEL;
604 }
605 }
606 $file = undef();
607 $path =~ s!(/*)!/!; $path =~ s!/\./!/!g;
608 }
609 $self->_beep($args->{'beepafter'});
610 my $rv = $self->rv();
611 $self->ra('NULL');
612 if ($rv && $rv >= 1) {
613 $self->rs('NULL');
614 return(0);
615 }
616 else {
617 $self->rs($file);
618 return($file);
619 }
620 }
621  
622 #:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
623 #: directory selection
624 sub dselect {
625 my $self = shift();
626 my $caller = (caller(1))[3] || 'main';
627 $caller = ($caller =~ /^UI\:\:Dialog\:\:Backend\:\:/) ? ((caller(2))[3]||'main') : $caller;
628 if ($_[0] && $_[0] eq 'caller') {
629 shift(); $caller = shift();
630 }
631 my $args = $self->_pre($caller,@_);
632 my $dirname;
633 $self->rv('NULL');
634 $self->rs('NULL');
635 $self->ra('NULL');
636 while (not $dirname && $self->state() !~ /^(CANCEL|ESC|ERROR)$/) {
637 $dirname = $self->fselect(@_,'dselect',1);
638 if ($self->state() =~ /^(CANCEL|ESC|ERROR)$/) {
639 return(0);
640 }
641 unless (not $dirname) {
642 # if it's a directory or not exist (assume new dir)
643 unless (-d $dirname || not -e $dirname) {
644 $self->msgbox( text => $dirname . " is not a directory.\nPlease select a directory." );
645 $dirname = undef();
646 }
647 }
648 }
649 return($dirname||'');
650 }
651  
652  
653 #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
654 #: Backend Methods
655 #:
656  
657 sub _pre {
658 my $self = shift();
659 my $caller = shift();
660 my $args = $self->_merge_attrs(@_);
661 $args->{'caller'} = $caller;
662 my $class = ref($self);
663  
664 my $CODEREFS = $args->{'callbacks'};
665 if (ref($CODEREFS) eq "HASH") {
666 my $PRECODE = $CODEREFS->{'PRE'};
667 if (ref($PRECODE) eq "CODE") {
668 &$PRECODE($args,$self->state());
669 }
670 }
671  
672 $self->_beep($args->{'beepbefore'});
673 $self->_clear($args->{'clearbefore'});
674 return($args);
675 }
676  
677 sub _post {
678 my $self = shift();
679 my $args = shift() || {};
680 my $class = ref($self);
681  
682 $self->_beep($args->{'beepafter'});
683 $self->_clear($args->{'clearafter'});
684  
685 my $CODEREFS = $args->{'callbacks'};
686 if (ref($CODEREFS) eq "HASH") {
687 my $state = $self->state();
688 if ($state eq "OK") {
689 my $OKCODE = $CODEREFS->{'OK'};
690 if (ref($OKCODE) eq "CODE") {
691 &$OKCODE($args);
692 }
693 }
694 elsif ($state eq "ESC") {
695 my $ESCCODE = $CODEREFS->{'ESC'};
696 if (ref($ESCCODE) eq "CODE") {
697 &$ESCCODE($args);
698 }
699 }
700 elsif ($state eq "CANCEL") {
701 my $CANCELCODE = $CODEREFS->{'CANCEL'};
702 if (ref($CANCELCODE) eq "CODE") {
703 &$CANCELCODE($args);
704 }
705 }
706 my $POSTCODE = $CODEREFS->{'POST'};
707 if (ref($POSTCODE) eq "CODE") {
708 &$POSTCODE($args,$state);
709 }
710 }
711  
712 return(1);
713 }
714  
715  
716 #: indent and organize the text argument
717 sub _organize_text {
718 my $self = $_[0];
719 my $text = $_[1];
720 my $width = $_[2] || 65;
721 my $trust = (exists $_[3] && defined $_[3]) ? $_[3] : '0';
722 $width -= 4; # take account of borders?
723 my @array;
724  
725 if (ref($text) eq "ARRAY") {
726 push(@array,@{$text});
727 }
728 elsif ($text =~ /\\n/) {
729 @array = split(/\\n/,$text);
730 }
731 else {
732 @array = split(/\n/,$text);
733 }
734 $text = undef;
735  
736 @array = $self->word_wrap($width,"","",@array);
737  
738 if ($self->{'scale'}) {
739 foreach my $line (@array) {
740 my $s_line = $line;#$self->__TRANSLATE_CLEAN($line);
741 $s_line =~ s!\[A\=\w+\]!!gi;
742 $self->{'width'} = length($s_line) + 5
743 if ($self->{'width'} - 5) < length($s_line)
744 && (length($s_line) <= $self->{'max-scale'});
745 }
746 }
747  
748 foreach my $line (@array) {
749 my $pad;
750 $self->clean_format( $trust, \$line );
751 my $s_line = $self->_strip_text($line);
752 if ($line =~ /\[A\=(\w+)\]/i) {
753 my $align = $1;
754 $line =~ s!\[A\=\w+\]!!gi;
755 if (uc($align) eq "CENTER" || uc($align) eq "C") {
756 $pad = ((($self->{'_opts'}->{'width'} - 5) - length($s_line)) / 2);
757 }
758 elsif (uc($align) eq "LEFT" || uc($align) eq "L") {
759 $pad = 0;
760 }
761 elsif (uc($align) eq "RIGHT" || uc($align) eq "R") {
762 $pad = (($self->{'_opts'}->{'width'} - 5) - length($s_line));
763 }
764 }
765 if ($pad) {
766 $text .= (" " x $pad).$line."\n";
767 }
768 else {
769 $text .= $line."\n";
770 }
771 }
772 $text = $self->_strip_text($text);
773 chomp($text) if $text;
774 return($text);
775 }
776  
777 #: merge the arguments with the default attributes, and arguments override defaults.
778 sub _merge_attrs {
779 my $self = shift();
780 my $args = (@_ % 2) ? { @_, '_odd' } : { @_ };
781 my $defs = $self->{'_opts'};
782  
783 foreach my $def (keys(%$defs)) {
784 # default unless exists
785 $args->{$def} = $defs->{$def} unless exists $args->{$def};
786 }
787  
788 # alias 'filename' and 'file' to path
789 $args->{'path'} = (($args->{'filename'}) ? $args->{'filename'} :
790 ($args->{'file'}) ? $args->{'file'} :
791 ($args->{'path'}) ? $args->{'path'} : "");
792  
793 $args->{'clear'} = $args->{'clearbefore'} || $args->{'clearafter'} || $args->{'autoclear'} || 0;
794 $args->{'beep'} = $args->{'beepbefore'} || $args->{'beepafter'} || $args->{'autobeep'} || 0;
795 return($args);
796 }
797  
798 #: search through the given paths for a specific variant
799 sub _find_bin {
800 my $self = $_[0];
801 my $variant = $_[1];
802 $self->{'PATHS'} = ((ref($self->{'PATHS'}) eq "ARRAY") ? $self->{'PATHS'} :
803 ($self->{'PATHS'}) ? [ $self->{'PATHS'} ] :
804 [ '/bin', '/usr/bin', '/usr/local/bin', '/opt/bin' ]);
805 foreach my $PATH (@{$self->{'PATHS'}}) {
806 return($PATH . '/' . $variant)
807 unless not -x $PATH . '/' . $variant;
808 }
809 return(0);
810 }
811  
812 #: clean the text arguments of all colour codes, alignments and attributes.
813 sub _strip_text {
814 my $self = $_[0];
815 my $text = $_[1];
816 $text ||= '';
817 $text =~ s!\\Z[0-7bBuUrRn]!!gmi;
818 $text =~ s!\[[AC]=\w+\]!!gmi;
819 $text =~ s!\[/?[BURN]\]!!gmi;
820 return($text);
821 }
822  
823 #: is this a BSD system?
824 sub _is_bsd {
825 my $self = shift();
826 return(1) if $^O =~ /bsd/i;
827 return(0);
828 }
829  
830 #: gather a list of the contents of a directory and return it in
831 #: two forms, one is the "simple" list of all the filenames and the
832 #: other is a 'menu' list corresponding to the simple list.
833 sub _list_dir {
834 my $self = shift();
835 my $path = shift() || return();
836 my $pref = shift();
837 my (@listing,@list);
838 if (opendir(GETDIR,$path)) {
839 my @dir_data = readdir(GETDIR);
840 closedir(GETDIR);
841 if ($pref) {
842 push(@listing,@{$pref});
843 }
844 foreach my $dir (sort(grep { -d $path."/".$_ } @dir_data)) {
845 push(@listing,$dir."/");
846 }
847 foreach my $item (sort(grep { !-d $path."/".$_ } @dir_data)) {
848 push(@listing,$item);
849 }
850 my $c = 1;
851 foreach my $item (@listing) {
852 push(@list,"$c",$item); $c++;
853 }
854 return(\@list,\@listing);
855 }
856 else {
857 return("failed to read directory: ".$path);
858 }
859 }
860  
861 sub _debug {
862 my $self = $_[0];
863 my $mesg = $_[1] || 'null debug message given!';
864 my $rate = $_[2] || 1;
865 return() unless $self->{'_opts'}->{'debug'} and $self->{'_opts'}->{'debug'} >= $rate;
866 chomp($mesg);
867 print STDERR "Debug: ".$mesg."\n";
868 }
869 sub _error {
870 my $self = $_[0];
871 my $mesg = $_[1] || 'null error message given!';
872 chomp($mesg);
873 print STDERR "Error: ".$mesg."\n";
874 }
875  
876 #: really make some noise
877 sub _beep {
878 my $self = $_[0];
879 my $beep = $_[1];
880 unless (not $beep) {
881 if (-x $self->{'_opts'}->{'beepbin'}) {
882 return(eval { system($self->{'_opts'}->{'beepbin'}); 1; });
883 }
884 else {
885 return (1) unless $ENV{'TERM'} && $ENV{'TERM'} ne "dumb";
886 print STDERR "\a";
887 }
888 }
889 return(1);
890 }
891  
892 #: The actual clear action.
893 sub _clear {
894 my $self = $_[0];
895 my $clear = $_[1] || 0;
896 # Useless with GUI based variants so we return here.
897 # Is the use of the "dumb" TERM appropriate? need feedback.
898 return (1) unless $ENV{'TERM'} && $ENV{'TERM'} ne "dumb";
899 unless (not $clear and not $self->{'_opts'}->{'autoclear'}) {
900 $self->{'_clear'} ||= `clear`;
901 print STDOUT $self->{'_clear'};
902 }
903 return(1);
904 }
905  
906  
907  
908 1;
« Dialog/Backend/Zenity.pm ↑ Summary Dialog/Backend/CDialog.pm »