| 1 | package Tk::Wizard::Installer; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use warnings::register; | ||
| 6 | |||
| 7 | our $VERSION = 2.41; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Tk::Wizard::Installer - Building-blocks for a software install wizard | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use Tk::Wizard::Installer; | ||
| 16 | my $wizard = new Tk::Wizard::Installer( -title => "Installer Test", ); | ||
| 17 | $wizard->addDownloadPage( | ||
| 18 | -no_retry => 1, | ||
| 19 | -files => { | ||
| 20 | 'http://www.cpan.org/' => './cpan_index1.html', | ||
| 21 | }, | ||
| 22 | ); | ||
| 23 | $wizard->addPage( sub { | ||
| 24 | return $wizard->blank_frame( | ||
| 25 | -title=>"Finished", | ||
| 26 | -subtitle => "Please press Finish to leave the Wizard.", | ||
| 27 | -text => "" | ||
| 28 | ); | ||
| 29 | }); | ||
| 30 | $wizard->Show; | ||
| 31 | MainLoop; | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | use lib '../../'; # dev | ||
| 36 | use Carp; | ||
| 37 | use Cwd; | ||
| 38 | use Data::Dumper; | ||
| 39 | use FileHandle; | ||
| 40 | use File::Path; | ||
| 41 | use File::Copy; | ||
| 42 | use File::Spec; | ||
| 43 | use Tk; | ||
| 44 | use Tk::ErrorDialog; | ||
| 45 | use Tk::LabFrame; | ||
| 46 | use Tk::ProgressBar; | ||
| 47 | use Tk::Wizard ':use' => 'FileSystem'; | ||
| 48 | |||
| 49 | # For uninstaller | ||
| 50 | use Fcntl; # For O_RDWR, O_CREAT, etc. | ||
| 51 | # use Fcntl ':flock'; | ||
| 52 | use SDBM_File; | ||
| 53 | |||
| 54 | # use Log4perl if we have it, otherwise stub: | ||
| 55 | # See Log::Log4perl::FAQ | ||
| 56 | BEGIN { | ||
| 57 | eval { require Log::Log4perl; }; | ||
| 58 | |||
| 59 | # No Log4perl so bluff: see Log4perl FAQ | ||
| 60 | if ($@) { | ||
| 61 | no strict qw"refs"; | ||
| 62 | *{__PACKAGE__."::$_"} = sub { } for qw(TRACE DEBUG INFO WARN ERROR FATAL); | ||
| 63 | *{__PACKAGE__."::LOGCROAK"} = *Carp::croak; | ||
| 64 | *{__PACKAGE__."::LOGCONFESS"} = *Carp::confess; | ||
| 65 | } | ||
| 66 | |||
| 67 | # Setup log4perl | ||
| 68 | else { | ||
| 69 | no warnings; | ||
| 70 | no strict qw"refs"; | ||
| 71 | require Log::Log4perl::Level; | ||
| 72 | Log::Log4perl::Level->import(__PACKAGE__); | ||
| 73 | Log::Log4perl->import(":easy"); | ||
| 74 | # It took four CPAN uploads and tests to workout why | ||
| 75 | # one user was getting syntax errors for TRACE: must | ||
| 76 | # be the Mithrasmas spirit (hic): | ||
| 77 | if ($Log::Log4perl::VERSION < 1.11){ | ||
| 78 | *{__PACKAGE__."::TRACE"} = *DEBUG; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | use Exporter; | ||
| 84 | use vars qw/ @EXPORT /; | ||
| 85 | |||
| 86 | use base "Tk::Wizard"; | ||
| 87 | @EXPORT = ("MainLoop"); | ||
| 88 | |||
| 89 | use constant DEBUG_FUNC => 0; | ||
| 90 | |||
| 91 | # See INTERNATIONALISATION | ||
| 92 | my %LABELS = ( | ||
| 93 | |||
| 94 | # Buttons | ||
| 95 | BACK => "< Back", | ||
| 96 | NEXT => "Next >", | ||
| 97 | FINISH => "Finish", | ||
| 98 | CANCEL => "Cancel", | ||
| 99 | HELP => "Help", | ||
| 100 | OK => "OK", | ||
| 101 | |||
| 102 | # licence agreement | ||
| 103 | LICENCE_ALERT_TITLE => "Licence Condition", | ||
| 104 | LICENCE_OPTION_NO => "I do not accept the terms of the licence agreement", | ||
| 105 | LICENCE_OPTION_YES => "I accept the terms of the licence agreement", | ||
| 106 | LICENCE_IGNORED => "You must read and agree to the licence before you can use this software.\n\nIf you do not agree to the terms of the licence, you must remove the software from your machine.", | ||
| 107 | LICENCE_DISAGREED => "You must read and agree to the licence before you can use this software.\n\nAs you indicated that you do not agree to the terms of the licence, please remove the software from your machine.\n\nSetup will now exit.", | ||
| 108 | |||
| 109 | # FileList | ||
| 110 | # - supplied as args: see POD for those sections | ||
| 111 | ); | ||
| 112 | |||
| 113 | |||
| 114 | =head1 DESCRIPTION | ||
| 115 | |||
| 116 | This module makes the first moves towards a C<Tk::Wizard> extension | ||
| 117 | to automate software installation, primarily for end-users, in the manner | ||
| 118 | of I<InstallShield>. | ||
| 119 | |||
| 120 | If you are looking for a freeware software installer that is not | ||
| 121 | dependent upon Perl, try I<Inno Setup> - C<http://www.jrsoftware.org/>. It's | ||
| 122 | so good, even Microsoft have been caught using it. | ||
| 123 | |||
| 124 | =head1 METHODS | ||
| 125 | |||
| 126 | C<Tk::Wizard::Installer> supports all the methods and means of L<Tk::Wizard|Tk::Wizard> | ||
| 127 | plus those listed in the remainder of this document. | ||
| 128 | |||
| 129 | =head2 addLicencePage | ||
| 130 | |||
| 131 | $wizard->addLicencePage ( -filepath => $path_to_licence_text ) | ||
| 132 | |||
| 133 | Adds a page (a L<Tk::Frame|Tk::Frame>) that contains a scroll text box | ||
| 134 | of a licence text file specified in the C<-filepath> argument. | ||
| 135 | Presents the user with two options: accept, or don't accept. The user | ||
| 136 | I<cannot> progress until the former option has been chosen. The | ||
| 137 | choice is entered into the object field C<licence_agree>, which is | ||
| 138 | automatically tested when the I<Next> button is pressed, or with the | ||
| 139 | Wizard's C<callback_licence_agreement> function. You can over-ride | ||
| 140 | this last action by providing your own C<-preNextButtonAction> callback. | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | sub addLicencePage { | ||
| 145 | my ( $self, $args ) = ( shift, {@_} ); | ||
| 146 | Carp::croak "No -filepath argument present" if not $args->{-filepath}; | ||
| 147 | my %btn_args = | ||
| 148 | map { my $x = delete $args->{$_}; $_ => $x } | ||
| 149 | grep { /ButtonAction$/ } | ||
| 150 | keys %$args; | ||
| 151 | return $self->addPage( | ||
| 152 | sub { $self->_page_licence_agreement($args) }, | ||
| 153 | %btn_args, | ||
| 154 | ( | ||
| 155 | $args->{'-preNextButtonAction'} | ||
| 156 | ? (-preNextButtonAction => sub { $self->callback_licence_agreement(@_); } ) | ||
| 157 | : () | ||
| 158 | ) | ||
| 159 | ); | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | # PRIVATE METHOD _page_licence_agreement | ||
| 164 | # | ||
| 165 | # Returns a C<Tk::Wizard> page entitled "End-user Licence Agreement", | ||
| 166 | # a scroll-box of the licence text, and an "Agree" and "Disagree" | ||
| 167 | # option. If the user agrees, the caller's package's global (yuck) | ||
| 168 | # C<$LICENCE_AGREE> is set to a Boolean true value. | ||
| 169 | # | ||
| 170 | # See also L</callback_licence_agreement>. | ||
| 171 | |||
| 172 | sub _page_licence_agreement { | ||
| 173 | my ( $self, $args ) = ( shift, shift ); | ||
| 174 | |||
| 175 | my $padx = $self->cget( -style ) eq 'top' ? 30 : 5; | ||
| 176 | $self->{licence_agree} = undef; | ||
| 177 | my $fname = $args->{-filepath}; | ||
| 178 | |||
| 179 | my $text; | ||
| 180 | open my $in, $fname or LOGCROAK "Could not read licence from $fname: $!"; | ||
| 181 | read $in, $text, -s $in; | ||
| 182 | close $in; | ||
| 183 | warn "Licence file $fname is empty!" if not length $text; | ||
| 184 | |||
| 185 | # Clean up line endings because Tk::ROText on *NIX displays them: | ||
| 186 | $text =~ s![\r\n]+!\n!g; | ||
| 187 | my ( $frame, @pl ) = $self->blank_frame( | ||
| 188 | -title => "End-user Licence Agreement", | ||
| 189 | -subtitle => "Please read the following licence agreement carefully.", | ||
| 190 | ); | ||
| 191 | my $t = $frame->Scrolled( | ||
| 192 | 'ROText', | ||
| 193 | -relief => 'sunken', | ||
| 194 | -borderwidth => 2, | ||
| 195 | -font => 'SMALL_FONT', | ||
| 196 | -width => 10, | ||
| 197 | -setgrid => 'true', | ||
| 198 | -height => 9, | ||
| 199 | -scrollbars => 'e', | ||
| 200 | -wrap => 'word', | ||
| 201 | ); | ||
| 202 | $t->insert( '0.0', $text ); | ||
| 203 | $t->configure( -state => "disabled" ); | ||
| 204 | $t->pack(qw/-expand 1 -fill both -padx 10 -pady 10/); | ||
| 205 | my %opts1 = ( | ||
| 206 | -font => $self->{defaultFont}, | ||
| 207 | -text => $LABELS{LICENCE_OPTION_YES}, | ||
| 208 | -variable => \${ $self->{licence_agree} }, | ||
| 209 | -relief => 'flat', | ||
| 210 | -value => 1, | ||
| 211 | -anchor => 'w', | ||
| 212 | ); | ||
| 213 | |||
| 214 | # Setting -background to undef causes core dump deep inside Tk! | ||
| 215 | $opts1{-background} = $self->cget("-background") | ||
| 216 | if $self->cget("-background"); | ||
| 217 | $opts1{-underline} = 2; # Third character is the "A" of "Accept" | ||
| 218 | $self->bind( '<Alt-a>' => sub { ${ $self->{licence_agree} } = 1 } ); | ||
| 219 | my $buttonAccept = $frame->Radiobutton(%opts1)->pack( -padx => $padx, -anchor => 'w', ); | ||
| 220 | |||
| 221 | my %opts2 = ( | ||
| 222 | -font => $self->{defaultFont}, | ||
| 223 | -text => $LABELS{LICENCE_OPTION_NO}, | ||
| 224 | -variable => \${ $self->{licence_agree} }, | ||
| 225 | -relief => 'flat', | ||
| 226 | -value => 0, | ||
| 227 | -anchor => 'w', | ||
| 228 | ); | ||
| 229 | # Setting -background to undef causes core dump deep inside Tk! | ||
| 230 | $opts2{-background} = $self->cget("-background") | ||
| 231 | if $self->cget("-background"); | ||
| 232 | $opts2{-underline} = 5; # 6th character = 'N' of 'Not' | ||
| 233 | $self->bind( '<Alt-n>' => sub { ${ $self->{licence_agree} } = 0 } ); | ||
| 234 | $frame->Radiobutton(%opts2)->pack( -padx => $padx, -anchor => 'w', ); | ||
| 235 | |||
| 236 | if ( $args->{-wait} ) { | ||
| 237 | Tk::Wizard::_fix_wait( \$args->{-wait} ); | ||
| 238 | |||
| 239 | # $frame->after($args->{-wait},sub{$self->forward}); | ||
| 240 | $frame->after( | ||
| 241 | $args->{-wait}, | ||
| 242 | sub { | ||
| 243 | $self->{nextButton}->configure( -state => 'normal' ); | ||
| 244 | $self->{nextButton}->invoke; | ||
| 245 | } | ||
| 246 | ); | ||
| 247 | } | ||
| 248 | return $frame; | ||
| 249 | } | ||
| 250 | |||
| 251 | |||
| 252 | =head2 addFileListPage | ||
| 253 | |||
| 254 | $wizard->addFileListPage ( name1=>value1 ... nameN=>valueN ) | ||
| 255 | |||
| 256 | Adds a page (a L<Tk::Frame|Tk::Frame>) that contains a progress bar | ||
| 257 | (L<Tk::ProgressBar|Tk::ProgressBar>) which is updated as a supplied | ||
| 258 | list of files is copied or moved from one location to another. | ||
| 259 | |||
| 260 | The I<Next> and I<Back> buttons of the Wizard are disabled whilst | ||
| 261 | the process takes place. | ||
| 262 | |||
| 263 | The two arguments (below) C<-to> and C<-from> should be references | ||
| 264 | to arrays (or anonymous arrays), where entries in the former are | ||
| 265 | moved or copied to the locations specified to the equivalent | ||
| 266 | entries in the latter, renaming and path creation occurring as needed: | ||
| 267 | |||
| 268 | -copy => 1, | ||
| 269 | -from => [ | ||
| 270 | '/html/index.html', | ||
| 271 | '/html/imgs/index.gif', | ||
| 272 | '/html/oldname.html' | ||
| 273 | ], | ||
| 274 | -to => [ | ||
| 275 | '/docs/', | ||
| 276 | '/docs/imgs/', | ||
| 277 | '/html/newname.html' | ||
| 278 | ], | ||
| 279 | |||
| 280 | The above example | ||
| 281 | copies C<index.html> to C</docs/index.html>, C<index.gif> is copied to | ||
| 282 | become C</docs/imgs/index.gif>, and C<oldname.html> is copied to | ||
| 283 | C<newname.html> in the same C<html> directory. | ||
| 284 | |||
| 285 | Arguments: | ||
| 286 | |||
| 287 | =over 4 | ||
| 288 | |||
| 289 | =item -title | ||
| 290 | |||
| 291 | =item -subtitle | ||
| 292 | |||
| 293 | =item -text | ||
| 294 | |||
| 295 | See L<Tk::Wizard/blank_frame>. | ||
| 296 | |||
| 297 | =item -copy | ||
| 298 | |||
| 299 | =item -move | ||
| 300 | |||
| 301 | Setting one or the other will determine whether files are copied (without deletion of originals) | ||
| 302 | or moved (with deletion of originals). The default action is the former. | ||
| 303 | |||
| 304 | =item -from | ||
| 305 | |||
| 306 | Reference to an array of locations to copy/move from | ||
| 307 | |||
| 308 | =item -to | ||
| 309 | |||
| 310 | Reference to an array of locations to move/copy to | ||
| 311 | |||
| 312 | =item -delay | ||
| 313 | |||
| 314 | Delay (in mS) before copying begins (see L<Tk::after>). Default is 1000. | ||
| 315 | |||
| 316 | =item -wait | ||
| 317 | |||
| 318 | Prevents display of the next Wizard page once the job is done. | ||
| 319 | |||
| 320 | =item -bar | ||
| 321 | |||
| 322 | A list of properties to pass to the L<Tk::ProgessBar|Tk::ProgessBar> | ||
| 323 | object created and used in this routine. Assumes reasonable defaults. | ||
| 324 | |||
| 325 | =item -label_frame_title | ||
| 326 | |||
| 327 | Text for the label frame (L<Tk::LabFrame|Tk::LabFrame> object) which | ||
| 328 | contains our moving parts. Defaults to C<Copying Files>. | ||
| 329 | |||
| 330 | =item -label_preparing | ||
| 331 | |||
| 332 | Text for label displayed whilst counting files to be copied. | ||
| 333 | Defaults to C<Preparing...>. | ||
| 334 | |||
| 335 | =item -label_from | ||
| 336 | |||
| 337 | The text of the label prepended to the name of the directory being copied. | ||
| 338 | Defaults to C<From:>. | ||
| 339 | |||
| 340 | =item -label_file | ||
| 341 | |||
| 342 | The text of the label prepended to the name of the file being copied. | ||
| 343 | Defaults to C<Copying:>. | ||
| 344 | |||
| 345 | =item -on_error | ||
| 346 | |||
| 347 | A code reference to handle errors, which are detailed in the anonymous hash C<-failed>, | ||
| 348 | where names are filenames and values are the error messages. | ||
| 349 | If not supplied, calls L</pre_install_files_quit>. | ||
| 350 | |||
| 351 | =item -uninstall_db | ||
| 352 | |||
| 353 | Path at which to store an uninstall database file for use with | ||
| 354 | L<addUninstallPage|addUninstallPage>. This path will be used | ||
| 355 | to create two files, one with a C<dir> extension and one | ||
| 356 | with a C<pag> extension. | ||
| 357 | |||
| 358 | =item -uninstall_db_perms | ||
| 359 | |||
| 360 | If you supply C<-uninstall_db> to create an uninstaller, | ||
| 361 | you may use this argument to provide a file permissions | ||
| 362 | for the db files; otherwise, the default is C<0666>. | ||
| 363 | Please see the notes in C<SDBM_File> that explain why | ||
| 364 | C<0666> is a good choice. | ||
| 365 | |||
| 366 | =back | ||
| 367 | |||
| 368 | =cut | ||
| 369 | |||
| 370 | # Internally (as private as it gets in Perl): | ||
| 371 | # | ||
| 372 | # =item -bar | ||
| 373 | # | ||
| 374 | # Confusingly, a progressBar object to update | ||
| 375 | # | ||
| 376 | # =item -labelFrom | ||
| 377 | # | ||
| 378 | # Label object to update | ||
| 379 | # | ||
| 380 | # =item -labelTo | ||
| 381 | # | ||
| 382 | # Label object to update | ||
| 383 | # | ||
| 384 | sub addFileListPage { | ||
| 385 | my ( $self, $args ) = ( shift, {@_} ); | ||
| 386 | # $self->addPage( sub { $self->_page_filelist($args) } ); | ||
| 387 | my %btn_args = | ||
| 388 | map { my $x = delete $args->{$_}; $_ => $x } | ||
| 389 | grep { /ButtonAction$/ } | ||
| 390 | keys %$args; | ||
| 391 | return $self->addPage( sub { $self->_page_filelist($args) }, %btn_args ); | ||
| 392 | } | ||
| 393 | |||
| 394 | =head2 addFileCopyPage | ||
| 395 | |||
| 396 | Alias for L<addFileListPage|addFileListPage>. | ||
| 397 | |||
| 398 | =cut | ||
| 399 | |||
| 400 | sub addFileCopyPage { | ||
| 401 | my $self = shift; | ||
| 402 | $self->addFileListPage(@_); | ||
| 403 | } | ||
| 404 | |||
| 405 | sub _page_filelist { | ||
| 406 | my ( $self, $args ) = ( shift, shift ); | ||
| 407 | |||
| 408 | Carp::croak "Arguments should be supplied as a hash ref" | ||
| 409 | if not ref $args or ref $args ne "HASH"; | ||
| 410 | Carp::croak "-from and -to are required" | ||
| 411 | if not $args->{-from} or not $args->{-to}; | ||
| 412 | Carp::croak "-from and -to are different lengths" | ||
| 413 | if $#{ $args->{-from} } != $#{ $args->{-to} }; | ||
| 414 | Carp::croak "Nothing to do! -from and -to are empty" | ||
| 415 | if $#{ $args->{-from} } == -1 or $#{ $args->{-to} } == -1; | ||
| 416 | |||
| 417 | # Uninstaller | ||
| 418 | if (exists $args->{-uninstall_db}){ | ||
| 419 | DEBUG "Setting-up uninstall SDBM"; | ||
| 420 | $self->{_uninstall_db_path} = $args->{-uninstall_db}; | ||
| 421 | $self->{_uninstall_db} = {}; | ||
| 422 | tie( | ||
| 423 | %{$self->{_uninstall_db}}, | ||
| 424 | 'SDBM_File', | ||
| 425 | $self->{_uninstall_db_path}, | ||
| 426 | O_WRONLY | O_CREAT, | ||
| 427 | $args->{-uninstall_db_perms} || 0666, | ||
| 428 | ) or die "Could not create uninstaller db file - failed to tie `SDBM file '$self->{_uninstall_db_path}': $!; aborting"; | ||
| 429 | } | ||
| 430 | |||
| 431 | my $frame = $self->blank_frame( | ||
| 432 | -title => $args->{-title} || "Copying Files", | ||
| 433 | -subtitle => $args->{-subtitle} || "Please wait whilst Setup copies files to your computer.", | ||
| 434 | -text => $args->{-text} || "\n" | ||
| 435 | ); | ||
| 436 | |||
| 437 | my %bar; # progress bar args | ||
| 438 | if ( $args->{-bar} ) { | ||
| 439 | %bar = @{ $args->{-bar} }; | ||
| 440 | |||
| 441 | # insert error checking here... | ||
| 442 | } | ||
| 443 | $bar{-gap} = 0 unless defined $bar{-gap}; | ||
| 444 | $bar{-blocks} = 0 unless defined $bar{-blocks}; | ||
| 445 | $bar{-colors} = [ 0 => 'blue' ] unless $bar{-colors}; | ||
| 446 | $bar{-borderwidth} = 2 unless $bar{-borderwidth}; | ||
| 447 | $bar{-relief} = 'sunken' unless $bar{-relief}; | ||
| 448 | $bar{-from} = 0 unless $bar{-from}; | ||
| 449 | $bar{-to} = 100 unless $bar{-to}; | ||
| 450 | |||
| 451 | my $f = $frame->LabFrame( | ||
| 452 | -label => $args->{-label_frame_title} || "Copying files", | ||
| 453 | -labelside => "acrosstop" | ||
| 454 | ); | ||
| 455 | $args->{-labelFrom} = $f->Label(qw//)->pack(qw/-padx 16 -side top -anchor w/); | ||
| 456 | $args->{-labelTo} = $f->Label(qw//)->pack(qw/-padx 16 -side top -anchor w/); | ||
| 457 | $self->{-bar} = $f->ProgressBar(%bar)->pack(qw/ -padx 20 -pady 10 -side top -anchor w -fill both -expand 1 /); | ||
| 458 | $f->pack(qw/-fill x -padx 30/); | ||
| 459 | |||
| 460 | $self->{nextButton}->configure( -state => "disable" ); | ||
| 461 | $self->{backButton}->configure( -state => "disable" ); | ||
| 462 | |||
| 463 | $self->{-bar}->after( | ||
| 464 | $args->{-delay} || 1000, | ||
| 465 | sub { | ||
| 466 | my $todo = $self->_pre_install_files($args); | ||
| 467 | DEBUG "Configure bar to $todo"; | ||
| 468 | $self->{-bar}->configure( -to => $todo ); | ||
| 469 | $self->_install_files($args); | ||
| 470 | $self->{nextButton}->configure( -state => "normal" ); | ||
| 471 | $self->{backButton}->configure( -state => "normal" ); | ||
| 472 | |||
| 473 | if ( $args->{-wait} ) { | ||
| 474 | Tk::Wizard::_fix_wait( \$args->{-wait} ); | ||
| 475 | |||
| 476 | $frame->after( | ||
| 477 | $args->{-wait}, | ||
| 478 | sub { | ||
| 479 | $self->{nextButton}->configure( -state => 'normal' ); | ||
| 480 | $self->{nextButton}->invoke; | ||
| 481 | } | ||
| 482 | ); | ||
| 483 | } | ||
| 484 | } | ||
| 485 | ); | ||
| 486 | return $frame; | ||
| 487 | } | ||
| 488 | |||
| 489 | |||
| 490 | # Pre-parse, counting files and expanding directories if necessary | ||
| 491 | # Return total number of files to process | ||
| 492 | # Puts any failures into %{$self->{-failed}} | ||
| 493 | sub _pre_install_files { | ||
| 494 | my ( $self, $args ) = ( shift, shift ); | ||
| 495 | Carp::croak "Arguments should be supplied as a hash ref" | ||
| 496 | if not ref $args or ref $args ne "HASH"; | ||
| 497 | Carp::croak "-from and -to are different lengths" | ||
| 498 | if $#{ $args->{-from} } != $#{ $args->{-from} }; | ||
| 499 | |||
| 500 | my $total = 0; | ||
| 501 | my $i = -1; | ||
| 502 | $args->{-labelFrom}->configure( -text => $args->{-label_preparing} || "Preparing..." ); | ||
| 503 | $args->{-labelFrom}->update; | ||
| 504 | $args->{-labelTo}->configure( -text => "" ); | ||
| 505 | $args->{-labelFrom}->update; | ||
| 506 | $self->{-failed} = {}; | ||
| 507 | |||
| 508 | # Make a local copy of our args: | ||
| 509 | my @asToOrig = @{ $args->{-to} }; | ||
| 510 | my @asFromOrig = @{ $args->{-from} }; | ||
| 511 | my ( @asFrom, @asTo ); | ||
| 512 | |||
| 513 | # Process parallel lists: | ||
| 514 | FILELIST_ELEMENT: | ||
| 515 | foreach my $sTo (@asToOrig) { | ||
| 516 | my $sFrom = shift @asFromOrig; | ||
| 517 | $i++; | ||
| 518 | if ( -d $sFrom ) { | ||
| 519 | |||
| 520 | # Sanity check: | ||
| 521 | if ( !-d $sTo ) { | ||
| 522 | $self->{-failed}->{$sTo} = qq{Can not copy directory $sFrom to file}; | ||
| 523 | next FILELIST_ELEMENT; | ||
| 524 | } | ||
| 525 | if ( !opendir DIR, $sFrom ) { | ||
| 526 | $self->{-failed}->{$sFrom} = qq{Can not read directory}; | ||
| 527 | next FILELIST_ELEMENT; | ||
| 528 | } | ||
| 529 | foreach ( grep { !/^\.{1,2}$/ } readdir DIR ) { | ||
| 530 | push @asFrom, "$sFrom/$_"; | ||
| 531 | push @asTo, "$sTo/$_"; | ||
| 532 | $total++; | ||
| 533 | } | ||
| 534 | closedir DIR or warn "Could not closedir: $!"; | ||
| 535 | next FILELIST_ELEMENT; | ||
| 536 | } | ||
| 537 | |||
| 538 | # Files: | ||
| 539 | elsif ( -f $sFrom ) { | ||
| 540 | if ( -d $sTo ) { | ||
| 541 | # Copy from file to directory: | ||
| 542 | my ( $sJunkVol, $sJunkPath, $fname ) = File::Spec->splitpath($sFrom); | ||
| 543 | $sTo = "$sTo/$fname"; | ||
| 544 | } | ||
| 545 | push @asFrom, $sFrom; | ||
| 546 | push @asTo, $sTo; | ||
| 547 | $total++; | ||
| 548 | } | ||
| 549 | else { | ||
| 550 | $self->{-failed}->{$sFrom} = qq{No such file or directory}; | ||
| 551 | } | ||
| 552 | } | ||
| 553 | |||
| 554 | if ( scalar keys %{ $self->{-failed} } > 0 ) { | ||
| 555 | DEBUG "Failed " . ( scalar keys %{ $self->{-failed} } );; | ||
| 556 | if ( ref $args->{-on_error} eq 'CODE' ) { | ||
| 557 | DEBUG "Calling -on_error handler."; | ||
| 558 | &{ $args->{-on_error} }; | ||
| 559 | } | ||
| 560 | else { | ||
| 561 | DEBUG "Calling self/pre_install_files_quit."; | ||
| 562 | $self->pre_install_files_quit( scalar keys %{ $self->{-failed} } ); | ||
| 563 | } | ||
| 564 | } | ||
| 565 | @{ $args->{-from} } = @asFrom; | ||
| 566 | @{ $args->{-to} } = @asTo; | ||
| 567 | return $total; # why was it total+1? | ||
| 568 | } | ||
| 569 | |||
| 570 | |||
| 571 | # See page_filelist | ||
| 572 | sub _install_files { | ||
| 573 | my ( $self, $args ) = ( shift, shift ); | ||
| 574 | Carp::croak "Arguments should be supplied as a hash ref" | ||
| 575 | if not ref $args or ref $args ne "HASH"; | ||
| 576 | Carp::croak "-from and -to are different lengths" | ||
| 577 | if $#{ $args->{-from} } != $#{ $args->{-from} }; | ||
| 578 | |||
| 579 | $args->{-label_from} = $args->{-move} ? 'Moving: ' : "Copying: " | ||
| 580 | if not $args->{-label_from}; | ||
| 581 | $args->{-label_file} = "To: " if not $args->{-label_file}; | ||
| 582 | $args->{-slowdown} ||= 0; | ||
| 583 | my $total = 0; | ||
| 584 | my $i = -1; | ||
| 585 | |||
| 586 | foreach ( @{ $args->{-to} } ) { | ||
| 587 | $i++; | ||
| 588 | |||
| 589 | # Directories: | ||
| 590 | if ( -d @{ $args->{-from} }[$i] ) { | ||
| 591 | local *DIR; | ||
| 592 | my $orig_dir = cwd; | ||
| 593 | chdir @{ $args->{-from} }[$i] or die "'From' dir does not exist - ".@{ $args->{-from} }[$i]; | ||
| 594 | opendir DIR, "." or warn 'Could not open directory ',$!; | ||
| 595 | foreach ( grep { !/^\.\.?$/ } readdir DIR ) { | ||
| 596 | push @{ $args->{-from} }, @{ $args->{-from} }[$i] . "/" . $_; | ||
| 597 | push @{ $args->{-to} }, @{ $args->{-to} }[$i] . "/" . $_; | ||
| 598 | } | ||
| 599 | closedir DIR or warn 'Could not close dir, ',$!; | ||
| 600 | chdir $orig_dir; | ||
| 601 | next; | ||
| 602 | } | ||
| 603 | |||
| 604 | # Files: | ||
| 605 | elsif ( -r @{ $args->{-from} }[$i] ) { | ||
| 606 | |||
| 607 | # update the display | ||
| 608 | my ( $fv, $fd, $ff ) = File::Spec->splitpath( @{ $args->{-from} }[$i] ); | ||
| 609 | my ( $tv, $td, $tf ) = File::Spec->splitpath( @{ $args->{-to} }[$i] ); | ||
| 610 | $args->{-labelFrom}->configure( -text => $args->{-label_from} . @{ $args->{-from} }[$i] ); | ||
| 611 | $args->{-labelFrom}->update; | ||
| 612 | $args->{-labelTo}->configure( -text => $args->{-label_file} . @{ $args->{-to} }[$i] ); | ||
| 613 | $args->{-labelTo}->update; | ||
| 614 | $self->{-bar}->value( $self->{-bar}->value + 1 ); | ||
| 615 | DEBUG "Updating bar to " . $self->{-bar}->value; | ||
| 616 | $self->{-bar}->update; | ||
| 617 | |||
| 618 | # Make the TO path, if needs be | ||
| 619 | my $d = File::Spec->catpath( $tv, $td, '' ); | ||
| 620 | DEBUG "Check dir $d"; | ||
| 621 | if ( !-d $d ) { | ||
| 622 | eval { File::Path::mkpath($d) }; | ||
| 623 | Carp::croak "Could not make path $d : $!" if $@; | ||
| 624 | DEBUG "Made $d"; | ||
| 625 | if (exists $self->{_uninstall_db} ){ | ||
| 626 | $self->{_uninstall_db}->{ Cwd::abs_path( $d ) } ++ | ||
| 627 | } | ||
| 628 | } | ||
| 629 | |||
| 630 | # Do the move/copy | ||
| 631 | if ( $args->{-move} ) { | ||
| 632 | if ( move( @{ $args->{-from} }[$i], @{ $args->{-to} }[$i] ) ) { | ||
| 633 | if (exists $self->{_uninstall_db} ){ | ||
| 634 | $self->{_uninstall_db}->{ | ||
| 635 | Cwd::abs_path( @{ $args->{-to} }[$i] ) | ||
| 636 | } ++ | ||
| 637 | } | ||
| 638 | } | ||
| 639 | else { | ||
| 640 | $self->{-failed}->{ @{ $args->{-to} }[$i] } = "Could not write file"; | ||
| 641 | @{ $args->{-from} }[$i] = undef; | ||
| 642 | } | ||
| 643 | } | ||
| 644 | else { | ||
| 645 | if ( copy( @{ $args->{-from} }[$i], @{ $args->{-to} }[$i] ) ) { | ||
| 646 | if (exists $self->{_uninstall_db} ){ | ||
| 647 | $self->{_uninstall_db}->{ | ||
| 648 | Cwd::abs_path( @{ $args->{-to} }[$i] ) | ||
| 649 | } ++ | ||
| 650 | } | ||
| 651 | } | ||
| 652 | else { | ||
| 653 | $self->{-failed}->{ @{ $args->{-to} }[$i] } = "Could not write file"; | ||
| 654 | @{ $args->{-from} }[$i] = undef; | ||
| 655 | } | ||
| 656 | } | ||
| 657 | } | ||
| 658 | |||
| 659 | else { | ||
| 660 | $self->{-failed}->{ @{ $args->{-from} }[$i] } = "Could not read file"; | ||
| 661 | @{ $args->{-from} }[$i] = undef; | ||
| 662 | } | ||
| 663 | |||
| 664 | DEBUG "slowdown is =$args->{-slowdown}=\n"; | ||
| 665 | sleep( $args->{-slowdown} / 1000 ) if exists $args->{-slowdown}; | ||
| 666 | } | ||
| 667 | |||
| 668 | if ( scalar keys %{ $self->{-failed} } > 0 ) { | ||
| 669 | ERROR "Failed " . ( scalar keys %{ $self->{-failed} } ); | ||
| 670 | if ( ref $args->{-on_error} eq 'CODE' ) { | ||
| 671 | TRACE "# Calling -on_error handler."; | ||
| 672 | &{ $args->{-on_error} }; | ||
| 673 | } | ||
| 674 | else { | ||
| 675 | TRACE "# Calling pre_install_files_quit."; | ||
| 676 | $self->pre_install_files_quit( scalar keys %{ $self->{-failed} } ); | ||
| 677 | } | ||
| 678 | } | ||
| 679 | |||
| 680 | else { | ||
| 681 | $args->{-labelFrom}->configure( -text => 'Completed.' ); | ||
| 682 | $args->{-labelTo}->configure( -text => '' ); | ||
| 683 | } | ||
| 684 | |||
| 685 | return $total; | ||
| 686 | } | ||
| 687 | |||
| 688 | |||
| 689 | =head2 addDownloadPage | ||
| 690 | |||
| 691 | $wizard->addDownloadPage( name1 => value1 ... nameN => valueN ) | ||
| 692 | |||
| 693 | Adds a page that will attempt to download specified | ||
| 694 | files to specified locations, updating two progress bars in the | ||
| 695 | process. | ||
| 696 | |||
| 697 | If a file cannot be downloaded, the user will be prompted to try | ||
| 698 | again. If the user sooner or later wishes to carry on even though | ||
| 699 | a file has not downloaded, the calling C<Wizard>'s C<-failed> | ||
| 700 | slot is filled with the URIs of the files that could not be downloaded, | ||
| 701 | and the supplied C<-on_error> argument comes into play - see below. | ||
| 702 | If no C<-on_error> parameter is provided, the Wizard will continue. | ||
| 703 | |||
| 704 | The I<Next> and I<Back> buttons of the Wizard are disabled whilst | ||
| 705 | the download process takes place. | ||
| 706 | |||
| 707 | =over 4 | ||
| 708 | |||
| 709 | =item -files | ||
| 710 | |||
| 711 | A reference to a hash, where keys are URIs and | ||
| 712 | values are local locations to place the contents of those URIs. | ||
| 713 | |||
| 714 | WARNING: Files that are successfully downloaded will be deleted from | ||
| 715 | the hash. | ||
| 716 | |||
| 717 | =item -wait | ||
| 718 | |||
| 719 | If supplied, the frame will remain on the screen when the download | ||
| 720 | is complete - default is to automate a click on the C<next> button | ||
| 721 | once the downloads are completed without errors. | ||
| 722 | |||
| 723 | =item -bar | ||
| 724 | |||
| 725 | A list of properties to pass to the L<Tk::ProgessBar|Tk::ProgessBar> | ||
| 726 | object created and used in this routine. Assumes reasonable defaults. | ||
| 727 | |||
| 728 | =item -no_retry | ||
| 729 | |||
| 730 | If set to a true value, will prevent the I<Try again?> dialogue. | ||
| 731 | |||
| 732 | =item -on_error | ||
| 733 | |||
| 734 | If a file cannot be downloaded and the user chooses not to keep trying, | ||
| 735 | then this parameter comes into operation. If it is a reference, then it | ||
| 736 | is assumed to be a code reference to execute; otherwise a dialogue box | ||
| 737 | asks the user if they really wish to quit. If they do, then the | ||
| 738 | Tk::Wizard CloseWindow event cycle is triggered -- the | ||
| 739 | default result of which is probably yet another confirmation of closure.... | ||
| 740 | |||
| 741 | If no C<-on_error> parameter is provided, the Wizard will continue even | ||
| 742 | if it cannot download the requested data. | ||
| 743 | |||
| 744 | =item -done_text | ||
| 745 | |||
| 746 | Text to display when complete. Default: I<complete>. | ||
| 747 | |||
| 748 | =back | ||
| 749 | |||
| 750 | Would it be useful to implement globbing for FTP URIs? | ||
| 751 | |||
| 752 | =cut | ||
| 753 | |||
| 754 | sub addDownloadPage { | ||
| 755 | my ( $self, $args ) = ( shift, {@_} ); | ||
| 756 | # $self->addPage( sub { $self->_page_download($args) } ); | ||
| 757 | my %btn_args = | ||
| 758 | map { my $x = delete $args->{$_}; $_ => $x } | ||
| 759 | grep { /ButtonAction$/ } | ||
| 760 | keys %$args; | ||
| 761 | return $self->addPage( sub { $self->_page_download($args) }, %btn_args ); | ||
| 762 | } | ||
| 763 | |||
| 764 | sub _page_download { | ||
| 765 | my ( $self, $args ) = ( shift, shift ); | ||
| 766 | Carp::croak "Arguments should be supplied as a hash ref" | ||
| 767 | if not ref $args or ref $args ne "HASH"; | ||
| 768 | Carp::croak "-files is required" if not $args->{-files}; | ||
| 769 | Carp::croak "-files should be a hash of uri => filepath pairs" | ||
| 770 | if ref $args->{-files} ne 'HASH'; | ||
| 771 | |||
| 772 | my @failed; | ||
| 773 | my $frame = $self->blank_frame( | ||
| 774 | -title => $args->{-title} || "Downloading Files", | ||
| 775 | -subtitle => $args->{-subtitle} || "Please wait whilst Setup downloads files to your computer.", | ||
| 776 | -text => $args->{-text} || "\n" | ||
| 777 | ); | ||
| 778 | my %bar; # progress bar args | ||
| 779 | |||
| 780 | if ( defined $args->{-bar} ) { | ||
| 781 | %bar = @{ $args->{-bar} }; | ||
| 782 | # insert error checking here... | ||
| 783 | } | ||
| 784 | $bar{-gap} = 0 unless defined $bar{-gap}; | ||
| 785 | $bar{-blocks} = 0 unless defined $bar{-blocks}; | ||
| 786 | $bar{-colors} = [ 0 => 'blue' ] unless $bar{-colors}; | ||
| 787 | $bar{-borderwidth} = 2 unless $bar{-borderwidth}; | ||
| 788 | $bar{-relief} = 'sunken' unless $bar{-relief}; | ||
| 789 | $bar{-from} = 0 unless $bar{-from}; | ||
| 790 | $bar{-to} = 100 unless $bar{-to}; | ||
| 791 | |||
| 792 | my $all = $frame->LabFrame( | ||
| 793 | -label => $args->{-label_all_files} || "Over-all Progress", | ||
| 794 | -labelside => "acrosstop" | ||
| 795 | ); | ||
| 796 | $self->{-bar} = $all->ProgressBar(%bar)->pack(qw/ -padx 20 -pady 10 -side top -anchor w -fill both -expand 1 /); | ||
| 797 | $all->pack(qw/-fill x -padx 30/); | ||
| 798 | |||
| 799 | $args->{file_label} = $frame->LabFrame( | ||
| 800 | -label => $args->{-label_this_file} || "This FIle", | ||
| 801 | -labelside => "acrosstop" | ||
| 802 | ); | ||
| 803 | $args->{-file_bar} = | ||
| 804 | $args->{file_label}->ProgressBar(%bar)->pack(qw/ -padx 20 -pady 10 -side top -anchor w -fill both -expand 1 /); | ||
| 805 | |||
| 806 | $args->{file_label}->pack(qw/-fill x -padx 30/); | ||
| 807 | $self->{nextButton}->configure( -state => "disable" ); | ||
| 808 | $self->{backButton}->configure( -state => "disable" ); | ||
| 809 | |||
| 810 | $self->{-bar}->after( | ||
| 811 | $args->{-delay} || 10, | ||
| 812 | sub { | ||
| 813 | require LWP::UserAgent; | ||
| 814 | require HTTP::Request; | ||
| 815 | while ( scalar keys %{ $args->{-files} } > 0 ) { | ||
| 816 | $args->{file_label}->configure( -label => 'Preparing to download...' ); | ||
| 817 | $args->{file_label}->update; | ||
| 818 | $self->{-bar}->value(0); | ||
| 819 | $self->{-bar}->configure( -to => scalar keys %{ $args->{-files} } ); | ||
| 820 | |||
| 821 | foreach my $uri ( keys %{ $args->{-files} } ) { | ||
| 822 | TRACE "Try $args->{-files}->{$uri}"; | ||
| 823 | my ($uri_msg) = $uri =~ m!^\w+:/+[^/]+(.*?)/?$!; | ||
| 824 | $args->{file_label}->configure( -label => $uri_msg || "Current File" ); | ||
| 825 | $args->{file_label}->update; | ||
| 826 | if ( | ||
| 827 | $self->_read_uri( | ||
| 828 | bar => $args->{-file_bar}, | ||
| 829 | uri => $uri, | ||
| 830 | target => $args->{-files}->{$uri}, | ||
| 831 | ) | ||
| 832 | ) | ||
| 833 | { | ||
| 834 | delete $args->{-files}->{$uri}; | ||
| 835 | } | ||
| 836 | $self->{-bar}->value( $self->{-bar}->value + 1 ); | ||
| 837 | $self->{-bar}->update; | ||
| 838 | $args->{-file_bar}->configure( -to => 0 ); | ||
| 839 | $args->{-file_bar}->value(0); | ||
| 840 | $args->{-file_bar}->update; | ||
| 841 | } | ||
| 842 | |||
| 843 | if ( scalar keys %{ $args->{-files} } > 0 ) { | ||
| 844 | DEBUG "Files left: ", ( scalar keys %{ $args->{-files} } ); | ||
| 845 | if ( $args->{-no_retry} | ||
| 846 | || !$self->confirm_download_again( scalar keys %{ $args->{-files} } ) ) | ||
| 847 | { | ||
| 848 | INFO "Not trying again"; | ||
| 849 | $self->{-failed} = $args->{-files}; | ||
| 850 | $args->{-files} = {}; | ||
| 851 | } | ||
| 852 | } | ||
| 853 | } | ||
| 854 | |||
| 855 | if ( scalar keys %{ $self->{-failed} } > 0 and $args->{-on_error} ) { | ||
| 856 | DEBUG "Failed to download"; | ||
| 857 | if ( ref $args->{-on_error} eq 'CODE' ) { | ||
| 858 | DEBUG "Calling -on_error handler."; | ||
| 859 | &{ $args->{-on_error} }; | ||
| 860 | } | ||
| 861 | elsif ( $args->{-on_error} ) { | ||
| 862 | DEBUG "Calling self/download_quit."; | ||
| 863 | $self->download_quit( scalar keys %{ $self->{-failed} } ); | ||
| 864 | } | ||
| 865 | } | ||
| 866 | else { | ||
| 867 | INFO "Failures: ", scalar keys %{ $self->{-failed} }; | ||
| 868 | foreach ( keys %{ $self->{-failed} } ) { | ||
| 869 | INFO "\t$_\n"; | ||
| 870 | } | ||
| 871 | $self->{-failed} = 0; | ||
| 872 | } | ||
| 873 | $self->{-bar}->packForget; | ||
| 874 | $args->{-file_bar}->packForget; | ||
| 875 | $args->{file_label}->packForget; | ||
| 876 | $all->packForget; | ||
| 877 | $frame->Label( -text => $args->{-done_text} || "Finished", )->pack( -fill => "both", -expand => 1 ); | ||
| 878 | |||
| 879 | # $self->{backButton}->configure(-state=>"normal"); | ||
| 880 | $self->{nextButton}->configure( -state => "normal" ); | ||
| 881 | if ( $args->{-wait} ) { | ||
| 882 | Tk::Wizard::_fix_wait( \$args->{-wait} ); | ||
| 883 | |||
| 884 | $frame->after( | ||
| 885 | $args->{-wait}, | ||
| 886 | sub { | ||
| 887 | $self->{nextButton}->configure( -state => 'normal' ); | ||
| 888 | $self->{nextButton}->invoke; | ||
| 889 | } | ||
| 890 | ); | ||
| 891 | } | ||
| 892 | } | ||
| 893 | ); | ||
| 894 | return $frame; | ||
| 895 | } | ||
| 896 | |||
| 897 | # c/o PPM.pm | ||
| 898 | sub _read_uri { | ||
| 899 | my ( $self, $args ) = ( shift, {@_} ); | ||
| 900 | carp "Require uri param" unless defined $args->{uri}; | ||
| 901 | carp "Require target param" unless defined $args->{target}; | ||
| 902 | my ( $proxy_user, $proxy_pass ); | ||
| 903 | ( $self->{response}, $self->{bytes_transferred}, $self->{errstr} ) = ( undef, 0, undef ); | ||
| 904 | my $ua = LWP::UserAgent->new; | ||
| 905 | $ua->timeout( $args->{timeout} || 10 ); | ||
| 906 | $ua->agent( $ENV{HTTP_PROXY_AGENT} || ( "$0/$Tk::Wizard::Installer::VERSION " . $ua->agent ) ); | ||
| 907 | |||
| 908 | if ( defined $args->{proxy} ) { | ||
| 909 | $proxy_user = $args->{HTTP_PROXY_USER}; | ||
| 910 | $proxy_pass = $args->{HTTP_PROXY_PASS}; | ||
| 911 | DEBUG "_read_uri: calling env_proxy: $args->{http_proxy}"; | ||
| 912 | $ua->env_proxy; | ||
| 913 | } | ||
| 914 | elsif ( defined $ENV{HTTP_PROXY} ) { | ||
| 915 | $proxy_user = $ENV{HTTP_PROXY_USER}; | ||
| 916 | $proxy_pass = $ENV{HTTP_PROXY_PASS}; | ||
| 917 | DEBUG "_read_uri: calling env_proxy: $ENV{HTTP_proxy}"; | ||
| 918 | $ua->env_proxy; | ||
| 919 | } | ||
| 920 | my $req = HTTP::Request->new( GET => $args->{uri} ); | ||
| 921 | if ( defined $proxy_user and defined $proxy_pass ) { | ||
| 922 | DEBUG "_read_uri: calling proxy_authorization_basic($proxy_user, $proxy_pass)"; | ||
| 923 | $req->proxy_authorization_basic( $proxy_user, $proxy_pass ); | ||
| 924 | } | ||
| 925 | |||
| 926 | # update the progress bar | ||
| 927 | ( $self->{response}, $self->{bytes_transferred} ) = ( undef, 0 ); | ||
| 928 | $self->{response} = $ua->request( $req, sub { &_lwp_callback( $self, $args->{bar}, @_ ) },, 4096 ); | ||
| 929 | if ( $self->{response} && $self->{response}->is_success ) { | ||
| 930 | my ( $dirs, $file ) = $args->{target} =~ /^(.*?)([^\\\/]+)$/; | ||
| 931 | if ( $dirs and $dirs !~ /^\.{1,2}$/ and !-d $dirs ) { | ||
| 932 | eval { File::Path::mkpath($dirs) }; | ||
| 933 | if ($@) { | ||
| 934 | Carp::croak "Could not make path $dirs : $!"; | ||
| 935 | } | ||
| 936 | } | ||
| 937 | my $TARGET; | ||
| 938 | if ( !open $TARGET, '>', $args->{target} ) { | ||
| 939 | ERROR "_read_uri: Couldn't open $args->{target} for writing"; | ||
| 940 | $self->{errstr} = "Couldn't open $args->{target} for writing: $!\n"; | ||
| 941 | return; | ||
| 942 | } | ||
| 943 | DEBUG "# Writing to $args->{target}..."; | ||
| 944 | $TARGET->binmode; | ||
| 945 | $TARGET->print( $self->{response}->content ) or warn; | ||
| 946 | $TARGET->close or warn; | ||
| 947 | return 1; | ||
| 948 | } | ||
| 949 | |||
| 950 | my $sMsg = "Error(2) reading $args->{uri}\n"; | ||
| 951 | if ( $self->{response} ) { | ||
| 952 | $sMsg = | ||
| 953 | join( ' ', qq{Error(1) reading $args->{uri}:}, $self->{response}->code, $self->{response}->message, "\n" ); | ||
| 954 | } | ||
| 955 | DEBUG "_read_uri: $sMsg"; | ||
| 956 | $self->{errstr} = $sMsg; | ||
| 957 | return 0; | ||
| 958 | } | ||
| 959 | |||
| 960 | # c/o PPM.pm | ||
| 961 | sub _lwp_callback { | ||
| 962 | my $self = shift; | ||
| 963 | my ( $bar, $data, $res, $protocol ) = @_; | ||
| 964 | $bar->configure( -to => $res->header('Content-Length') ); | ||
| 965 | |||
| 966 | # $bar->configure(-to => $res->{_headers}->content_length); | ||
| 967 | $bar->value( $bar->value + length $data ); | ||
| 968 | $bar->update; | ||
| 969 | $self->{response} = $res; | ||
| 970 | $self->{response}->add_content($data); | ||
| 971 | $self->{bytes_transferred} += length $data; | ||
| 972 | } | ||
| 973 | |||
| 974 | |||
| 975 | =head1 CALLBACKS | ||
| 976 | |||
| 977 | =head2 callback_licence_agreement | ||
| 978 | |||
| 979 | Intended to be used with an action-event handler like C<-preNextButtonAction>, | ||
| 980 | this routine check that the object field C<licence_agree> | ||
| 981 | is a Boolean true value. If that operand is not set, it warns | ||
| 982 | the user to read the licence; if that operand is set to a | ||
| 983 | Boolean false value, a message box says goodbye and quits the | ||
| 984 | program. | ||
| 985 | |||
| 986 | =cut | ||
| 987 | |||
| 988 | sub callback_licence_agreement { | ||
| 989 | my $self = shift; | ||
| 990 | if ( not defined ${ $self->{licence_agree} } ) { | ||
| 991 | my $button = $self->parent->messageBox( | ||
| 992 | '-icon' => 'info', | ||
| 993 | -type => 'ok', | ||
| 994 | -title => $LABELS{LICENCE_ALERT_TITLE}, | ||
| 995 | -message => $LABELS{LICENCE_IGNORED} | ||
| 996 | ); | ||
| 997 | return 0; | ||
| 998 | } | ||
| 999 | elsif ( not ${ $self->{licence_agree} } ) { | ||
| 1000 | my $button = $self->parent->messageBox( | ||
| 1001 | '-icon' => 'warning', | ||
| 1002 | -type => 'ok', | ||
| 1003 | -title => $LABELS{LICENCE_ALERT_TITLE}, | ||
| 1004 | -message => $LABELS{LICENCE_DISAGREED} | ||
| 1005 | ); | ||
| 1006 | exit; | ||
| 1007 | } | ||
| 1008 | return 1; | ||
| 1009 | } | ||
| 1010 | |||
| 1011 | =head2 confirm_download_again | ||
| 1012 | |||
| 1013 | This callback is triggered when a file download fails | ||
| 1014 | during a DownloadPage. | ||
| 1015 | In the default implementation, the user is asked if they would like to try again | ||
| 1016 | (Yes or No). | ||
| 1017 | |||
| 1018 | =cut | ||
| 1019 | |||
| 1020 | sub confirm_download_again { | ||
| 1021 | my ( $self, $failed ) = ( shift, shift ); | ||
| 1022 | my $button = $self->parent->messageBox( | ||
| 1023 | -icon => 'question', | ||
| 1024 | -type => 'yesno', | ||
| 1025 | -default => 'yes', | ||
| 1026 | -title => 'File Download', | ||
| 1027 | -message => $failed . " file" | ||
| 1028 | . ( $failed != 1 ? "s" : "" ) | ||
| 1029 | . " were not downloaded.\n\nWould you like to try again?", | ||
| 1030 | ); | ||
| 1031 | return lc $button eq 'yes' ? 1 : 0; | ||
| 1032 | } | ||
| 1033 | |||
| 1034 | =head2 confirm_download_quit | ||
| 1035 | |||
| 1036 | This callback is triggered when the -on_error condition happens | ||
| 1037 | at the end of a DownloadPage. | ||
| 1038 | In the default implementation, the user is asked if they would like to abort the entire installation process | ||
| 1039 | (Yes or No). | ||
| 1040 | |||
| 1041 | =cut | ||
| 1042 | |||
| 1043 | sub confirm_download_quit { | ||
| 1044 | my ( $self, $failed ) = ( shift, shift ); | ||
| 1045 | my $button = $self->parent->messageBox( | ||
| 1046 | -icon => 'error', | ||
| 1047 | -type => 'yesno', | ||
| 1048 | -default => 'no', | ||
| 1049 | -title => 'Abort Installation?', | ||
| 1050 | -message => "Without downloading the remaining $failed file" | ||
| 1051 | . ( $failed != 1 ? "s" : "" ) | ||
| 1052 | . ", the Installer cannot complete the installation.\n\n" | ||
| 1053 | . "Should the Installation process be aborted?", | ||
| 1054 | ); | ||
| 1055 | $self->CloseWindowEventCycle if lc $button eq 'yes'; | ||
| 1056 | } | ||
| 1057 | |||
| 1058 | |||
| 1059 | |||
| 1060 | =head2 pre_install_files_quit | ||
| 1061 | |||
| 1062 | Asks if the user wishes to continue after file copy errors. | ||
| 1063 | |||
| 1064 | =cut | ||
| 1065 | |||
| 1066 | sub pre_install_files_quit { | ||
| 1067 | my ( $self, $failed ) = ( shift, shift ); | ||
| 1068 | TRACE "Enter pre_install_files_quit ..."; | ||
| 1069 | |||
| 1070 | SHOW: | ||
| 1071 | my $d = $self->Dialog( | ||
| 1072 | -bitmap => 'error', | ||
| 1073 | -buttons => [ 'Yes', 'No', 'Details' ], | ||
| 1074 | -default_button => 'no', | ||
| 1075 | -title => 'Abort Installation?', | ||
| 1076 | -text => "Failed to copy $failed file" | ||
| 1077 | . ( $failed != 1 ? "s" : "" ) . ".\n\n" | ||
| 1078 | . "Do you wish to continue anyway?", | ||
| 1079 | ); | ||
| 1080 | my $button = $d->Show; | ||
| 1081 | if ( lc $button eq 'details' ) { | ||
| 1082 | $self->messageBox( | ||
| 1083 | -title => 'File error details', | ||
| 1084 | -type => 'Ok', | ||
| 1085 | -icon => 'info', | ||
| 1086 | -message => join( | ||
| 1087 | "\n", map { join( ' ', $self->{-failed}->{$_}, $_ ) } | ||
| 1088 | keys %{ $self->{-failed} } | ||
| 1089 | ), | ||
| 1090 | ); | ||
| 1091 | goto SHOW; # XXX Martin, why goto?! | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | if ( lc $button eq 'no' ) { | ||
| 1095 | DEBUG "Won't continue...."; | ||
| 1096 | $self->{cancelButton}->configure( -state => 'normal' ); | ||
| 1097 | $self->{cancelButton}->invoke; | ||
| 1098 | } | ||
| 1099 | else { | ||
| 1100 | DEBUG "Will continue."; | ||
| 1101 | } | ||
| 1102 | |||
| 1103 | # Clear out the error list so we don't inform the user twice about | ||
| 1104 | # the same problem: | ||
| 1105 | $self->{-failed} = undef; | ||
| 1106 | } | ||
| 1107 | |||
| 1108 | |||
| 1109 | |||
| 1110 | =head2 addUninstallPage | ||
| 1111 | |||
| 1112 | Basically the same as L<addFileListPage|addFileListPage>, | ||
| 1113 | but rather than taking arguments to indicate from and to where | ||
| 1114 | files are to be moved or copied, takes the argument | ||
| 1115 | C<-uninstall_db>, which should be the same value as supplied to | ||
| 1116 | L<addFileListPage|addFileListPage>. | ||
| 1117 | |||
| 1118 | =cut | ||
| 1119 | |||
| 1120 | |||
| 1121 | sub addUninstallPage { | ||
| 1122 | my ( $self, $args ) = ( shift, {@_} ); | ||
| 1123 | # $self->addPage( sub { $self->_page_uninstall($args) } ); | ||
| 1124 | my %btn_args = | ||
| 1125 | map { my $x = delete $args->{$_}; $_ => $x } | ||
| 1126 | grep { /ButtonAction$/ } | ||
| 1127 | keys %$args; | ||
| 1128 | return $self->addPage( sub { $self->_page_uninstall($args) }, %btn_args ); | ||
| 1129 | } | ||
| 1130 | |||
| 1131 | sub _page_uninstall { | ||
| 1132 | my ( $self, $args ) = ( shift, shift ); | ||
| 1133 | |||
| 1134 | my $frame = $self->blank_frame( | ||
| 1135 | -title => $args->{-title} || "Uninstalling Files", | ||
| 1136 | -subtitle => $args->{-subtitle} || "Please wait whilst files are uninstalled.", | ||
| 1137 | -text => $args->{-text} || "\n" | ||
| 1138 | ); | ||
| 1139 | |||
| 1140 | my %bar; # progress bar args | ||
| 1141 | if ( $args->{-bar} ) { | ||
| 1142 | %bar = @{ $args->{-bar} }; | ||
| 1143 | # insert error checking here... | ||
| 1144 | } | ||
| 1145 | $bar{-gap} = 0 unless defined $bar{-gap}; | ||
| 1146 | $bar{-blocks} = 0 unless defined $bar{-blocks}; | ||
| 1147 | $bar{-colors} = [ 0 => 'blue' ] unless $bar{-colors}; | ||
| 1148 | $bar{-borderwidth} = 2 unless $bar{-borderwidth}; | ||
| 1149 | $bar{-relief} = 'sunken' unless $bar{-relief}; | ||
| 1150 | $bar{-from} = 0 unless $bar{-from}; | ||
| 1151 | $bar{-to} = 100 unless $bar{-to}; | ||
| 1152 | |||
| 1153 | my $f = $frame->LabFrame( | ||
| 1154 | -label => $args->{-label_frame_title} || "Uninstalling files", | ||
| 1155 | -labelside => "acrosstop" | ||
| 1156 | ); | ||
| 1157 | $args->{-labelFrom} = $f->Label(qw//)->pack(qw/-padx 16 -side top -anchor w/); | ||
| 1158 | $args->{-labelTo} = $f->Label(qw//)->pack(qw/-padx 16 -side top -anchor w/); | ||
| 1159 | $self->{-bar} = $f->ProgressBar(%bar)->pack(qw/ -padx 20 -pady 10 -side top -anchor w -fill both -expand 1 /); | ||
| 1160 | $f->pack(qw/-fill x -padx 30/); | ||
| 1161 | |||
| 1162 | $self->{nextButton}->configure( -state => "disable" ); | ||
| 1163 | $self->{backButton}->configure( -state => "disable" ); | ||
| 1164 | |||
| 1165 | # Get the db file ready | ||
| 1166 | $self->{_uninstall_db_path} = $args->{-uninstall_db}; | ||
| 1167 | $self->{_uninstall_db} = {}; | ||
| 1168 | tie( | ||
| 1169 | %{$self->{_uninstall_db}}, | ||
| 1170 | 'SDBM_File', | ||
| 1171 | $self->{_uninstall_db_path}, | ||
| 1172 | O_RDWR, | ||
| 1173 | $args->{-uninstall_db_perms} || 0666, | ||
| 1174 | ) or die "Could not create uninstaller db file - failed to tie `SDBM file '$self->{_uninstall_db_path}': $!; aborting"; | ||
| 1175 | |||
| 1176 | $self->{-bar}->after( | ||
| 1177 | $args->{-delay} || 1000, | ||
| 1178 | sub { | ||
| 1179 | my $todo = scalar keys %{$self->{_uninstall_db}}; | ||
| 1180 | TRACE "Configure bar to $todo\n"; | ||
| 1181 | $self->{-bar}->configure( -to => $todo ); | ||
| 1182 | |||
| 1183 | # $self->_install_files($args); | ||
| 1184 | |||
| 1185 | # Process files | ||
| 1186 | my $i = 0; | ||
| 1187 | foreach my $file (keys %{$self->{_uninstall_db}}){ | ||
| 1188 | # Skip dirs for now - process below | ||
| 1189 | next if -d $file; | ||
| 1190 | # Process files if they have not been deleted by user | ||
| 1191 | if (-e $file){ | ||
| 1192 | if (unlink $file){ | ||
| 1193 | # No report of removed files | ||
| 1194 | delete $self->{_uninstall_db}->{$file}; | ||
| 1195 | } | ||
| 1196 | else { | ||
| 1197 | # Report info | ||
| 1198 | $self->{_uninstall_db}->{$file} = 'could not remove file - '.$@; | ||
| 1199 | } | ||
| 1200 | } | ||
| 1201 | else { | ||
| 1202 | # Report info | ||
| 1203 | $self->{_uninstall_db}->{$file} = 'does not exist'; | ||
| 1204 | } | ||
| 1205 | $self->{-bar}->value( $self->{-bar}->value + 1 ); | ||
| 1206 | # DEBUG "Updating bar to " . $self->{-bar}->value; | ||
| 1207 | $self->{-bar}->update; | ||
| 1208 | $i ++; | ||
| 1209 | } | ||
| 1210 | |||
| 1211 | # Process dirs | ||
| 1212 | foreach my $dir (keys %{$self->{_uninstall_db}}){ | ||
| 1213 | next unless -d $dir; | ||
| 1214 | |||
| 1215 | INFO "Try to remove $dir"; | ||
| 1216 | |||
| 1217 | my $pwd = getcwd; | ||
| 1218 | if (not chdir $dir){ | ||
| 1219 | ERROR "Could not cd to $dir - $@"; | ||
| 1220 | ERROR " -e $dir == ".(-e $dir); | ||
| 1221 | next; | ||
| 1222 | } | ||
| 1223 | chdir $pwd; | ||
| 1224 | my @present; | ||
| 1225 | if (opendir my $d, $dir){ | ||
| 1226 | @present = grep { !/^\.+$/ } readdir $d; | ||
| 1227 | closedir $d; | ||
| 1228 | } else { | ||
| 1229 | warn $pwd," - ",$!; | ||
| 1230 | } | ||
| 1231 | |||
| 1232 | if (@present){ | ||
| 1233 | $self->{_uninstall_db}->{$dir} = "contains user-created files"; | ||
| 1234 | } | ||
| 1235 | elsif (not rmdir $dir){ | ||
| 1236 | $self->{_uninstall_db}->{$dir} = $!; | ||
| 1237 | } | ||
| 1238 | else { | ||
| 1239 | delete $self->{_uninstall_db}->{$dir}; | ||
| 1240 | } | ||
| 1241 | $self->{-bar}->value( $self->{-bar}->value + 1 ); | ||
| 1242 | DEBUG "Updating bar to " . $self->{-bar}->value; | ||
| 1243 | $self->{-bar}->update; | ||
| 1244 | $i ++; | ||
| 1245 | } | ||
| 1246 | |||
| 1247 | $self->{nextButton}->configure( -state => "normal" ); | ||
| 1248 | $self->{backButton}->configure( -state => "normal" ); | ||
| 1249 | |||
| 1250 | if (scalar keys %{ $self->{_uninstall_db} }){ | ||
| 1251 | # report | ||
| 1252 | my $button = $self->messageBox( | ||
| 1253 | '-icon' => 'warning', | ||
| 1254 | -type => 'ok', | ||
| 1255 | -title => 'Some files could not be removed', | ||
| 1256 | -message => join( "\n", | ||
| 1257 | "Some files could not be removed:\n" | ||
| 1258 | . join("\n", | ||
| 1259 | map { $_ ." - ". $self->{_uninstall_db}->{$_} } | ||
| 1260 | keys %{ $self->{_uninstall_db} } | ||
| 1261 | ) | ||
| 1262 | ) | ||
| 1263 | ); | ||
| 1264 | } | ||
| 1265 | |||
| 1266 | $self->{_uninstall_db} = undef; | ||
| 1267 | untie %{ $self->{_uninstall_db} }; | ||
| 1268 | foreach my $i (qw( dir pag )){ | ||
| 1269 | my $fn = $self->{_uninstall_db_path}.'.'.$i; | ||
| 1270 | DEBUG "Try to remove $fn"; | ||
| 1271 | if (not unlink $fn ){ | ||
| 1272 | my $perm = (stat $fn)[2]; # umask | ||
| 1273 | ERROR "Could not remove ".$fn." - $! (perms without umask = $perm"; | ||
| 1274 | } | ||
| 1275 | } | ||
| 1276 | |||
| 1277 | if ($args->{-wait}) { | ||
| 1278 | Tk::Wizard::_fix_wait( \$args->{-wait} ); | ||
| 1279 | $frame->after( | ||
| 1280 | $args->{-wait}, | ||
| 1281 | sub { | ||
| 1282 | $self->{nextButton}->configure( -state => 'normal' ); | ||
| 1283 | $self->{nextButton}->invoke; | ||
| 1284 | } | ||
| 1285 | ); | ||
| 1286 | } | ||
| 1287 | } | ||
| 1288 | ); | ||
| 1289 | return $frame; | ||
| 1290 | } | ||
| 1291 | |||
| 1292 | =head1 DIALOGUES | ||
| 1293 | |||
| 1294 | =head2 DIALOGUE_really_quit | ||
| 1295 | |||
| 1296 | Called when the user tries to quit. | ||
| 1297 | As opposed to the base C<Wizard>'s dialogue of the same name, | ||
| 1298 | this dialogue refers to "the Installer", rather than "the Wizard". | ||
| 1299 | |||
| 1300 | =cut | ||
| 1301 | |||
| 1302 | sub DIALOGUE_really_quit { | ||
| 1303 | TRACE "Enter Installer DIALOGUE_really_quit ..."; | ||
| 1304 | my $self = shift; | ||
| 1305 | return 0 if $self->{nextButton}->cget( -text ) eq $LABELS{FINISH}; | ||
| 1306 | unless ( $self->{really_quit} ) { | ||
| 1307 | my $button = $self->parent->messageBox( | ||
| 1308 | -icon => 'question', | ||
| 1309 | -type => 'yesno', | ||
| 1310 | -default => 'no', | ||
| 1311 | -title => 'Quit The Installation?', | ||
| 1312 | -message => "The Installer has not finished running.\n\nIf you quit now, the installation will be incomplete.\n\nDo you really wish to quit?" | ||
| 1313 | ); | ||
| 1314 | $self->{really_quit} = lc $button eq 'yes' ? 1 : 0; | ||
| 1315 | } | ||
| 1316 | if ( $self->{really_quit} ) { | ||
| 1317 | DEBUG "Quitting\n"; | ||
| 1318 | $self->{cancelButton}->configure( -state => 'normal' ); | ||
| 1319 | $self->{cancelButton}->invoke; | ||
| 1320 | } | ||
| 1321 | else { | ||
| 1322 | DEBUG "Ok, continuing\n"; | ||
| 1323 | } | ||
| 1324 | return !$self->{really_quit}; | ||
| 1325 | } | ||
| 1326 | |||
| 1327 | 1; | ||
| 1328 | |||
| 1329 | __END__ |