#!/usr/bin/env python

import cgi
import dbus
import dbus.glib
import pygtk
pygtk.require('2.0')
import gtk
import gtk.glade
import pango
import gobject
import os
import sys

__module_name__ = "bmpx-monitor-small"
__module_description__ = "BMPx Python Status Watcher (Small Version)"

prefix = '/usr'

class BMPMonitor:
    def __init__(self):

	self.statusText = ''
	self.uri = ''
	self.metadata = []
	self.length = 0
	self.currentTrack = 0

	# Init Glade shizzle
	self.mainGlade = gtk.glade.XML(prefix + '/share/bmpx-clients/pygtk/bmpx_client.glade')

	self.windowMain = self.mainGlade.get_widget('window_main')
	self.windowMain.set_keep_above(1)
	self.windowMain.show_all()

	self.l_title  = self.mainGlade.get_widget('label_title')
	self.l_status = self.mainGlade.get_widget('label_status')
	self.s_volume = self.mainGlade.get_widget('hscale_volume')

	self.mainGlade.get_widget('button_next').connect ('clicked', self.bmpx_play_next) 
	self.mainGlade.get_widget('button_prev').connect ('clicked', self.bmpx_play_prev) 
	self.mainGlade.get_widget('button_stop').connect ('clicked', self.bmpx_play_stop) 
	self.mainGlade.get_widget('button_play').connect ('clicked', self.bmpx_play_current) 
	self.mainGlade.get_widget('button_pause').connect ('clicked', self.bmpx_play_pause) 
	self.mainGlade.get_widget('header_image').set_from_file (prefix + '/share/bmpx-clients/pygtk/bmpx-client-image.png') 
	self.mainGlade.get_widget('header_eventbox').connect ('button-press-event', self.bmpx_button_press)

	# colorLabelBG = gtk.gdk.color_parse('#45617b')
	# colorLabelBG = gtk.gdk.color_parse('#7f9ae7')
	# colorLabelBG = gtk.gdk.color_parse('#5e7cd2')
	colorLabelBG = gtk.gdk.color_parse('#494e5a')

	self.mainGlade.get_widget('eventbox_label').modify_bg(gtk.STATE_NORMAL, colorLabelBG)
	self.mainGlade.get_widget('eventbox_label').modify_base(gtk.STATE_NORMAL, colorLabelBG)

	# Init DBus shizzle
	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')

	self.bmpx.connect_to_signal('SetPlaystatus', self.bmpx_set_playstatus)
	self.bmpx.connect_to_signal('SetStreamPos',  self.bmpx_set_stream_pos)
	self.bmpx.connect_to_signal('SetVolume',     self.bmpx_set_volume)

	# Request status so we can initialize ourselves 
	self.bmpx.SendStatus()
	self.currentVolume = int( self.bmpx.VolumeGet() )
	self.s_volume.set_value (self.bmpx.VolumeGet())
	self.s_volume.connect ('value-changed', self.bmpx_volume_changed)
    
	#Volume fix by daelstorm, thanks!

    def bmpx_button_press(self, widget, event):
	if event.type == gtk.gdk._3BUTTON_PRESS:
	    gtk.main_quit()
	elif event.type == gtk.gdk.BUTTON_PRESS:
	    self.windowMain.begin_move_drag(event.button, event.x_root, event.y_root, long(event.time))
    
    def bmpx_volume_changed(self, widget):
	volume = int(widget.get_value())
	if self.currentVolume != volume:
		self.currentVolume = volume
		self.s_volume.set_value (volume)
		self.bmpx.VolumeSet (volume)

    def bmpx_play_current(self, widget):
	self.bmpx.PlayCurrent()

    def bmpx_play_next(self, widget):
	self.bmpx.PlayNext()

    def bmpx_play_prev(self, widget):
	self.bmpx.PlayPrev()

    def bmpx_play_pause(self, widget):
	self.bmpx.PlayPause()

    def bmpx_play_stop(self, widget):
	self.bmpx.PlayStop()

    def clear_info(self):
	self.l_title.set_text ('')
	self.l_title.set_sensitive (0)
	self.l_status.set_text ('STOPPED')

	return 1

    def update_info(self):
	self.l_title.set_sensitive (1)

	self.uri = self.bmpx.GetCurrentUri()
	self.currentTrack = self.bmpx.GetCurrentTrack()
	self.metadata = self.bmpx.GetMetadataForUri(self.uri)
	self.length = int(self.metadata['time'])

	artist = cgi.escape(self.metadata['artist'])
	title = cgi.escape(self.metadata['title'])
	album = cgi.escape(self.metadata['album'])
	artist_s = artist.strip()
	title_s = title.strip()
	album_s = album.strip()

	self.l_title.set_markup  ('<span size="small" weight="bold" foreground="#FFFFFF" rise="5000">(%d)  %s \'%s\' <i>(%s)</i></span>' % (self.currentTrack+1, artist_s, title_s, album_s))

	return 1

    def bmpx_set_volume(self, volume): 
	if self.s_volume.get_value() != int(volume):
		self.currentVolume = int(volume)
		self.s_volume.set_value (int(volume))

    def bmpx_set_stream_pos(self, stream_pos):
	minutes = int(stream_pos) / int(60)
	seconds = stream_pos % 60;

	minutes_length = int(self.length) / int(60)
	seconds_length = int(self.length) % int(60)

	self.l_status.set_markup('<b>%2.2d:%2.2d</b> / %2.2d:%2.2d' % (minutes, seconds, minutes_length, seconds_length)) 

    def bmpx_set_playstatus(self, playstatus):
	if playstatus == 2:	    #Stopped
	    self.clear_info()
	elif playstatus == 4:	    #Playing
	    self.statusText = 'PLAYING'
	    self.update_info()
	elif playstatus == 8:	    #Paused
	    self.l_title.set_sensitive (0)
	    self.statusText = 'PAUSED'

	return 1

m = BMPMonitor()

gtk.main()
