POD updates - use L<> to make links between documentation for various
[audio-mpd.git] / lib / Audio / MPD / Collection.pm
blobd2da0e74f2fec4f3118fd695f3a312f6dab31bbe
2 # This file is part of Audio::MPD
3 # Copyright (c) 2007-2009 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 Audio::MPD::Collection;
12 use strict;
13 use warnings;
14 use Scalar::Util qw[ weaken ];
16 use base qw[ Class::Accessor::Fast ];
17 __PACKAGE__->mk_accessors( qw[ _mpd ] );
20 #our ($VERSION) = '$Rev: 5284 $' =~ /(\d+)/;
23 #--
24 # Constructor
27 # my $collection = Audio::MPD::Collection->new( $mpd );
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.
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.
37 sub new {
38 my ($pkg, $mpd) = @_;
40 my $self = { _mpd => $mpd };
41 weaken( $self->{_mpd} );
42 bless $self, $pkg;
43 return $self;
47 #--
48 # Public methods
50 # -- Collection: retrieving songs & directories
53 # my @items = $collection->all_items( [$path] );
55 # Return *all* AMC::Items (both songs & directories) currently known
56 # by mpd.
58 # If $path is supplied (relative to mpd root), restrict the retrieval to
59 # songs and dirs in this directory.
61 sub all_items {
62 my ($self, $path) = @_;
63 $path ||= '';
64 $path =~ s/"/\\"/g;
66 return $self->_mpd->_cooked_command_as_items( qq[listallinfo "$path"\n] );
71 # my @items = $collection->all_items_simple( [$path] );
73 # Return *all* AMC::Items (both songs & directories) currently known
74 # by mpd.
76 # If $path is supplied (relative to mpd root), restrict the retrieval to
77 # songs and dirs in this directory.
79 # /!\ Warning: the AMC::Item::Song objects will only have their tag
80 # file filled. Any other tag will be empty, so don't use this sub for any
81 # other thing than a quick scan!
83 sub all_items_simple {
84 my ($self, $path) = @_;
85 $path ||= '';
86 $path =~ s/"/\\"/g;
88 return $self->_mpd->_cooked_command_as_items( qq[listall "$path"\n] );
93 # my @items = $collection->items_in_dir( [$path] );
95 # Return the items in the given $path. If no $path supplied, do it on mpd's
96 # root directory.
98 # Note that this sub does not work recusrively on all directories.
100 sub items_in_dir {
101 my ($self, $path) = @_;
102 $path ||= '';
103 $path =~ s/"/\\"/g;
105 return $self->_mpd->_cooked_command_as_items( qq[lsinfo "$path"\n] );
110 # -- Collection: retrieving the whole collection
113 # my @songs = $collection->all_songs( [$path] );
115 # Return *all* AMC::Item::Songs currently known by mpd.
117 # If $path is supplied (relative to mpd root), restrict the retrieval to
118 # songs and dirs in this directory.
120 sub all_songs {
121 my ($self, $path) = @_;
122 return grep { $_->isa('Audio::MPD::Common::Item::Song') } $self->all_items($path);
127 # my @albums = $collection->all_albums;
129 # Return the list of all albums (strings) currently known by mpd.
131 sub all_albums {
132 my ($self) = @_;
133 return $self->_mpd->_cooked_command_strip_first_field( "list album\n" );
138 # my @artists = $collection->all_artists;
140 # Return the list of all artists (strings) currently known by mpd.
142 sub all_artists {
143 my ($self) = @_;
144 return $self->_mpd->_cooked_command_strip_first_field( "list artist\n" );
149 # my @titles = $collection->all_titles;
151 # Return the list of all titles (strings) currently known by mpd.
153 sub all_titles {
154 my ($self) = @_;
155 return $self->_mpd->_cooked_command_strip_first_field( "list title\n" );
160 # my @pathes = $collection->all_pathes;
162 # Return the list of all pathes (strings) currently known by mpd.
164 sub all_pathes {
165 my ($self) = @_;
166 return $self->_mpd->_cooked_command_strip_first_field( "list filename\n" );
171 # my @items = $collection->all_playlists;
173 # Return the list of playlists (strings) currently known by mpd.
175 sub all_playlists {
176 my ($self) = @_;
178 return
179 map { /^playlist: (.*)$/ ? ($1) : () }
180 $self->_mpd->_send_command( "lsinfo\n" );
185 # -- Collection: picking songs
188 # my $song = $collection->song( $path );
190 # Return the AMC::Item::Song which correspond to $path.
192 sub song {
193 my ($self, $what) = @_;
194 $what =~ s/"/\\"/g;
196 my ($item) = $self->_mpd->_cooked_command_as_items( qq[find filename "$what"\n] );
197 return $item;
202 # my $song = $collection->songs_with_filename_partial( $string );
204 # Return the AMC::Item::Songs containing $string in their path.
206 sub songs_with_filename_partial {
207 my ($self, $what) = @_;
208 $what =~ s/"/\\"/g;
210 return $self->_mpd->_cooked_command_as_items( qq[search filename "$what"\n] );
214 # -- Collection: songs, albums & artists relations
217 # my @albums = $collection->albums_by_artist($artist);
219 # Return all albums (strings) performed by $artist or where $artist
220 # participated.
222 sub albums_by_artist {
223 my ($self, $artist) = @_;
224 $artist =~ s/"/\\"/g;
225 return $self->_mpd->_cooked_command_strip_first_field( qq[list album "$artist"\n] );
230 # my @songs = $collection->songs_by_artist( $artist );
232 # Return all AMC::Item::Songs performed by $artist.
234 sub songs_by_artist {
235 my ($self, $what) = @_;
236 $what =~ s/"/\\"/g;
238 return $self->_mpd->_cooked_command_as_items( qq[find artist "$what"\n] );
243 # my @songs = $collection->songs_by_artist_partial( $string );
245 # Return all AMC::Item::Songs performed by an artist with $string
246 # in her name.
248 sub songs_by_artist_partial {
249 my ($self, $what) = @_;
250 $what =~ s/"/\\"/g;
252 return $self->_mpd->_cooked_command_as_items( qq[search artist "$what"\n] );
257 # my @songs = $collection->songs_from_album( $album );
259 # Return all AMC::Item::Songs appearing in $album.
261 sub songs_from_album {
262 my ($self, $what) = @_;
263 $what =~ s/"/\\"/g;
265 return $self->_mpd->_cooked_command_as_items( qq[find album "$what"\n] );
270 # my @songs = $collection->songs_from_album_partial( $string );
272 # Return all AMC::Item::Songs appearing in album containing $string.
274 sub songs_from_album_partial {
275 my ($self, $what) = @_;
276 $what =~ s/"/\\"/g;
278 return $self->_mpd->_cooked_command_as_items( qq[search album "$what"\n] );
283 # my @songs = $collection->songs_with_title( $title );
285 # Return all AMC::Item::Songs which title is exactly $title.
287 sub songs_with_title {
288 my ($self, $what) = @_;
289 $what =~ s/"/\\"/g;
291 return $self->_mpd->_cooked_command_as_items( qq[find title "$what"\n] );
296 # my @songs = $collection->songs_with_title_partial( $string );
298 # Return all AMC::Item::Songs where $string is part of the title.
300 sub songs_with_title_partial {
301 my ($self, $what) = @_;
302 $what =~ s/"/\\"/g;
304 return $self->_mpd->_cooked_command_as_items( qq[search title "$what"\n] );
310 __END__
313 =head1 NAME
315 Audio::MPD::Collection - an object to query MPD's collection
318 =head1 SYNOPSIS
320 my $song = $mpd->collection->random_song;
323 =head1 DESCRIPTION
325 C<Audio::MPD::Collection> is a class meant to access & query MPD's
326 collection. You will be able to use those high-level methods instead
327 of using the low-level methods provided by mpd itself.
330 =head1 PUBLIC METHODS
332 =head2 Constructor
334 =over 4
336 =item new( $mpd )
338 This will create the object, holding a back-reference to the L<Audio::MPD>
339 object itself (for communication purposes). But in order to play safe and
340 to free the memory in time, this reference is weakened.
342 Note that you're not supposed to call this constructor yourself, an
343 L<Audio::MPD::Collection> is automatically created for you during the creation
344 of an L<Audio::MPD> object.
346 =back
349 =head2 Retrieving songs & directories
351 =over 4
353 =item $coll->all_items( [$path] )
355 Return B<all> L<Audio::MPD::Common::Item>s (both songs & directories)
356 currently known by mpd.
358 If C<$path> is supplied (relative to mpd root), restrict the retrieval to
359 songs and dirs in this directory.
362 =item $coll->all_items_simple( [$path] )
364 Return B<all> L<Audio::MPD::Common::Item>s (both songs & directories)
365 currently known by mpd.
367 If C<$path> is supplied (relative to mpd root), restrict the retrieval to
368 songs and dirs in this directory.
370 B</!\ Warning>: the L<Audio::MPD::Common::Item::Song> objects will only
371 have their tag C<file> filled. Any other tag will be empty, so don't use
372 this sub for any other thing than a quick scan!
375 =item $coll->items_in_dir( [$path] )
377 Return the items in the given C<$path>. If no C<$path> supplied, do it on
378 mpd's root directory.
380 Note that this sub does not work recusrively on all directories.
383 =back
386 =head2 Retrieving the whole collection
388 =over 4
390 =item $coll->all_songs( [$path] )
392 Return B<all> L<Audio::MPD::Common::Item::Song>s currently known by mpd.
394 If C<$path> is supplied (relative to mpd root), restrict the retrieval to
395 songs and dirs in this directory.
398 =item $coll->all_albums()
400 Return the list of all albums (strings) currently known by mpd.
403 =item $coll->all_artists()
405 Return the list of all artists (strings) currently known by mpd.
408 =item $coll->all_titles()
410 Return the list of all song titles (strings) currently known by mpd.
413 =item $coll->all_pathes()
415 Return the list of all pathes (strings) currently known by mpd.
418 =item $coll->all_playlists()
420 Return the list of all playlists (strings) currently known by mpd.
423 =back
426 =head2 Picking a song
428 =over 4
430 =item $coll->song( $path )
432 Return the L<Audio::MPD::Common::Item::Song> which correspond to C<$path>.
435 =item $coll->songs_with_filename_partial( $string )
437 Return the L<Audio::MPD::Common::Item::Song>s containing C<$string> in
438 their path.
441 =back
444 =head2 Songs, albums & artists relations
446 =over 4
448 =item $coll->albums_by_artist( $artist )
450 Return all albums (strings) performed by C<$artist> or where C<$artist>
451 participated.
454 =item $coll->songs_by_artist( $artist )
456 Return all L<Audio::MPD::Common::Item::Song>s performed by C<$artist>.
459 =item $coll->songs_by_artist_partial( $string )
461 Return all L<Audio::MPD::Common::Item::Song>s performed by an artist
462 with C<$string> in her name.
465 =item $coll->songs_from_album( $album )
467 Return all L<Audio::MPD::Common::Item::Song>s appearing in C<$album>.
470 =item $coll->songs_from_album_partial( $string )
472 Return all L<Audio::MPD::Common::Item::Song>s appearing in album
473 containing C<$string>.
476 =item $coll->songs_with_title( $title )
478 Return all L<Audio::MPD::Common::Item::Song>s which title is exactly
479 C<$title>.
482 =item $coll->songs_with_title_partial( $string )
484 Return all L<Audio::MPD::Common::Item::Song>s where C<$string> is part
485 of the title.
488 =back
491 =head1 SEE ALSO
493 For all related information (bug reporting, mailing-list, pointers to
494 MPD, etc.), refer to L<Audio::MPD>'s pod, section C<SEE ALSO>
497 =head1 AUTHOR
499 Jerome Quelin, C<< <jquelin@cpan.org> >>
502 =head1 COPYRIGHT & LICENSE
504 Copyright (c) 2007-2009 Jerome Quelin, all rights reserved.
506 This program is free software; you can redistribute it and/or modify
507 it under the same terms as Perl itself.
509 =cut