1# 2# This file is part of Audio-MPD 3# 4# This software is copyright (c) 2007 by Jerome Quelin. 5# 6# This is free software; you can redistribute it and/or modify it under 7# the same terms as the Perl 5 programming language system itself. 8# 9use 5.008; 10use warnings; 11use strict; 12 13package Audio::MPD::Playlist; 14# ABSTRACT: class to mess MPD's playlist 15$Audio::MPD::Playlist::VERSION = '2.004'; 16use Moose; 17use MooseX::Has::Sugar; 18use MooseX::SemiAffordanceAccessor; 19 20has _mpd => ( ro, required, weak_ref ); 21 22 23#-- 24# Constructor 25 26# 27# my $collection = Audio::MPD::Playlist->new( _mpd => $mpd ); 28# 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. 32# 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. 36# 37 38 39#-- 40# Public methods 41 42# -- Playlist: retrieving information 43 44 45sub as_items { 46 my ($self) = @_; 47 48 my @list = $self->_mpd->_cooked_command_as_items("playlistinfo\n"); 49 return @list; 50} 51 52 53 54sub items_changed_since { 55 my ($self, $plid) = @_; 56 return $self->_mpd->_cooked_command_as_items("plchanges $plid\n"); 57} 58 59 60# -- Playlist: adding / removing songs 61 62 63sub add { 64 my ($self, @pathes) = @_; 65 my $command = 66 "command_list_begin\n" 67 . join( '', map { my $p=$_; $p=~s/"/\\"/g; qq[add "$p"\n] } @pathes ) 68 . "command_list_end\n"; 69 $self->_mpd->_send_command( $command ); 70} 71 72 73 74sub delete { 75 my ($self, @songs) = @_; 76 my $command = 77 "command_list_begin\n" 78 . join( '', map { my $p=$_; $p=~s/"/\\"/g; "delete $p\n" } @songs ) 79 . "command_list_end\n"; 80 $self->_mpd->_send_command( $command ); 81} 82 83 84 85sub deleteid { 86 my ($self, @songs) = @_; 87 my $command = 88 "command_list_begin\n" 89 . join( '', map { "deleteid $_\n" } @songs ) 90 . "command_list_end\n"; 91 $self->_mpd->_send_command( $command ); 92} 93 94 95 96sub clear { 97 my ($self) = @_; 98 $self->_mpd->_send_command("clear\n"); 99} 100 101 102 103sub crop { 104 my ($self) = @_; 105 106 my $status = $self->_mpd->status; 107 my $cur = $status->song; 108 my $len = $status->playlistlength - 1; 109 110 # we need to reverse the list, to remove the bigest ids before 111 my $command = 112 "command_list_begin\n" 113 . join( '', map { $_ != $cur ? "delete $_\n" : '' } reverse 0..$len ) 114 . "command_list_end\n"; 115 $self->_mpd->_send_command( $command ); 116} 117 118 119# -- Playlist: changing playlist order 120 121 122 123sub shuffle { 124 my ($self) = @_; 125 $self->_mpd->_send_command("shuffle\n"); 126} 127 128 129 130sub swap { 131 my ($self, $from, $to) = @_; 132 $self->_mpd->_send_command("swap $from $to\n"); 133} 134 135 136 137sub swapid { 138 my ($self, $from, $to) = @_; 139 $self->_mpd->_send_command("swapid $from $to\n"); 140} 141 142 143 144sub move { 145 my ($self, $song, $pos) = @_; 146 $self->_mpd->_send_command("move $song $pos\n"); 147} 148 149 150 151sub moveid { 152 my ($self, $song, $pos) = @_; 153 $self->_mpd->_send_command("moveid $song $pos\n"); 154} 155 156 157# -- Playlist: managing playlists 158 159 160sub load { 161 my ($self, $playlist) = @_; 162 $self->_mpd->_send_command( qq[load "$playlist"\n] ); 163} 164 165 166 167sub save { 168 my ($self, $playlist) = @_; 169 $self->_mpd->_send_command( qq[save "$playlist"\n] ); 170} 171 172 173 174sub rm { 175 my ($self, $playlist) = @_; 176 $self->_mpd->_send_command( qq[rm "$playlist"\n] ); 177} 178 179 180no Moose; 181__PACKAGE__->meta->make_immutable; 1821; 183 184__END__ 185 186=pod 187 188=head1 NAME 189 190Audio::MPD::Playlist - class to mess MPD's playlist 191 192=head1 VERSION 193 194version 2.004 195 196=head1 SYNOPSIS 197 198 $mpd->playlist->shuffle; 199 # and lots of other methods 200 201=head1 DESCRIPTION 202 203L<Audio::MPD::Playlist> is a class meant to access & update MPD's 204playlist. 205 206Note that you're not supposed to call the constructor yourself, an 207L<Audio::MPD::Playlist> is automatically created for you during the 208creation of an L<Audio::MPD> object - it can then be used with the 209C<playlist()> accessor. 210 211=head1 RETRIEVING INFORMATION 212 213=head2 as_items 214 215 my @items = $pl->as_items; 216 217Return an array of L<Audio::MPD::Common::Item::Song>s, one for each of the 218songs in the current playlist. 219 220=head2 items_changed_since 221 222 my @items = $pl->items_changed_since( $plversion ); 223 224Return a list with all the songs (as L<Audio::MPD::Common::Item::Song> objects) 225added to the playlist since playlist C<$plversion>. 226 227=head1 ADDING / REMOVING SONGS 228 229=head2 add 230 231 $pl->add( $path [, $path [...] ] ); 232 233Add the songs identified by C<$path> (relative to MPD's music directory) to the 234current playlist. No return value. 235 236=head2 delete 237 238 $pl->delete( $song [, $song [...] ] ); 239 240Remove the specified C<$song> numbers (starting from 0) from the current 241playlist. No return value. 242 243=head2 deleteid 244 245 $pl->deleteid( $songid [, $songid [...] ] ); 246 247Remove the specified C<$songid>s (as assigned by mpd when inserted in playlist) 248from the current playlist. No return value. 249 250=head2 clear 251 252 $pl->clear; 253 254Remove all the songs from the current playlist. No return value. 255 256=head2 crop 257 258 $pl->crop; 259 260Remove all of the songs from the current playlist B<except> the 261song currently playing. 262 263=head1 CHANGING PLAYLIST ORDER 264 265=head2 shuffle 266 267 $pl->shuffle; 268 269Shuffle the current playlist. No return value. 270 271=head2 swap 272 273 $pl->swap( $song1, $song2 ); 274 275Swap positions of song number C<$song1> and C<$song2> in the current 276playlist. No return value. 277 278=head2 swapid 279 280 $pl->swapid( $songid1, $songid2 ); 281 282Swap the postions of song ID C<$songid1> with song ID C<$songid2> in the 283current playlist. No return value. 284 285=head2 move 286 287 $pl->move( $song, $newpos ); 288 289Move song number C<$song> to the position C<$newpos>. No return value. 290 291=head2 moveid 292 293 $pl->moveid( $songid, $newpos ); 294 295Move song ID C<$songid> to the position C<$newpos>. No return value. 296 297=head1 MANAGING PLAYLISTS 298 299=head2 load 300 301 $pl->load( $playlist ); 302 303Load list of songs from specified C<$playlist> file. No return value. 304 305=head2 save 306 307 $pl->save( $playlist ); 308 309Save the current playlist to a file called C<$playlist> in MPD's playlist 310directory. No return value. 311 312=head2 rm 313 314 $pl->rm( $playlist ); 315 316Delete playlist named C<$playlist> from MPD's playlist directory. No 317return value. 318 319=head1 AUTHOR 320 321Jerome Quelin 322 323=head1 COPYRIGHT AND LICENSE 324 325This software is copyright (c) 2007 by Jerome Quelin. 326 327This is free software; you can redistribute it and/or modify it under 328the same terms as the Perl 5 programming language system itself. 329 330=cut 331