| 1 | package Win32::UrlCache::Title; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | use Win32::TieRegistry ( Delimiter => '/' ); | ||
| 7 | use File::Spec; | ||
| 8 | use Encode; | ||
| 9 | |||
| 10 | my @CacheDirs; | ||
| 11 | |||
| 12 | sub import { | ||
| 13 | my $class = shift; | ||
| 14 | |||
| 15 | my $cachedir = $Registry->{'CUser/Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders//Cache'}; | ||
| 16 | my $ie5_dir = File::Spec->catdir( $cachedir, 'Content.IE5' ); | ||
| 17 | $cachedir = $ie5_dir if -d $ie5_dir; | ||
| 18 | |||
| 19 | opendir my $dh, $cachedir or croak $!; | ||
| 20 | while( my $entry = readdir $dh ) { | ||
| 21 | next if $entry =~ /^\./; | ||
| 22 | next if -f $entry; | ||
| 23 | push @CacheDirs, File::Spec->catdir( $cachedir, $entry ); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | sub extract { | ||
| 28 | my ($class, $file) = @_; | ||
| 29 | |||
| 30 | foreach my $dir ( @CacheDirs ) { | ||
| 31 | opendir( my $dh, $dir ); | ||
| 32 | while( my $entry = readdir $dh ) { | ||
| 33 | if ( $entry eq $file ) { | ||
| 34 | return _extract( File::Spec->catfile( $dir, $entry ) ); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | closedir $dh; | ||
| 38 | } | ||
| 39 | return; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub _extract { | ||
| 43 | my $file = shift; | ||
| 44 | |||
| 45 | open my $fh, '<', $file or return; | ||
| 46 | binmode $fh; | ||
| 47 | read $fh, my $chunk, 1024; | ||
| 48 | close $fh; | ||
| 49 | |||
| 50 | return unless $chunk; | ||
| 51 | |||
| 52 | my ($title) = $chunk =~ m{<title>(.+?)</title>}is; | ||
| 53 | return unless $title; | ||
| 54 | |||
| 55 | my ($charset) = $chunk =~ m{<meta[^>]+?;\s*charset=['"]?(['">]+?)}is; | ||
| 56 | $charset ||= 'utf8'; | ||
| 57 | |||
| 58 | eval { $title = Encode::decode( $charset => $title ) }; | ||
| 59 | |||
| 60 | return $title; | ||
| 61 | } | ||
| 62 | |||
| 63 | 1; | ||
| 64 | |||
| 65 | __END__ |