Switch to use correct method for checking if we're connected.
[picture-display.git] / lib / Display / Plugins / MPD.pm
1 package Display::Plugins::MPD;
2
3 use Clutter;
4 use POSIX qw/strftime/;
5 use base ('Display::Plugin');
6 use POE qw{ Component::Client::MPD };
7
8 sub new {
9   my $proto = shift;
10   my $class = ref($proto) || $proto;
11
12   my $self = $class->SUPER::new(@_);
13
14   $self->{'file'} = '';
15   $self->{'active'} = 0;
16
17   bless ($self, $class);
18   $self->init();
19   return $self;
20 }
21
22 sub display {
23   my ($self, $msg, $answer) = @_[OBJECT, ARG0, ARG1];
24
25 warn "Got a response from MPD\n";
26   my $current = $answer->current();
27
28   if ($self->{'file'} ne $current->file()) {
29     my @lines = ();
30     push @lines, $current->title()  if defined $current->title();
31     push @lines, $current->album()  if defined $current->album();
32     push @lines, $current->artist() if defined $current->artist();
33
34     $self->{'status'}->set_text(join("\n", @lines));
35
36     $self->{'file'} = $current->file();
37
38     $self->{'bg'}->set_height($self->{'status'}->get_height() + 10);
39     $self->{'bg'}->set_anchor_point(1, $self->{'bg'}->get_height());
40     $self->{'bg'}->set_position(10, $self->{'stage'}->get_height() - 10);
41
42     $self->{'status'}->set_anchor_point(1, $self->{'status'}->get_height());
43     $self->{'status'}->set_position(15, $self->{'stage'}->get_height() - 20);
44
45     if (! $self->{'active'}) {
46       $self->{'stage'}->add($self->{'bg'});
47       $self->{'stage'}->add($self->{'status'});
48       
49       $self->{'active'} = time() + 30;
50     }
51
52     $self->{'KERNEL'}->delay( hide_display, 30 );
53   }
54
55   $self->{'KERNEL'}->delay( mpd, 5, current );
56 }
57
58 sub hide_display {
59   my $self = shift;
60
61   if ($self->{'active'} <= time()) {
62     $self->{'stage'}->remove($self->{'status'});
63     $self->{'stage'}->remove($self->{'bg'});
64
65     $self->{'active'} = 0;
66   }
67
68   return 0;
69 }
70
71 sub error {
72   my ($kernel, $msg, $errstr) = @_[KERNEL, ARG0, ARG1];
73
74   warn "MPD: Error $errstr\n";
75
76   $kernel->delay( mpd, 5, current );
77 }
78
79 sub init {
80   my $self = shift;
81
82   $self->{'kernel'}->state('mpd_connected', $self, 'connected');
83   $self->{'kernel'}->state('mpd_result', $self, 'display');
84   $self->{'kernel'}->state('mpd_error', $self, 'error');
85   $self->{'kernel'}->state('mpd_hide_display', $self, 'hide_display');
86   POE::Component::Client::MPD->new( {
87     alias => 'mpd',
88     status_msgs_to => 'display',
89   } );
90
91   $self->{'status'} = Clutter::Label->new('Sans 20', "Song\nAlbum");
92   $self->{'status'}->set_color(Clutter::Color->parse('White'));
93
94   $self->{'bg'} = Clutter::Rectangle->new(Clutter::Color->parse('Black'));
95   $self->{'bg'}->set_width($self->{'stage'}->get_width() - 40);
96   $self->{'bg'}->set_opacity(100);
97 }
98
99 sub connected {
100   my ($self, $kernel) = @_[OBJECT, KERNEL];
101
102   $kernel->post( 'mpd', 'current' );
103 }
104
105 1;