2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation; either version 2 of the License, or
5 # (at your option) any later version.
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 package Audio
::MPD
::Collection
;
22 use Audio
::MPD
::Item
::Directory
;
23 use Audio
::MPD
::Item
::Song
;
24 use Scalar
::Util qw
[ weaken
];
26 use base qw
[ Class
::Accessor
::Fast
];
27 __PACKAGE__
->mk_accessors( qw
[ _mpd
] );
30 #our ($VERSION) = '$Rev: 5828 $' =~ /(\d+)/;
37 # my $collection = Audio::MPD::Collection->new( $mpd );
39 # This will create the object, holding a back-reference to the Audio::MPD
40 # object itself (for communication purposes). But in order to play safe and
41 # to free the memory in time, this reference is weakened.
43 # Note that you're not supposed to call this constructor yourself, an
44 # Audio::MPD::Collection is automatically created for you during the creation
45 # of an Audio::MPD object.
50 my $self = { _mpd
=> $mpd };
51 weaken
( $self->{_mpd
} );
60 # -- Collection: retrieving songs & directories
63 # my @items = $collection->all_items( [$path] );
65 # Return *all* Audio::MPD::Items (both songs & directories) currently known
68 # If $path is supplied (relative to mpd root), restrict the retrieval to
69 # songs and dirs in this directory.
72 my ($self, $path) = @_;
75 return $self->_mpd->_cooked_command_as_items( qq[listallinfo
"$path"\n] );
80 # my @items = $collection->all_items_simple( [$path] );
82 # Return *all* Audio::MPD::Items (both songs & directories) currently known
85 # If $path is supplied (relative to mpd root), restrict the retrieval to
86 # songs and dirs in this directory.
88 # /!\ Warning: the Audio::MPD::Item::Song objects will only have their tag
89 # file filled. Any other tag will be empty, so don't use this sub for any
90 # other thing than a quick scan!
92 sub all_items_simple
{
93 my ($self, $path) = @_;
96 return $self->_mpd->_cooked_command_as_items( qq[listall
"$path"\n] );
101 # my @items = $collection->items_in_dir( [$path] );
103 # Return the items in the given $path. If no $path supplied, do it on mpd's
106 # Note that this sub does not work recusrively on all directories.
109 my ($self, $path) = @_;
112 return $self->_mpd->_cooked_command_as_items( qq[lsinfo
"$path"\n] );
117 # -- Collection: retrieving the whole collection
120 # my @albums = $collection->all_albums;
122 # Return the list of all albums (strings) currently known by mpd.
126 return $self->_mpd->_cooked_command_strip_first_field( "list album\n" );
131 # my @artists = $collection->all_artists;
133 # Return the list of all artists (strings) currently known by mpd.
137 return $self->_mpd->_cooked_command_strip_first_field( "list artist\n" );
142 # my @titles = $collection->all_titles;
144 # Return the list of all titles (strings) currently known by mpd.
148 return $self->_mpd->_cooked_command_strip_first_field( "list title\n" );
153 # my @pathes = $collection->all_pathes;
155 # Return the list of all pathes (strings) currently known by mpd.
159 return $self->_mpd->_cooked_command_strip_first_field( "list filename\n" );
163 # -- Collection: picking songs
166 # my $song = $collection->song( $path );
168 # Return the Audio::MPD::Item::Song which correspond to $path.
171 my ($self, $what) = @_;
173 my ($item) = $self->_mpd->_cooked_command_as_items( qq[find filename
"$what"\n] );
179 # my $song = $collection->songs_with_filename_partial( $path );
181 # Return the Audio::MPD::Item::Songs containing $string in their path.
183 sub songs_with_filename_partial
{
184 my ($self, $what) = @_;
186 return $self->_mpd->_cooked_command_as_items( qq[search filename
"$what"\n] );
190 # -- Collection: songs, albums & artists relations
193 # my @albums = $collection->albums_by_artist($artist);
195 # Return all albums (strings) performed by $artist or where $artist
198 sub albums_by_artist
{
199 my ($self, $artist) = @_;
200 return $self->_mpd->_cooked_command_strip_first_field( qq[list album
"$artist"\n] );
205 # my @songs = $collection->songs_by_artist( $artist );
207 # Return all Audio::MPD::Item::Songs performed by $artist.
209 sub songs_by_artist
{
210 my ($self, $what) = @_;
212 return $self->_mpd->_cooked_command_as_items( qq[find artist
"$what"\n] );
217 # my @songs = $collection->songs_by_artist_partial( $string );
219 # Return all Audio::MPD::Item::Songs performed by an artist with $string
222 sub songs_by_artist_partial
{
223 my ($self, $what) = @_;
225 return $self->_mpd->_cooked_command_as_items( qq[search artist
"$what"\n] );
230 # my @songs = $collection->songs_from_album( $album );
232 # Return all Audio::MPD::Item::Songs appearing in $album.
234 sub songs_from_album
{
235 my ($self, $what) = @_;
237 return $self->_mpd->_cooked_command_as_items( qq[find album
"$what"\n] );
242 # my @songs = $collection->songs_from_album_partial( $string );
244 # Return all Audio::MPD::Item::Songs appearing in album containing $string.
246 sub songs_from_album_partial
{
247 my ($self, $what) = @_;
249 return $self->_mpd->_cooked_command_as_items( qq[search album
"$what"\n] );
254 # my @songs = $collection->songs_with_title( $title );
256 # Return all Audio::MPD::Item::Songs which title is exactly $title.
258 sub songs_with_title
{
259 my ($self, $what) = @_;
261 return $self->_mpd->_cooked_command_as_items( qq[find title
"$what"\n] );
266 # my @songs = $collection->songs_with_title_partial( $string );
268 # Return all Audio::MPD::Item::Songs where $string is part of the title.
270 sub songs_with_title_partial
{
271 my ($self, $what) = @_;
273 return $self->_mpd->_cooked_command_as_items( qq[search title
"$what"\n] );
284 Audio::MPD::Collection - an object to query MPD's collection
289 my $song = $mpd->collection->random_song;
294 C<Audio::MPD::Collection> is a class meant to access & query MPD's
295 collection. You will be able to use those high-level methods instead
296 of using the low-level methods provided by mpd itself.
299 =head1 PUBLIC METHODS
307 This will create the object, holding a back-reference to the C<Audio::MPD>
308 object itself (for communication purposes). But in order to play safe and
309 to free the memory in time, this reference is weakened.
311 Note that you're not supposed to call this constructor yourself, an
312 C<Audio::MPD::Collection> is automatically created for you during the creation
313 of an C<Audio::MPD> object.
318 =head2 Retrieving songs & directories
322 =item $coll->all_items( [$path] )
324 Return B<all> C<Audio::MPD::Item>s (both songs & directories) currently known
327 If C<$path> is supplied (relative to mpd root), restrict the retrieval to
328 songs and dirs in this directory.
331 =item $coll->all_items_simple( [$path] )
333 Return B<all> C<Audio::MPD::Item>s (both songs & directories) currently known
336 If C<$path> is supplied (relative to mpd root), restrict the retrieval to
337 songs and dirs in this directory.
339 B</!\ Warning>: the C<Audio::MPD::Item::Song> objects will only have their
340 tag file filled. Any other tag will be empty, so don't use this sub for any
341 other thing than a quick scan!
344 =item $coll->items_in_dir( [$path] )
346 Return the items in the given C<$path>. If no C<$path> supplied, do it on
347 mpd's root directory.
349 Note that this sub does not work recusrively on all directories.
355 =head2 Retrieving the whole collection
359 =item $coll->all_albums( )
361 Return the list of all albums (strings) currently known by mpd.
364 =item $coll->all_artists( )
366 Return the list of all artists (strings) currently known by mpd.
369 =item $coll->all_titles( )
371 Return the list of all song titles (strings) currently known by mpd.
374 =item $coll->all_pathes( )
376 Return the list of all pathes (strings) currently known by mpd.
382 =head2 Picking a song
386 =item $coll->song( $path )
388 Return the C<Audio::MPD::Item::Song> which correspond to C<$path>.
391 =item $coll->songs_with_filename_partial( $path )
393 Return the C<Audio::MPD::Item::Song>s containing $string in their path.
399 =head2 Songs, albums & artists relations
403 =item $coll->albums_by_artist( $artist )
405 Return all albums (strings) performed by C<$artist> or where C<$artist>
409 =item $coll->songs_by_artist( $artist )
411 Return all C<Audio::MPD::Item::Song>s performed by C<$artist>.
414 =item $coll->songs_by_artist_partial( $string )
416 Return all C<Audio::MPD::Item::Song>s performed by an artist with C<$string>
420 =item $coll->songs_from_album( $album )
422 Return all C<Audio::MPD::Item::Song>s appearing in C<$album>.
425 =item $coll->songs_from_album_partial( $string )
427 Return all C<Audio::MPD::Item::Song>s appearing in album containing C<$string>.
430 =item $coll->songs_with_title( $title )
432 Return all C<Audio::MPD::Item::Song>s which title is exactly C<$title>.
435 =item $coll->songs_with_title_partial( $string )
437 Return all C<Audio::MPD::Item::Song>s where C<$string> is part of the title.
445 You can find more information on the mpd project on its homepage at
446 L<http://www.musicpd.org>, or its wiki L<http://mpd.wikia.com>.
448 Regarding this Perl module, you can report bugs on CPAN via
449 L<http://rt.cpan.org/Public/Bug/Report.html?Queue=Audio-MPD>.
451 Audio::MPD development takes place on <audio-mpd@googlegroups.com>: feel free
452 to join us. (use L<http://groups.google.com/group/audio-mpd> to sign in). Our
453 subversion repository is located at L<https://svn.musicpd.org>.
458 Jerome Quelin <jquelin@cpan.org>
461 =head1 COPYRIGHT AND LICENSE
463 Copyright (c) 2007 Jerome Quelin <jquelin@cpan.org>
466 This program is free software; you can redistribute it and/or modify
467 it under the terms of the GNU General Public License as published by
468 the Free Software Foundation; either version 2 of the License, or
469 (at your option) any later version.
471 This program is distributed in the hope that it will be useful,
472 but WITHOUT ANY WARRANTY; without even the implied warranty of
473 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
474 GNU General Public License for more details.