#!/usr/bin/false
package amaroK; 

our $dcop;
our $format;

sub new(@) {
    my $self = shift();
    bless \$self, "amaroK";

    ($format, $dcop) = @_;

    $dcop = "/usr/bin/dcop"     unless ($dcop);
    die "Can't find dcop!"      unless (`$dcop`);
    die "amaroK isn't running?" unless (`$dcop amarok 2> /dev/null`);

    return $self;
}

sub get {
    my $status = `$dcop amarok default status`;
    if(!$status || $status==0) {
        die "amaroK isn't playing anything!";
    } elsif($status==1) {
        $status = "paused";
    } else {
        $status = "playing";
    }

    $_ = $format;
    s/\{status\}/$status/;
    while(m/(\{([a-zA-Z]+?)\})/) {
        # get infos, stderr to /dev/null
        my $returnval = `$dcop amarok default $2 2> /dev/null`;

        # remove spaces
        chomp $returnval;

        # replace {foo} with bar
        s/$1/$returnval/;
    }
    return $_;
}

sub set(@) {
    $format = shift;
}

sub command(@) {
    (my $self, $_) = @_;
    if(/^[a-zA-Z \d]+$/i) {
        # execute command, stderr to /dev/null
        return `$dcop amarok default $_ 2> /dev/null`;
    } else {
        die "Unknown command!";
    }
}

1;

__END__

=head1 NAME

amaroK - Module for controlling amaroK-player

=head1 SYNOPSIS

With this Perl-module you can easily get information from amaroK
and control amaroK. You can get information with get()-method and
execute commands with command()-method.

Usage:

    #!/usr/bin/perl -w
    use strict;
    use amaroK;
    #!! or
    #require "/path/to/amaroK.pm";

    my $dcop = "/usr/bin/dcop";
    my $format = "{artist} - {title} ( {currentTime}/{totalTime} )";

    my $amarok = new amaroK($format, $dcop);
    print $amarok->get ."\n";

    # execute a couple of commands
    $amarok->command( "next" );
    $amarok->command( "setVolume 50" );

=head1 DESCRIPTION

With this Perl-module you can easily get information from amaroK
and control amaroK. You can get information with get()-method and
execute commands with command()-method. Before you can use get()-method
you have to set format for output, you can set it with set().

=head2 METHODS

=over 3

=item new( $format, [$dcopbin] )

 $format - See set()-method
 $dcopbin - /path/to/your/dcop, default is /usr/bin/dcop

=item get( )

If C<$format> is set to "C<{artist} - {title} ( 
{currentTime}/{totalTime} )>" it returns:
C<David Lee Roth - Yankee Rose ( 0:37/3:49 )>

=item set( $format )

Set output format to C<$format>. Syntax is C<{keyword}>.
List of keywords and what they return.

  nowPlaying
    David Lee Roth - Yankee Rose
  artist
    David Lee Roth
  title
    Yankee Rose
  track
    14
  album
    GTA Vice City Radio - V-Rock
  totalTime
    3:49
  currentTime
    0:37
  genre
    Soundtrack
  year
    2002
  comment
    Track 14  
  bitrate
    214 kbps
  sampleRate
    44100
  getVolume ( between 0 and 100 )
    50

=item command( $command )

You can control amaroK with this method. You can execute some commands
like "next". If function takes parameters syntax is this: C<$amarok->command( 
"setVolume 51" );>. Method returns anything printed in stdout.

Following commands are available:

  play
  playPause
  stop
  next
  prev
  pause
  scanCollection
  volumeUp
  volumeDown
  mute
  configEqualizer
  showOSD
  enableOSD(bool enable)
  seek(int s)
  seekRelative(int s)
  enableRandomMode(bool enable)
  setEqualizerEnabled(bool active)
  setEqualizer(int,int,int,int,int,int,int,int,int,int,int)
  equalizerEnabled
  isPlaying
  ... and anything listed after set()-method

=back 3

=head1 AUTHOR

 Markus "Linkku" Lindqvist

=cut

