2 # This file is part of Audio::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 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 @genre = $collection->all_genre;
162 # Return the list of all genres (strings) currently known by mpd.
166 return $self->_mpd->_cooked_command_strip_first_field( "list genre\n" );
171 # my @yers = $collection->all_years;
173 # Return the list of all years (strings) currently known by mpd.
177 return $self->_mpd->_cooked_command_strip_first_field( "list date\n" );
182 # my @pathes = $collection->all_pathes;
184 # Return the list of all pathes (strings) currently known by mpd.
188 return $self->_mpd->_cooked_command_strip_first_field( "list filename\n" );
193 # my @items = $collection->all_playlists;
195 # Return the list of playlists (strings) currently known by mpd.
201 map { /^playlist: (.*)$/ ?
($1) : () }
202 $self->_mpd->_send_command( "lsinfo\n" );
207 # -- Collection: picking songs
210 # my $song = $collection->song( $path );
212 # Return the AMC::Item::Song which correspond to $path.
215 my ($self, $what) = @_;
218 my ($item) = $self->_mpd->_cooked_command_as_items( qq[find filename
"$what"\n] );
224 # my $song = $collection->songs_with_filename_partial( $path );
226 # Return the AMC::Item::Songs containing $string in their path.
228 sub songs_with_filename_partial
{
229 my ($self, $what) = @_;
232 return $self->_mpd->_cooked_command_as_items( qq[search filename
"$what"\n] );
236 # -- Collection: songs, albums & artists relations
239 # my @albums = $collection->albums_by_artist($artist);
241 # Return all albums (strings) performed by $artist or where $artist
244 sub albums_by_artist
{
245 my ($self, $artist) = @_;
246 $artist =~ s/"/\\"/g;
247 return $self->_mpd->_cooked_command_strip_first_field( qq[list album
"$artist"\n] );
252 # my @songs = $collection->songs_by_artist( $genre );
254 # Return all AMC::Item::Songs performed in $genre.
258 my ($self, $what) = @_;
261 return $self->_mpd->_cooked_command_as_items( qq[find genre
"$what"\n] );
265 sub filenames_by_artist
{
266 my ($self, $what) = @_;
269 return $self->_mpd->_cooked_command_as_filename( qq[find artist
"$what"\n] );
273 sub filenames_by_year
{
274 my ($self, $what) = @_;
277 return $self->_mpd->_cooked_command_as_filename( qq[find date
"$what"\n] );
280 sub filenames_by_genre
{
281 my ($self, $what) = @_;
284 return $self->_mpd->_cooked_command_as_filename( qq[find genre
"$what"\n] );
288 sub filenames_by_album
{
289 my ($self, $what) = @_;
292 return $self->_mpd->_cooked_command_as_filename( qq[find album
"$what"\n] );
297 # my @songs = $collection->songs_by_artist_partial( $string );
299 # Return all AMC::Item::Songs performed by an artist with $string
302 sub songs_by_artist_partial
{
303 my ($self, $what) = @_;
306 return $self->_mpd->_cooked_command_as_items( qq[search artist
"$what"\n] );
311 # my @songs = $collection->songs_from_album( $album );
313 # Return all AMC::Item::Songs appearing in $album.
315 sub songs_from_album
{
316 my ($self, $what) = @_;
319 return $self->_mpd->_cooked_command_as_items( qq[find album
"$what"\n] );
324 # my @songs = $collection->songs_from_album_partial( $string );
326 # Return all AMC::Item::Songs appearing in album containing $string.
328 sub songs_from_album_partial
{
329 my ($self, $what) = @_;
332 return $self->_mpd->_cooked_command_as_items( qq[search album
"$what"\n] );
337 # my @songs = $collection->songs_with_title( $title );
339 # Return all AMC::Item::Songs which title is exactly $title.
341 sub songs_with_title
{
342 my ($self, $what) = @_;
345 return $self->_mpd->_cooked_command_as_items( qq[find title
"$what"\n] );
350 # my @songs = $collection->songs_with_title_partial( $string );
352 # Return all AMC::Item::Songs where $string is part of the title.
354 sub songs_with_title_partial
{
355 my ($self, $what) = @_;
358 return $self->_mpd->_cooked_command_as_items( qq[search title
"$what"\n] );
362 # my @songs = $collection->songs_with_title_partial_filename( $string );
364 # Return all AMC::Item::Songs where $string is part of the title.
366 sub songs_with_title_partial_filename
{
367 my ($self, $what) = @_;
371 return $self->_mpd->_cooked_command_as_filename( qq[search title
"$what"\n] );
374 # my @songs = $collection->songs_with_artist_partial_filename( $string );
376 # Return all AMC::Item::Songs where $string is part of the artist.
378 sub songs_with_artist_partial_filename
{
379 my ($self, $what) = @_;
382 return $self->_mpd->_cooked_command_as_filename( qq[search artist
"$what"\n] );
386 # my @songs = $collection->songs_with_album_partial_filename( $string );
388 # Return all AMC::Item::Songs where $string is part of the album.
390 sub songs_with_album_partial_filename
{
391 my ($self, $what) = @_;
394 return $self->_mpd->_cooked_command_as_filename( qq[search album
"$what"\n] );
405 Audio::MPD::Collection - an object to query MPD's collection
410 my $song = $mpd->collection->random_song;
415 C<Audio::MPD::Collection> is a class meant to access & query MPD's
416 collection. You will be able to use those high-level methods instead
417 of using the low-level methods provided by mpd itself.
420 =head1 PUBLIC METHODS
428 This will create the object, holding a back-reference to the C<Audio::MPD>
429 object itself (for communication purposes). But in order to play safe and
430 to free the memory in time, this reference is weakened.
432 Note that you're not supposed to call this constructor yourself, an
433 C<Audio::MPD::Collection> is automatically created for you during the creation
434 of an C<Audio::MPD> object.
439 =head2 Retrieving songs & directories
443 =item $coll->all_items( [$path] )
445 Return B<all> C<Audio::MPD::Common::Item>s (both songs & directories)
446 currently known by mpd.
448 If C<$path> is supplied (relative to mpd root), restrict the retrieval to
449 songs and dirs in this directory.
452 =item $coll->all_items_simple( [$path] )
454 Return B<all> C<Audio::MPD::Common::Item>s (both songs & directories)
455 currently known by mpd.
457 If C<$path> is supplied (relative to mpd root), restrict the retrieval to
458 songs and dirs in this directory.
460 B</!\ Warning>: the C<Audio::MPD::Common::Item::Song> objects will only have
461 their tag file filled. Any other tag will be empty, so don't use this sub for
462 any other thing than a quick scan!
465 =item $coll->items_in_dir( [$path] )
467 Return the items in the given C<$path>. If no C<$path> supplied, do it on
468 mpd's root directory.
470 Note that this sub does not work recusrively on all directories.
476 =head2 Retrieving the whole collection
480 =item $coll->all_songs( [$path] )
482 Return B<all> C<Audio::MPD::Common::Item::Song>s currently known by mpd.
484 If C<$path> is supplied (relative to mpd root), restrict the retrieval to
485 songs and dirs in this directory.
488 =item $coll->all_albums()
490 Return the list of all albums (strings) currently known by mpd.
493 =item $coll->all_artists()
495 Return the list of all artists (strings) currently known by mpd.
498 =item $coll->all_titles()
500 Return the list of all song titles (strings) currently known by mpd.
503 =item $coll->all_pathes()
505 Return the list of all pathes (strings) currently known by mpd.
508 =item $coll->all_playlists()
510 Return the list of all playlists (strings) currently known by mpd.
516 =head2 Picking a song
520 =item $coll->song( $path )
522 Return the C<Audio::MPD::Common::Item::Song> which correspond to C<$path>.
525 =item $coll->songs_with_filename_partial( $path )
527 Return the C<Audio::MPD::Common::Item::Song>s containing $string in their path.
533 =head2 Songs, albums & artists relations
537 =item $coll->albums_by_artist( $artist )
539 Return all albums (strings) performed by C<$artist> or where C<$artist>
543 =item $coll->songs_by_artist( $artist )
545 Return all C<Audio::MPD::Common::Item::Song>s performed by C<$artist>.
548 =item $coll->songs_by_artist_partial( $string )
550 Return all C<Audio::MPD::Common::Item::Song>s performed by an artist with
551 C<$string> in her name.
554 =item $coll->songs_from_album( $album )
556 Return all C<Audio::MPD::Common::Item::Song>s appearing in C<$album>.
559 =item $coll->songs_from_album_partial( $string )
561 Return all C<Audio::MPD::Common::Item::Song>s appearing in album containing C<$string>.
564 =item $coll->songs_with_title( $title )
566 Return all C<Audio::MPD::Common::Item::Song>s which title is exactly C<$title>.
569 =item $coll->songs_with_title_partial( $string )
571 Return all C<Audio::MPD::Common::Item::Song>s where C<$string> is part of the title.
584 Jerome Quelin, C<< <jquelin at cpan.org> >>
587 =head1 COPYRIGHT & LICENSE
589 Copyright (c) 2007 Jerome Quelin, all rights reserved.
591 This program is free software; you can redistribute it and/or modify
592 it under the same terms as Perl itself.