From: Andrew Ruthven Date: Sat, 21 Feb 2009 07:39:39 +0000 (+1300) Subject: Show images alternating between two directories. X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=picture-display.git Show images alternating between two directories. --- diff --git a/lib/Display/Plugins/Photo/AlternatingDirectories.pm b/lib/Display/Plugins/Photo/AlternatingDirectories.pm new file mode 100644 index 0000000..0c5c7c1 --- /dev/null +++ b/lib/Display/Plugins/Photo/AlternatingDirectories.pm @@ -0,0 +1,57 @@ +package Display::Plugins::Photo::AlternatingDirectories; + +use base ('Display::Plugins::Photo'); +use Clutter; +use POE::Session; +use FindBin qw/$Bin/; + +sub new { + my $proto = shift; + my $class = ref($proto) || $proto; + + my $self = $class->SUPER::new(@_); + + $self->{'current_directory'} = -1; + + bless ($self, $class); + $self->{'kernel'}->yield('find_photo'); + return $self; +} + +sub find_photo { + my $self = shift; + + $self->{'current directory'} = + ($self->{'current directory'} + 1) % @{ $self->{'config'}{'_config'}{'plugins'}{'AlternatingDirectories'}{'Directories'} }; + +#pr#int join(", ", @{ $self->{'config'}{'_config'}{'plugins'}{'AlternatingDirectories'}{'Directories'} }) . "\n"; +#print "dir: $self->{'current directory'}\n"; + + my $directory = $self->{'config'}{'_config'}{'plugins'}{'AlternatingDirectories'}{'Directories'}[$self->{'current directory'}]; + + $self->{'directory state'}{$directory} ||= ''; + + if (opendir(DIR, $directory)) { + my @files = sort readdir(DIR); + closedir DIR; + + my $photo = $files[0]; + for my $file (@files) { +#print "$file cmp $self->{'directory state'}{$directory} = " . ($file cmp $self->{'directory state'}{$directory}) . "\n"; + if ($file cmp $self->{'directory state'}{$directory} > 1) { + next unless $file =~ /(jpg|png|gif)$/; + $photo = $file; + last; + } + } + + $self->{'directory state'}{$directory} = $photo; + +#print "photo: $directory/$photo\n"; + $self->{'kernel'}->yield('display_photo', join("/", $directory, $photo)); + } else { + warn "Failed to open $directory for reading: $!\n"; + } +} + +1;