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
;
22 use Scalar
::Util qw
[ weaken
];
24 use base qw
[ Class
::Accessor
::Fast
];
25 __PACKAGE__
->mk_accessors( qw
[ _mpd
] );
28 #our ($VERSION) = '$Rev$' =~ /(\d+)/;
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.
48 my $self = { _mpd
=> $mpd };
49 weaken
( $self->{_mpd
} );
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.
69 my @list = $self->_mpd->_cooked_command_as_items("playlistinfo\n");
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
92 # Add the song identified by $path (relative to MPD's music directory) to
93 # the current playlist. No return value.
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
108 my ($self, @songs) = @_;
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.
124 my ($self, @songs) = @_;
126 "command_list_begin\n"
127 . join( '', map { "deleteid $_\n" } @songs )
128 . "command_list_end\n";
129 $self->_mpd->_send_command( $command );
136 # Remove all the songs from the current playlist. No return value.
140 $self->_mpd->_send_command("clear\n");
147 # Remove all of the songs from the current playlist *except* the current one.
152 my $status = $self->_mpd->status;
153 my $cur = $status->song;
154 my $len = $status->playlistlength - 1;
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
169 # Shuffle the current playlist. No return value.
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.
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.
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.
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.
218 my ($self, $song, $pos) = @_;
219 $self->_mpd->_send_command("moveid $song $pos\n");
223 my ($self, $playlist) = @_;
224 return unless defined $playlist;
225 $self->_mpd->_send_command( qq[load
"$playlist"\n] );
229 my ($self, $playlist) = @_;
230 return unless defined $playlist;
231 $self->_mpd->_send_command( qq[save
"$playlist"\n] );
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);
254 my ($self, $playlist) = @_;
255 return unless defined $playlist;
256 $self->_mpd->_send_command( qq[rm
"$playlist"\n] );
268 Audio::MPD::Playlist - an object to mess MPD's playlist
273 my $song = $mpd->playlist->randomize;
278 C<Audio::MPD::Playlist> is a class meant to access & update MPD's
282 =head1 PUBLIC METHODS
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.
301 =head2 Retrieving information
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.
320 =head2 Adding / removing songs
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
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.
344 Remove all the songs from the current playlist. No return value.
349 Remove all of the songs from the current playlist *except* the
350 song currently playing.
356 =head2 Changing playlist order
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.
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>.
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.