<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#
# This file is part of Audio::MPD
# Copyright (c) 2007 Jerome Quelin, all rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
#

package Audio::MPD::Playlist;

use strict;
use warnings;
use Scalar::Util qw[ weaken ];

use base qw[ Class::Accessor::Fast ];
__PACKAGE__-&gt;mk_accessors( qw[ _mpd ] );


#our ($VERSION) = '$Rev$' =~ /(\d+)/;


#--
# Constructor

#
# my $collection = Audio::MPD::Playlist-&gt;new( $mpd );
#
# This will create the object, holding a back-reference to the Audio::MPD
# object itself (for communication purposes). But in order to play safe and
# to free the memory in time, this reference is weakened.
#
# Note that you're not supposed to call this constructor yourself, an
# Audio::MPD::Playlist is automatically created for you during the creation
# of an Audio::MPD object.
#
sub new {
    my ($pkg, $mpd) = @_;

    my $self = { _mpd =&gt; $mpd };
    weaken( $self-&gt;{_mpd} );
    bless $self, $pkg;
    return $self;
}


#--
# Public methods

# -- Playlist: retrieving information

#
# my @items = $pl-&gt;as_items;
#
# Return an array of AMC::Item::Songs, one for each of the
# songs in the current playlist.
#
sub as_items {
    my ($self) = @_;

    my @list = $self-&gt;_mpd-&gt;_cooked_command_as_items("playlistinfo\n");
    return @list;
}


#
# my @items = $pl-&gt;items_changed_since( $plversion );
#
# Return a list with all the songs (as API::Song objects) added to
# the playlist since playlist $plversion.
#
sub items_changed_since {
    my ($self, $plid) = @_;
    return $self-&gt;_mpd-&gt;_cooked_command_as_items("plchanges $plid\n");
}



# -- Playlist: adding / removing songs

#
# $pl-&gt;add( $path [, $path [...] ] );
#
# Add the songs identified by $path (relative to MPD's music directory) to
# the current playlist. No return value.
#
sub add {
    my ($self, @pathes) = @_;
    my $command =
          "command_list_begin\n"
        . join( '', map { s/"/\\"/g; qq[add "$_"\n] } @pathes )
        . "command_list_end\n";
    $self-&gt;_mpd-&gt;_send_command( $command );
}


#
# $pl-&gt;delete( $song [, $song [...] ] );
#
# Remove song number $song (starting from 0) from the current playlist. No
# return value.
#
sub delete {
    my ($self, @songs) = @_;
    my $command =
          "command_list_begin\n"
        . join( '', map { s/"/\\"/g; "delete $_\n" } @songs )
        . "command_list_end\n";
    $self-&gt;_mpd-&gt;_send_command( $command );
}


#
# $pl-&gt;deleteid( $songid [, $songid [...] ]);
#
# Remove the specified $songid (as assigned by mpd when inserted in playlist)
# from the current playlist. No return value.
#
sub deleteid {
    my ($self, @songs) = @_;
    my $command =
          "command_list_begin\n"
        . join( '', map { "deleteid $_\n" } @songs )
        . "command_list_end\n";
    $self-&gt;_mpd-&gt;_send_command( $command );
}


#
# $pl-&gt;clear;
#
# Remove all the songs from the current playlist. No return value.
#
sub clear {
    my ($self) = @_;
    $self-&gt;_mpd-&gt;_send_command("clear\n");
}


#
# $pl-&gt;crop;
#
#  Remove all of the songs from the current playlist *except* the current one.
#
sub crop {
    my ($self) = @_;

    my $status = $self-&gt;_mpd-&gt;status;
    my $cur = $status-&gt;song;
    my $len = $status-&gt;playlistlength - 1;

    my $command =
          "command_list_begin\n"
        . join( '', map { $_  != $cur ? "delete $_\n" : '' } reverse 0..$len )
        . "command_list_end\n";
    $self-&gt;_mpd-&gt;_send_command( $command );
}


# -- Playlist: changing playlist order

#
# $pl-&gt;shuffle();
#
# Shuffle the current playlist. No return value.
#
sub shuffle {
    my ($self) = @_;
    $self-&gt;_mpd-&gt;_send_command("shuffle\n");
}


#
# $pl-&gt;swap( $song1, $song2 );
#
# Swap positions of song number $song1 and $song2 in the current playlist.
# No return value.
#
sub swap {
    my ($self, $from, $to) = @_;
    $self-&gt;_mpd-&gt;_send_command("swap $from $to\n");
}


#
# $pl-&gt;swapid( $songid1, $songid2 );
#
# Swap the postions of song ID $songid1 with song ID $songid2 in the
# current playlist. No return value.
#
sub swapid {
    my ($self, $from, $to) = @_;
    $self-&gt;_mpd-&gt;_send_command("swapid $from $to\n");
}


