| 1 | package XMMS::InfoPipe::Playlist; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.01'; | ||
| 6 | our $PIPE = '/tmp/xmms-playlist-info'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | XMMS::InfoPipe::Playlist - A small module to gather the information produced | ||
| 11 | by the playlist-infopipe plugin for XMMS | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use XMMS::InfoPipe::Playlist; | ||
| 16 | |||
| 17 | my $pl = XMMS::InfoPipe::Playlist->new(); | ||
| 18 | |||
| 19 | for ($pl->get_playlist()) { | ||
| 20 | print "Position: $_->{position}\n"; | ||
| 21 | print "Title: $_->{title}\n"; | ||
| 22 | print "File: $_->{file}\n\n"; | ||
| 23 | } | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This module was written to provide a way to snag the current XMMS playlist | ||
| 28 | from the pipe produced by the xmms-playlist-infopipe plugin for XMMS. | ||
| 29 | |||
| 30 | B<Nota Bene:> If the XMMS plugin isn't enabled, then this module will NOT | ||
| 31 | return results as expected (if it even works). | ||
| 32 | |||
| 33 | =head1 METHODS | ||
| 34 | |||
| 35 | =head2 new | ||
| 36 | |||
| 37 | my $pl = XMMS::InfoPipe::Playlist->new(); | ||
| 38 | |||
| 39 | Creates a new XMMS::InfoPipe::Playlist instance. By default this parses the | ||
| 40 | file before returning the object. This will undoubtedly cause some initial | ||
| 41 | slowdown (the bottleneck of XMMS::InfoPipe::Playlist is when it must grab | ||
| 42 | information from the named pipe the XMMS plugin provides), and so you may | ||
| 43 | disable this first parsing by specifying a false value to ForceParse. For | ||
| 44 | example: | ||
| 45 | |||
| 46 | my $pl = XMMS::InfoPipe::Playlist->new(ForceParse => 0); | ||
| 47 | |||
| 48 | will create the object and immediately return it, without first populating | ||
| 49 | it with the information from XMMS. This means that before trying to obtain | ||
| 50 | this information, you should first call C<$pl-E<gt>update_playlist()>. | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | |||
| 55 | sub new { | ||
| 56 | my $class = shift; | ||
| 57 | my %args = @_; | ||
| 58 | |||
| 59 | $args{ForceParse} = 1 unless exists $args{ForceParse}; | ||
| 60 | |||
| 61 | my $self = bless { | ||
| 62 | error => '', | ||
| 63 | playlist => [], | ||
| 64 | _force_parse=> $args{ForceParse} | ||
| 65 | }, $class; | ||
| 66 | |||
| 67 | $self->update_playlist if $self->{_force_parse}; | ||
| 68 | return $self; | ||
| 69 | } | ||
| 70 | |||
| 71 | =head2 get_playlist | ||
| 72 | |||
| 73 | @list = $pl->get_playlist(); | ||
| 74 | |||
| 75 | Returns an array of hashrefs that represents the current playlist. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub get_playlist { | ||
| 80 | my $self = shift; | ||
| 81 | return @{$self->{playlist}}; | ||
| 82 | } | ||
| 83 | |||
| 84 | =head2 xmms_is_running | ||
| 85 | |||
| 86 | $pl->xmms_is_running() | ||
| 87 | |||
| 88 | Returns 1 if XMMS is running and 0 if not. This relies on the fact that | ||
| 89 | the named pipe does not exist if XMMS is not running. If the infopipe | ||
| 90 | plugin isn't enabled, this will also return 0. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub is_running { | ||
| 95 | my $self = shift; | ||
| 96 | return $self->{playlist} ? 1 : 0; | ||
| 97 | } | ||
| 98 | |||
| 99 | =head2 update_playlist | ||
| 100 | |||
| 101 | $pl->update_playlist() | ||
| 102 | |||
| 103 | Updates C<$pl-E<gt>{playlist}>. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | sub update_playlist { | ||
| 108 | my $self = shift; | ||
| 109 | open DATA, $PIPE or return []; | ||
| 110 | |||
| 111 | while (<DATA>) { | ||
| 112 | next if /^#/; | ||
| 113 | chomp; | ||
| 114 | |||
| 115 | my ($pos, $title, $file) = split /\t/, $_, 3; | ||
| 116 | push @{$self->{playlist}}, { | ||
| 117 | position => $pos, | ||
| 118 | title => $title, | ||
| 119 | file => $file, | ||
| 120 | }; | ||
| 121 | } | ||
| 122 | close DATA; | ||
| 123 | } | ||
| 124 | |||
| 125 | =head1 VARIABLES | ||
| 126 | |||
| 127 | =head2 $XMMS::InfoPipe::Playlist::PIPE | ||
| 128 | |||
| 129 | $XMMS::InfoPipe::Playlist::PIPE = '/tmp/other-name'; | ||
| 130 | |||
| 131 | This variable defaults to C</tmp/xmms-playlist-info> which should be a | ||
| 132 | symlink (created by xmms-playlist-infopipe) to the real named pipe | ||
| 133 | (something like C</tmp/xmms-playlist-info_user.0>). If for whatever reason | ||
| 134 | you need to change it (maybe you have a file generated by something else | ||
| 135 | that follows the same format as xmms-playlist-infopipe), just set it before | ||
| 136 | C<update_playlist> is called (by default that means before C<new> is called) | ||
| 137 | for the right file to be used. | ||
| 138 | |||
| 139 | =head1 LICENSE | ||
| 140 | |||
| 141 | This module is free software, and may be distributed under the same | ||
| 142 | terms as Perl itself. | ||
| 143 | |||
| 144 | =head1 AUTHOR | ||
| 145 | |||
| 146 | Copyright (C) 2003, Thomas R. Sibley C<tsibley@cpan.org> | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | 1; |