2 # This file is part of POE::Component::Client::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 POE
::Component
::Client
::MPD
::Playlist
;
16 use POE
::Component
::Client
::MPD
::Message
;
17 use base qw
[ Class
::Accessor
::Fast
];
19 # -- Playlist: retrieving information
22 # event: pl.as_items()
24 # Return an array of C<POCOCM::Item::Song>s, one for each of the
25 # songs in the current playlist.
28 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
29 _from
=> $_[SENDER
]->ID,
30 _request
=> $_[STATE
],
32 _commands
=> [ 'playlistinfo' ],
33 _cooking
=> $AS_ITEMS,
35 $_[KERNEL
]->yield( '_send', $msg );
40 # event: pl.items_changed_since( $plversion )
42 # Return a list with all the songs (as POCOM::Item::Song objects) added to
43 # the playlist since playlist $plversion.
45 sub _onpub_items_changed_since
{
47 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
48 _from
=> $_[SENDER
]->ID,
49 _request
=> $_[STATE
],
51 _commands
=> [ "plchanges $plid" ],
52 _cooking
=> $AS_ITEMS,
54 $_[KERNEL
]->yield( '_send', $msg );
58 # -- Playlist: adding / removing songs
61 # event: pl.add( $path, $path, ... )
63 # Add the songs identified by $path (relative to MPD's music directory) to
64 # the current playlist.
68 my @pathes = @_[ARG0
.. $#_]; # args of the poe event
69 my @commands = ( # build the commands
71 map( qq[add
"$_"], @pathes ),
74 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
75 _from
=> $_[SENDER
]->ID,
76 _request
=> $_[STATE
],
78 _commands
=> \
@commands,
81 $_[KERNEL
]->yield( '_send', $msg );
86 # event: pl.delete( $number, $number, ... )
88 # Remove song $number (starting from 0) from the current playlist.
92 my @numbers = @_[ARG0
.. $#_]; # args of the poe event
93 my @commands = ( # build the commands
95 map( qq[delete $_], reverse sort {$a<=>$b} @numbers ),
98 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
99 _from
=> $_[SENDER
]->ID,
100 _request
=> $_[STATE
],
102 _commands
=> \
@commands,
105 $_[KERNEL
]->yield( '_send', $msg );
110 # event: pl.deleteid( $songid, $songid, ... )
112 # Remove the specified $songid (as assigned by mpd when inserted in playlist)
113 # from the current playlist.
115 sub _onpub_deleteid
{
116 my @songids = @_[ARG0
.. $#_]; # args of the poe event
117 my @commands = ( # build the commands
118 'command_list_begin',
119 map( qq[deleteid
$_], @songids ),
122 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
123 _from
=> $_[SENDER
]->ID,
124 _request
=> $_[STATE
],
126 _commands
=> \
@commands,
129 $_[KERNEL
]->yield( '_send', $msg );
136 # Remove all the songs from the current playlist.
139 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
140 _from
=> $_[SENDER
]->ID,
141 _request
=> $_[STATE
],
143 _commands
=> [ 'clear' ],
146 $_[KERNEL
]->yield( '_send', $msg );
153 # Remove all of the songs from the current playlist *except* the current one.
156 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
157 _from
=> $_[SENDER
]->ID,
158 _request
=> $_[STATE
],
161 _pre_from
=> '_crop_status',
162 _pre_event
=> 'status',
164 $_[KERNEL
]->yield( '_send', $msg );
169 # event: _crop_status( $msg, $status)
171 # Use $status to get current song, before sending real crop $msg.
173 sub _onpriv_crop_status
{
174 my ($msg, $status) = @_[ARG0
, ARG1
];
175 my $cur = $status->data->song;
176 my $len = $status->data->playlistlength - 1;
179 'command_list_begin',
180 map( { $_ != $cur ?
"delete $_" : '' } reverse 0..$len ),
183 $msg->_commands( \
@commands );
184 $_[KERNEL
]->yield( '_send', $msg );
188 # -- Playlist: changing playlist order
191 # event: pl.shuffle()
193 # Shuffle the current playlist.
196 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
197 _from
=> $_[SENDER
]->ID,
198 _request
=> $_[STATE
],
200 _commands
=> [ 'shuffle' ],
203 $_[KERNEL
]->yield( '_send', $msg );
208 # event: pl.swap( $song1, song2 )
210 # Swap positions of song number $song1 and $song2 in the current playlist.
213 my ($from, $to) = @_[ARG0
, ARG1
];
214 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
215 _from
=> $_[SENDER
]->ID,
216 _request
=> $_[STATE
],
218 _commands
=> [ "swap $from $to" ],
221 $_[KERNEL
]->yield( '_send', $msg );
226 # event: pl.swapid( $songid1, songid2 )
228 # Swap positions of song id $songid1 and $songid2 in the current playlist.
231 my ($from, $to) = @_[ARG0
, ARG1
];
232 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
233 _from
=> $_[SENDER
]->ID,
234 _request
=> $_[STATE
],
236 _commands
=> [ "swapid $from $to" ],
239 $_[KERNEL
]->yield( '_send', $msg );
244 # event: pl.move( $song, $newpos );
246 # Move song number $song to the position $newpos.
249 my ($song, $pos) = @_[ARG0
, ARG1
];
250 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
251 _from
=> $_[SENDER
]->ID,
252 _request
=> $_[STATE
],
254 _commands
=> [ "move $song $pos" ],
257 $_[KERNEL
]->yield( '_send', $msg );
262 # event: pl.moveid( $songid, $newpos );
264 # Move song id $songid to the position $newpos.
267 my ($songid, $pos) = @_[ARG0
, ARG1
];
268 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
269 _from
=> $_[SENDER
]->ID,
270 _request
=> $_[STATE
],
272 _commands
=> [ "moveid $songid $pos" ],
275 $_[KERNEL
]->yield( '_send', $msg );
279 # -- Playlist: managing playlists
282 # event: pl.load( $playlist );
284 # Load list of songs from specified $playlist file.
287 my ($playlist) = $_[ARG0
];
288 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
289 _from
=> $_[SENDER
]->ID,
290 _request
=> $_[STATE
],
292 _commands
=> [ qq[load
"$playlist"] ],
295 $_[KERNEL
]->yield( '_send', $msg );
300 # event: pl.save( $playlist );
302 # Save the current playlist to a file called $playlist in MPD's
303 # playlist directory.
306 my ($playlist) = $_[ARG0
];
307 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
308 _from
=> $_[SENDER
]->ID,
309 _request
=> $_[STATE
],
311 _commands
=> [ qq[save
"$playlist"] ],
314 $_[KERNEL
]->yield( '_send', $msg );
319 # event: pl.save( $playlist );
321 # Delete playlist named $playlist from MPD's playlist directory.
324 my ($playlist) = $_[ARG0
];
325 my $msg = POE
::Component
::Client
::MPD
::Message
->new( {
326 _from
=> $_[SENDER
]->ID,
327 _request
=> $_[STATE
],
329 _commands
=> [ qq[rm
"$playlist"] ],
332 $_[KERNEL
]->yield( '_send', $msg );
343 POE::Component::Client::MPD::Playlist - module handling playlist commands
348 C<POCOCM::Playlist> is responsible for handling playlist-related commands.
349 To achieve those commands, send the corresponding event to the POCOCM
350 session you created: it will be responsible for dispatching the event
356 The following is a list of general purpose events accepted by POCOCM.
359 =head2 Retrieving information
361 =head2 Adding / removing songs
363 =head2 Changing playlist order
365 =head2 Managing playlists
370 For all related information (bug reporting, mailing-list, pointers to
371 MPD and POE, etc.), refer to C<POE::Component::Client::MPD>'s pod,
377 Jerome Quelin, C<< <jquelin at cpan.org> >>
380 =head1 COPYRIGHT & LICENSE
382 Copyright (c) 2007 Jerome Quelin, all rights reserved.
384 This program is free software; you can redistribute it and/or modify
385 it under the same terms as Perl itself.