[project @ 6332]
[audio-mpd-common.git] / lib / POE / Component / Client / MPD / Playlist.pm
blobf94c3d55adf73fcc89d39b4cfa3d8cbf2adb07f5
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;
12 use strict;
13 use warnings;
15 use POE;
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.
27 sub _onpub_as_items {
28 my $msg = POE::Component::Client::MPD::Message->new( {
29 _from => $_[SENDER]->ID,
30 _request => $_[STATE],
31 _answer => $SEND,
32 _commands => [ 'playlistinfo' ],
33 _cooking => $AS_ITEMS,
34 } );
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 {
46 my $plid = $_[ARG0];
47 my $msg = POE::Component::Client::MPD::Message->new( {
48 _from => $_[SENDER]->ID,
49 _request => $_[STATE],
50 _answer => $SEND,
51 _commands => [ "plchanges $plid" ],
52 _cooking => $AS_ITEMS,
53 } );
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.
65 # No return event.
67 sub _onpub_add {
68 my @pathes = @_[ARG0 .. $#_]; # args of the poe event
69 my @commands = ( # build the commands
70 'command_list_begin',
71 map( qq[add "$_"], @pathes ),
72 'command_list_end',
74 my $msg = POE::Component::Client::MPD::Message->new( {
75 _from => $_[SENDER]->ID,
76 _request => $_[STATE],
77 _answer => $DISCARD,
78 _commands => \@commands,
79 _cooking => $RAW,
80 } );
81 $_[KERNEL]->yield( '_send', $msg );
86 # event: pl.delete( $number, $number, ... )
88 # Remove song $number (starting from 0) from the current playlist.
89 # No return event.
91 sub _onpub_delete {
92 my @numbers = @_[ARG0 .. $#_]; # args of the poe event
93 my @commands = ( # build the commands
94 'command_list_begin',
95 map( qq[delete $_], reverse sort {$a<=>$b} @numbers ),
96 'command_list_end',
98 my $msg = POE::Component::Client::MPD::Message->new( {
99 _from => $_[SENDER]->ID,
100 _request => $_[STATE],
101 _answer => $DISCARD,
102 _commands => \@commands,
103 _cooking => $RAW,
104 } );
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 ),
120 'command_list_end',
122 my $msg = POE::Component::Client::MPD::Message->new( {
123 _from => $_[SENDER]->ID,
124 _request => $_[STATE],
125 _answer => $DISCARD,
126 _commands => \@commands,
127 _cooking => $RAW,
128 } );
129 $_[KERNEL]->yield( '_send', $msg );
134 # event: clear()
136 # Remove all the songs from the current playlist.
138 sub _onpub_clear {
139 my $msg = POE::Component::Client::MPD::Message->new( {
140 _from => $_[SENDER]->ID,
141 _request => $_[STATE],
142 _answer => $DISCARD,
143 _commands => [ 'clear' ],
144 _cooking => $RAW,
145 } );
146 $_[KERNEL]->yield( '_send', $msg );
151 # event: crop()
153 # Remove all of the songs from the current playlist *except* the current one.
155 sub _onpub_crop {
156 my $msg = POE::Component::Client::MPD::Message->new( {
157 _from => $_[SENDER]->ID,
158 _request => $_[STATE],
159 _answer => $DISCARD,
160 _cooking => $RAW,
161 _pre_from => '_crop_status',
162 _pre_event => 'status',
163 } );
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;
178 my @commands = (
179 'command_list_begin',
180 map( { $_ != $cur ? "delete $_" : '' } reverse 0..$len ),
181 'command_list_end'
183 $msg->_commands( \@commands );
184 $_[KERNEL]->yield( '_send', $msg );
188 # -- Playlist: changing playlist order
191 # event: pl.shuffle()
193 # Shuffle the current playlist.
195 sub _onpub_shuffle {
196 my $msg = POE::Component::Client::MPD::Message->new( {
197 _from => $_[SENDER]->ID,
198 _request => $_[STATE],
199 _answer => $DISCARD,
200 _commands => [ 'shuffle' ],
201 _cooking => $RAW,
202 } );
203 $_[KERNEL]->yield( '_send', $msg );
208 # event: pl.swap( $song1, song2 )
210 # Swap positions of song number $song1 and $song2 in the current playlist.
212 sub _onpub_swap {
213 my ($from, $to) = @_[ARG0, ARG1];
214 my $msg = POE::Component::Client::MPD::Message->new( {
215 _from => $_[SENDER]->ID,
216 _request => $_[STATE],
217 _answer => $DISCARD,
218 _commands => [ "swap $from $to" ],
219 _cooking => $RAW,
220 } );
221 $_[KERNEL]->yield( '_send', $msg );
226 # event: pl.swapid( $songid1, songid2 )
228 # Swap positions of song id $songid1 and $songid2 in the current playlist.
230 sub _onpub_swapid {
231 my ($from, $to) = @_[ARG0, ARG1];
232 my $msg = POE::Component::Client::MPD::Message->new( {
233 _from => $_[SENDER]->ID,
234 _request => $_[STATE],
235 _answer => $DISCARD,
236 _commands => [ "swapid $from $to" ],
237 _cooking => $RAW,
238 } );
239 $_[KERNEL]->yield( '_send', $msg );
244 # event: pl.move( $song, $newpos );
246 # Move song number $song to the position $newpos.
248 sub _onpub_move {
249 my ($song, $pos) = @_[ARG0, ARG1];
250 my $msg = POE::Component::Client::MPD::Message->new( {
251 _from => $_[SENDER]->ID,
252 _request => $_[STATE],
253 _answer => $DISCARD,
254 _commands => [ "move $song $pos" ],
255 _cooking => $RAW,
256 } );
257 $_[KERNEL]->yield( '_send', $msg );
262 # event: pl.moveid( $songid, $newpos );
264 # Move song id $songid to the position $newpos.
266 sub _onpub_moveid {
267 my ($songid, $pos) = @_[ARG0, ARG1];
268 my $msg = POE::Component::Client::MPD::Message->new( {
269 _from => $_[SENDER]->ID,
270 _request => $_[STATE],
271 _answer => $DISCARD,
272 _commands => [ "moveid $songid $pos" ],
273 _cooking => $RAW,
274 } );
275 $_[KERNEL]->yield( '_send', $msg );
279 # -- Playlist: managing playlists
282 # event: pl.load( $playlist );
284 # Load list of songs from specified $playlist file.
286 sub _onpub_load {
287 my ($playlist) = $_[ARG0];
288 my $msg = POE::Component::Client::MPD::Message->new( {
289 _from => $_[SENDER]->ID,
290 _request => $_[STATE],
291 _answer => $DISCARD,
292 _commands => [ qq[load "$playlist"] ],
293 _cooking => $RAW,
294 } );
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.
305 sub _onpub_save {
306 my ($playlist) = $_[ARG0];
307 my $msg = POE::Component::Client::MPD::Message->new( {
308 _from => $_[SENDER]->ID,
309 _request => $_[STATE],
310 _answer => $DISCARD,
311 _commands => [ qq[save "$playlist"] ],
312 _cooking => $RAW,
313 } );
314 $_[KERNEL]->yield( '_send', $msg );
319 # event: pl.save( $playlist );
321 # Delete playlist named $playlist from MPD's playlist directory.
323 sub _onpub_rm {
324 my ($playlist) = $_[ARG0];
325 my $msg = POE::Component::Client::MPD::Message->new( {
326 _from => $_[SENDER]->ID,
327 _request => $_[STATE],
328 _answer => $DISCARD,
329 _commands => [ qq[rm "$playlist"] ],
330 _cooking => $RAW,
331 } );
332 $_[KERNEL]->yield( '_send', $msg );
339 __END__
341 =head1 NAME
343 POE::Component::Client::MPD::Playlist - module handling playlist commands
346 =head1 DESCRIPTION
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
351 where it is needed.
354 =head1 PUBLIC EVENTS
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
368 =head1 SEE ALSO
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,
372 section C<SEE ALSO>
375 =head1 AUTHOR
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.
387 =cut