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