The Notifications object now handles displaying multiple notifications.
[picture-display.git] / lib / Display / Plugins / MPD.pm
index 8a7362d..13d92df 100644 (file)
@@ -1,29 +1,41 @@
 package Display::Plugins::MPD;
 
 use Clutter;
+use Carp;
 use Audio::MPD;
 use POSIX qw/strftime/;
-use base ('Display::Plugin');
+use POE::Session;
+use base ('Display::Plugins');
+use FindBin qw/$Bin/;
+use strict;
 
-my $delay = 15;
+my $delay = 5;
 
 sub new {
   my $proto = shift;
   my $class = ref($proto) || $proto;
 
   my $self = $class->SUPER::new(@_);
+  bless ($self, $class);
+
+  eval {
+    $self->{'mpd'} = Audio::MPD->new();
+  };
+  if ($@) {
+    carp "Failed to connect to MPD: $@";
+  } else {
+    $self->init_delay($delay, 'mpd_display');
+    $self->init();
+  }
 
-  $self->{'mpd'} = Audio::MPD->new();
   $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();
 
@@ -33,63 +45,22 @@ sub display {
     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(20, $self->{'stage'}->get_height() - 20);
-
-    if (! $self->{'active'}) {
-      $self->{'block'}->set_opacity(0);
-      $self->{'stage'}->add($self->{'block'});
-      my $effect = Clutter::EffectTemplate->new_for_duration(1000, 'main::smoothstep_inc');
-      my $timeline = Clutter::Effect->fade($effect, $self->{'block'}, 255);
-      $timeline->start();
-      
-      $self->{'active'} = time() + $delay;
-    }
-
-    Glib::Timeout->add_seconds($delay, $self->can('hide_display'), $self);
+    $kernel->post('display', 'notifications_add', join("\n", @lines), $self->{'logo'});
   }
 
-  return 1;
-}
-
-sub hide_display {
-  my $self = shift;
-
-  if ($self->{'active'} <= time()) {
-    $self->{'stage'}->remove($self->{'block'});
-
-    $self->{'active'} = 0;
-  }
-
-  return 0;
+  $self->delay();
 }
 
 sub init {
   my $self = shift;
 
-  $self->{'block'} = Clutter::Group->new();
-
-  $self->{'bg'} = Clutter::Rectangle->new(Clutter::Color->parse('Black'));
-  $self->{'bg'}->set_width($self->{'stage'}->get_width() - 20);
-  $self->{'bg'}->set_opacity(100);
-
-  $self->{'status'} = Clutter::Label->new('Sans 20', "Song\nAlbum");
-  $self->{'status'}->set_color(Clutter::Color->parse('White'));
-  $self->{'status'}->set_ellipsize('end');
-  $self->{'status'}->set_width($self->{'bg'}->get_width() - 20);
+  $self->{'kernel'}->state('mpd_display', $self, 'display');
 
-  $self->{'block'}->add($self->{'bg'});
-  $self->{'block'}->add($self->{'status'});
+  $self->{'logo'} = "$Bin/share/MPD.png";
 
-  Glib::Timeout->add_seconds(5, $self->can('display'), $self);
+  $self->{'kernel'}->yield('mpd_display');
 }
 
 1;