| 1 | # $Id: Folder.pm 193 2009-01-16 13:42:25Z fish $ | ||
| 2 | package WWW::MeGa::Item::Folder; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | WWW::MeGa::Item::Folder - Representing a folder / album in L<WWW::MeGa> | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | See L<WWW::MeGa::Item> | ||
| 13 | |||
| 14 | =head1 CHANGED METHODS | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | |||
| 19 | use base 'WWW::MeGa::Item'; | ||
| 20 | |||
| 21 | our $VERSION = '0.11'; | ||
| 22 | |||
| 23 | |||
| 24 | =head2 thumbnail_source | ||
| 25 | |||
| 26 | looks for a file named by the C<album_thumb> config parameter, defaults | ||
| 27 | to C<THUMBNAIL> in this folder and return it if found. | ||
| 28 | |||
| 29 | If not, select the first item of the directory as thumbnail. | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | sub thumbnail_source | ||
| 34 | { | ||
| 35 | my $self = shift; | ||
| 36 | my $thumb = File::Spec->catdir($self->{path}, $self->{config}->param('album_thumb')); | ||
| 37 | |||
| 38 | return $thumb if -e $thumb; | ||
| 39 | warn "$thumb not found, autoselecting" if $self->{config}->param('debug'); | ||
| 40 | my $first = $self->first or return; | ||
| 41 | my $item = WWW::MeGa::Item->new($first,$self->{config},$self->{cache}); | ||
| 42 | return $item->thumbnail_source; | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | =head2 list | ||
| 47 | |||
| 48 | returns a list of all files in the directory. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub list | ||
| 53 | { | ||
| 54 | my $self = shift; | ||
| 55 | my $thumb = $self->{config}->param('album_thumb'); | ||
| 56 | my @dir; | ||
| 57 | opendir my $dh, $self->{path}; | ||
| 58 | while (my $file = readdir $dh) | ||
| 59 | { | ||
| 60 | next if $file eq '.' or $file eq '..'; | ||
| 61 | next if $file eq $thumb; | ||
| 62 | push @dir, File::Spec->catdir($self->{path_rel},$file); | ||
| 63 | } | ||
| 64 | closedir $dh; | ||
| 65 | return sort @dir | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 first | ||
| 69 | |||
| 70 | returns the first file of the directory. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub first | ||
| 75 | { | ||
| 76 | my $self = shift; | ||
| 77 | opendir my $dh, $self->{path}; | ||
| 78 | while(my $file = readdir $dh) | ||
| 79 | { | ||
| 80 | next if $file eq '.' or $file eq '..'; | ||
| 81 | close $dh; | ||
| 82 | return File::Spec->catdir($self->{path_rel},$file); | ||
| 83 | } | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | |||
| 87 | =head2 neighbours($path) | ||
| 88 | |||
| 89 | return the item before and after the item specified by $path in the represented directory. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub neighbours | ||
| 94 | { | ||
| 95 | my $self = shift; | ||
| 96 | my $path = shift; | ||
| 97 | my @files = $self->list; | ||
| 98 | my $i; | ||
| 99 | my %index = map { $_ => $i++ } @files; | ||
| 100 | |||
| 101 | my $idx = $index{$path}; | ||
| 102 | |||
| 103 | return $files[$idx-1], $files[$idx+1]; | ||
| 104 | } | ||
| 105 | 1; |