File Coverage

blib/lib/UI/Dialog/Gauged.pm
Criterion Covered Total %
statement 62 123 50.4
branch 21 54 38.8
condition 13 39 33.3
subroutine 9 33 27.2
pod 6 25 24.0
total 111 274 40.5


line stmt bran cond sub pod time code
1             package UI::Dialog::Gauged;
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 1     1   13755 use 5.006;
  1         2  
20 1     1   4 use strict;
  1         1  
  1         16  
21 1     1   2 use warnings;
  1         1  
  1         25  
22 1     1   3 use Carp;
  1         1  
  1         68  
23              
24             BEGIN {
25 1     1   4 use vars qw($VERSION);
  1         1  
  1         37  
26 1     1   1132 $VERSION = '1.21';
27             }
28              
29             #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
30             #: Constructor Method
31             #:
32              
33             sub new {
34 2     2 1 380 my $proto = shift();
35 2   33     9 my $class = ref($proto) || $proto;
36 2   50     6 my $cfg = {@_} || {};
37 2         3 my $self = {};
38 2         3 bless($self, $class);
39              
40 2   50     14 $self->{'debug'} = $cfg->{'debug'} || 0;
41              
42             #: Dynamic path discovery...
43 2         3 my $CFG_PATH = $cfg->{'PATH'};
44 2 50       7 if ($CFG_PATH) {
    50          
45 0 0       0 if (ref($CFG_PATH) eq "ARRAY") { $self->{'PATHS'} = $CFG_PATH; }
  0 0       0  
    0          
46 0         0 elsif ($CFG_PATH =~ m!:!) { $self->{'PATHS'} = [ split(/:/,$CFG_PATH) ]; }
47 0         0 elsif (-d $CFG_PATH) { $self->{'PATHS'} = [ $CFG_PATH ]; }
48 2         11 } elsif ($ENV{'PATH'}) { $self->{'PATHS'} = [ split(/:/,$ENV{'PATH'}) ]; }
49 0         0 else { $self->{'PATHS'} = ''; }
50              
51 2 50 33     11 if (not $cfg->{'order'} and ($ENV{'DISPLAY'} && length($ENV{'DISPLAY'}) > 0)) {
      33        
52             #: Pick a GUI mode 'cause a DISPLAY was detected
53 0 0       0 if ($ENV{'TERM'} =~ /^dumb$/i) {
54             # we're running free of a terminal
55 0         0 $cfg->{'order'} = [ 'zenity', 'xdialog' ];
56             } else {
57             # we're running in a terminal
58 0         0 $cfg->{'order'} = [ 'zenity', 'xdialog', 'cdialog', 'whiptail' ];
59             }
60             }
61             # verify and repair the order
62             $cfg->{'order'} = ((ref($cfg->{'order'}) eq "ARRAY") ? $cfg->{'order'} :
63 2 50       9 ($cfg->{'order'}) ? [ $cfg->{'order'} ] :
    50          
64             [ 'cdialog', 'whiptail' ]);
65              
66 2   50     11 $self->_debug("ENV->UI_DIALOGS: ".($ENV{'UI_DIALOGS'}||'NULL'),2);
67 2 50       4 $cfg->{'order'} = [ split(/\:/,$ENV{'UI_DIALOGS'}) ] if $ENV{'UI_DIALOGS'};
68              
69 2   50     10 $self->_debug("ENV->UI_DIALOG: ".($ENV{'UI_DIALOG'}||'NULL'),2);
70 2 50       5 unshift(@{$cfg->{'order'}},$ENV{'UI_DIALOG'}) if $ENV{'UI_DIALOG'};
  0         0  
71              
72             $cfg->{'trust-input'} =
73             ( exists $cfg->{'trust-input'}
74 2 50 33     15 && $cfg->{'trust-input'}==1
75             ) ? 1 : 0;
76              
77 2         4 my @opts = ();
78 2         6 foreach my $opt (keys(%$cfg)) { push(@opts,$opt,$cfg->{$opt}); }
  5         7  
79              
80 2         2 $self->_debug("order: @{$cfg->{'order'}}",2);
  2         7  
81              
82 2 50       5 if (ref($cfg->{'order'}) eq "ARRAY") {
83 2         2 foreach my $try (@{$cfg->{'order'}}) {
  2         4  
84 4 50       31 if ($try =~ /^zenity$/i) {
    50          
    100          
    50          
85 0         0 $self->_debug("trying zenity",2);
86 0 0 0     0 if (eval "require UI::Dialog::Backend::Zenity; 1" && $self->_has_variant('zenity')) {
87 0         0 require UI::Dialog::Backend::Zenity;
88 0         0 $self->{'_ui_dialog'} = new UI::Dialog::Backend::Zenity (@opts);
89 0         0 $self->_debug("using zenity",2);
90 0         0 last;
91 0         0 } else { next; }
92             } elsif ($try =~ /^(?:xdialog|X)$/i) {
93 0         0 $self->_debug("trying xdialog",2);
94 0 0 0     0 if (eval "require UI::Dialog::Backend::XDialog; 1" && $self->_has_variant('Xdialog')) {
95 0         0 require UI::Dialog::Backend::XDialog;
96 0         0 $self->{'_ui_dialog'} = new UI::Dialog::Backend::XDialog (@opts,'XDIALOG_HIGH_DIALOG_COMPAT',1);
97 0         0 $self->_debug("using xdialog",2);
98 0         0 last;
99 0         0 } else { next; }
100             } elsif ($try =~ /^(?:dialog|cdialog)$/i) {
101 2         5 $self->_debug("trying cdialog",2);
102 2 50 33     107 if (eval "require UI::Dialog::Backend::CDialog; 1" && $self->_has_variant('dialog')) {
103 0         0 require UI::Dialog::Backend::CDialog;
104 0         0 $self->{'_ui_dialog'} = new UI::Dialog::Backend::CDialog (@opts);
105 0         0 $self->_debug("using cdialog",2);
106 0         0 last;
107 2         9 } else { next; }
108             } elsif ($try =~ /^whiptail$/i) {
109 2         4 $self->_debug("trying whiptail",2);
110 2 50 33     87 if (eval "require UI::Dialog::Backend::Whiptail; 1" && $self->_has_variant('whiptail')) {
111 2         7 require UI::Dialog::Backend::Whiptail;
112 2         10 $self->{'_ui_dialog'} = new UI::Dialog::Backend::Whiptail (@opts);
113 2         3 $self->_debug("using whiptail",2);
114 2         3 last;
115 0         0 } else { next; }
116             } else {
117             # we don't know what they're asking for... try UI::Dialog...
118 0 0       0 if (eval "require UI::Dialog; 1") {
119 0         0 require UI::Dialog;
120 0         0 $self->{'_ui_dialog'} = new UI::Dialog (@opts);
121 0         0 $self->_debug(ref($self)." unknown backend: '".$try."', using UI::Dialog instead.",2);
122 0         0 last;
123 0         0 } else { next; }
124             }
125             }
126             } else {
127 0         0 carp("Failed to load any suitable dialog variant backend.");
128             }
129              
130 2 50       7 ref($self->{'_ui_dialog'}) or croak("unable to load suitable backend.");
131 2         17 return($self);
132             }
133              
134             #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
135             #: Private Methods
136             #:
137              
138             #: purely internal usage
139             sub _debug {
140 12     12   13 my $self = $_[0];
141 12   50     17 my $mesg = $_[1] || 'null error message given!';
142 12   50     16 my $rate = $_[2] || 1;
143 12 50 33     30 return() unless $self->{'debug'} and $self->{'debug'} >= $rate;
144 0         0 chomp($mesg);
145 0         0 print STDERR "Debug: ".$mesg."\n";
146             }
147              
148             sub _has_variant {
149 4     4   7 my $self = $_[0];
150 4         4 my $variant = $_[1];
151             $self->{'PATHS'} = ((ref($self->{'PATHS'}) eq "ARRAY") ? $self->{'PATHS'} :
152 4 0       12 ($self->{'PATHS'}) ? [ $self->{'PATHS'} ] :
    50          
153             [ '/bin', '/usr/bin', '/usr/local/bin', '/opt/bin' ]);
154 4         2 foreach my $PATH (@{$self->{'PATHS'}}) {
  4         9  
155 26 100       181 return($PATH . '/' . $variant)
156             unless not -x $PATH . '/' . $variant;
157             }
158 2         8 return(0);
159             }
160              
161             #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
162             #: Public Methods
163             #:
164              
165             #: dialog variant state methods:
166 0     0 0   sub state { return(shift()->{'_ui_dialog'}->state(@_)); }
167 0     0 0   sub ra { return(shift()->{'_ui_dialog'}->ra(@_)); }
168 0     0 0   sub rs { return(shift()->{'_ui_dialog'}->rs(@_)); }
169 0     0 0   sub rv { return(shift()->{'_ui_dialog'}->rv(@_)); }
170              
171             #: Frills
172             #: all backends support nautilus scripts.
173 0     0 0   sub nautilus { return(shift()->{'_ui_dialog'}->nautilus(@_)); }
174             #: same with osd_cat (aka: xosd).
175 0     0 0   sub xosd { return(shift()->{'_ui_dialog'}->xosd(@_)); }
176             #: Beep & Clear may have no affect when using GUI backends
177 0     0 0   sub beep { return(shift()->{'_ui_dialog'}->beep(@_)); }
178 0     0 0   sub clear { return(shift()->{'_ui_dialog'}->clear(@_)); }
179              
180             #: widget methods:
181 0     0 0   sub yesno { return(shift()->{'_ui_dialog'}->yesno(@_)); }
182 0     0 0   sub msgbox { return(shift()->{'_ui_dialog'}->msgbox(@_)); }
183 0     0 0   sub inputbox { return(shift()->{'_ui_dialog'}->inputbox(@_)); }
184 0     0 0   sub password { return(shift()->{'_ui_dialog'}->password(@_)); }
185 0     0 0   sub textbox { return(shift()->{'_ui_dialog'}->textbox(@_)); }
186 0     0 0   sub menu { return(shift()->{'_ui_dialog'}->menu(@_)); }
187 0     0 0   sub checklist { return(shift()->{'_ui_dialog'}->checklist(@_)); }
188 0     0 0   sub radiolist { return(shift()->{'_ui_dialog'}->radiolist(@_)); }
189 0     0 0   sub fselect { return(shift()->{'_ui_dialog'}->fselect(@_)); }
190 0     0 0   sub dselect { return(shift()->{'_ui_dialog'}->dselect(@_)); }
191              
192             # gauge methods
193 0     0 1   sub gauge_start { return(shift()->{'_ui_dialog'}->gauge_start(@_)); }
194 0     0 1   sub gauge_stop { return(shift()->{'_ui_dialog'}->gauge_stop(@_)); }
195 0     0 1   sub gauge_inc { return(shift()->{'_ui_dialog'}->gauge_inc(@_)); }
196 0     0 0   sub gauge_dec { return(shift()->{'_ui_dialog'}->gauge_dec(@_)); }
197 0     0 1   sub gauge_set { return(shift()->{'_ui_dialog'}->gauge_set(@_)); }
198 0     0 1   sub gauge_text { return(shift()->{'_ui_dialog'}->gauge_text(@_)); }
199              
200              
201             1;