#!/usr/bin/env python

import dbus
import xchat
import string

__module_name__ = "BMPx XChat"
__module_description__ = "BMPx Python Module for XChat 2"
version = "0.1"

# Set addbuttons to 1 if you want buttons to be added or 0 if don't want them
addbuttons = 1

# Variables used by the script. Don't touch these
savedvol = 0
muted = 0

class BMPXChat:
    def __init__(self):
	self.bus = dbus.SessionBus()
	self.bmpx_obj = self.bus.get_object("org.mpris.bmpx", "/org/mpris/bmpx/SystemControl")
	self.bmpx = dbus.Interface(self.bmpx_obj, "org.mpris.bmpx")

    def showPlaying(self, color):

	uri = self.bmpx.GetCurrentUri()
	metadata = self.bmpx.GetMetadataForUri(uri)

	tracknumber = self.bmpx.GetCurrentTrack()
	listidx = self.bmpx.GetCurrentTracklistNumber()
	listname = self.bmpx.GetTracklistName (listidx) 
	samplerate = float(metadata.get('samplerate','0'))

	if samplerate != 0:
	    samplerate = samplerate/1000.0

	if color == 1:
		xchat.command ("me \00314is playing\00314 ::[\0039 %d\00315 |\0033 %s\00315 |\00310 %d kbit/s\00315 |\00310 %.1f KHz\00314 ]::\0037" % (tracknumber+1, self.bmpx.GetCurrentTitle(), metadata.get('bitrate',0), samplerate))
	else:
		xchat.command ("me is playing ::[ %d | %s | %d kbit/s | %.1f KHz ]::" % (tracknumber+1, self.bmpx.GetCurrentTitle(), metadata.get('bitrate',0), samplerate))

    def xchatcommand_cb(self, word, word_eol, userdata):
	global savedvol, muted
        if len(word) < 2:
	    self.showPlaying(1)
            return xchat.EAT_ALL
        else:
	    if  word[1] == "uri":
		uri = self.bmpx.GetCurrentUri()
		xchat.prnt ("Current URI is: " + uri)
	    elif word[1] == "urim":
		uri = self.bmpx.GetCurrentUri()
		metadata = self.bmpx.GetMetadataForUri(uri)
		xchat.prnt (str(metadata))
            elif word[1] == "next":
                self.bmpx.PlayNext()
            elif word[1] == "prev":
                self.bmpx.PlayPrev()
            elif word[1] == "pause":
                self.bmpx.PlayPause()
            elif word[1] == "stop":
                self.bmpx.PlayStop()
	    elif word[1] == "play":
                self.bmpx.PlayCurrent()
	    elif word[1] == "vol":
                self.bmpx.VolumeSet(int(word[2]))
		xchat.prnt ("BMPx Volume: " + word[2])
	    elif word[1] == "nc":
	    	self.showPlaying(0)
	    elif word[1] == "mute":
		if muted == 0:
                    savedvol = int( self.bmpx.VolumeGet() )
                    self.bmpx.VolumeSet(int(0))
		    muted = 1
		else:
                    self.bmpx.VolumeSet(int(savedvol))
		    muted = 0
            else:
                print "Unknown command for /bmp, see /help bmp for help"
	    return xchat.EAT_ALL


m = BMPXChat()

xchat.hook_command("bmp", m.xchatcommand_cb, help="\00311Usage:\0038/bmp <arg>\n"
		"\00311Where <arg> is one of the following:\n"
		"\0039uri\0030\t \0037Show the URI of current song\n"
		"\0039urim\0030\t \0037Show full metadata of current song\n"
		"\0039next\0030\t \0037Skip to the next song\n"
		"\0039prev\0030\t \0037Skip to the previous song\n"
		"\0039pause\0030\t \0037Pause playback\n"
		"\0039stop\0030\t \0037Stop playback\n"
		"\0039play\0030\t \0037Start playback\n"
		"\0039mute\0030\t \0037Mute/Unmute\n"
		"\0039vol <num>\0030\t \0037Set volume to the given percentage (1-100)\n"
		"\0039nc\0030\t \0037Show info about the current track without using colors\n\n"
		"\00311If none of the above is used then it shows info about the current track")
if addbuttons == 1:
    xchat.command("delbutton bmp")
    xchat.command("delbutton \"bmp nc\"")
    xchat.command("delbutton next")
    xchat.command("delbutton prev")
    xchat.command("delbutton pause")
    xchat.command("delbutton stop")
    xchat.command("delbutton play")
    xchat.command("delbutton mute")
    xchat.command("addbutton bmp bmp")
    xchat.command("addbutton \"bmp nc\" bmp nc")
    xchat.command("addbutton next bmp next")
    xchat.command("addbutton prev bmp prev")
    xchat.command("addbutton pause bmp pause")
    xchat.command("addbutton stop bmp stop")
    xchat.command("addbutton play bmp play")
    xchat.command("addbutton mute bmp mute")

print "bmp-xchat v" + version + " loaded."

