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
;
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+)/;
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.
40 my $self = { _mpd
=> $mpd };
41 weaken
( $self->{_mpd
} );
50 # -- Collection: retrieving songs & directories
53 # my @items = $collection->all_items( [$path] );
55 # Return *all* AMC::Items (both songs & directories) currently known
58 # If $path is supplied (relative to mpd root), restrict the retrieval to
59 # songs and dirs in this directory.
62 my ($self, $path) = @_;
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
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) = @_;
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
98 # Note that this sub does not work recusrively on all directories.
101 my ($self, $path) = @_;
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.
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.
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.
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.
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.
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.
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.
193 my ($self, $what) = @_;
196 my ($item) = $self->_mpd->_cooked_command_as_items( qq[find filename
"$what"\n] );
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) = @_;
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
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) = @_;
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
248 sub songs_by_artist_partial
{
249 my ($self, $what) = @_;
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) = @_;
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) = @_;
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) = @_;
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) = @_;
304 return $self->_mpd->_cooked_command_as_items( qq[search title
"$what"\n] );
315 Audio::MPD::Collection - an object to query MPD's collection
320 my $song = $mpd->collection->random_song;
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
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.
349 =head2 Retrieving songs & directories
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.
386 =head2 Retrieving the whole collection
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.
426 =head2 Picking a song
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
444 =head2 Songs, albums & artists relations
448 =item $coll->albums_by_artist( $artist )
450 Return all albums (strings) performed by C<$artist> or where C<$artist>
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
482 =item $coll->songs_with_title_partial( $string )
484 Return all L<Audio::MPD::Common::Item::Song>s where C<$string> is part
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>
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.