#!/usr/bin/env perl
# Copyright 2005 İsmail Dönmez (Resistence is futile, turn on unicode!)
# Licensed under GPLv2 or later at your option
#
# One media command to rule them all, inspired from Kopete's now listening plugin

use strict;
use warnings;

my $port=shift;
my $server=shift;
my $target=shift;

# Variables
my $amarok;
my $kaffeine;
my $juk;
my $noatun;
my $title;
my $artist;
my $album;
my $error;

sub check_running 
{
    my $application=shift;
    
    if (`dcop $application`)
    {
	return 1;
    }
    else
    {
	return 0;
    }
}

sub pretty_print
{
    my $title=shift;
    my $artist=shift;
    my $album=shift;

    utf8::decode($title);
    utf8::decode($artist);
    utf8::decode($album);

    if($artist && $album && $artist ne $album)
    {
	return "/me is playing \x{266B} $title \x{266B} by $artist on $album";
    }
    elsif($artist)
    {
	return "/me is playing \x{266B} $title \x{266B} by $artist";
    }
    elsif($album)
    {
	return "/me is playing \x{266B} $title \x{266B} on $album";
    }
    else
    {
	return "/me is playing $title";
    }
}

$amarok=check_running("amarok");
$juk=check_running("juk");
$kaffeine=check_running("kaffeine");
$noatun=check_running("noatun");

if($amarok)
{
    $title=`dcop amarok player title`;
    $artist=`dcop amarok player artist`;
    $album=`dcop amarok player album`;
       
    chomp $title;
    chomp $artist;
    chomp $album;

    if($title)
    {
	my $string = pretty_print($title,$artist,$album)." [amaroK]";
	exec 'dcop', $port, 'Konversation', 'say', $server, $target, $string;
    }
    else
    {
	$error="amaroK";
    }
}

if($juk)
{
    $title=`dcop juk Player trackProperty Title`;
    $artist=`dcop juk Player trackProperty Artist`;
    $album=`dcop juk Player trackProperty Album`;
    
    if($title)
    {
	my $string = pretty_print($title,$artist,$album)." [JuK]";
	exec 'dcop', $port, 'Konversation', 'say', $server, $target, $string;
    }
    else
    {
	if($error)
	{
	    $error=join(',',$error,"JuK");
	}
	else
	{
	    $error="JuK";
	}
    }
}

if($kaffeine)
{
    my $string=`dcop kaffeine KaffeineIface title`;
    chomp $string;
    
    if($string)
    {
	utf8::decode($string);
	exec 'dcop', $port, 'Konversation', 'say', $server, $target, "/me is playing \x{266B} $string \x{266B} \[Kaffeine\]";
    }
    else
    {
	if($error)
	{
	    $error=join(',',$error,"Kaffeine");
	}
        else
        {
            $error="Kaffeine";
        }
    }
}

if($noatun)
{
    my $string=`dcop noatun Noatun title`;
    chomp $string;

    if($string)
    {
	utf8::decode($string);
	exec 'dcop', $port, 'Konversation', 'say', $server, $target, "/me is playing \x{266B} $string \x{266B} \[Noatun\]";
    }
    else
    {
	if($error)
	{
	    $error=join(',',$error,"Noatun");
	}
        else
        {
            $error="Noatun";
        }
    }
}

if(!$amarok && !$juk && !$kaffeine && !$noatun)
{
	exec 'dcop', $port, 'Konversation', 'error', 'No supported media player is running';
}

if($error)
{
	exec 'dcop', $port, 'Konversation', 'error', "Nothing is playing in $error";
}
