[project @ 5828]
[audio-mpd.git] / lib / Audio / MPD / Collection.pm
blobc2193be9f2baa03588a559128f7e30204639097f
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;
20 use strict;
21 use warnings;
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+)/;
33 #--
34 # Constructor
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.
47 sub new {
48 my ($pkg, $mpd) = @_;
50 my $self = { _mpd => $mpd };
51 weaken( $self->{_mpd} );
52 bless $self, $pkg;
53 return $self;
57 #--
58 # Public methods
60 # -- Collection: retrieving songs & directories
63 # my @items = $collection->all_items( [$path] );
65 # Return *all* Audio::MPD::Items (both songs & directories) currently known
66 # by mpd.
68 # If $path is supplied (relative to mpd root), restrict the retrieval to
69 # songs and dirs in this directory.
71 sub all_items {
72 my ($self, $path) = @_;
73 $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
83 # by mpd.
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) = @_;
94 $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
104 # root directory.
106 # Note that this sub does not work recusrively on all directories.
108 sub items_in_dir {
109 my ($self, $path) = @_;
110 $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.
124 sub all_albums {
125 my ($self) = @_;
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.
135 sub all_artists {
136 my ($self) = @_;
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.
146 sub all_titles {
147 my ($self) = @_;
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.
157 sub all_pathes {
158 my ($self) = @_;
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.
170 sub song {
171 my ($self, $what) = @_;
173 my ($item) = $self->_mpd->_cooked_command_as_items( qq[find filename "$what"\n] );
174 return $item;
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
196 # participated.
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
220 # in her name.
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] );
279 __END__
282 =head1 NAME
284 Audio::MPD::Collection - an object to query MPD's collection
287 =head1 SYNOPSIS
289 my $song = $mpd->collection->random_song;
292 =head1 DESCRIPTION
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
301 =head2 Constructor
303 =over 4
305 =item new( $mpd )
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.
315 =back
318 =head2 Retrieving songs & directories
320 =over 4
322 =item $coll->all_items( [$path] )
324 Return B<all> C<Audio::MPD::Item>s (both songs & directories) currently known
325 by mpd.
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
334 by mpd.
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.
352 =back
355 =head2 Retrieving the whole collection
357 =over 4
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.
379 =back
382 =head2 Picking a song
384 =over 4
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.
396 =back
399 =head2 Songs, albums & artists relations
401 =over 4
403 =item $coll->albums_by_artist( $artist )
405 Return all albums (strings) performed by C<$artist> or where C<$artist>
406 participated.
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>
417 in her name.
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.
440 =back
443 =head1 SEE ALSO
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>.
456 =head1 AUTHORS
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.
476 =cut