#!/usr/bin/perl
#
use DCOP;

$options{'help_text'} = 'XChat-2 Kaffeine remote controller by John Holden.
Calling "/kaffeine" will announce what Kaffeine is currently playing.
Supported options (type /kaffeine <option>):
  next  - play next item in playlist
  pause - pause/play
  mute  - toggle mute on/off
  help  - display this help
  quit  - close Kaffeine
  openurl, url - open specified URL
  playdvd, opendvd, dvd, dvd://   - play a DVD
  playaudiocd, audiocd, cd, cd:// - play a CD
';

Xchat::register( "kaffeine", "0.3", "A simple plugin for Kaffeine users.");

Xchat::hook_command( "kaffeine", read_command, %options);

sub read_command {
  $client = new DCOP();
  $client->attach();
  $kaffeine = $client->createObject("kaffeine", "KaffeineIface");
  if (!kaffeine) {
    Xchat::print("Kaffeine is not running.\n");
  }

  $arg1 = $_[0][1] or undef;
  $arg2 = $_[0][2] or undef;
  if ($arg1 =~ m/(next)|n/i) {
    &next();
  }
  elsif ($arg1 =~ m/(previous)/i) {
    &previous();
  }
  elsif ($arg1 =~ m/(quit)|q/i) {
    &quit();
  }
  elsif ($arg1 =~ m/^pause$/i) {
    &pause();
  }
  elsif ($arg1 =~ m/^play$/i) {
    &play();
  }
  elsif ($arg1 =~ m/(mute)|m/i) {
    &mute();
  }
  elsif ($arg1 =~ m/(playdvd)|(opendvd)|(dvd)|(dvd:\/\/)/i) {
    &playDVD();
  }
  elsif ($arg1 =~ m/(playaudiocd)|(audiocd)|(cd)|(cd:\/\/)/i) {
    &playAudioCD();
  }
  elsif ($arg1 =~ m/(openurl)|(url)/i) {
    &openURL($arg2);
  }
  elsif ($arg1 =~ m/(help)|h/i) {
    &help();
  }
  else {
    &getPlaying();
  }
}

sub getPlaying {
  $nick = Xchat::get_info( "nick" );
  if ($kaffeine->isPlaying()) {
    $title = $kaffeine->title();
    Xchat::print($nick." is currently listening to ".$title."\n");
  }
  else {
    Xchat::print($nick." is not listening to anything right now.\n");
  }
}

sub next {
  if ($kaffeine->isPlaying()) {
    $kaffeine->next();
    &getPlaying();
  }
  else {
    $kaffeine->play();
    $kaffeine->next();
    &getPlaying();
  }
}

sub previous {
  if ($kaffeine->isPlaying()) {
    $kaffeine->previous();
    &getPlaying();
  }
  else {
    $kaffeine->play();
    $kaffeine->previous();
    &getPlaying();
  }
}

sub quit {
  $kaffeine->quit();
}

sub mute {
  $kaffeine->mute();
}

sub pause {
  $kaffeine->pause();
  Xchat::print("Pause/Play\n");
}

sub play {
  if ($kaffeine->isPlaying()) {
    &getPlaying();
  }
  else {
    $kaffeine->play();
    &getPlaying();
  }
}

sub playDVD {
  $kaffeine->playDVD();
  &getPlaying();
}

sub playAudioCD {
  $kaffeine->playAudioCD();
  &getPlaying();
}

sub openURL {
  my $content = shift or undef;
  if ($content) {
    $kaffeine->openURL($content);
    &getPlaying();
  }
  else {
    &help();
  }
}

sub help {
  Xchat::print( $options{'help_text'} );
}

__END__

=head1 NAME

XChat-2 Kaffeine Remote Control

=head1 SYNOPSIS

 This program uses Kaffeine's DCOP interface to remotely control Kaffeine
 through XChat-2.

=head1 DESCRIPTION

 Simply copy this file into ~/.xchat2/ and restart XChat-2 to use this program.
 Start Kaffeine, if it isn't already running, and use the /kaffeine command to
 control Kaffeine through XChat-2. Type '/kaffeine help' for a list of
 supported commands.

=head1 SEE ALSO

 http://xchat.org/docs/xchat2-perl.html
 Kaffeine's provided DCOP documentation

=head1 AUTHOR

 John Holden (wjholden@users.sourceforge.net)

=head1 VERSION

 0.3

=head1 BUGS

 - if paused, Kaffeine does not 'unpause' if you use the 'play' function.
 - /help kaffeine doesn't work even though I used the %options hash...no biggie
 - if you quit Kaffeine, a ton of errors are thrown once Perl realises it can't
   access the DCOP Kaffeine object anymore. This doesn't cause any problems,
   but it's a mess graphically.
 - it isn't possible to launch Kaffeine, as far as I know, without running into
   problems with the operating system treating Kaffeine as a child of X-Chat
   and restricting how much memory it can use.

=cut
