The Notifications object now handles displaying multiple notifications.
[picture-display.git] / lib / Display / Plugins / MPD.pm
index 4db3081..13d92df 100644 (file)
@@ -1,83 +1,66 @@
 package Display::Plugins::MPD;
 
 use Clutter;
+use Carp;
 use Audio::MPD;
 use POSIX qw/strftime/;
+use POE::Session;
+use base ('Display::Plugins');
+use FindBin qw/$Bin/;
+use strict;
+
+my $delay = 5;
 
 sub new {
-  my ($class,$stage) = @_;
+  my $proto = shift;
+  my $class = ref($proto) || $proto;
+
+  my $self = $class->SUPER::new(@_);
+  bless ($self, $class);
 
-  my $self = {
-    'stage' => $stage,
-    'mpd' => Audio::MPD->new(),
-    'file' => '',
-    'active'=> 0,
+  eval {
+    $self->{'mpd'} = Audio::MPD->new();
   };
+  if ($@) {
+    carp "Failed to connect to MPD: $@";
+  } else {
+    $self->init_delay($delay, 'mpd_display');
+    $self->init();
+  }
+
+  $self->{'file'} = '';
+  $self->{'active'} = 0;
 
-  bless ($self, $class);
-  $self->init();
   return $self;
 }
 
 sub display {
-  my $self = shift;
+  my ($self, $kernel) = @_[OBJECT, KERNEL];
 
   my $current = $self->{'mpd'}->current();
 
-  if ($self->{'file'} ne $current->file()) {
+  if (defined $current && $self->{'file'} ne $current->file()) {
     my @lines = ();
     push @lines, $current->title()  if defined $current->title();
     push @lines, $current->album()  if defined $current->album();
     push @lines, $current->artist() if defined $current->artist();
 
-    $self->{'status'}->set_text(join("\n", @lines));
-
     $self->{'file'} = $current->file();
 
-    $self->{'bg'}->set_height($self->{'status'}->get_height() + 10);
-    $self->{'bg'}->set_anchor_point(1, $self->{'bg'}->get_height());
-    $self->{'bg'}->set_position(10, $self->{'stage'}->get_height() - 10);
-
-    $self->{'status'}->set_anchor_point(1, $self->{'status'}->get_height());
-    $self->{'status'}->set_position(15, $self->{'stage'}->get_height() - 20);
-
-    if (! $self->{'active'}) {
-      $self->{'stage'}->add($self->{'bg'});
-      $self->{'stage'}->add($self->{'status'});
-      
-      $self->{'active'} = time() + 30;
-    }
-
-    Glib::Timeout->add_seconds(30, $self->can('hide_display'), $self);
-  }
-
-  return 1;
-}
-
-sub hide_display {
-  my $self = shift;
-
-  if ($self->{'active'} <= time()) {
-    $self->{'stage'}->remove($self->{'status'});
-    $self->{'stage'}->remove($self->{'bg'});
-
-    $self->{'active'} = 0;
+    $kernel->post('display', 'notifications_add', join("\n", @lines), $self->{'logo'});
   }
 
-  return 0;
+  $self->delay();
 }
 
 sub init {
   my $self = shift;
 
-  $self->{'status'} = Clutter::Label->new('Sans 20', "Song\nAlbum");
-  $self->{'status'}->set_color(Clutter::Color->parse('White'));
+  $self->{'kernel'}->state('mpd_display', $self, 'display');
 
-  $self->{'bg'} = Clutter::Rectangle->new(Clutter::Color->parse('Black'));
-  $self->{'bg'}->set_width($self->{'stage'}->get_width() - 40);
-  $self->{'bg'}->set_opacity(100);
+  $self->{'logo'} = "$Bin/share/MPD.png";
 
-  Glib::Timeout->add_seconds(5, $self->can('display'), $self);
+  $self->{'kernel'}->yield('mpd_display');
 }
 
 1;