#
# $pl-&gt;move( $song, $newpos );
#
# Move song number $song to the position $newpos. No return value.
#
sub move {
    my ($self, $song, $pos) = @_;
    $self-&gt;_mpd-&gt;_send_command("move $song $pos\n");
}


#
# $pl-&gt;moveid( $songid, $newpos );
#
# Move song ID $songid to the position $newpos. No return value.
#
sub moveid {
    my ($self, $song, $pos) = @_;
    $self-&gt;_mpd-&gt;_send_command("moveid $song $pos\n");
}


# -- Playlist: managing playlists

#
# $pl-&gt;load( $playlist );
#
# Load list of songs from specified $playlist file. No return value.
#
sub load {
    my ($self, $playlist) = @_;
    $self-&gt;_mpd-&gt;_send_command( qq[load "$playlist"\n] );
}


#
# $pl-&gt;save( $playlist );
#
# Save the current playlist to a file called $playlist in MPD's playlist
# directory. No return value.
#
sub save {
    my ($self, $playlist) = @_;
    $self-&gt;_mpd-&gt;_send_command( qq[save "$playlist"\n] );
}


#
# $pl-&gt;rm( $playlist )
#
# Delete playlist named $playlist from MPD's playlist directory. No
# return value.
#
sub rm {
    my ($self, $playlist) = @_;
    $self-&gt;_mpd-&gt;_send_command( qq[rm "$playlist"\n] );
}



1;

__END__


=head1 NAME

Audio::MPD::Playlist - an object to mess MPD's playlist


=head1 SYNOPSIS

    my $song = $mpd-&gt;playlist-&gt;randomize;


=head1 DESCRIPTION

C&lt;Audio::MPD::Playlist&gt; is a class meant to access &amp; update MPD's
playlist.


=head1 PUBLIC METHODS

=head2 Constructor

=over 4

=item new( $mpd )

This will create the object, holding a back-reference to the C&lt;Audio::MPD&gt;
object itself (for communication purposes). But in order to play safe and
to free the memory in time, this reference is weakened.

Note that you're not supposed to call this constructor yourself, an
C&lt;Audio::MPD::Playlist&gt; is automatically created for you during the creation
of an C&lt;Audio::MPD&gt; object.

=back


=head2 Retrieving information

=over 4

=item $pl-&gt;as_items()

Return an array of C&lt;Audio::MPD::Common::Item::Song&gt;s, one for each of the
songs in the current playlist.


=item $pl-&gt;items_changed_since( $plversion )

Return a list with all the songs (as AMC::Item::Song objects) added to
the playlist since playlist $plversion.


=back


=head2 Adding / removing songs

=over 4

=item $pl-&gt;add( $path [, $path [...] ] )

Add the songs identified by C&lt;$path&gt; (relative to MPD's music directory) to the
current playlist. No return value.


=item $pl-&gt;delete( $song [, $song [...] ] )

Remove song number C&lt;$song&gt;s (starting from 0) from the current playlist. No
return value.


=item $pl-&gt;deleteid( $songid [, $songid [...] ] )

Remove the specified C&lt;$songid&gt;s (as assigned by mpd when inserted in playlist)
from the current playlist. No return value.


=item $pl-&gt;clear()

Remove all the songs from the current playlist. No return value.


=item $pl-&gt;crop()

Remove all of the songs from the current playlist *except* the
song currently playing.


=back


=head2 Changing playlist order

=over 4

=item $pl-&gt;shuffle()

Shuffle the current playlist. No return value.


=item $pl-&gt;swap( $song1, $song2 )

Swap positions of song number C&lt;$song1&gt; and C&lt;$song2&gt; in the current
playlist. No return value.


=item $pl-&gt;swapid( $songid1, $songid2 )

Swap the postions of song ID C&lt;$songid1&gt; with song ID C&lt;$songid2&gt; in the
current playlist. No return value.


=item $pl-&gt;move( $song, $newpos )

Move song number C&lt;$song&gt; to the position C&lt;$newpos&gt;. No return value.


=item $pl-&gt;moveid( $songid, $newpos )

Move song ID C&lt;$songid&gt; to the position C&lt;$newpos&gt;. No return value.


=back


=head2 Managing playlists

=over 4

=item $pl-&gt;load( $playlist )

Load list of songs from specified C&lt;$playlist&gt; file. No return value.


=item $pl-&gt;save( $playlist )

Save the current playlist to a file called C&lt;$playlist&gt; in MPD's playlist
directory. No return value.


=item $pl-&gt;rm( $playlist )

Delete playlist named C&lt;$playlist&gt; from MPD's playlist directory. No
return value.


=back


=head1 SEE ALSO

L&lt;Audio::MPD&gt;


=head1 AUTHOR

Jerome Quelin, C&lt;&lt; &lt;jquelin at cpan.org&gt; &gt;&gt;


=head1 COPYRIGHT &amp; LICENSE

Copyright (c) 2007 Jerome Quelin, all rights reserved.

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut
</pre></body></html>