File Coverage

blib/lib/Audio/MPD/Collection.pm
Criterion Covered Total %
statement 20 79 25.3
branch 0 2 0.0
condition 0 6 0.0
subroutine 7 27 25.9
pod 20 20 100.0
total 47 134 35.0


line stmt bran cond sub pod time code
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             #
9 9     9   179 use 5.008;
  9         18  
10 9     9   31 use warnings;
  9         8  
  9         202  
11 9     9   25 use strict;
  9         10  
  9         313  
12              
13             package Audio::MPD::Collection;
14             # ABSTRACT: class to query MPD's collection
15             $Audio::MPD::Collection::VERSION = '2.002';
16 9     9   28 use Moose;
  9         8  
  9         38  
17 9     9   35997 use MooseX::Has::Sugar;
  9         10  
  9         63  
18 9     9   728 use MooseX::SemiAffordanceAccessor;
  9         11  
  9         47  
19              
20             has _mpd => ( ro, required, weak_ref );
21              
22              
23             #--
24             # Constructor
25              
26             #
27             # my $collection = Audio::MPD::Collection->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::Collection is automatically created for you during the creation
35             # of an Audio::MPD object.
36             #
37              
38              
39             #--
40             # Public methods
41              
42             # -- Collection: retrieving songs & directories
43              
44              
45             sub all_items {
46 0     0 1   my ($self, $path) = @_;
47 0   0       $path ||= '';
48 0           $path =~ s/"/\\"/g;
49              
50 0           return $self->_mpd->_cooked_command_as_items( qq[listallinfo "$path"\n] );
51             }
52              
53              
54              
55             sub all_items_simple {
56 0     0 1   my ($self, $path) = @_;
57 0   0       $path ||= '';
58 0           $path =~ s/"/\\"/g;
59              
60 0           return $self->_mpd->_cooked_command_as_items( qq[listall "$path"\n] );
61             }
62              
63              
64              
65             sub items_in_dir {
66 0     0 1   my ($self, $path) = @_;
67 0   0       $path ||= '';
68 0           $path =~ s/"/\\"/g;
69              
70 0           return $self->_mpd->_cooked_command_as_items( qq[lsinfo "$path"\n] );
71             }
72              
73              
74             # -- Collection: retrieving the whole collection
75              
76              
77             sub all_songs {
78 0     0 1   my ($self, $path) = @_;
79 0           return grep { $_->isa('Audio::MPD::Common::Item::Song') } $self->all_items($path);
  0            
80             }
81              
82              
83              
84             sub all_albums {
85 0     0 1   my ($self) = @_;
86 0           return $self->_mpd->_cooked_command_strip_first_field( "list album\n" );
87             }
88              
89              
90              
91             sub all_artists {
92 0     0 1   my ($self) = @_;
93 0           return $self->_mpd->_cooked_command_strip_first_field( "list artist\n" );
94             }
95              
96              
97              
98             sub all_titles {
99 0     0 1   my ($self) = @_;
100 0           return $self->_mpd->_cooked_command_strip_first_field( "list title\n" );
101             }
102              
103              
104              
105             sub all_pathes {
106 0     0 1   my ($self) = @_;
107 0           return $self->_mpd->_cooked_command_strip_first_field( "list filename\n" );
108             }
109              
110              
111              
112             sub all_playlists {
113 0     0 1   my ($self) = @_;
114              
115             return
116 0 0         map { /^playlist: (.*)$/ ? ($1) : () }
  0            
117             $self->_mpd->_send_command( "lsinfo\n" );
118             }
119              
120              
121              
122             sub all_genres {
123 0     0 1   my ($self) = @_;
124 0           return $self->_mpd->_cooked_command_strip_first_field( "list genre\n" );
125             }
126              
127              
128             # -- Collection: picking songs
129              
130              
131             sub song {
132 0     0 1   my ($self, $what) = @_;
133 0           $what =~ s/"/\\"/g;
134              
135 0           my ($item) = $self->_mpd->_cooked_command_as_items( qq[find filename "$what"\n] );
136 0           return $item;
137             }
138              
139              
140              
141             sub songs_with_filename_partial {
142 0     0 1   my ($self, $what) = @_;
143 0           $what =~ s/"/\\"/g;
144              
145 0           return $self->_mpd->_cooked_command_as_items( qq[search filename "$what"\n] );
146             }
147              
148              
149             # -- Collection: songs, albums, artists & genres relations
150              
151              
152             sub albums_by_artist {
153 0     0 1   my ($self, $artist) = @_;
154 0           $artist =~ s/"/\\"/g;
155 0           return $self->_mpd->_cooked_command_strip_first_field( qq[list album "$artist"\n] );
156             }
157              
158              
159              
160             sub songs_by_artist {
161 0     0 1   my ($self, $what) = @_;
162 0           $what =~ s/"/\\"/g;
163              
164 0           return $self->_mpd->_cooked_command_as_items( qq[find artist "$what"\n] );
165             }
166              
167              
168              
169             sub songs_by_artist_partial {
170 0     0 1   my ($self, $what) = @_;
171 0           $what =~ s/"/\\"/g;
172              
173 0           return $self->_mpd->_cooked_command_as_items( qq[search artist "$what"\n] );
174             }
175              
176              
177              
178             sub songs_from_album {
179 0     0 1   my ($self, $what) = @_;
180 0           $what =~ s/"/\\"/g;
181              
182 0           return $self->_mpd->_cooked_command_as_items( qq[find album "$what"\n] );
183             }
184              
185              
186              
187             sub songs_from_album_partial {
188 0     0 1   my ($self, $what) = @_;
189 0           $what =~ s/"/\\"/g;
190              
191 0           return $self->_mpd->_cooked_command_as_items( qq[search album "$what"\n] );
192             }
193              
194              
195             sub songs_with_title {
196 0     0 1   my ($self, $what) = @_;
197 0           $what =~ s/"/\\"/g;
198              
199 0           return $self->_mpd->_cooked_command_as_items( qq[find title "$what"\n] );
200             }
201              
202              
203              
204             sub songs_with_title_partial {
205 0     0 1   my ($self, $what) = @_;
206 0           $what =~ s/"/\\"/g;
207              
208 0           return $self->_mpd->_cooked_command_as_items( qq[search title "$what"\n] );
209             }
210              
211              
212              
213             sub artists_by_genre {
214 0     0 1   my ($self, $genre) = @_;
215 0           $genre =~ s/"/\\"/g;
216 0           return $self->_mpd->_cooked_command_strip_first_field( qq[list artist genre "$genre"\n] );
217             }
218              
219              
220 9     9   26691 no Moose;
  9         20  
  9         50  
