#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 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

import os
import sys
from subprocess import *

global port;
global server;
global target;

def check_running(application):
    running = Popen(["dcop", application+'*'], stdout=PIPE).communicate()[0]
    if type(running) is list:
        return running[0].strip()
    return running.strip()

def pretty_print(title, artist='', album=''):
    if title.decode('utf-8'):
        title = title.decode('utf-8')
    if artist.decode('utf-8'):
        artist = artist.decode('utf-8')
    if album.decode('utf-8'):
        album = album.decode('utf-8')

    lang = os.environ.get("LANG")
    lcall = os.environ.get("LC_ALL")

    if (artist and album ) and (artist != album):
        return "/me is listening to \"%s\" by %s on %s"%(title,artist,album)
    elif artist:
        return "/me is listening to \"%s\" by %s"%(title,artist)
    elif album:
        return "/me is listening to \"%s\" on %s"%(title,album)
    else:
        return "/me is listening to \"%s\" "%(title)


if __name__ == "__main__":
    port = sys.argv[1]
    server = sys.argv[2]
    target = sys.argv[3]
    bad_apps = None
    error = None

    amarok = check_running('amarok')
    if amarok:
        title = os.popen('dcop '+amarok+' player title').readline().rstrip('\n')
        artist = os.popen('dcop '+amarok+' player artist').readline().rstrip('\n')
        album = os.popen('dcop '+amarok+' player album').readline().rstrip('\n')

        if title:
            string = pretty_print(title,artist,album)+' [amaroK]'
            Popen(['dcop', port, 'Konversation', 'say', server, target, string]).communicate()
            sys.exit(0)
        else:
            error = True
            bad_apps = ['amaroK']

    juk = check_running('juk')
    if juk:
        title = os.popen('dcop '+juk+' Player trackProperty Title').readline().rstrip('\n')
        artist = os.popen('dcop '+juk+' Player trackProperty Artist').readline().rstrip('\n')
        album = os.popen('dcop '+juk+' Player trackProperty Album').readline().rstrip('\n')

        if title:
            string = pretty_print(title,artist,album)+' [JuK]';
            Popen(['dcop', port, 'Konversation', 'say', server, target, string]).communicate()
            sys.exit(0)
        else:
            if error:
                bad_apps.append('JuK')
            else:
                error = True
                bad_apps = ['JuK']

    kaffeine = check_running('kaffeine')
    if kaffeine:        
        title = os.popen('dcop '+kaffeine+' KaffeineIface title').readline().rstrip('\n')
        artist = os.popen('dcop '+kaffeine+' KaffeineIface artist').readline().rstrip('\n')
        album = os.popen('dcop '+kaffeine+' KaffeineIface album').readline().rstrip('\n') 
       
        if title:
            string=pretty_print(title,artist,album)+' [Kaffeine]' 
            Popen(['dcop', port, 'Konversation', 'say', server, target, string]).communicate()
            sys.exit(0)
        else:
            if error:
                bad_apps.append('Kaffeine')
            else:
                error = True
                bad_apps = ['Kaffeine']

    noatun = check_running('noatun')
    if noatun:
        string = os.popen('dcop '+noatun+' Noatun title').readline().rstrip('\n');

        if string:
            string=pretty_print(string)+' [Noatun]'
            Popen(['dcop', port, 'Konversation', 'say', server, target, string]).communicate();
            sys.exit(0)
        else:
            if error:
                bad_apps.append('Noatun')
            else:
                error = True
                bad_apps = ['Noatun']

    yammi = check_running('yammi')
    if yammi:
        title = os.popen('dcop '+yammi+' YammiPlayer songTitle').readline().rstrip('\n')
        artist = os.popen('dcop '+yammi+' YammiPlayer songArtist').readline().rstrip('\n')
        album = os.popen('dcop '+yammi+' YammiPlayer songAlbum').readline().rstrip('\n')

        if title:
            string = pretty_print(title,artist,album)+' [Yammi]';
            Popen(['dcop', port, 'Konversation', 'say', server, target, string]).communicate()
            sys.exit(0)
        else:
            if error:
                bad_apps.append('Yammi')
            else:
                error = True
                bad_apps = ['Yammi']
                                                                                            
        
    if not amarok and not juk and not kaffeine and not noatun and not yammi:
        os.popen("dcop %s Konversation error \"No supported media player is running\""%(port))

    if bad_apps != None:
        error = ','.join(bad_apps)
        os.popen("dcop %s Konversation error \"Nothing is playing in %s\""%(port,error))
        sys.exit(1)

