[project @ 5841]
[audio-mpd.git] / lib / Audio / MPD / Playlist.pm
blob71ad05764e91fb0c0261d111accf232668b09ba6
2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation; either version 2 of the License, or
5 # (at your option) any later version.
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 package Audio::MPD::Playlist;
20 use strict;
21 use warnings;
22 use Scalar::Util qw[ weaken ];
24 use base qw[ Class::Accessor::Fast ];
25 __PACKAGE__->mk_accessors( qw[ _mpd ] );
28 #our ($VERSION) = '$Rev$' =~ /(\d+)/;
31 #--
32 # Constructor
35 # my $collection = Audio::MPD::Playlist->new( $mpd );
37 # This will create the object, holding a back-reference to the Audio::MPD
38 # object itself (for communication purposes). But in order to play safe and
39 # to free the memory in time, this reference is weakened.
41 # Note that you're not supposed to call this constructor yourself, an
42 # Audio::MPD::Playlist is automatically created for you during the creation
43 # of an Audio::MPD object.
45 sub new {
46 my ($pkg, $mpd) = @_;
48 my $self = { _mpd => $mpd };
49 weaken( $self->{_mpd} );
50 bless $self, $pkg;
51 return $self;
55 #--
56 # Public methods
58 # -- Playlist: retrieving information
61 # my @items = $pl->as_items;
63 # Return an array of C<Audio::MPD::Item::Song>s, one for each of the
64 # songs in the current playlist.
66 sub as_items {
67 my ($self) = @_;
69 my @list = $self->_mpd->_cooked_command_as_items("playlistinfo\n");
70 return @list;
75 # my @items = $pl->items_changed_since( $plversion );
77 # Return a list with all the songs (as API::Song objects) added to
78 # the playlist since playlist $plversion.
80 sub items_changed_since {
81 my ($self, $plid) = @_;
82 return $self->_mpd->_cooked_command_as_items("plchanges $plid\n");
87 # -- Playlist: adding / removing songs
90 # $pl->add( $path );
92 # Add the song identified by $path (relative to MPD's music directory) to
93 # the current playlist. No return value.
95 sub add {
96 my ($self, $path) = @_;
97 $self->_mpd->_send_command( qq[add "$path"\n] );
102 # $pl->delete( $song [, $song [...] ] );
104 # Remove song number $song (starting from 0) from the current playlist. No
105 # return value.
107 sub delete {
108 my ($self, @songs) = @_;
109 my $command =
110 "command_list_begin\n"
111 . join( '', map { "delete $_\n" } @songs )
112 . "command_list_end\n";
113 $self->_mpd->_send_command( $command );
118 # $pl->deleteid( $songid [, $songid [...] ]);
120 # Remove the specified $songid (as assigned by mpd when inserted in playlist)
121 # from the current playlist. No return value.
123 sub deleteid {
124 my ($self, @songs) = @_;
125 my $command =
126 "command_list_begin\n"
127 . join( '', map { "deleteid $_\n" } @songs )
128 . "command_list_end\n";
129 $self->_mpd->_send_command( $command );
134 # $pl->clear;
136 # Remove all the songs from the current playlist. No return value.
138 sub clear {
139 my ($self) = @_;
140 $self->_mpd->_send_command("clear\n");
145 # $pl->crop;
147 # Remove all of the songs from the current playlist *except* the current one.
149 sub crop {
150 my ($self) = @_;
152 my $status = $self->_mpd->status;
153 my $cur = $status->song;
154 my $len = $status->playlistlength - 1;
156 my $command =
157 "command_list_begin\n"
158 . join( '', map { $_ != $cur ? "delete $_\n" : '' } reverse 0..$len )
159 . "command_list_end\n";
160 $self->_mpd->_send_command( $command );
164 # -- Playlist: changing playlist order
167 # $pl->shuffle();
169 # Shuffle the current playlist. No return value.
171 sub shuffle {
172 my ($self) = @_;
173 $self->_mpd->_send_command("shuffle\n");
178 # $pl->swap( $song1, $song2 );
180 # Swap positions of song number $song1 and $song2 in the current playlist.
181 # No return value.
183 sub swap {
184 my ($self, $from, $to) = @_;
185 $self->_mpd->_send_command("swap $from $to\n");
190 # $pl->swapid( $songid1, $songid2 );
192 # Swap the postions of song ID $songid1 with song ID $songid2 in the
193 # current playlist. No return value.
195 sub swapid {
196 my ($self, $from, $to) = @_;
197 $self->_mpd->_send_command("swapid $from $to\n");
202 # $pl->move( $song, $newpos );
204 # Move song number $song to the position $newpos. No return value.
206 sub move {
207 my ($self, $song, $pos) = @_;
208 $self->_mpd->_send_command("move $song $pos\n");
213 # $pl->moveid( $songid, $newpos );
215 # Move song ID $songid to the position $newpos. No return value.
217 sub moveid {
218 my ($self, $song, $pos) = @_;
219 $self->_mpd->_send_command("moveid $song $pos\n");
222 sub load {
223 my ($self, $playlist) = @_;
224 return unless defined $playlist;
225 $self->_mpd->_send_command( qq[load "$playlist"\n] );
228 sub save {
229 my ($self, $playlist) = @_;
230 return unless defined $playlist;
231 $self->_mpd->_send_command( qq[save "$playlist"\n] );
233 =begin FIXME
235 if(!$self->_process_feedback)
237 # Does the playlist already exist?
238 if(${$self->get_error}[0] eq '56' && $config{'OVERWRITE_PLAYLIST'})
240 $self->rm($playlist);
241 $self->save($playlist);
242 return 1;
245 return 1;
247 =end FIXME
249 =cut
253 sub rm {
254 my ($self, $playlist) = @_;
255 return unless defined $playlist;
256 $self->_mpd->_send_command( qq[rm "$playlist"\n] );
263 __END__
266 =head1 NAME
268 Audio::MPD::Playlist - an object to mess MPD's playlist
271 =head1 SYNOPSIS
273 my $song = $mpd->playlist->randomize;
276 =head1 DESCRIPTION
278 C<Audio::MPD::Playlist> is a class meant to access & update MPD's
279 playlist.
282 =head1 PUBLIC METHODS
284 =head2 Constructor
286 =over 4
288 =item new( $mpd )
290 This will create the object, holding a back-reference to the C<Audio::MPD>
291 object itself (for communication purposes). But in order to play safe and
292 to free the memory in time, this reference is weakened.
294 Note that you're not supposed to call this constructor yourself, an
295 C<Audio::MPD::Playlist> is automatically created for you during the creation
296 of an C<Audio::MPD> object.
298 =back
301 =head2 Retrieving information
303 =over 4
305 =item $pl->as_items( )
307 Return an array of C<Audio::MPD::Item::Song>s, one for each of the
308 songs in the current playlist.
311 =item $pl->items_changed_since( $plversion )
313 Return a list with all the songs (as API::Song objects) added to
314 the playlist since playlist $plversion.
317 =back
320 =head2 Adding / removing songs
322 =over 4
324 =item $pl->add( $path )
326 Add the song identified by C<$path> (relative to MPD's music directory) to the
327 current playlist. No return value.
330 =item $pl->delete( $song [, $song [...] ] )
332 Remove song number C<$song>s (starting from 0) from the current playlist. No
333 return value.
336 =item $pl->deleteid( $songid [, $songid [...] ] )
338 Remove the specified C<$songid>s (as assigned by mpd when inserted in playlist)
339 from the current playlist. No return value.
342 =item $pl->clear( )
344 Remove all the songs from the current playlist. No return value.
347 =item $pl->crop( )
349 Remove all of the songs from the current playlist *except* the
350 song currently playing.
353 =back
356 =head2 Changing playlist order
358 =over 4
360 =item $pl->shuffle( )
362 Shuffle the current playlist. No return value.
365 =item $pl->swap( $song1, $song2 )
367 Swap positions of song number C<$song1> and C<$song2> in the current
368 playlist. No return value.
371 =item $pl->swapid( $songid1, $songid2 )
373 Swap the postions of song ID C<$songid1> with song ID C<$songid2> in the
374 current playlist. No return value.
377 =item $pl->move( $song, $newpos )
379 Move song number C<$song> to the position C<$newpos>. No return value.
382 =item $pl->moveid( $songid, $newpos )
384 Move song ID C<$songid> to the position C<$newpos>. No return value.
387 =item $pl->load( $playlist )
389 Load list of songs from specified $playlist file. No return value.
392 =item $pl->save( $playlist )
394 Save the current playlist to a file called $playlist in MPD's playlist
395 directory. No return value.
398 =item $pl->rm( $playlist )
400 Delete playlist named $playlist from MPD's playlist directory. No return value.
402 =back
405 =head1 SEE ALSO
407 You can find more information on the mpd project on its homepage at
408 L<http://www.musicpd.org>, or its wiki L<http://mpd.wikia.com>.
410 Regarding this Perl module, you can report bugs on CPAN via
411 L<http://rt.cpan.org/Public/Bug/Report.html?Queue=Audio-MPD>.
413 Audio::MPD development takes place on <audio-mpd@googlegroups.com>: feel free
414 to join us. (use L<http://groups.google.com/group/audio-mpd> to sign in). Our
415 subversion repository is located at L<https://svn.musicpd.org>.
418 =head1 AUTHORS
420 Jerome Quelin <jquelin@cpan.org>
423 =head1 COPYRIGHT AND LICENSE
425 Copyright (c) 2007 Jerome Quelin <jquelin@cpan.org>
428 This program is free software; you can redistribute it and/or modify
429 it under the terms of the GNU General Public License as published by
430 the Free Software Foundation; either version 2 of the License, or
431 (at your option) any later version.
433 This program is distributed in the hope that it will be useful,
434 but WITHOUT ANY WARRANTY; without even the implied warranty of
435 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
436 GNU General Public License for more details.
438 =cut