Show images alternating between two directories. master
authorAndrew Ruthven <puck@catalyst.net.nz>
Sat, 21 Feb 2009 07:39:39 +0000 (20:39 +1300)
committerAndrew Ruthven <puck@dirk.wgtn.cat-it.co.nz>
Sat, 21 Feb 2009 07:39:39 +0000 (20:39 +1300)
lib/Display/Plugins/Photo/AlternatingDirectories.pm [new file with mode: 0644]

diff --git a/lib/Display/Plugins/Photo/AlternatingDirectories.pm b/lib/Display/Plugins/Photo/AlternatingDirectories.pm
new file mode 100644 (file)
index 0000000..0c5c7c1
--- /dev/null
@@ -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;