Show images alternating between two directories.
[picture-display.git] / lib / Display / Plugins / Photo / AlternatingDirectories.pm
1 package Display::Plugins::Photo::AlternatingDirectories;
2
3 use base ('Display::Plugins::Photo');
4 use Clutter;
5 use POE::Session;
6 use FindBin qw/$Bin/;
7
8 sub new {
9   my $proto = shift;
10   my $class = ref($proto) || $proto;
11
12   my $self = $class->SUPER::new(@_);
13
14   $self->{'current_directory'} = -1;
15
16   bless ($self, $class);
17   $self->{'kernel'}->yield('find_photo');
18   return $self;
19 }
20
21 sub find_photo {
22   my $self = shift;
23
24   $self->{'current directory'} =
25     ($self->{'current directory'} + 1) % @{ $self->{'config'}{'_config'}{'plugins'}{'AlternatingDirectories'}{'Directories'} };
26
27 #pr#int join(", ", @{ $self->{'config'}{'_config'}{'plugins'}{'AlternatingDirectories'}{'Directories'} }) . "\n";
28 #print "dir: $self->{'current directory'}\n";
29
30   my $directory = $self->{'config'}{'_config'}{'plugins'}{'AlternatingDirectories'}{'Directories'}[$self->{'current directory'}];
31
32   $self->{'directory state'}{$directory} ||= '';
33
34   if (opendir(DIR, $directory)) {
35     my @files = sort readdir(DIR);
36     closedir DIR;
37
38     my $photo = $files[0];
39     for my $file (@files) {
40 #print "$file cmp $self->{'directory state'}{$directory} = " . ($file cmp $self->{'directory state'}{$directory}) . "\n";
41       if ($file cmp $self->{'directory state'}{$directory} > 1) {
42         next unless $file =~ /(jpg|png|gif)$/;
43         $photo = $file;
44         last;
45       }
46     } 
47
48     $self->{'directory state'}{$directory} = $photo;
49
50 #print "photo: $directory/$photo\n";
51     $self->{'kernel'}->yield('display_photo', join("/", $directory, $photo));
52   } else {
53     warn "Failed to open $directory for reading: $!\n";
54   }
55 }
56
57 1;