| 1 | package Tk::RemoteFileSelect; | ||
| 2 | # Temp version of CPAN. | ||
| 3 | $VERSION=0.56; | ||
| 4 | my $RCSRevKey = '$Revision: 0.56 $'; | ||
| 5 | $RCSRevKey =~ /Revision: (.*?) /; | ||
| 6 | $VERSION=$1; | ||
| 7 | use vars qw($VERSION @EXPORT_OK); | ||
| 8 | @EXPORT_OK = qw(glob_to_re); | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | RemoteFileSelect.pm--Browse directories with FTP. | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | require Tk::RemoteFileSelect; | ||
| 17 | |||
| 18 | my $file = $mw -> Tk::RemoteFileSelect( -directory => '.' ); | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | A RemoteFileSelect contains two listboxes that display subdirectories | ||
| 23 | and files, a directory entry and a file name entry, and buttons for | ||
| 24 | each operation, which are labeled with Alt-key accelerators. | ||
| 25 | |||
| 26 | When entering a file name, the RemoteFileSelect verifies whether the | ||
| 27 | file already exists. If a file is selected in the listbox, the | ||
| 28 | RemoteFileSelect returns that file's name when the user clicks the | ||
| 29 | 'Accept' button, presses Enter after typing a name in the file entry, | ||
| 30 | or double clicks on a selection in the file list box. | ||
| 31 | |||
| 32 | Additionally, if the Net::FTP module is installed, RemoteFileSelect | ||
| 33 | will activate an additional "Host" button on the FileSelect widget, | ||
| 34 | where you can enter the host name, and your user id and password, and | ||
| 35 | select files on the remote host. | ||
| 36 | |||
| 37 | If a file name is selected on the local host, then the | ||
| 38 | RemoteFileSelect widget returns the path to the file name, the same as | ||
| 39 | a standard FileSelect widget. | ||
| 40 | |||
| 41 | If a file is selected on a remote host, then the RemoteFileSelect | ||
| 42 | widget returns the name in the form: | ||
| 43 | |||
| 44 | host:/full-pathname-of-file | ||
| 45 | |||
| 46 | RemoteFileSelect requires the Net::FTP module to be installed. If it | ||
| 47 | cannot find and load Net::FTP, the RemoteFileSelect widget behaves | ||
| 48 | like a standard FileSelect widget, and the "Host" button is grayed | ||
| 49 | out. | ||
| 50 | |||
| 51 | RemoteFileSelect.pm was developed with the Net::FTP module distributed | ||
| 52 | with libnet-1.0703, from http://www.cpan.org/. | ||
| 53 | |||
| 54 | All other operations function as in a FileSelect widget. Please refer | ||
| 55 | to the FileSelect.pm POD documentation. | ||
| 56 | |||
| 57 | =head1 VERSION INFO | ||
| 58 | |||
| 59 | First development version. | ||
| 60 | |||
| 61 | $Revision: 0.56 $ | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | use Tk qw(Ev); | ||
| 66 | use strict; | ||
| 67 | use Carp; | ||
| 68 | use base qw(Tk::Toplevel); | ||
| 69 | use Tk::widgets qw(LabEntry Button Frame Listbox Scrollbar); | ||
| 70 | use File::Basename; | ||
| 71 | |||
| 72 | my $menufont="*-helvetica-medium-r-*-*-12-*"; | ||
| 73 | |||
| 74 | Construct Tk::Widget 'RemoteFileSelect'; | ||
| 75 | |||
| 76 | use vars qw(%error_text); | ||
| 77 | %error_text = ( | ||
| 78 | '-r' => 'is not readable by effective uid/gid', | ||
| 79 | '-w' => 'is not writeable by effective uid/gid', | ||
| 80 | '-x' => 'is not executable by effective uid/gid', | ||
| 81 | '-R' => 'is not readable by real uid/gid', | ||
| 82 | '-W' => 'is not writeable by real uid/gid', | ||
| 83 | '-X' => 'is not executable by real uid/gid', | ||
| 84 | '-o' => 'is not owned by effective uid/gid', | ||
| 85 | '-O' => 'is not owned by real uid/gid', | ||
| 86 | '-e' => 'does not exist', | ||
| 87 | '-z' => 'is not of size zero', | ||
| 88 | '-s' => 'does not exists or is of size zero', | ||
| 89 | '-f' => 'is not a file', | ||
| 90 | '-d' => 'is not a directory', | ||
| 91 | '-l' => 'is not a link', | ||
| 92 | '-S' => 'is not a socket', | ||
| 93 | '-p' => 'is not a named pipe', | ||
| 94 | '-b' => 'is not a block special file', | ||
| 95 | '-c' => 'is not a character special file', | ||
| 96 | '-u' => 'is not setuid', | ||
| 97 | '-g' => 'is not setgid', | ||
| 98 | '-k' => 'is not sticky', | ||
| 99 | '-t' => 'is not a terminal file', | ||
| 100 | '-T' => 'is not a text file', | ||
| 101 | '-B' => 'is not a binary file', | ||
| 102 | '-M' => 'has no modification date/time', | ||
| 103 | '-A' => 'has no access date/time', | ||
| 104 | '-C' => 'has no inode change date/time', | ||
| 105 | ); | ||
| 106 | |||
| 107 | sub import { | ||
| 108 | if (defined $_[1] and $_[1] eq 'as_default') { | ||
| 109 | local $^W = 0; | ||
| 110 | package Tk; | ||
| 111 | *FDialog = \&Tk::RemoteFileSelect::FDialog; | ||
| 112 | *MotifFDialog = \&Tk::RemoteFileSelect::FDialog; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | sub Cancel | ||
| 117 | { | ||
| 118 | my ($cw) = @_; | ||
| 119 | $cw->{Selected} = undef; | ||
| 120 | my $hostname = $cw -> cget( -hostname ); | ||
| 121 | if( $hostname ne '' ) { | ||
| 122 | my $ftp = $cw -> cget( -ftp ); | ||
| 123 | $ftp -> quit; | ||
| 124 | $cw -> configure( -ftp => undef, | ||
| 125 | -connected => '' ); | ||
| 126 | } | ||
| 127 | $cw->withdraw unless $cw->cget('-transient'); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub host { | ||
| 131 | my ($cw) = @_; | ||
| 132 | my ($hostid, $transcript, $resp); | ||
| 133 | my $dlg = $cw->Subwidget('hostdialog'); | ||
| 134 | return if ( ($resp = $dlg -> Show ) =~ /Cancel/); | ||
| 135 | $hostid = $dlg -> Subwidget( 'hostentry' ) -> get; | ||
| 136 | $transcript = $cw -> cget( '-transcript' ); | ||
| 137 | $cw -> configure( -hostname => $hostid, | ||
| 138 | -transcript => $transcript ); | ||
| 139 | my $logindlg = $cw -> Subwidget('logindialog'); | ||
| 140 | return if ( ($resp = $logindlg -> Show ) =~ /Cancel/); | ||
| 141 | $cw -> configure( -userid => ($logindlg -> Subwidget( 'uidentry' ) -> get), | ||
| 142 | -password => ($logindlg -> Subwidget( 'pwdentry' ) -> get) ); | ||
| 143 | my $ftp = $cw -> remoteLogin( $hostid, | ||
| 144 | $cw -> cget( -userid ), | ||
| 145 | $cw -> cget( -password ), | ||
| 146 | $transcript ); | ||
| 147 | |||
| 148 | if( defined $ftp ) { | ||
| 149 | my $dir = $ftp -> pwd(); | ||
| 150 | $cw -> remoteDirectory( $dir ); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | sub remoteLogin { | ||
| 155 | my ($cw, $hostid, $userid, $password, $transcript) = @_; | ||
| 156 | |||
| 157 | my $ftp = undef; | ||
| 158 | my $debug = ( $transcript =~ /1/ ? 1 : 0 ); | ||
| 159 | $ftp = Net::FTP -> | ||
| 160 | new( $hostid, | ||
| 161 | Debug => $debug ); | ||
| 162 | if( ! defined $ftp ) { | ||
| 163 | my $edlg = $cw -> Subwidget( 'errormessage' ); | ||
| 164 | $edlg -> configure( -text => $@ ); | ||
| 165 | $edlg -> Show; | ||
| 166 | $cw -> configure( -hostname => '', | ||
| 167 | -connected => ''); | ||
| 168 | return; | ||
| 169 | } | ||
| 170 | if( $ftp -> login( $userid, $password ) ) { | ||
| 171 | $cw -> configure( -ftp => $ftp, | ||
| 172 | -connected => '1'); | ||
| 173 | } else { | ||
| 174 | my $edlg = $cw -> Subwidget( 'errormessage' ); | ||
| 175 | $edlg -> configure( -text => "Error: Could not login to $hostid\." ); | ||
| 176 | $edlg -> Show; | ||
| 177 | $cw -> configure( -ftp => $ftp, | ||
| 178 | -connected => ''); | ||
| 179 | } | ||
| 180 | return $ftp; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub Accept { | ||
| 184 | |||
| 185 | # Accept the file or directory name if possible. | ||
| 186 | my ($cw) = @_; | ||
| 187 | |||
| 188 | my($path, $so) = ($cw->cget('-directory'), $cw->SelectionOwner); | ||
| 189 | my $ftp = $cw -> cget( -ftp ); | ||
| 190 | my $leaf = undef; | ||
| 191 | my $leaves; | ||
| 192 | |||
| 193 | if (defined $so and | ||
| 194 | $so == $cw->Subwidget('dir_list')->Subwidget('listbox')) { | ||
| 195 | $leaves = [$cw->Subwidget('dir_list')->getSelected]; | ||
| 196 | $leaves = [$cw->Subwidget('dir_entry')->get] if !scalar(@$leaves); | ||
| 197 | } else { | ||
| 198 | $leaves = [$cw->Subwidget('file_list')->getSelected]; | ||
| 199 | $leaves = [$cw->Subwidget('file_entry')->get] if !scalar(@$leaves); | ||
| 200 | } | ||
| 201 | |||
| 202 | foreach $leaf (@$leaves) | ||
| 203 | { | ||
| 204 | if (defined $leaf and $leaf ne '') { | ||
| 205 | if (!$cw->cget('-create') || -e "$path/$leaf") | ||
| 206 | { | ||
| 207 | foreach (@{$cw->cget('-verify')}) { | ||
| 208 | my $r = ref $_; | ||
| 209 | if (defined $r and $r eq 'ARRAY') { | ||
| 210 | #local $_ = $leaf; # use strict var problem here | ||
| 211 | return if not &{$_->[0]}($cw, $path, $leaf, @{$_}[1..$#{$_}]); | ||
| 212 | } else { | ||
| 213 | my $s = eval "$_ '$path/$leaf'"; | ||
| 214 | print $@ if $@; | ||
| 215 | if (not $s) { | ||
| 216 | my $err; | ||
| 217 | if (substr($_,0,1) eq '!') | ||
| 218 | { | ||
| 219 | my $t = substr($_,1); | ||
| 220 | if (exists $error_text{$t}) | ||
| 221 | { | ||
| 222 | $err = $error_text{$t}; | ||
| 223 | $err =~ s/\b(?:no|not) //; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | $err = $error_text{$_} unless defined $err; | ||
| 227 | $err = "failed '$_' test" unless defined $err; | ||
| 228 | $cw->Error("'$leaf' $err."); | ||
| 229 | return; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | } # forend | ||
| 233 | } | ||
| 234 | else | ||
| 235 | { | ||
| 236 | unless (-w $path) | ||
| 237 | { | ||
| 238 | $cw->Error("Cannot write to $path"); | ||
| 239 | return; | ||
| 240 | } | ||
| 241 | } | ||
| 242 | if( ( $cw -> cget( -connected ) ) eq '1' ) { | ||
| 243 | $path = $ftp -> pwd; | ||
| 244 | $leaf = ($cw -> cget( -hostname ) ).":$path/$leaf"; | ||
| 245 | } else { | ||
| 246 | $leaf = $path . '/' . $leaf; | ||
| 247 | } | ||
| 248 | } else { | ||
| 249 | $leaf = undef; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | if (scalar(@$leaves)) | ||
| 253 | { | ||
| 254 | my $sm = $cw->Subwidget('file_list')->cget(-selectmode); | ||
| 255 | $cw->{Selected} = $leaves; | ||
| 256 | my $command = $cw->cget('-command'); | ||
| 257 | $command->Call(@{$cw->{Selected}}) if defined $command; | ||
| 258 | } | ||
| 259 | } # end Accept | ||
| 260 | |||
| 261 | sub Accept_dir | ||
| 262 | { | ||
| 263 | my ($cw,$new) = @_; | ||
| 264 | my $dir = $cw->cget('-directory'); | ||
| 265 | $cw -> SelectionClear; | ||
| 266 | $cw->configure(-directory => "$dir/$new"); | ||
| 267 | } | ||
| 268 | |||
| 269 | sub Populate { | ||
| 270 | |||
| 271 | my ($w, $args) = @_; | ||
| 272 | |||
| 273 | require Tk::Listbox; | ||
| 274 | require Tk::Button; | ||
| 275 | require Tk::Dialog; | ||
| 276 | require Tk::DialogBox; | ||
| 277 | require Tk::Toplevel; | ||
| 278 | require Tk::LabEntry; | ||
| 279 | require Cwd; | ||
| 280 | |||
| 281 | my $havenet; | ||
| 282 | $havenet = 1 if &requirecond( "Net::FTP" ); | ||
| 283 | |||
| 284 | $w->SUPER::Populate($args); | ||
| 285 | $w->protocol('WM_DELETE_WINDOW' => ['Cancel', $w ]); | ||
| 286 | |||
| 287 | $w->{'reread'} = 0; | ||
| 288 | $w->withdraw; | ||
| 289 | |||
| 290 | # Create directory/filter entry, place at the top. | ||
| 291 | my $e = $w->Component( | ||
| 292 | LabEntry => 'dir_entry', | ||
| 293 | -textvariable => \$w->{DirectoryString}, | ||
| 294 | -labelVariable => \$w->{Configure}{-dirlabel}, | ||
| 295 | ); | ||
| 296 | $e->pack(-side => 'top', -expand => 0, -pady => 5, -padx => 5, | ||
| 297 | -fill => 'x'); | ||
| 298 | $e->bind('<Return>' => [$w => 'validateDir', Ev(['get'])]); | ||
| 299 | |||
| 300 | # Create file entry, place at the bottom. | ||
| 301 | $e = $w->Component( | ||
| 302 | LabEntry => 'file_entry', | ||
| 303 | -textvariable => \$w->{Configure}{-initialfile}, | ||
| 304 | -labelVariable => \$w->{Configure}{-filelabel}, | ||
| 305 | ); | ||
| 306 | $e->pack(-side => 'bottom', -expand => 0, -pady => 5, -padx => 5, | ||
| 307 | -fill => 'x'); | ||
| 308 | $e->bind('<Return>' => [$w => 'validateFile', Ev(['get'])]); | ||
| 309 | |||
| 310 | # Create directory scrollbox, place at the left-middle. | ||
| 311 | my $b = $w->Component( | ||
| 312 | ScrlListbox => 'dir_list', | ||
| 313 | -labelVariable => \$w->{Configure}{-dirlistlabel}, | ||
| 314 | -scrollbars => 'se', | ||
| 315 | ); | ||
| 316 | $b -> Subwidget('yscrollbar') -> configure(-width=>10); | ||
| 317 | $b -> Subwidget('xscrollbar') -> configure(-width=>10); | ||
| 318 | $b->pack(-side => 'left', -expand => 1, -fill => 'both'); | ||
| 319 | $b->bind('<Double-Button-1>' => [$w => 'Accept_dir', Ev(['getSelected'])]); | ||
| 320 | |||
| 321 | # Add a label. | ||
| 322 | |||
| 323 | my $f = $w->Frame(); | ||
| 324 | $f->pack(-side => 'right', -fill => 'y', -expand => 0); | ||
| 325 | $b = $f->Button('-textvariable' => \$w->{'Configure'}{'-acceptlabel'}, | ||
| 326 | -underline => 0, | ||
| 327 | -command => [ 'Accept', $w ], | ||
| 328 | ); | ||
| 329 | $w -> bind( '<Alt-a>', [$w => 'Accept', Ev(['getSelected'])]); | ||
| 330 | |||
| 331 | $b->pack(-side => 'top', -fill => 'x', -expand => 1); | ||
| 332 | $b = $f->Button('-textvariable' => \$w->{'Configure'}{'-hostlabel'}, | ||
| 333 | -underline => 0, | ||
| 334 | -command => [ 'host', $w ], | ||
| 335 | -state => ($havenet?'normal':'disabled') | ||
| 336 | ); | ||
| 337 | $w -> bind( '<Alt-h>', [$w => 'host', $w]); | ||
| 338 | $b->pack(-side => 'top', -fill => 'x', -expand => 1); | ||
| 339 | $b = $f->Button('-textvariable' => \$w->{'Configure'}{'-cancellabel'}, | ||
| 340 | -underline => 0, | ||
| 341 | -command => [ 'Cancel', $w ], | ||
| 342 | ); | ||
| 343 | $w -> bind( '<Alt-c>', [$w => 'Cancel', $w]); | ||
| 344 | $b->pack(-side => 'top', -fill => 'x', -expand => 1); | ||
| 345 | $b = $f->Button('-textvariable' => \$w->{'Configure'}{'-resetlabel'}, | ||
| 346 | -underline => 0, | ||
| 347 | -command => [$w => 'configure','-directory','.'], | ||
| 348 | ); | ||
| 349 | $w -> bind( '<Alt-r>', [$w => 'configure','-directory','.']); | ||
| 350 | $b->pack(-side => 'top', -fill => 'x', -expand => 1); | ||
| 351 | $b = $f->Button('-textvariable' => \$w->{'Configure'}{'-homelabel'}, | ||
| 352 | -underline => 2, | ||
| 353 | -command => [$w => 'configure','-directory',$ENV{'HOME'}], | ||
| 354 | ); | ||
| 355 | $w -> bind( '<Alt-m>', [$w => 'configure','-directory',$ENV{'HOME'}]); | ||
| 356 | $b->pack(-side => 'top', -fill => 'x', -expand => 1); | ||
| 357 | |||
| 358 | # Create file scrollbox, place at the right-middle. | ||
| 359 | |||
| 360 | $b = $w->Component( | ||
| 361 | ScrlListbox => 'file_list', | ||
| 362 | -labelVariable => \$w->{Configure}{-filelistlabel}, | ||
| 363 | -scrollbars => 'se' | ||
| 364 | ); | ||
| 365 | $b -> Subwidget('yscrollbar') -> configure(-width=>10); | ||
| 366 | $b -> Subwidget('xscrollbar') -> configure(-width=>10); | ||
| 367 | $b->pack(-side => 'right', -expand => 1, -fill => 'both'); | ||
| 368 | $b->bind('<Double-1>' => [$w => 'Accept']); | ||
| 369 | |||
| 370 | # Create -very dialog. | ||
| 371 | |||
| 372 | my $v = $w->Component( | ||
| 373 | Dialog => 'dialog', | ||
| 374 | -title => 'Verify Error', | ||
| 375 | -bitmap => 'error', | ||
| 376 | -buttons => ['Dismiss'], | ||
| 377 | ); | ||
| 378 | |||
| 379 | # Host dialog | ||
| 380 | my $h = $w -> Component( | ||
| 381 | DialogBox => 'hostdialog', | ||
| 382 | -title => 'Select Remote Host', | ||
| 383 | -buttons => [ 'Ok', 'Cancel' ] ); | ||
| 384 | $h -> Component( Label => 'toplabel', | ||
| 385 | -text => "Enter Name or IP Address of Remote Host:" ) | ||
| 386 | -> pack( -expand => '1', -fill => 'x' ); | ||
| 387 | $h -> Component( Entry => 'hostentry', | ||
| 388 | -textvariable => \$w -> {'Configure'}{'-hostname'}, | ||
| 389 | ) -> pack( -expand => '1', -fill => 'x' ); | ||
| 390 | |||
| 391 | $h -> Component( Checkbutton => 'transcriptbutton', | ||
| 392 | -text => 'Log Session on Terminal.', | ||
| 393 | -variable => \$w -> {'Configure'}{'-transcript'}) | ||
| 394 | -> pack( -anchor => 'w' ); | ||
| 395 | |||
| 396 | # login user/password dialog | ||
| 397 | my $l = $w -> Component( | ||
| 398 | DialogBox => 'logindialog', | ||
| 399 | -title => 'Login', | ||
| 400 | -buttons => [ 'Ok', 'Cancel' ] ); | ||
| 401 | $l -> Component( Label => 'useridlabel', | ||
| 402 | -text => 'Please enter your User ID and Password:' | ||
| 403 | ) -> pack( -expand => '1', -fill => 'x' ); | ||
| 404 | $l -> Component ( LabEntry => 'uidentry', | ||
| 405 | -labelVariable => \$w -> {'Configure'}{'-uidlabel'} ) | ||
| 406 | -> pack( -anchor => 'w', -expand => '1', -fill => 'x' ); | ||
| 407 | $l -> Component( LabEntry => 'pwdentry', | ||
| 408 | -labelVariable => \$w -> {'Configure'}{'-pwdlabel'}, | ||
| 409 | -show => '*' ) | ||
| 410 | -> pack( -anchor => 'w', -expand => '1', -fill => 'x' ); | ||
| 411 | |||
| 412 | my $l = $w -> Component( Dialog => 'errormessage', | ||
| 413 | -title => "Network Error", | ||
| 414 | -font => $menufont, | ||
| 415 | -bitmap => 'error' ); | ||
| 416 | |||
| 417 | $w->ConfigSpecs( | ||
| 418 | -width => [ ['file_list','dir_list'], undef, undef, 14 ], | ||
| 419 | -height => [ ['file_list','dir_list'], undef, undef, 14 ], | ||
| 420 | -directory => [ 'METHOD', undef, undef, '.' ], | ||
| 421 | -initialdir => '-directory', | ||
| 422 | -filelabel => [ 'PASSIVE', 'fileLabel', 'FileLabel', | ||
| 423 | 'File Name:' ], | ||
| 424 | -initialfile => [ 'PASSIVE', undef, undef, '' ], | ||
| 425 | -filelistlabel => [ 'PASSIVE', undef, undef, 'Files' ], | ||
| 426 | -filter => [ 'METHOD', undef, undef, undef ], | ||
| 427 | -hostname => [ 'PASSIVE', undef, undef, '' ], | ||
| 428 | -transcript => [ 'PASSIVE', undef, undef, '' ], | ||
| 429 | -userid => [ 'PASSIVE', undef, undef, '' ], | ||
| 430 | -ftp => [ 'PASSIVE', undef, undef, undef ], | ||
| 431 | -networkerror => [ 'PASSIVE', undef, undef, undef ], | ||
| 432 | -password => [ 'PASSIVE', undef, undef, '' ], | ||
| 433 | -defaultextension => [ 'METHOD', undef, undef, undef ], | ||
| 434 | -regexp => [ 'METHOD', undef, undef, undef ], | ||
| 435 | -dirlistlabel => [ 'PASSIVE', undef, undef, 'Directories'], | ||
| 436 | -dirlabel => [ 'PASSIVE', undef, undef, 'Directory:'], | ||
| 437 | '-accept' => [ 'CALLBACK',undef,undef, undef ], | ||
| 438 | -command => [ 'CALLBACK',undef,undef, undef ], | ||
| 439 | -transient => [ 'PASSIVE', undef, undef, 1 ], | ||
| 440 | -verify => [ 'PASSIVE', undef, undef, ['!-d'] ], | ||
| 441 | -create => [ 'PASSIVE', undef, undef, 0 ], | ||
| 442 | -acceptlabel => [ 'PASSIVE', undef, undef, 'Accept'], | ||
| 443 | -hostlabel => [ 'PASSIVE', undef, undef, 'Host'], | ||
| 444 | -cancellabel => [ 'PASSIVE', undef, undef, 'Cancel'], | ||
| 445 | -resetlabel => [ 'PASSIVE', undef, undef, 'Reset'], | ||
| 446 | -homelabel => [ 'PASSIVE', undef, undef, 'Home'], | ||
| 447 | -uidlabel => ['PASSIVE', undef, undef, 'User ID:'], | ||
| 448 | -pwdlabel => ['PASSIVE', undef, undef, 'Password:'], | ||
| 449 | -connected => ['PASSIVE', undef, undef, '' ], | ||
| 450 | DEFAULT => [ 'file_list' ], | ||
| 451 | ); | ||
| 452 | $w->Delegates(DEFAULT => 'file_list'); | ||
| 453 | |||
| 454 | return $w; | ||
| 455 | |||
| 456 | } # end Populate | ||
| 457 | |||
| 458 | sub translate | ||
| 459 | { | ||
| 460 | my ($bs,$ch) = @_; | ||
| 461 | return "\\$ch" if (length $bs); | ||
| 462 | return '.*' if ($ch eq '*'); | ||
| 463 | return '.' if ($ch eq '?'); | ||
| 464 | return "\\." if ($ch eq '.'); | ||
| 465 | return "\\/" if ($ch eq '/'); | ||
| 466 | return "\\\\" if ($ch eq '\\'); | ||
| 467 | return $ch; | ||
| 468 | } | ||
| 469 | |||
| 470 | sub glob_to_re | ||
| 471 | { | ||
| 472 | my $regex = shift; | ||
| 473 | $regex =~ s/(\\?)(.)/&translate($1,$2)/ge; | ||
| 474 | return sub { shift =~ /^${regex}$/ }; | ||
| 475 | } | ||
| 476 | |||
| 477 | sub filter | ||
| 478 | { | ||
| 479 | my ($cw,$val) = @_; | ||
| 480 | my $var = \$cw->{Configure}{'-filter'}; | ||
| 481 | if (@_ > 1 || !defined($$var)) | ||
| 482 | { | ||
| 483 | $val = '*' unless defined $val; | ||
| 484 | $$var = $val; | ||
| 485 | $cw->{'match'} = glob_to_re($val) unless defined $cw->{'match'}; | ||
| 486 | unless ($cw->{'reread'}++) | ||
| 487 | { | ||
| 488 | $cw->Busy; | ||
| 489 | if( ( $cw -> cget( '-connected' ) ) =~ /1/ ) { | ||
| 490 | $cw->afterIdle(['rereadRemote',$cw,$cw->cget('-directory')]) | ||
| 491 | } else { | ||
| 492 | $cw->afterIdle(['reread',$cw,$cw->cget('-directory')]) | ||
| 493 | } | ||
| 494 | } | ||
| 495 | } | ||
| 496 | return $$var; | ||
| 497 | } | ||
| 498 | |||
| 499 | sub regexp | ||
| 500 | { | ||
| 501 | my ($cw,$val) = @_; | ||
| 502 | my $var = \$cw->{Configure}{'-regexp'}; | ||
| 503 | if (@_ > 1) | ||
| 504 | { | ||
| 505 | $$var = $val; | ||
| 506 | $cw->{'match'} = sub { shift =~ m|^${val}$| }; | ||
| 507 | unless ($cw->{'reread'}++) | ||
| 508 | { | ||
| 509 | $cw->Busy; | ||
| 510 | $cw->afterIdle(['reread',$cw]) | ||
| 511 | } | ||
| 512 | } | ||
| 513 | return $$var; | ||
| 514 | } | ||
| 515 | |||
| 516 | sub defaultextension | ||
| 517 | { | ||
| 518 | my ($cw,$val) = @_; | ||
| 519 | if (@_ > 1) | ||
| 520 | { | ||
| 521 | $val = ".$val" if ($val !~ /^\./); | ||
| 522 | $cw->filter("*$val"); | ||
| 523 | } | ||
| 524 | else | ||
| 525 | { | ||
| 526 | $val = $cw->filter; | ||
| 527 | my ($ext) = $val =~ /(\.[^\.]*)$/; | ||
| 528 | return $ext; | ||
| 529 | } | ||
| 530 | } | ||
| 531 | |||
| 532 | sub remoteDirectory { | ||
| 533 | my ($cw, $dir) = @_; | ||
| 534 | return if ( ($cw -> cget( -connected ) ) ne '1' ); | ||
| 535 | my $ftp = $cw -> cget( -ftp ); | ||
| 536 | return if( ! $ftp ); | ||
| 537 | my $current = $ftp -> pwd; | ||
| 538 | my $ndir; | ||
| 539 | if( @_ > 1 && defined $dir ) { | ||
| 540 | if( $current eq $dir ) { | ||
| 541 | $cw->{Configure}{'-directory'} = "$dir"; | ||
| 542 | $cw -> rereadRemote; | ||
| 543 | return; | ||
| 544 | } | ||
| 545 | if( ! $ftp -> cwd( "$current/$dir" ) ) { | ||
| 546 | $cw -> error( "Cannot cwd to $current/$dir." ); | ||
| 547 | $cw -> rereadRemote; | ||
| 548 | return; | ||
| 549 | } | ||
| 550 | $ndir = $ftp -> pwd; | ||
| 551 | $cw->{Configure}{'-directory'} = "$ndir"; | ||
| 552 | $cw -> rereadRemote; | ||
| 553 | } | ||
| 554 | } | ||
| 555 | |||
| 556 | sub directory | ||
| 557 | { | ||
| 558 | my ($cw,$dir) = @_; | ||
| 559 | if( ( $cw -> cget( '-connected' ) ) =~ /1/ ) { | ||
| 560 | $cw -> remoteDirectory( $dir ); | ||
| 561 | return $dir; | ||
| 562 | } | ||
| 563 | my $var = \$cw->{Configure}{'-directory'}; | ||
| 564 | if (@_ > 1 && defined $dir) | ||
| 565 | { | ||
| 566 | if (substr($dir,0,1) eq '~') | ||
| 567 | { | ||
| 568 | if (substr($dir,1,1) eq '/') | ||
| 569 | { | ||
| 570 | $dir = $ENV{'HOME'} . substr($dir,1); | ||
| 571 | } | ||
| 572 | else | ||
| 573 | {my ($uid,$rest) = ($dir =~ m#^~([^/]+)(/.*$)#); | ||
| 574 | $dir = (getpwnam($uid))[7] . $rest; | ||
| 575 | } | ||
| 576 | } | ||
| 577 | $dir =~ s#([^/\\])[\\/]+$#$1#; | ||
| 578 | if (-d $dir) | ||
| 579 | { | ||
| 580 | unless (Tk::tainting()) | ||
| 581 | { | ||
| 582 | my $pwd = Cwd::getcwd(); | ||
| 583 | if (chdir( (defined($dir) ? $dir : '') ) ) | ||
| 584 | { | ||
| 585 | my $new = Cwd::getcwd(); | ||
| 586 | if ($new) | ||
| 587 | { | ||
| 588 | $dir = $new; | ||
| 589 | } | ||
| 590 | else | ||
| 591 | { | ||
| 592 | carp "Cannot getcwd in '$dir'"; | ||
| 593 | } | ||
| 594 | chdir($pwd) || carp "Cannot chdir($pwd) : $!"; | ||
| 595 | $cw->{Configure}{'-directory'} = $dir; | ||
| 596 | } | ||
| 597 | else | ||
| 598 | { | ||
| 599 | $cw->BackTrace("Cannot chdir($dir) :$!"); | ||
| 600 | } | ||
| 601 | } | ||
| 602 | $$var = $dir; | ||
| 603 | unless ($cw->{'reread'}++) | ||
| 604 | { | ||
| 605 | $cw->Busy; | ||
| 606 | $cw->afterIdle(['reread',$cw]) | ||
| 607 | } | ||
| 608 | } | ||
| 609 | } | ||
| 610 | return $$var; | ||
| 611 | } | ||
| 612 | |||
| 613 | sub rereadRemote { | ||
| 614 | my $w = shift; | ||
| 615 | if( ( $w -> cget( -connected ) ) eq '1' ) { | ||
| 616 | $w -> Busy; | ||
| 617 | my ($name, $filter); | ||
| 618 | my $dl = $w->Subwidget('dir_list'); | ||
| 619 | $dl->delete(0, 'end'); | ||
| 620 | my $fl = $w->Subwidget('file_list'); | ||
| 621 | $fl->delete(0, 'end'); | ||
| 622 | my $ftp = $w -> cget( -ftp ); | ||
| 623 | my $dir = $ftp -> pwd; | ||
| 624 | my @files = $ftp -> dir; | ||
| 625 | $dl -> insert( 'end', '..' ); | ||
| 626 | foreach my $f ( @files ) { | ||
| 627 | next if $f =~ /^total/; | ||
| 628 | $name = $f; | ||
| 629 | if ( $f =~ /^l/ ) { | ||
| 630 | $name =~ s/.* (.*) \-\> .*/\1/; | ||
| 631 | } else { | ||
| 632 | $name =~ s/.* //; | ||
| 633 | } | ||
| 634 | if( $f =~ /^d/ ) { | ||
| 635 | $dl -> insert( 'end', $name ); | ||
| 636 | } else { | ||
| 637 | $fl -> insert( 'end', $name ); | ||
| 638 | } | ||
| 639 | } | ||
| 640 | my $host = $w -> cget( '-hostname' ); | ||
| 641 | $w -> {DirectoryString} = "$host\:$dir" . '/' . $w -> cget( '-filter' ); | ||
| 642 | $w -> Unbusy; | ||
| 643 | } | ||
| 644 | } | ||
| 645 | |||
| 646 | sub reread | ||
| 647 | { | ||
| 648 | my ($w) = @_; | ||
| 649 | my $dir = $w->cget('-directory'); | ||
| 650 | if (defined $dir) | ||
| 651 | { | ||
| 652 | if (!defined $w->cget('-filter') or $w->cget('-filter') eq '') | ||
| 653 | { | ||
| 654 | $w->configure('-filter', '*'); | ||
| 655 | } | ||
| 656 | my $dl = $w->Subwidget('dir_list'); | ||
| 657 | $dl->delete(0, 'end'); | ||
| 658 | my $fl = $w->Subwidget('file_list'); | ||
| 659 | $fl->delete(0, 'end'); | ||
| 660 | local *DIR; | ||
| 661 | my $h; | ||
| 662 | if( ( $w -> cget( -connected ) ) eq '1' ) { | ||
| 663 | return $w -> rereadRemote( $dir ); | ||
| 664 | } else { # ! $w -> connected | ||
| 665 | if (opendir(DIR, $dir)) | ||
| 666 | { | ||
| 667 | my $file = $w->cget('-initialfile'); | ||
| 668 | my $seen = 0; | ||
| 669 | my $accept = $w->cget('-accept'); | ||
| 670 | foreach my $f (sort(readdir(DIR))) | ||
| 671 | { | ||
| 672 | next if ($f eq '.'); | ||
| 673 | my $path = "$dir/$f"; | ||
| 674 | if (-d $path) | ||
| 675 | { | ||
| 676 | $dl->insert('end', $f); | ||
| 677 | } | ||
| 678 | else | ||
| 679 | { | ||
| 680 | if (&{$w->{match}}($f)) | ||
| 681 | { | ||
| 682 | if (!defined($accept) || $accept->Call($path)) | ||
| 683 | { | ||
| 684 | $seen = $fl->index('end') if ($file && $f eq $file); | ||
| 685 | $fl->insert('end', $f) | ||
| 686 | } | ||
| 687 | } | ||
| 688 | } | ||
| 689 | } | ||
| 690 | closedir(DIR); | ||
| 691 | if ($seen) | ||
| 692 | { | ||
| 693 | $fl->selectionSet($seen); | ||
| 694 | $fl->see($seen); | ||
| 695 | } | ||
| 696 | else | ||
| 697 | { | ||
| 698 | $w->configure(-initialfile => undef) unless $w->cget('-create'); | ||
| 699 | } | ||
| 700 | } | ||
| 701 | $w->{DirectoryString} = $dir . '/' . $w->cget('-filter'); | ||
| 702 | } | ||
| 703 | $w->{'reread'} = 0; | ||
| 704 | $w->Unbusy; | ||
| 705 | } | ||
| 706 | } | ||
| 707 | |||
| 708 | sub validateDir | ||
| 709 | { | ||
| 710 | my ($cw,$name) = @_; | ||
| 711 | if( ( $cw -> cget( '-connected' ) ) =~ /1/ ) { | ||
| 712 | $name =~ s/^.*\://; | ||
| 713 | } | ||
| 714 | my ($leaf,$base) = fileparse($name); | ||
| 715 | if ($leaf =~ /[*?]/) | ||
| 716 | { | ||
| 717 | $cw->configure('-directory' => $base,'-filter' => $leaf); | ||
| 718 | } | ||
| 719 | else | ||
| 720 | { | ||
| 721 | $cw->configure('-directory' => $name); | ||
| 722 | } | ||
| 723 | } | ||
| 724 | |||
| 725 | sub validateFile | ||
| 726 | { | ||
| 727 | my ($cw,$name) = @_; | ||
| 728 | my $i = 0; | ||
| 729 | my $n = $cw->index('end'); | ||
| 730 | # See if it is an existing file | ||
| 731 | for ($i= 0; $i < $n; $i++) | ||
| 732 | { | ||
| 733 | my $f = $cw->get($i); | ||
| 734 | if ($f eq $name) | ||
| 735 | { | ||
| 736 | $cw->selection('set',$i); | ||
| 737 | $cw->Accept; | ||
| 738 | } | ||
| 739 | } | ||
| 740 | # otherwise allow if -create is set, directory is writable | ||
| 741 | # and it passes filter and accept criteria | ||
| 742 | if ($cw->cget('-create')) | ||
| 743 | { | ||
| 744 | my $path = $cw->cget('-directory'); | ||
| 745 | if (-w $path) | ||
| 746 | { | ||
| 747 | if (&{$cw->{match}}($name)) | ||
| 748 | { | ||
| 749 | my $accept = $cw->cget('-accept'); | ||
| 750 | my $full = "$path/$name"; | ||
| 751 | if (!defined($accept) || $accept->Call($full)) | ||
| 752 | { | ||
| 753 | $cw->{Selected} = [$full]; | ||
| 754 | $cw->Callback(-command => @{$cw->{Selected}}); | ||
| 755 | } | ||
| 756 | else | ||
| 757 | { | ||
| 758 | $cw->Error("$name is not 'acceptable'"); | ||
| 759 | } | ||
| 760 | } | ||
| 761 | else | ||
| 762 | { | ||
| 763 | $cw->Error("$name does not match '".$cw->cget('-filter').'\''); | ||
| 764 | } | ||
| 765 | } | ||
| 766 | else | ||
| 767 | { | ||
| 768 | $cw->Error("Directory '$path' is not writable"); | ||
| 769 | return; | ||
| 770 | } | ||
| 771 | } | ||
| 772 | } | ||
| 773 | |||
| 774 | sub Error | ||
| 775 | { | ||
| 776 | my $cw = shift; | ||
| 777 | my $msg = shift; | ||
| 778 | my $dlg = $cw->Subwidget('dialog'); | ||
| 779 | $dlg->configure(-text => $msg); | ||
| 780 | $dlg->Show; | ||
| 781 | } | ||
| 782 | |||
| 783 | sub Show | ||
| 784 | { | ||
| 785 | my ($cw,@args) = @_; | ||
| 786 | if ($cw->cget('-transient')) { | ||
| 787 | $cw->Popup(@args); | ||
| 788 | $cw->focus; | ||
| 789 | $cw->waitVariable(\$cw->{Selected}); | ||
| 790 | $cw->withdraw; | ||
| 791 | return defined($cw->{Selected}) | ||
| 792 | ? (wantarray) ? @{$cw->{Selected}} : $cw->{Selected}[0] | ||
| 793 | : undef; | ||
| 794 | } else { | ||
| 795 | $cw->Popup(@args); | ||
| 796 | } | ||
| 797 | } | ||
| 798 | |||
| 799 | sub FDialog | ||
| 800 | { | ||
| 801 | my($cmd, %args) = @_; | ||
| 802 | if ($cmd =~ /Save/) | ||
| 803 | { | ||
| 804 | $args{-create} = 1; | ||
| 805 | $args{-verify} = [qw(!-d -w)]; | ||
| 806 | } | ||
| 807 | delete $args{-filetypes}; | ||
| 808 | delete $args{-force}; | ||
| 809 | Tk::DialogWrapper('FileSelect',$cmd, %args); | ||
| 810 | } | ||
| 811 | |||
| 812 | sub requirecond { | ||
| 813 | my ($modulename) = @_; | ||
| 814 | my ($filename, $fullname, $result); | ||
| 815 | $filename = $modulename; | ||
| 816 | $filename .= '.pm' if $filename !~ /.pm$/; | ||
| 817 | $filename =~ s/\:\:/\//; | ||
| 818 | foreach my $prefix ( @INC ) { | ||
| 819 | $fullname = "$prefix/$filename"; | ||
| 820 | if( -f $fullname ) { return do $fullname; } | ||
| 821 | } | ||
| 822 | return 0; | ||
| 823 | } | ||
| 824 | |||
| 825 | 1; | ||
| 826 | |||
| 827 | __END__ |