221             __PACKAGE__->meta->make_immutable;
222             1;
223              
224             __END__
225              
226             =pod
227              
228             =head1 NAME
229              
230             Audio::MPD::Collection - class to query MPD's collection
231              
232             =head1 VERSION
233              
234             version 2.002
235              
236             =head1 SYNOPSIS
237              
238             my @songs = $mpd->collection->all_songs;
239             # and lots of other methods
240              
241             =head1 DESCRIPTION
242              
243             L<Audio::MPD::Collection> is a class meant to access & query MPD's
244             collection. You will be able to use those high-level methods instead
245             of using the low-level methods provided by mpd itself.
246              
247             Note that you're not supposed to call the constructor yourself, an
248             L<Audio::MPD::Collection> is automatically created for you during the
249             creation of an L<Audio::MPD> object - it can then be used with the
250             C<collection()> accessor.
251              
252             =head1 RETRIEVING SONGS & DIRECTORIES
253              
254             =head2 all_items
255              
256             my @items = $coll->all_items( [$path] );
257              
258             Return B<all> L<Audio::MPD::Common::Item>s (both songs & directories)
259             currently known by mpd.
260              
261             If C<$path> is supplied (relative to mpd root), restrict the retrieval to
262             songs and dirs in this directory.
263              
264             =head2 all_items_simple
265              
266             my @items = $coll->all_items_simple( [$path] );
267              
268             Return B<all> L<Audio::MPD::Common::Item>s (both songs & directories)
269             currently known by mpd.
270              
271             If C<$path> is supplied (relative to mpd root), restrict the retrieval to
272             songs and dirs in this directory.
273              
274             B</!\ Warning>: the L<Audio::MPD::Common::Item::Song> objects will only
275             have their tag C<file> filled. Any other tag will be empty, so don't use
276             this sub for any other thing than a quick scan!
277              
278             =head2 items_in_dir
279              
280             my @items = $coll->items_in_dir( [$path] );
281              
282             Return the items in the given C<$path>. If no C<$path> supplied, do it on
283             mpd's root directory.
284              
285             Note that this sub does not work recusrively on all directories.
286              
287             =head1 RETRIEVING THE WHOLE COLLECTION
288              
289             =head2 all_songs
290              
291             my @songs = $coll->all_songs( [$path] );
292              
293             Return B<all> L<Audio::MPD::Common::Item::Song>s currently known by mpd.
294              
295             If C<$path> is supplied (relative to mpd root), restrict the retrieval to
296             songs and dirs in this directory.
297              
298             =head2 all_albums
299              
300             my @albums = $coll->all_albums;
301              
302             Return the list of all albums (strings) currently known by mpd.
303              
304             =head2 all_artists
305              
306             my @artists = $coll->all_artists;
307              
308             Return the list of all artists (strings) currently known by mpd.
309              
310             =head2 all_titles
311              
312             my @titles = $coll->all_titles;
313              
314             Return the list of all song titles (strings) currently known by mpd.
315              
316             =head2 all_pathes
317              
318             my @pathes = $coll->all_pathes;
319              
320             Return the list of all pathes (strings) currently known by mpd.
321              
322             =head2 all_playlists
323              
324             my @lists = $coll->all_playlists;
325              
326             Return the list of all playlists (strings) currently known by mpd.
327              
328             =head2 all_genres
329              
330             my @genres = $coll->all_genres;
331              
332             Return the list of all genres (strings) currently known by mpd.
333              
334             =head1 PICKING A SONG
335              
336             =head2 song
337              
338             my $song = $coll->song( $path );
339              
340             Return the L<Audio::MPD::Common::Item::Song> which correspond to C<$path>.
341              
342             =head2 songs_with_filename_partial
343              
344             my @songs = $coll->songs_with_filename_partial( $string );
345              
346             Return the L<Audio::MPD::Common::Item::Song>s containing C<$string> in
347             their path.
348              
349             =head1 SONGS, ALBUMS, ARTISTS & GENRES RELATIONS
350              
351             =head2 albums_by_artist
352              
353             my @albums = $coll->albums_by_artist( $artist );
354              
355             Return all albums (strings) performed by C<$artist> or where C<$artist>
356             participated.
357              
358             =head2 songs_by_artist
359              
360             my @songs = $coll->songs_by_artist( $artist );
361              
362             Return all L<Audio::MPD::Common::Item::Song>s performed by C<$artist>.
363              
364             =head2 songs_by_artist_partial
365              
366             my @songs = $coll->songs_by_artist_partial( $string );
367              
368             Return all L<Audio::MPD::Common::Item::Song>s performed by an artist
369             with C<$string> in her name.
370              
371             =head2 songs_from_album
372              
373             my @songs = $coll->songs_from_album( $album );
374              
375             Return all L<Audio::MPD::Common::Item::Song>s appearing in C<$album>.
376              
377             =head2 songs_from_album_partial
378              
379             my @songs = $coll->songs_from_album_partial( $string );
380              
381             Return all L<Audio::MPD::Common::Item::Song>s appearing in album
382             containing C<$string>.
383              
384             =head2 songs_with_title
385              
386             my @songs = $coll->songs_with_title( $title );
387              
388             Return all L<Audio::MPD::Common::Item::Song>s which title is exactly
389             C<$title>.
390              
391             =head2 songs_with_title_partial
392              
393             my @songs = $coll->songs_with_title_partial( $string );
394              
395             Return all L<Audio::MPD::Common::Item::Song>s where C<$string> is part
396             of the title.
397              
398             =head2 artists_by_genre
399              
400             my @artists = $coll->artists_by_genre( $genre );
401              
402             Return all artists (strings) of C<$genre>.
403              
404             =head1 AUTHOR
405              
406             Jerome Quelin
407              
408             =head1 COPYRIGHT AND LICENSE
409              
410             This software is copyright (c) 2007 by Jerome Quelin.
411              
412             This is free software; you can redistribute it and/or modify it under
413             the same terms as the Perl 5 programming language system itself.
414              
415             =cut