]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/mpfire/perl/Audio/MPD/Playlist.pm
2 # This file is part of Audio::MPD
3 # Copyright (c) 2007 Jerome Quelin, all rights reserved.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the same terms as Perl itself.
10 package Audio
::MPD
::Playlist
;
14 use Scalar
::Util qw
[ weaken
];
16 use base qw
[ Class
::Accessor
::Fast
];
17 __PACKAGE__
->mk_accessors( qw
[ _mpd
] );
20 #our ($VERSION) = '$Rev$' =~ /(\d+)/;
27 # my $collection = Audio::MPD::Playlist->new( $mpd );
29 # This will create the object, holding a back-reference to the Audio::MPD
30 # object itself (for communication purposes). But in order to play safe and
31 # to free the memory in time, this reference is weakened.
33 # Note that you're not supposed to call this constructor yourself, an
34 # Audio::MPD::Playlist is automatically created for you during the creation
35 # of an Audio::MPD object.
40 my $self = { _mpd
=> $mpd };
41 weaken
( $self->{_mpd
} );
50 # -- Playlist: retrieving information
53 # my @items = $pl->as_items;
55 # Return an array of AMC::Item::Songs, one for each of the
56 # songs in the current playlist.
61 my @list = $self->_mpd->_cooked_command_as_items("playlistinfo\n");
67 # my @items = $pl->items_changed_since( $plversion );
69 # Return a list with all the songs (as API::Song objects) added to
70 # the playlist since playlist $plversion.
72 sub items_changed_since
{
73 my ($self, $plid) = @_;
74 return $self->_mpd->_cooked_command_as_items("plchanges $plid\n");
79 # -- Playlist: adding / removing songs
82 # $pl->add( $path [, $path [...] ] );
84 # Add the songs identified by $path (relative to MPD's music directory) to
85 # the current playlist. No return value.
88 my ($self, @pathes) = @_;
90 "command_list_begin\n"
91 . join( '', map { s/"/\\"/g; qq[add
"$_"\n] } @pathes )
92 . "command_list_end\n";
93 $self->_mpd->_send_command( $command );
98 # $pl->delete( $song [, $song [...] ] );
100 # Remove song number $song (starting from 0) from the current playlist. No
104 my ($self, @songs) = @_;
106 "command_list_begin\n"
107 . join( '', map { s/"/\\"/g; "delete $_\n" } @songs )
108 . "command_list_end\n";
109 $self->_mpd->_send_command( $command );
114 # $pl->deleteid( $songid [, $songid [...] ]);
116 # Remove the specified $songid (as assigned by mpd when inserted in playlist)
117 # from the current playlist. No return value.
120 my ($self, @songs) = @_;
122 "command_list_begin\n"
123 . join( '', map { "deleteid $_\n" } @songs )
124 . "command_list_end\n";
125 $self->_mpd->_send_command( $command );
132 # Remove all the songs from the current playlist. No return value.
136 $self->_mpd->_send_command("clear\n");
143 # Remove all of the songs from the current playlist *except* the current one.
148 my $status = $self->_mpd->status;
149 my $cur = $status->song;
150 my $len = $status->playlistlength - 1;
153 "command_list_begin\n"
154 . join( '', map { $_ != $cur ?
"delete $_\n" : '' } reverse 0..$len )
155 . "command_list_end\n";
156 $self->_mpd->_send_command( $command );
160 # -- Playlist: changing playlist order
165 # Shuffle the current playlist. No return value.
169 $self->_mpd->_send_command("shuffle\n");
174 # $pl->swap( $song1, $song2 );
176 # Swap positions of song number $song1 and $song2 in the current playlist.
180 my ($self, $from, $to) = @_;
181 $self->_mpd->_send_command("swap $from $to\n");
186 # $pl->swapid( $songid1, $songid2 );
188 # Swap the postions of song ID $songid1 with song ID $songid2 in the
189 # current playlist. No return value.
192 my ($self, $from, $to) = @_;
193 $self->_mpd->_send_command("swapid $from $to\n");
198 # $pl->move( $song, $newpos );
200 # Move song number $song to the position $newpos. No return value.
203 my ($self, $song, $pos) = @_;
204 $self->_mpd->_send_command("move $song $pos\n");
209 # $pl->moveid( $songid, $newpos );
211 # Move song ID $songid to the position $newpos. No return value.
214 my ($self, $song, $pos) = @_;
215 $self->_mpd->_send_command("moveid $song $pos\n");
219 # -- Playlist: managing playlists
222 # $pl->load( $playlist );
224 # Load list of songs from specified $playlist file. No return value.
227 my ($self, $playlist) = @_;
228 $self->_mpd->_send_command( qq[load
"$playlist"\n] );
233 # $pl->save( $playlist );
235 # Save the current playlist to a file called $playlist in MPD's playlist
236 # directory. No return value.
239 my ($self, $playlist) = @_;
240 $self->_mpd->_send_command( qq[save
"$playlist"\n] );
245 # $pl->rm( $playlist )
247 # Delete playlist named $playlist from MPD's playlist directory. No
251 my ($self, $playlist) = @_;
252 $self->_mpd->_send_command( qq[rm
"$playlist"\n] );
264 Audio::MPD::Playlist - an object to mess MPD's playlist
269 my $song = $mpd->playlist->randomize;
274 C<Audio::MPD::Playlist> is a class meant to access & update MPD's
278 =head1 PUBLIC METHODS
286 This will create the object, holding a back-reference to the C<Audio::MPD>
287 object itself (for communication purposes). But in order to play safe and
288 to free the memory in time, this reference is weakened.
290 Note that you're not supposed to call this constructor yourself, an
291 C<Audio::MPD::Playlist> is automatically created for you during the creation
292 of an C<Audio::MPD> object.
297 =head2 Retrieving information
301 =item $pl->as_items()
303 Return an array of C<Audio::MPD::Common::Item::Song>s, one for each of the
304 songs in the current playlist.
307 =item $pl->items_changed_since( $plversion )
309 Return a list with all the songs (as AMC::Item::Song objects) added to
310 the playlist since playlist $plversion.
316 =head2 Adding / removing songs
320 =item $pl->add( $path [, $path [...] ] )
322 Add the songs identified by C<$path> (relative to MPD's music directory) to the
323 current playlist. No return value.
326 =item $pl->delete( $song [, $song [...] ] )
328 Remove song number C<$song>s (starting from 0) from the current playlist. No
332 =item $pl->deleteid( $songid [, $songid [...] ] )
334 Remove the specified C<$songid>s (as assigned by mpd when inserted in playlist)
335 from the current playlist. No return value.
340 Remove all the songs from the current playlist. No return value.
345 Remove all of the songs from the current playlist *except* the
346 song currently playing.
352 =head2 Changing playlist order
358 Shuffle the current playlist. No return value.
361 =item $pl->swap( $song1, $song2 )
363 Swap positions of song number C<$song1> and C<$song2> in the current
364 playlist. No return value.
367 =item $pl->swapid( $songid1, $songid2 )
369 Swap the postions of song ID C<$songid1> with song ID C<$songid2> in the
370 current playlist. No return value.
373 =item $pl->move( $song, $newpos )
375 Move song number C<$song> to the position C<$newpos>. No return value.
378 =item $pl->moveid( $songid, $newpos )
380 Move song ID C<$songid> to the position C<$newpos>. No return value.
386 =head2 Managing playlists
390 =item $pl->load( $playlist )
392 Load list of songs from specified C<$playlist> file. No return value.
395 =item $pl->save( $playlist )
397 Save the current playlist to a file called C<$playlist> in MPD's playlist
398 directory. No return value.
401 =item $pl->rm( $playlist )
403 Delete playlist named C<$playlist> from MPD's playlist directory. No
417 Jerome Quelin, C<< <jquelin at cpan.org> >>
420 =head1 COPYRIGHT & LICENSE
422 Copyright (c) 2007 Jerome Quelin, all rights reserved.
424 This program is free software; you can redistribute it and/or modify
425 it under the same terms as Perl itself.