# amarok_xchat_variant by christian herberger (aka kabarakh)
#
# script to display the song currently played in amarok
#
#
# when you changed a setting, please reload the script in xchat (if xchat was running while changing)

__module_name__ = "amarok_xchat_variant" 
__module_version__ = "0.5" 
__module_description__ = "python module for xchat to display facts about songs played" 
import xchat, commands

# set switch to True to display the progress bar
switch = True

# set bar_mode to 1, 2 or 3 to decide which progress bar to use:
#    1: 10% = one unit, length 10, 0-10% = 0, 11-20% = 1, 21-30% = 2, ...
#    2: 10% = one unit, length 10, 0-4% = 0, 5-14% = 1, 15-24% = 2, ...
#    3: 5% = one unit, length 20, 0-5% = 0, 6-10% = 1, 11-15% = 2, ...
bar_mode = 3

bar = ""

def print_version(word, word_eol, userdata):
    xchat.command('me uses Kabarakh\'s amarok_xchat_variant version 0.5, get it at http://kabarakh.pytalhost.de/blog/python-scripte/')
    return xchat.EAT_ALL

def bar1(percent):
    global bar
    i = 10
    while ( i <= 100 ):
        if percent >= i:
            bar = bar + "\002:\002"
        else:
            bar = bar + "-"
        i = i + 10
    bar = " \002progress\002[" + bar + " " + (str)(percent) + "%]"

def bar2(percent):
    global bar
    i = 5
    while ( i <= 95 ): 
        if percent >= i:
            bar = bar + "\002:\002"
        else:
            bar = bar + "-"
        i = i + 10
    bar = " \002progress\002[" + bar + " " + (str)(percent) + "%]"
    
def bar3(percent):
    global bar
    i = 5
    while ( i <= 100 ): 
        if percent >= i:
            bar = bar + "\002:\002"
        else:
            bar = bar + "-"
        i = i + 5
    bar = " \002progress\002[" + bar + " " + (str)(percent) + "%]"

def amarok_send(word, word_eol, userdata):
    global bar, switch, bar_mode
    if ( commands.getoutput('dcop amarok') == "No such application: 'amarok'" ) or ( commands.getoutput('dcop amarok') == "ERROR: Couldn't attach to DCOP server!" ):
        xchat.command('me isn\'t playing any song...')
    else: 
        if (commands.getoutput('dcop amarok player isPlaying')=="true"):
            if switch == True:
                percent = (int)((float)(commands.getoutput('dcop amarok player trackCurrentTime'))/(float)(commands.getoutput('dcop amarok player trackTotalTime')) * 100)
                if bar_mode == 1:
                    bar1(percent)
                elif bar_mode == 2:
                    bar2(percent)
                else:
                    bar3(percent)
                xchat.command('me is listening to \002title\002[' + commands.getoutput('dcop amarok player title') + "] \002artist\002[" + commands.getoutput('dcop amarok player artist') + "] \002album\002[" + commands.getoutput('dcop amarok player album') + "] \002duration\002["+ commands.getoutput('dcop amarok player currentTime') + "/" + commands.getoutput('dcop amarok player totalTime') + "]" + bar)
                bar = ""
        else: 
            xchat.command('me isn\'t playing any song...')
    return xchat.EAT_ALL

xchat.hook_command('amarok', amarok_send)
xchat.hook_command('amaversion', print_version)

xchat.prnt('amarok_xchat_variant v0.5 loaded...')
