| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Archive::Tyd; | ||
| 5 | use MIME::Base64 qw(encode_base64); | ||
| 6 | use Tk; | ||
| 7 | use Tk::HList; | ||
| 8 | use Tk::JPEG; | ||
| 9 | use Tk::PNG; | ||
| 10 | |||
| 11 | $SIG{__WARN__} = sub { | ||
| 12 | }; | ||
| 13 | |||
| 14 | our $main = MainWindow->new ( | ||
| 15 | -title => 'Tydra', | ||
| 16 | ); | ||
| 17 | $main->geometry ('640x480'); | ||
| 18 | $main->optionAdd ('*tearOff', 'false'); | ||
| 19 | |||
| 20 | our $body = { | ||
| 21 | file => undef, # Save file | ||
| 22 | }; | ||
| 23 | |||
| 24 | our $tyd = new Archive::Tyd(); | ||
| 25 | |||
| 26 | ##################### | ||
| 27 | ## Menu Bar ## | ||
| 28 | ##################### | ||
| 29 | |||
| 30 | our $menu = $main->Menu (-type => 'menubar'); | ||
| 31 | $main->configure (-menu => $menu); | ||
| 32 | |||
| 33 | our $fileMenu = $menu->cascade ( | ||
| 34 | -label => '~File', | ||
| 35 | ); | ||
| 36 | |||
| 37 | $fileMenu->command (-label => '~New Archive', -accelerator => 'Ctrl+N', -command => sub { | ||
| 38 | &newArchive(); | ||
| 39 | }); | ||
| 40 | |||
| 41 | $fileMenu->command (-label => '~Open Archive', -accelerator => 'Ctrl+O', -command => sub { | ||
| 42 | &openArchive(); | ||
| 43 | }); | ||
| 44 | |||
| 45 | $fileMenu->command (-label => '~Save Archive', -accelerator => 'Ctrl+S', -command => sub { | ||
| 46 | &saveArchive(); | ||
| 47 | }); | ||
| 48 | |||
| 49 | $fileMenu->command (-label => 'Save Archive ~As...', -accelerator => 'Shift+Ctrl+S', -command => sub { | ||
| 50 | &saveArchive('as'); | ||
| 51 | }); | ||
| 52 | |||
| 53 | $fileMenu->command (-label => '~Close Archive', -accelerator => 'Ctrl+W', -command => sub { | ||
| 54 | &newArchive(); | ||
| 55 | }); | ||
| 56 | |||
| 57 | $fileMenu->separator; | ||
| 58 | |||
| 59 | $fileMenu->command (-label => '~Exit Tydra', -accelerator => 'Alt+F4', -command => sub { | ||
| 60 | exit(0); | ||
| 61 | }); | ||
| 62 | |||
| 63 | our $tydMenu = $menu->cascade ( | ||
| 64 | -label => '~Tyd', | ||
| 65 | ); | ||
| 66 | |||
| 67 | $tydMenu->command (-label => '~Add File...', -accelerator => 'Ctrl+A', -command => sub { | ||
| 68 | &addFile(); | ||
| 69 | }); | ||
| 70 | |||
| 71 | $tydMenu->command (-label => '~Extract File...', -accelerator => 'Ctrl+E', -command => sub { | ||
| 72 | &extractFile(); | ||
| 73 | }); | ||
| 74 | |||
| 75 | $tydMenu->command (-label => '~Delete File', -accelerator => 'Ctrl+X', -command => sub { | ||
| 76 | &delFile(); | ||
| 77 | }); | ||
| 78 | |||
| 79 | $tydMenu->command (-label => '~View File', -accelerator => 'Enter', -command => sub { | ||
| 80 | &viewFile(); | ||
| 81 | }); | ||
| 82 | |||
| 83 | our $helpMenu = $menu->cascade ( | ||
| 84 | -label => '~Help', | ||
| 85 | ); | ||
| 86 | |||
| 87 | $helpMenu->command (-label => '~About...', -accelerator => 'F1', -command => sub { | ||
| 88 | &about(); | ||
| 89 | }); | ||
| 90 | |||
| 91 | ##################### | ||
| 92 | ## Binding Keys ## | ||
| 93 | ##################### | ||
| 94 | |||
| 95 | $main->bind ('<Control-n>', \&newArchive); | ||
| 96 | $main->bind ('<Control-o>', \&openArchive); | ||
| 97 | $main->bind ('<Control-s>', \&saveArchive); | ||
| 98 | $main->bind ('<Control-S>', sub { &saveArchive('as'); }); | ||
| 99 | $main->bind ('<Control-w>', \&newArchive); | ||
| 100 | $main->bind ('<Control-a>', \&addFile); | ||
| 101 | $main->bind ('<Control-e>', \&extractFile); | ||
| 102 | $main->bind ('<Control-x>', \&delFile); | ||
| 103 | $main->bind ('<Return>', \&viewFile); | ||
| 104 | $main->bind ('<F1>', \&about); | ||
| 105 | |||
| 106 | ##################### | ||
| 107 | ## Workspace ## | ||
| 108 | ##################### | ||
| 109 | |||
| 110 | our $table = $main->Scrolled ('HList', | ||
| 111 | -scrollbars => 'ose', | ||
| 112 | -header => 1, | ||
| 113 | -columns => 2, | ||
| 114 | -foreground => '#000000', | ||
| 115 | -background => '#FFFFFF', | ||
| 116 | -selectforeground => '#000000', | ||
| 117 | -selectbackground => '#FFFF00', | ||
| 118 | -selectborder => 0, | ||
| 119 | -command => sub { | ||
| 120 | &viewFile(); | ||
| 121 | }, | ||
| 122 | )->pack (-fill => 'both', -expand => 1); | ||
| 123 | |||
| 124 | $table->header ('create', 0, -text => 'File Name'); | ||
| 125 | $table->header ('create', 1, -text => 'Size'); | ||
| 126 | |||
| 127 | MainLoop; | ||
| 128 | |||
| 129 | sub newArchive { | ||
| 130 | $tyd = undef; | ||
| 131 | $tyd = new Archive::Tyd(); | ||
| 132 | |||
| 133 | &refresh(); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub openArchive { | ||
| 137 | my $file = $main->getOpenFile ( | ||
| 138 | -defaultextension => 'tyd', | ||
| 139 | -filetypes => [ | ||
| 140 | [ 'Tyd Archive', ['*.tyd', '*.dat'] ], | ||
| 141 | [ 'All Files', '*.*' ], | ||
| 142 | ], | ||
| 143 | -initialdir => '.', | ||
| 144 | -title => 'Open Archive...', | ||
| 145 | ); | ||
| 146 | return unless defined $file; | ||
| 147 | |||
| 148 | my $password = &password(); | ||
| 149 | return unless defined $password; | ||
| 150 | |||
| 151 | $body->{file} = $file; | ||
| 152 | |||
| 153 | my $read = new Archive::Tyd (password => $password); | ||
| 154 | $read->openArchive ($file); | ||
| 155 | $tyd = $read; | ||
| 156 | |||
| 157 | # Populate the list. | ||
| 158 | &refresh(); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub saveArchive { | ||
| 162 | my $as = shift || undef; | ||
| 163 | |||
| 164 | my $file = $main->getSaveFile ( | ||
| 165 | -defaultextension => 'tyd', | ||
| 166 | -filetypes => [ | ||
| 167 | [ 'Tyd Archive', ['*.tyd', '*.dat'] ], | ||
| 168 | [ 'All Files', '*.*' ], | ||
| 169 | ], | ||
| 170 | -initialdir => '.', | ||
| 171 | -title => 'Save Archive...', | ||
| 172 | ); | ||
| 173 | return unless defined $file; | ||
| 174 | |||
| 175 | my $password = &password; | ||
| 176 | return unless defined $password; | ||
| 177 | |||
| 178 | unlink ($file) if (-e $file); | ||
| 179 | |||
| 180 | my $write = new Archive::Tyd (password => $password); | ||
| 181 | $write->{files} = $tyd->{files}; | ||
| 182 | $write->writeArchive ($file); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub addFile { | ||
| 186 | my $file = $main->getOpenFile ( | ||
| 187 | -defaultextension => 'tyd', | ||
| 188 | -filetypes => [ | ||
| 189 | [ 'Common Files', '*.*' ], | ||
| 190 | [ 'All Files', '*.*' ], | ||
| 191 | ], | ||
| 192 | -initialdir => '.', | ||
| 193 | -title => 'Select File...', | ||
| 194 | ); | ||
| 195 | return unless defined $file; | ||
| 196 | |||
| 197 | $tyd->addFile ($file); | ||
| 198 | &refresh(); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub extractFile { | ||
| 202 | my $index = $table->selectionGet; | ||
| 203 | my $file = $table->itemCget ($index,0,-text); | ||
| 204 | return unless exists $tyd->{files}->{$file}; | ||
| 205 | |||
| 206 | my ($ext) = $file =~ /\.(\w+)$/i; | ||
| 207 | print "Ext: $ext\n"; | ||
| 208 | |||
| 209 | my $target = $main->getSaveFile ( | ||
| 210 | -defaultextension => ".$ext", | ||
| 211 | -filetypes => [ | ||
| 212 | [ 'Text Document', [ '*.txt' ] ], | ||
| 213 | [ 'Perl File', [ '*.pl', '*.pm', ] ], | ||
| 214 | [ 'JPEG Image', [ '*.jpeg', '*.jpg', '*.jpe' ] ], | ||
| 215 | [ 'GIF Image', [ '*.gif', ] ], | ||
| 216 | [ 'PNG Image', [ '*.png', ] ], | ||
| 217 | [ 'BMP Image', [ '*.bmp', ] ], | ||
| 218 | [ 'All Files', '*.*' ], | ||
| 219 | ], | ||
| 220 | -initialdir => '.', | ||
| 221 | -title => 'Extract File...', | ||
| 222 | ); | ||
| 223 | return unless defined $target; | ||
| 224 | |||
| 225 | my $bin = $tyd->readFile ($file); | ||
| 226 | open (OUT, ">$target"); | ||
| 227 | binmode OUT; | ||
| 228 | print OUT $bin; | ||
| 229 | close (OUT); | ||
| 230 | } | ||
| 231 | |||
| 232 | sub delFile { | ||
| 233 | my $index = $table->selectionGet; | ||
| 234 | my $file = $table->itemCget ($index,0,-text); | ||
| 235 | return unless exists $tyd->{files}->{$file}; | ||
| 236 | |||
| 237 | # Delete the file. | ||
| 238 | $tyd->deleteFile ($file); | ||
| 239 | &refresh(); | ||
| 240 | } | ||
| 241 | |||
| 242 | sub viewFile { | ||
| 243 | my $index = $table->selectionGet; | ||
| 244 | my $file = $table->itemCget ($index,0,-text); | ||
| 245 | return unless exists $tyd->{files}->{$file}; | ||
| 246 | |||
| 247 | # Figure out its extension. | ||
| 248 | if ($file =~ /\.(jpg|jpe|jpeg)$/i) { | ||
| 249 | my $bin = $tyd->readFile ($file); | ||
| 250 | my $base = encode_base64 ($bin); | ||
| 251 | |||
| 252 | my $image = $main->Photo (-data => $base, -format => 'JPEG'); | ||
| 253 | |||
| 254 | my $show = $main->DialogBox ( | ||
| 255 | -title => "$file", | ||
| 256 | -buttons => [ 'Close' ], | ||
| 257 | ); | ||
| 258 | |||
| 259 | $show->Label ( | ||
| 260 | -image => $image, | ||
| 261 | )->pack (-padx => 5, -pady => 5); | ||
| 262 | |||
| 263 | $show->Show; | ||
| 264 | } | ||
| 265 | elsif ($file =~ /\.gif$/i) { | ||
| 266 | my $bin = $tyd->readFile ($file); | ||
| 267 | my $base = encode_base64 ($bin); | ||
| 268 | |||
| 269 | my $image = $main->Photo (-data => $base, -format => 'GIF'); | ||
| 270 | |||
| 271 | my $show = $main->DialogBox ( | ||
| 272 | -title => "$file", | ||
| 273 | -buttons => [ 'Close' ], | ||
| 274 | ); | ||
| 275 | |||
| 276 | $show->Label ( | ||
| 277 | -image => $image, | ||
| 278 | )->pack (-padx => 5, -pady => 5); | ||
| 279 | |||
| 280 | $show->Show; | ||
| 281 | } | ||
| 282 | elsif ($file =~ /\.png$/i) { | ||
| 283 | my $bin = $tyd->readFile ($file); | ||
| 284 | my $base = encode_base64 ($bin); | ||
| 285 | |||
| 286 | my $image = $main->Photo (-data => $base, -format => 'PNG'); | ||
| 287 | |||
| 288 | my $show = $main->DialogBox ( | ||
| 289 | -title => "$file", | ||
| 290 | -buttons => [ 'Close' ], | ||
| 291 | ); | ||
| 292 | |||
| 293 | $show->Label ( | ||
| 294 | -image => $image, | ||
| 295 | )->pack (-padx => 5, -pady => 5); | ||
| 296 | |||
| 297 | $show->Show; | ||
| 298 | } | ||
| 299 | elsif ($file =~ /\.bmp$/i) { | ||
| 300 | my $bin = $tyd->readFile ($file); | ||
| 301 | my $base = encode_base64 ($bin); | ||
| 302 | |||
| 303 | my $image = $main->Photo (-data => $base, -format => 'BMP'); | ||
| 304 | |||
| 305 | my $show = $main->DialogBox ( | ||
| 306 | -title => "$file", | ||
| 307 | -buttons => [ 'Close' ], | ||
| 308 | ); | ||
| 309 | |||
| 310 | $show->Label ( | ||
| 311 | -image => $image, | ||
| 312 | )->pack (-padx => 5, -pady => 5); | ||
| 313 | |||
| 314 | $show->Show; | ||
| 315 | } | ||
| 316 | elsif ($file =~ /\.(htm|html)$/i) { | ||
| 317 | my $bin = $tyd->readFile ($file); | ||
| 318 | open (TMP, ">./tmp.html"); | ||
| 319 | print TMP $bin; | ||
| 320 | close (TMP); | ||
| 321 | |||
| 322 | system ("start tmp.html"); | ||
| 323 | unlink ("./tmp.html"); | ||
| 324 | } | ||
| 325 | else { | ||
| 326 | # We'll assume it's text. | ||
| 327 | my $show = $main->DialogBox ( | ||
| 328 | -title => "$file", | ||
| 329 | -buttons => [ 'Close' ], | ||
| 330 | ); | ||
| 331 | |||
| 332 | my $view = $show->Scrolled ('ROText', | ||
| 333 | -foreground => '#000000', | ||
| 334 | -background => '#FFFFFF', | ||
| 335 | -scrollbars => 'ose', | ||
| 336 | -wrap => 'word', | ||
| 337 | )->pack (-fill => 'both', -expand => 1); | ||
| 338 | |||
| 339 | $view->insert ('end',$tyd->readFile($file)); | ||
| 340 | |||
| 341 | $show->Show; | ||
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 345 | sub about { | ||
| 346 | my $show = $main->DialogBox ( | ||
| 347 | -title => "About Tydra", | ||
| 348 | -buttons => [ 'Close' ], | ||
| 349 | ); | ||
| 350 | |||
| 351 | my $view = $show->Scrolled ('ROText', | ||
| 352 | -foreground => '#000000', | ||
| 353 | -background => '#FFFFFF', | ||
| 354 | -scrollbars => 'ose', | ||
| 355 | -wrap => 'word', | ||
| 356 | )->pack (-fill => 'both', -expand => 1); | ||
| 357 | |||
| 358 | $view->insert ('end',"About Tydra\n\n" | ||
| 359 | . "Tydra is a Perl/Tk interface for the Tyd archiver.\n\n" | ||
| 360 | . "Tydra and the Tyd archiving format are owned by Cerone Kirsle\n" | ||
| 361 | . "(http://search.cpan.org/~kirsle/)"); | ||
| 362 | |||
| 363 | $show->Show; | ||
| 364 | } | ||
| 365 | |||
| 366 | sub refresh { | ||
| 367 | my @files = $tyd->contents; | ||
| 368 | |||
| 369 | $table->delete ('all'); | ||
| 370 | |||
| 371 | my $row = 0; | ||
| 372 | foreach my $file (@files) { | ||
| 373 | my $bin = $tyd->readFile ($file); | ||
| 374 | my $len = length $bin; | ||
| 375 | |||
| 376 | $table->add ($row); | ||
| 377 | $table->itemCreate ($row, 0, -text => $file); | ||
| 378 | $table->itemCreate ($row, 1, -text => "$len bytes"); | ||
| 379 | $row++; | ||
| 380 | } | ||
| 381 | } | ||
| 382 | |||
| 383 | sub password { | ||
| 384 | my $pass = $main->DialogBox ( | ||
| 385 | -title => 'Enter Archive Password', | ||
| 386 | -buttons => ['Ok', 'Cancel'], | ||
| 387 | -default_button => 'Ok', | ||
| 388 | ); | ||
| 389 | |||
| 390 | $pass->Label ( | ||
| 391 | -text => 'Enter the password for this archive:', | ||
| 392 | )->pack(); | ||
| 393 | |||
| 394 | my $password = ''; | ||
| 395 | |||
| 396 | $pass->Entry ( | ||
| 397 | -textvariable => \$password, | ||
| 398 | -takefocus => 1, | ||
| 399 | -show => '*', | ||
| 400 | )->pack (-fill => 'x', -expand => 1); | ||
| 401 | |||
| 402 | $pass->focusForce(); | ||
| 403 | |||
| 404 | my $choice = $pass->Show; | ||
| 405 | |||
| 406 | if ($choice =~ /ok/i) { | ||
| 407 | return $password; | ||
| 408 | } | ||
| 409 | |||
| 410 | return undef; | ||
| 411 | } | ||
| 412 | |||
| 413 | =head1 NAME | ||
| 414 | |||
| 415 | Tydra - A "WinZip" style application for Tyd documents. | ||
| 416 | |||
| 417 | =head1 SYNOPSIS | ||
| 418 | |||
| 419 | $ perl Tydra.pl | ||
| 420 | |||
| 421 | =head1 DESCRIPTION | ||
| 422 | |||
| 423 | Tydra is a Perl/Tk application as a front-end to viewing and saving Tyd archives visually, | ||
| 424 | in a similar format to WinZip. | ||
| 425 | |||
| 426 | =cut |