Wizard/Installer/Win32.pm untested

filter: Statementstmt 0.0% 0 / 236 Branchbran 0.0% 0 / 112 Conditioncond 0.0% 0 / 64 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 487 CC 57 SCAR 56.2
CC
57
Cov
0%
_page_start_menu
702.0
callback_create_shortcut
272.0
register_with_windows
56.0
CRAP 275.4
1 use strict;
2 use warnings;
3  
4 package Tk::Wizard::Installer::Win32;
5 use warnings::register;
6 our $VERSION = 2.22;
7  
8 =head1 NAME
9  
10 Tk::Wizard::Installer::Win32 - Win32-specific routines for Tk::Wizard::Installer
11  
12 =cut
13  
14  
15 use lib "../../../"; # dev
16 use Carp ();
17 use Cwd;
18 use File::Path;
19 use Exporter;
20 use Tk::Wizard ':use' => 'FileSystem';
21 use base 'Tk::Wizard::Installer';
22  
23 use vars '@EXPORT';
24 @EXPORT = ("MainLoop");
25  
26 use constant DEBUG_FRAME => 0;
27  
28 # use Log4perl if we have it, otherwise stub:
29 # See Log::Log4perl::FAQ
30 BEGIN {
31 eval { require Log::Log4perl; };
32 if($@) {
33 no strict qw"refs";
34 *{__PACKAGE__."::$_"} = sub { } for qw(TRACE DEBUG INFO WARN ERROR FATAL);
35 } else {
36 no warnings;
37 no strict qw"refs";
38 require Log::Log4perl::Level;
39 Log::Log4perl::Level->import(__PACKAGE__);
40 Log::Log4perl->import(":easy");
41 if ($Log::Log4perl::VERSION < 1.11){
42 *{__PACKAGE__."::TRACE"} = *DEBUG;
43 }
44 }
45  
46 use Win32::Shortcut;
47 require Win32;
48 if ( $Win32::VERSION lt 0.2 ) {
49 eval 'use Win32::OLE'; # autouse is still not very good?
50 die "Could not load Win32::OLE: $@" if $@;
51 }
52 eval {
53 use Win32;
54 use Win32::TieRegistry( Delimiter => "/", ArrayValues => 0 );
55 };
56 die $@ if $@;
57 }
58  
59  
60 =head1 DESCRIPTION
61  
62 As a sub-class of L<Tk::Wizard|Tk::Wizard> and
63 L<Tk::Wizard::Installer|Tk::Wizard::Installer>, this module offers
64 all of those methods and means, plus the below, which
65 are thought to be specific to the Microsoft Windows platform.
66  
67 If you are looking for a freeware software installer that is not
68 dependant upon Perl, try Inno Setup - C<http://www.jrsoftware.org/>. It's
69 so good, even Microsoft have been caught using it.
70  
71 If you are looking for a means to update the Windows C<Path> variable,
72 have a look for I<PathTool.exe>, a tiny Windows 32-bit executable
73 by Luke Bailey (C<luke@notts.flexeprint.com>). This tool can also be
74 used to add new, persistant environment variables to the system.
75  
76 =head1 METHODS
77  
78 =head2 register_with_windows
79  
80 Registers an application with Windows so that it can be "uninstalled"
81 using the I<Control Panel>'s I<Add/Remove Programs> dialogue.
82  
83 An entry is created in the Windows' registry pointing to the
84 uninstall script path. See C<UninstallString>, below.
85  
86 Returns C<undef> on failure, C<1> on success.
87 Does nothing on non-MSWin32 platforms
88  
89 Aguments are:
90  
91 =over 4
92  
93 =item uninstall_key_name
94  
95 The name of the registery sub-key to be used. This is transparent to the
96 end-user, but should be unique for all applications.
97  
98 =item UninstallString
99  
100 The command-line to execute to uninstall the script.
101  
102 According to Microsoft at L<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwue/html/ch11d.asp>:
103  
104 "You must supply complete names for both the DisplayName and UninstallString
105 values for your uninstall program to appear in the Add/Remove Programs
106 utility. The path you supply to Uninstall-String must be the complete
107 command line used to carry out your uninstall program. The command line you
108 supply should carry out the uninstall program directly rather than from a
109 batch file or subprocess."
110  
111 The default value is:
112  
113 perl -e '$args->{app_path} -u'
114  
115 This default assumes you have set the argument C<app_path>, and that it
116 checks and reacts to the the command line switch C<-u>:
117  
118 package MyInstaller;
119 use strict;
120 use Tk::Wizard;
121 if ($ARGV[0] =~ /^-*u$/i){
122 # ... Have been passed the uninstall switch: uninstall myself now ...
123 }
124  
125 Or something like that.
126  
127 =item QuietUninstallString
128  
129 As C<UninstallString> above, but for ... quiet uninstalls.
130  
131 =item app_path
132  
133 Please see the entry for C<UninstallString>, above.
134  
135 =item DisplayName
136  
137 =item DisplayVersion
138  
139 =item Size
140  
141 The strings displayed in the Control Panel's Add/Remove Programs list.
142  
143 =item ModifyPath
144  
145 =item NoRepair NoModify NoRemove
146  
147 =item EstimatedSize InstallSorce InstallDate InstallLocation
148  
149 =item AthorizedCDFPrefix Language ProductID
150  
151 Unknown
152  
153 =item Comments
154  
155 =item RegOwner
156  
157 =item RegCompnay
158  
159 =item Contact
160  
161 =item HelpTelephone
162  
163 =item Publisher
164  
165 =item URLUpdateInfo
166  
167 =item URLInfoAbout
168  
169 =item HelpLink
170  
171 These are all displayed when the Support Information link
172 is clicked in the Add/Remove Programs dialogue. The last
173 should be full URIs.
174  
175 =back
176  
177 The routine will also try to add any other parameters to the
178 registry tree in the current location: YMMV.
179  
180 =cut
181  
182 sub register_with_windows {
183 my ( $self, $args ) = ( shift, {@_} );
184 return 1 if $^O !~ /mswin32/i;
185  
186 unless ($args->{DisplayName}
187 and $args->{UninstallString}
188 and ( $args->{uninstall_key_name} or $args->{app_path} ) ){
189 die __PACKAGE__
190 . "::register_with_windows requires an argument of name/value pairs which must include the keys 'UninstallString', 'uninstall_key_name' and 'DisplayName'";
191 }
192  
193 if ( not $args->{UninstallString} and not $args->{app_path} ) {
194 die __PACKAGE__ . "::register_with_windows requires either argument 'app_path' or 'UninstallString' be set.";
195 }
196  
197 if ( $args->{app_path} ) {
198 $args->{app_path} = "perl -e '$args->{app_path} -u'";
199 }
200 my $uninst_key_ref =
201 $Registry->{'LMachine/SOFTWARE/Microsoft/Windows/CurrentVersion/Uninstall/'}->CreateKey(
202 $args->{uninstall_key_name}
203 );
204 Carp::confess "Perl Win32::TieRegistry error" if !$uninst_key_ref;
205  
206 foreach my $i ( keys %$args ) {
207 next if $i =~ /^(app_path|uninstall_key_name)$/g;
208 $uninst_key_ref->{"/$i"} = $args->{$i};
209 }
210  
211 return 1;
212 }
213  
214 =head2 addStartMenuPage
215  
216 Adds a page that allows
217 users to select a location on the Windows "Start Menu",
218 perhaps to add a shortcut there.
219  
220 This routine does not currently create the directory in the
221 I<Start Menu>, nor does it place a link there - see
222 L</callback_create_shortcut> for that. Rather, the
223 caller supplies a C<-variable> parameter that is a reference
224 to a scalar which, once the page is 'run', will contain
225 either the path to the user's chosen directory, or C<undef>
226 if the option to not select was chosen.
227  
228 In addition, when the page is 'run', it places the path to the
229 current user's I<Start Menu/Programs> directory into the object
230 field C<startmenu_dir_current>, and the path to the common
231 I<Start Menu/Programs> in the object field C<startmenu_dir_common>.
232  
233 The physical creation of the shortcut is left as an exercise to the reader.
234 Have a look at the C<$mkdir> C<Button> in C<Tk::Wizard/page_dirSelect>.
235  
236 =over 4
237  
238 =item -user
239  
240 Set to C<all> or C<current> to list the "Start Menu" for all users, or
241 just the current user. Default is C<all>. Note that in the current
242 versions of Windows in common use, if there exist entries with the
243 same name in both the common and current user's I<Start Menu>, the
244 entry in the common menu takes precedence.
245  
246 =item -variable
247  
248 A reference to a variable that, when the page is completed,
249 will contain the directory the user has chosen to create an item in. Note
250 this is I<not> the full path: see above.
251  
252 =item -program_group
253  
254 Name of the directory to create on the start menu, if any. If
255 defined, this will be appended to any selection the user makes. Note
256 that since addStartMenuPage() is just the GUI part, no directory will
257 actually be created until C</callback_create_shortcut> is called.
258  
259 =item -disable_nochoice
260  
261 Set to prevent the display of the checkbox which allows the user not
262 to use this feature. See C<-label_nochoice>, below.
263  
264 =item -label_nochoice
265  
266 If the parameter C<-disable_nochoice> has not been set,
267 C<-label_nochoice> should contain text to use for the label by the
268 checkbox which disables choices on
269 this page and causes the page to set the C<-variable> parameter to
270 C<undef>.
271 Default text is C<Do not create a shortcut on the Start Menu>.
272  
273 =item -listHeight
274  
275 Height of the list box, default is C<10> but you may
276 vary this if your C<-text> attribute takes up more or less much room.
277  
278 =back
279  
280 Accepts the Standard Options that are common to the C<HList> and
281 C<Label> widgets, but does not accept aliases:
282  
283 -relief -highlightthickness -background -borderwidth -cursor
284 -highlightcolor -foreground -font
285  
286 You can supply the common Tk::Wizard options:
287  
288 -title -subtitle -text
289  
290 This method will initially attempt to use F<Win32.pm>; failing that,
291 it will attempt to use a Windows Scripting Host object created via
292 L<Win32::OLE|Win32::OLE>. If both fail (WSH only existing by default
293 in Win98 and above), the routine will return C<undef>, rather than a
294 page frame object. This may not be ideal but works for me --
295 suggestions welcomed for a better idea.
296  
297 =cut
298  
299 sub addStartMenuPage {
300 my ( $self, $args ) = ( shift, {@_} );
301 # return $self->addPage( sub { $self->_page_start_menu($args) } );
302 my %btn_args =
303 map { my $x = delete $args->{$_}; $_ => $x }
304 grep { /ButtonAction$/ }
305 keys %$args;
306 return $self->addPage( sub { $self->_page_start_menu($args) }, %btn_args );
307 }
308  
309 sub _page_start_menu {
310 local *DIR;
311 my ($self) = (shift);
312 my $args;
313 if ( ref $_[0] eq 'HASH' ) {
314 $args = shift;
315 }
316 else {
317 $args = {@_};
318 }
319 my $cwd = cwd;
320 my $do_set = 1;
321  
322 Carp::croak "You must set -variable parameter"if not exists $args->{-variable};
323  
324 $args->{-user} ||= 'common';
325 $args->{-background} = 'white' unless exists $args->{-background};
326 $args->{-relief} = 'sunken' unless exists $args->{-relief};
327 $args->{-border} = 1 unless exists $args->{-border};
328 $args->{-listHeight} = 10 unless exists $args->{-listHeight};
329 $args->{-title} ||= "Create Shortcuts";
330 $args->{-subtitle} ||= "Please select where to place an icon on the start menu";
331  
332 $args->{-text} ||= "
333 If you want the new Program Group to be installed within an existing
334 folder in your Start Menu, select that folder below.
335 If you do not want to install the new Program in your Start Folder,
336 check the checkbox below.";
337  
338 $args->{-label_nochoice} ||= "Do not create a shortcut on the Start Menu";
339 $self->{-program_group} = $args->{-program_group};
340  
341 my $common_formatting = {};
342  
343 $common_formatting->{-background} = $args->{-background}
344 if exists $args->{-background};
345  
346 $common_formatting->{-relief} = $args->{-relief} if exists $args->{-relief};
347  
348 $common_formatting->{-highlightthickness} = $args->{-highlightthickness}
349 if exists $args->{-highlightthickness};
350  
351 $common_formatting->{-borderwidth} = $args->{-borderwidth}
352 if exists $args->{-borderwidth};
353  
354 $common_formatting->{-cursor} = $args->{-cursor} if exists $args->{-cursor};
355  
356 $common_formatting->{-highlightcolor} = $args->{-highlightcolor}
357 if exists $args->{-highlightcolor};
358  
359 $common_formatting->{-foreground} = $args->{-foreground}
360 if exists $args->{-foreground};
361  
362 $common_formatting->{-font} = $args->{-font} if exists $args->{-font};
363  
364 # Don't pass these to other modules
365 my $variable = $args->{-variable};
366 delete $args->{-variable};
367 my $group = $args->{-program_group};
368 delete $args->{-program_group};
369  
370 my $frame = $self->blank_frame(%$args);
371 DEBUG_FRAME && $frame->configure( -background => 'blue' );
372  
373 if ( $Win32::VERSION gt 0.1999999 ) {
374 $self->{startmenu_dir_current} = eval('Win32::GetFolderPath(Win32::CSIDL_STARTMENU)') . '\Programs';
375 $self->{startmenu_dir_common} = eval('Win32::GetFolderPath(Win32::CSIDL_COMMON_STARTMENU)') . '\Programs';
376 }
377  
378 # The above may not work if non-standard/non-English setup, so:
379 if ( not $self->{startmenu_dir_current}
380 or not $self->{startmenu_dir_common} ) {
381 my $WshShell = eval 'Win32::OLE->CreateObject("WScript.Shell")';
382 if ( ref $WshShell eq 'Win32::OLE' ) {
383 $self->{startmenu_dir_current} = $WshShell->SpecialFolders(17);
384 $self->{startmenu_dir_common} = $WshShell->SpecialFolders(2);
385 }
386 else {
387 warn "Could not find special folders using Win32 or OLE!";
388 return undef;
389 }
390 }
391  
392 # The default is to install for all users:
393 my $dir_parent = $self->{startmenu_dir_common};
394 if ( $args->{-user} eq 'current' ) {
395 $dir_parent = $self->{startmenu_dir_current};
396 }
397  
398 # This is the default answer, if the user just clicks "Next":
399 $$variable = "$dir_parent\\$group";
400  
401 # Recursively read the Start Menu folder, building up a list of
402 # all folders in there:
403 my @asTry = ($dir_parent);
404 my @asDir;
405  
406 TRY_DIR:
407 while (@asTry) {
408 local $" = ',';
409  
410 # $sTry is the FULL path of this folder:
411 my $sTry = shift @asTry;
412 next TRY_DIR if !-d $sTry;
413 push @asDir, $sTry;
414 opendir DIR, $sTry
415 or Carp::croak "Can not open the start menu ($sTry): $!";
416 my @asTryChildren = sort readdir DIR;
417  
418 push @asTry, grep { !m/^\.\.?$/ && s/^(.*)$/$sTry\\$1/ && -d } @asTryChildren;
419 closedir DIR or warn $!;
420 }
421  
422 my $text = $frame->Label(
423 -justify => 'left',
424 -textvariable => $variable,
425 -anchor => 'w',
426 %$common_formatting,
427 )->pack(
428 -side => 'top',
429 -anchor => 'w',
430 -fill => 'x',
431 -padx => 10,
432 );
433  
434 $text->configure( -background => 'magenta' ) if DEBUG_FRAME;
435  
436 my $hlist = $frame->Scrolled(
437 'HList',
438 -scrollbars => "osoe",
439 -selectmode => 'single',
440 -height => $args->{-listHeight},
441 -itemtype => 'text',
442 -separator => '\\',
443 -browsecmd => sub {
444 $$variable = shift;
445 $$variable .= "\\" . $group if $group;
446 },
447 %$common_formatting,
448 )->pack(
449 -expand => 1,
450 -fill => 'both',
451 -padx => 10,
452 -pady => 5,
453 );
454 $hlist->configure( -background => 'yellow' ) if DEBUG_FRAME;
455  
456 foreach my $i (@asDir) {
457 my $t = $i;
458 $t =~ s!\Q$dir_parent!!;
459 $t =~ s!\A\\!!;
460 if ( !$hlist->info( "exists", $t ) ) {
461  
462 DEBUG "trying to add i=$i= t=$t= to hlist...\n";
463 $hlist->add(
464 $t,
465 -text => $t,
466 -data => $i,
467 );
468 }
469 }
470  
471 $self->{_hlist_active_} = 0;
472 $self->_toggle_hlist( $hlist, $variable, $group );
473 if ( !$args->{-disable_nochoice} ) {
474 my $b = $frame->Checkbutton(
475 -text => $args->{-label_nochoice},
476 -anchor => 'w',
477 -command => [ \&_toggle_hlist, $self, $hlist, $variable, $group ],
478 )->pack(
479 -side => 'left',
480 -anchor => 'w',
481 -padx => 10,
482 -pady => 5,
483 );
484 $b->configure( -background => 'red' ) if DEBUG_FRAME;
485 }
486  
487 return $frame;
488 }
489  
490  
491 sub _toggle_hlist {
492 my $self = shift;
493  
494 # Required arg1 = the Scrolled HList widget:
495 my $hlist = shift || return;
496  
497 # Required arg2 = reference to variable that HList is bound to:
498 my $rs = shift || return;
499  
500 # Optional arg3 = the group string being added onto the path:
501 my $group = shift;
502 $self->{_hlist_active_} = !$self->{_hlist_active_};
503  
504 my $w = $hlist->Subwidget('scrolled');
505 if ( !$self->{_hlist_active_} ) {
506 $$rs = '';
507 # Deactivate the entire HList. HList does not support the
508 # -state configuration option, therefore we use the bindtags
509 # "hack":
510 $self->{_hlist_bindtags_} = $w->bindtags;
511 $w->bindtags( ['Freeze'] );
512 }
513  
514 else {
515 $$rs = $hlist->info('anchor');
516 $$rs .= "\\" . $group if $group;
517 if ( $self->{_hlist_bindtags_} ) {
518 $w->bindtags( $self->{_hlist_bindtags_} );
519 }
520 }
521 }
522  
523  
524 =head1 CALLBACKS
525  
526 =head2 callback_create_shortcut
527  
528 A convenience interface to L<Win32::Shortcut|Win32::Shortcut> method
529 that creates a shortcut at the path specified. Parameters are pretty
530 much what you see when you right-click a shortcut:
531  
532 =over 4
533  
534 =item -save_path
535  
536 The location at which the shortcut should be saved. This should be
537 the full path including filename ending in C<.lnk>.
538  
539 The filename minus extension will be visible in the shortcut.
540 If the C<-program_group> parameter was passed to
541 C<METHOD page_start_menu>, the directory it refers to
542 will be included in the save path you supply. To avoid
543 this, either C<undef>ine the object field C<-program_group>,
544 or supply the parameter C<-no_program_group>.
545  
546 =item -no_program_group>
547  
548 See C<-save_path>, above.
549  
550 =item -target
551  
552 The shortcut points to this file, directory, or URI -- see notes for
553 C<-save_path>, above.
554  
555 =item -workingdir
556  
557 The working directory for the C<-target>, above.
558  
559 =item -description
560  
561 This is what you see when you mouse-over a shortcut
562 in more "modern" (Win2k/ME+) Windows.
563  
564 =item -iconpath
565  
566 Path to the icon file -- an C<.exe>, C<.dll>, C<.ico> or
567 other acceptable format.
568  
569 =item -iconindex
570  
571 Index of the icon in the file if a C<.exe> or C<.dll>.
572  
573 =item -arguments
574  
575 Um... it's the second parameter in Win32::Shortcut::Set -
576 could well be parameters for the target, but I'm too much of
577 a rush to check. XXX
578  
579 =item -show
580  
581 Whether the C<-target>, above, should be
582 started maximized or minimized. Acceptable values are
583 the constants:
584  
585 SW_SHOWMAXIMIZED SW_SHOWMINNOACTIVE SW_SHOWNORMAL
586  
587 =item -hotkey
588  
589 Key combination to activate the shortcut. Probably looks
590 something like C<ctrl+t>.
591  
592 =back
593  
594 On success, returns the C<-save_path>; on failure, C<undef>.
595  
596 =cut
597  
598 sub callback_create_shortcut {
599 my ( $self, $args ) = ( shift, {@_} );
600 Carp::croak "-target is required (you gave " . ( join ", ", keys %$args ) . ")"
601 unless defined $args->{-target} and $args->{-target} ne '';
602  
603 Carp::croak "-save_path is required (you gave " . ( join ", ", keys %$args ) . ")"
604 unless defined $args->{-save_path} and $args->{-save_path} ne '';
605  
606 $args->{-arguments} = '' unless exists $args->{-arguments};
607 $args->{-workingdir} = '' unless exists $args->{-workingdir};
608 $args->{-description} = '' unless exists $args->{-description};
609 $args->{-show} = 0 unless exists $args->{-show};
610 $args->{-hotkey} = 0 if not exists $args->{-hotkey} or $args->{-hotkey} eq '';
611 $args->{-iconpath} = 0 unless exists $args->{-iconpath};
612 $args->{-iconindex} = 0 unless exists $args->{-iconindex};
613 delete $self->{-program_group} if exists $args->{-no_program_group};
614  
615 if ( exists $self->{-program_group}
616 and exists $args->{-save_path} ) {
617 my ( $base, $file ) = $args->{-save_path} =~ /(.*?)([^\/\\]+)$/;
618  
619 # Historical error on $self->{-program_groups}
620 $base .= "\\" . ($self->{-program_groups} || $self->{-program_group});
621  
622 mkpath $base if !-e $base;
623 $args->{-save_path} = $base . "\\" . $file;
624 $args->{-save_path} =~ s/[\\\/]+/\\/g;
625 }
626  
627 if ( $args->{-target} =~ /^(ht|f)tp:\/\//
628 and $args->{-save_path} !~ /\.uri$/i ) {
629 Carp::croak "Internet shortcuts require a .uri ending!";
630 }
631  
632 if ( $args->{-target} !~ /^(ht|f)tp:\/\//
633 and $args->{-save_path} =~ /\.uri$/i ) {
634 Carp::croak "Only internet shortcuts require a .uri ending!";
635 }
636  
637 my $s = Win32::Shortcut->new;
638 $s->Set(
639 $args->{-target},
640 $args->{-arguments},
641 $args->{-workingdir},
642 $args->{-description},
643 $args->{-show},
644 $args->{-hotkey},
645 $args->{-iconpath},
646 $args->{-iconindex},
647 );
648 my $r = $s->Save(
649 $args->{-save_path}
650 ) ?
651 $args->{-save_path} : undef;
652  
653 $s->Close;
654  
655 return $r;
656 }
657  
658  
659 =head2 callback_create_shortcuts
660  
661 Convenience method to create multiple shortcuts at once.
662 Supply an array of hashes, each hash being arguments
663 to supply to C<callback_create_shortcut>.
664  
665 Returns an array or reference to an array that contains
666 the reults of the shortcut creation.
667  
668 See L<callback_create_shortcut>.
669  
670 =cut
671  
672 sub callback_create_shortcuts {
673 my $self = shift;
674 my @paths;
675 foreach (@_) {
676 Carp::confess "Not a hash reference" unless ref $_ eq 'HASH';
677 $self->callback_create_shortcut(%$_);
678 }
679 return wantarray ? @paths : \@paths;
680 }
681  
682  
683 sub Tk::Error::RedefineIfNeeded {
684 local $\ = "\n";
685 print STDERR @_;
686 }
687  
688 1;
689  
690 __END__