update pod to be more coherent in email addresses + see also
[audio-mpd.git] / lib / Audio / MPD / Playlist.pm
blobfea14b8e6273e24e69795142bf9caf9588ec7266
2 # This file is part of Audio::MPD
3 # Copyright (c) 2007-2008 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;
12 use strict;
13 use warnings;
14 use Scalar::Util qw[ weaken ];
16 use base qw[ Class::Accessor::Fast ];
17 __PACKAGE__->mk_accessors( qw[ _mpd ] );
20 #our ($VERSION) = '$Rev$' =~ /(\d+)/;
23 #--
24 # Constructor
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.
37 sub new {
38 my ($pkg, $mpd) = @_;
40 my $self = { _mpd => $mpd };
41 weaken( $self->{_mpd} );
42 bless $self, $pkg;
43 return $self;
47 #--
48 # Public methods
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.
58 sub as_items {
59 my ($self) = @_;
61 my @list = $self->_mpd->_cooked_command_as_items("playlistinfo\n");
62 return @list;
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.
87 sub add {
88 my ($self, @pathes) = @_;
89 my $command =
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
101 # return value.
103 sub delete {
104 my ($self, @songs) = @_;
105 my $command =
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.
119 sub deleteid {
120 my ($self, @songs) = @_;
121 my $command =
122 "command_list_begin\n"
123 . join( '', map { "deleteid $_\n" } @songs )
124 . "command_list_end\n";
125 $self->_mpd->_send_command( $command );
130 # $pl->clear;
132 # Remove all the songs from the current playlist. No return value.
134 sub clear {
135 my ($self) = @_;
136 $self->_mpd->_send_command("clear\n");
141 # $pl->crop;
143 # Remove all of the songs from the current playlist *except* the current one.
145 sub crop {
146 my ($self) = @_;
148 my $status = $self->_mpd->status;
149 my $cur = $status->song;
150 my $len = $status->playlistlength - 1;
152 my $command =
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
163 # $pl->shuffle();
165 # Shuffle the current playlist. No return value.
167 sub shuffle {
168 my ($self) = @_;
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.
177 # No return value.
179 sub swap {
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.
191 sub swapid {
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.
202 sub move {
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.
213 sub moveid {
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.
226 sub load {
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.
238 sub save {
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
248 # return value.
250 sub rm {
251 my ($self, $playlist) = @_;
252 $self->_mpd->_send_command( qq[rm "$playlist"\n] );
259 __END__
262 =head1 NAME
264 Audio::MPD::Playlist - an object to mess MPD's playlist
267 =head1 SYNOPSIS
269 my $song = $mpd->playlist->randomize;
272 =head1 DESCRIPTION
274 C<Audio::MPD::Playlist> is a class meant to access & update MPD's
275 playlist.
278 =head1 PUBLIC METHODS
280 =head2 Constructor
282 =over 4
284 =item new( $mpd )
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.
294 =back
297 =head2 Retrieving information
299 =over 4
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.
313 =back
316 =head2 Adding / removing songs
318 =over 4
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
329 return value.
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.
338 =item $pl->clear()
340 Remove all the songs from the current playlist. No return value.
343 =item $pl->crop()
345 Remove all of the songs from the current playlist *except* the
346 song currently playing.
349 =back
352 =head2 Changing playlist order
354 =over 4
356 =item $pl->shuffle()
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.
383 =back
386 =head2 Managing playlists
388 =over 4
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
404 return value.
407 =back
410 =head1 SEE ALSO
412 For all related information (bug reporting, mailing-list, pointers to
413 MPD, etc.), refer to C<Audio::MPD>'s pod, section C<SEE ALSO>
416 =head1 AUTHOR
418 Jerome Quelin, C<< <jquelin@cpan.org> >>
421 =head1 COPYRIGHT & LICENSE
423 Copyright (c) 2007-2008 Jerome Quelin, all rights reserved.
425 This program is free software; you can redistribute it and/or modify
426 it under the same terms as Perl itself.
428 =cut