blib/lib/POE/Component/Client/MPD/Playlist.pm

filter: Statementstmt 25.0% 30 / 120 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 37.5% 9 / 24 Podpod n/a totaltotal 26.3% 39 / 148 CC 3 SCAR 10.3
CC
3
Cov
24%
_do_crop
12.0
_do_as_items
2.0
_do_items_changed_since
2.0
CRAP 2.8
« Build ↑ Summary blib/lib/POE/Component/Client/MPD/Collection.pm »
1 #
2 # This file is part of POE-Component-Client-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 #
9 16 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package POE::Component::Client::MPD::Playlist;
14 # ABSTRACT: module handling playlist commands
15 16 $POE::Component::Client::MPD::Playlist::VERSION = '2.000';
16 16 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 16 ▶use MooseX::Has::Sugar;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 16 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 16 ▶use POE::Component::Client::MPD::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 # -- attributes
25  
26 16 has mpd => ( ro, required, weak_ref, );# isa=>'POE::Component::Client::MPD' );
27  
28  
29 # -- Playlist: retrieving information
30  
31  
32 sub _do_as_items {
33 0 ▶ my ($self, $msg) = @_;
Subroutine _do_as_items: not called CC=1 SCAR=6.9
34  
35 0 $msg->_set_commands ( [ 'playlistinfo' ] );
36 0 $msg->_set_cooking ( 'as_items' );
37 0 $self->mpd->_send_to_mpd( $msg );
38 }
39  
40  
41  
42 sub _do_items_changed_since {
43 0 ▶ my ($self, $msg) = @_;
Subroutine _do_items_changed_since: not called CC=1 SCAR=6.9
44 0 my $plid = $msg->params->[0];
45  
46 0 $msg->_set_commands ( [ "plchanges $plid" ] );
47 0 $msg->_set_cooking ( 'as_items' );
48 0 $self->mpd->_send_to_mpd( $msg );
49 }
50  
51  
52 # -- Playlist: adding / removing songs
53  
54  
55  
56 sub _do_add {
57 0 ▶ my ($self, $msg) = @_;
Subroutine _do_add: not called CC=1 SCAR=6.9
58  
59 0 my $args = $msg->params;
60 0 my @pathes = @$args; # args of the poe event
61 0 my @commands = ( # build the commands
62 'command_list_begin',
63 map( qq{add "$_"}, @pathes ),
64 'command_list_end',
65 );
66 0 $msg->_set_commands ( \@commands );
67 0 $msg->_set_cooking ( 'raw' );
68 0 $self->mpd->_send_to_mpd( $msg );
69 }
70  
71  
72  
73 sub _do_delete {
74 0 ▶ my ($self, $msg) = @_;
Subroutine _do_delete: not called CC=1 SCAR=6.9
75  
76 0 my $args = $msg->params;
77 0 my @numbers = @$args;
78 my @commands = ( # build the commands
79 'command_list_begin',
80 0 map( qq{delete $_}, reverse sort {$a<=>$b} @numbers ),
81 'command_list_end',
82 );
83 0 $msg->_set_commands ( \@commands );
84 0 $msg->_set_cooking ( 'raw' );
85 0 $self->mpd->_send_to_mpd( $msg );
86 }
87  
88  
89  
90 sub _do_deleteid {
91 0 ▶ my ($self, $msg) = @_;
Subroutine _do_deleteid: not called CC=1 SCAR=6.9
92  
93 0 my $args = $msg->params;
94 0 my @songids = @$args;
95 0 my @commands = ( # build the commands
96 'command_list_begin',
97 map( qq{deleteid $_}, @songids ),
98 'command_list_end',
99 );
100 0 $msg->_set_commands ( \@commands );
101 0 $msg->_set_cooking ( 'raw' );
102 0 $self->mpd->_send_to_mpd( $msg );
103 }
104  
105  
106  
107 sub _do_clear {
108 0 ▶ my ($self, $msg) = @_;
Subroutine _do_clear: not called CC=1 SCAR=6.9
109  
110 0 $msg->_set_commands ( [ 'clear' ] );
111 0 $msg->_set_cooking ( 'raw' );
112 0 $self->mpd->_send_to_mpd( $msg );
113 }
114  
115  
116  
117 sub _do_crop {
118 0 ▶ my ($self, $msg) = @_;
Subroutine _do_crop: not called CC=3 SCAR=24.8
119  
120 0 ▶ if ( not defined $msg->_data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->_data) 0 0 0
121 # no status yet - fire an event
122 0 $msg->_set_post( 'pl.crop' );
123 0 $self->mpd->_dispatch('status', $msg);
124 0 return;
125 }
126  
127 # now we know what to remove
128 0 my $cur = $msg->_data->song;
129 0 my $len = $msg->_data->playlistlength - 1;
130 my @commands = (
131 'command_list_begin',
132 0 ▶ map( { $_ != $cur ? "delete $_" : '' } reverse 0..$len ),
Branch: 1 branch
BranchTrueFalseTotal
$_ != $cur ? : 0 0 0
133 'command_list_end'
134 );
135  
136 0 $msg->_set_cooking ( 'raw' );
137 0 $msg->_set_commands ( \@commands );
138 0 $self->mpd->_send_to_mpd( $msg );
139 }
140  
141  
142 # -- Playlist: changing playlist order
143  
144  
145  
146 sub _do_shuffle {
147 0 ▶ my ($self, $msg) = @_;
Subroutine _do_shuffle: not called CC=1 SCAR=6.9
148  
149 0 $msg->_set_cooking ( 'raw' );
150 0 $msg->_set_commands ( [ 'shuffle' ] );
151 0 $self->mpd->_send_to_mpd( $msg );
152 }
153  
154  
155  
156 sub _do_swap {
157 0 ▶ my ($self, $msg) = @_;
Subroutine _do_swap: not called CC=1 SCAR=6.9
158 0 my ($from, $to) = @{ $msg->params }[0,1];
159  
160 0 $msg->_set_cooking ( 'raw' );
161 0 $msg->_set_commands ( [ "swap $from $to" ] );
162 0 $self->mpd->_send_to_mpd( $msg );
163 }
164  
165  
166  
167 sub _do_swapid {
168 0 ▶ my ($self, $msg) = @_;
Subroutine _do_swapid: not called CC=1 SCAR=6.9
169 0 my ($from, $to) = @{ $msg->params }[0,1];
170  
171 0 $msg->_set_cooking ( 'raw' );
172 0 $msg->_set_commands ( [ "swapid $from $to" ] );
173 0 $self->mpd->_send_to_mpd( $msg );
174 }
175  
176  
177  
178 sub _do_move {
179 0 ▶ my ($self, $msg) = @_;
Subroutine _do_move: not called CC=1 SCAR=6.9
180 0 my ($song, $pos) = @{ $msg->params }[0,1];
181  
182 0 $msg->_set_cooking ( 'raw' );
183 0 $msg->_set_commands ( [ "move $song $pos" ] );
184 0 $self->mpd->_send_to_mpd( $msg );
185 }
186  
187  
188  
189 sub _do_moveid {
190 0 ▶ my ($self, $msg) = @_;
Subroutine _do_moveid: not called CC=1 SCAR=6.9
191 0 my ($songid, $pos) = @{ $msg->params }[0,1];
192  
193 0 $msg->_set_cooking ( 'raw' );
194 0 $msg->_set_commands ( [ "moveid $songid $pos" ] );
195 0 $self->mpd->_send_to_mpd( $msg );
196 }
197  
198  
199 # -- Playlist: managing playlists
200  
201  
202  
203 sub _do_load {
204 0 ▶ my ($self, $msg) = @_;
Subroutine _do_load: not called CC=1 SCAR=6.9
205 0 my $playlist = $msg->params->[0];
206  
207 0 $msg->_set_cooking ( 'raw' );
208 0 $msg->_set_commands ( [ qq{load "$playlist"} ] );
209 0 $self->mpd->_send_to_mpd( $msg );
210 }
211  
212  
213  
214 sub _do_save {
215 0 ▶ my ($self, $msg) = @_;
Subroutine _do_save: not called CC=1 SCAR=6.9
216 0 my $playlist = $msg->params->[0];
217  
218 0 $msg->_set_cooking ( 'raw' );
219 0 $msg->_set_commands ( [ qq{save "$playlist"} ] );
220 0 $self->mpd->_send_to_mpd( $msg );
221 }
222  
223  
224  
225 sub _do_rm {
226 0 ▶ my ($self, $msg) = @_;
Subroutine _do_rm: not called CC=1 SCAR=6.9
227 0 my $playlist = $msg->params->[0];
228  
229 0 $msg->_set_cooking ( 'raw' );
230 0 $msg->_set_commands ( [ qq{rm "$playlist"} ] );
231 0 $self->mpd->_send_to_mpd( $msg );
232 }
233  
234  
235 16 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
236 16 __PACKAGE__->meta->make_immutable;
237 16 1;
238  
239 __END__
240  
241 =pod
242  
243 =head1 NAME
244  
245 POE::Component::Client::MPD::Playlist - module handling playlist commands
246  
247 =head1 VERSION
248  
249 version 2.000
250  
251 =head1 DESCRIPTION
252  
253 L<POE::Component::Client::MPD::Playlist> is responsible for handling
254 general purpose commands. They are in a dedicated module to achieve
255 easier code maintenance.
256  
257 To achieve those commands, send the corresponding event to the POCOCM
258 session you created: it will be responsible for dispatching the event
259 where it is needed. Under no circumstance should you call directly subs
260 or methods from this module directly.
261  
262 Read L<POCOCM|POE::Component::Client::MPD>'s pod to learn how to deal
263 with answers from those commands.
264  
265 Following is a list of playlist-related events accepted by POCOCM.
266  
267 =head1 RETRIEVING INFORMATION
268  
269 =head2 pl.as_items( )
270  
271 Return an array of L<Audio::MPD::Common::Item::Song>s, one for each of
272 the songs in the current playlist.
273  
274 =head2 pl.items_changed_since( $plversion )
275  
276 Return a list with all the songs (as L<Audio::MPD::Common::Item::Song>
277 objects) added to the playlist since playlist C<$plversion>.
278  
279 =head1 ADDING / REMOVING SONGS
280  
281 =head2 pl.add( $path, $path, ... )
282  
283 Add the songs identified by C<$path> (relative to MPD's music directory)
284 to the current playlist.
285  
286 =head2 pl.delete( $number, $number, ... )
287  
288 Remove song C<$number> (starting from 0) from the current playlist.
289  
290 =head2 pl.deleteid( $songid, $songid, ... )
291  
292 Remove the specified C<$songid> (as assigned by mpd when inserted in
293 playlist) from the current playlist.
294  
295 =head2 pl.clear( )
296  
297 Remove all the songs from the current playlist.
298  
299 =head2 pl.crop( )
300  
301 Remove all of the songs from the current playlist *except* the current one.
302  
303 =head1 CHANGING PLAYLIST ORDER
304  
305 =head2 pl.shuffle( )
306  
307 Shuffle the current playlist.
308  
309 =head2 pl.swap( $song1, $song2 )
310  
311 Swap positions of song number C<$song1> and C<$song2> in the current
312 playlist.
313  
314 =head2 pl.swapid( $songid1, $songid2 )
315  
316 Swap positions of song id C<$songid1> and C<$songid2> in the current
317 playlist.
318  
319 =head2 pl.move( $song, $newpos )
320  
321 Move song number C<$song> to the position C<$newpos>.
322  
323 =head2 pl.moveid( $songid, $newpos )
324  
325 Move song id C<$songid> to the position C<$newpos>.
326  
327 =head1 MANAGING PLAYLISTS
328  
329 =head2 pl.load( $playlist )
330  
331 Load list of songs from specified C<$playlist> file.
332  
333 =head2 pl.save( $playlist )
334  
335 Save the current playlist to a file called C<$playlist> in MPD's
336 playlist directory.
337  
338 =head2 pl.rm( $playlist )
339  
340 Delete playlist named C<$playlist> from MPD's playlist directory.
341  
342 =head1 AUTHOR
343  
344 Jerome Quelin
345  
346 =head1 COPYRIGHT AND LICENSE
347  
348 This software is copyright (c) 2007 by Jerome Quelin.
349  
350 This is free software; you can redistribute it and/or modify it under
351 the same terms as the Perl 5 programming language system itself.
352  
353 =cut
« Build ↑ Summary blib/lib/POE/Component/Client/MPD/Collection.pm »