#!/usr/bin/env python
#
# part of
# re.codec is not a real codec
#
#
# (c) 2004 <j@reboot.fm>
#
from time import *
from os import system
from urllib2 import urlopen
from pdi.icalendar import VCalendar,ICalendar
import pdi.parser

import pygtk; pygtk.require("2.0")
import gtk
import gtk.glade
import pango

ics_url="http://localhost/current.ics"

class UpcomingShowsControl(gtk.Label):
	def __init__(self):
		gtk.Label.__init__(self, "")  # invoke the super constructor
	
		gtk.timeout_add(1000, self.__update)  # a tick every second
		gtk.timeout_add(1000, self.__update_meta) 
		self.text="loading..."

	def __update(self):
		self.set_markup('<span size="12000">%s</span>' % self.text)
		return gtk.TRUE
	def __update_meta(self):
		global ics_url
		try:
			ics_fh=urlopen(ics_url)
			calendar = pdi.parser.fromFileObject(ics_fh, ICalendar())
			ics_fh.close
		except:
			self.text="error! could not parse the ical file!"
			return gtk.TRUE
			
		self.text="no show anounced"
		
		icaltimeformat_utc="%Y%m%dT%H%M%SZ"
		icaltimeformat="%Y%m%dT%H%M%S"
		for show in calendar.components:
			try:
				next_show=strptime(show.properties['DTSTART'].getContent().strip(), icaltimeformat)
				next_show_end=strptime(show.properties['DTEND'].getContent().strip(), icaltimeformat)
			except:
				next_show=strptime(show.properties['DTSTART'].getContent().strip(), icaltimeformat_utc)
				next_show_end=strptime(show.properties['DTEND'].getContent().strip(), icaltimeformat_utc)
			if int(mktime(next_show_end)) > int(time()) and int(mktime(next_show)) <= int(time()):
				ntext = "%s  . " % show.properties['SUMMARY'].getContent().strip()
				ntext +="%s - %s . " % (strftime("%H:%M",next_show),strftime("%H:%M",next_show_end))
				togo=strftime("%H:%M:%S",gmtime(int(mktime(next_show_end))-int(time())))
				
				ntext +="to go %s\n" % togo
				self.text = ntext;
				
				
			return gtk.TRUE


class CountdownControl(gtk.Label):
	def __init__(self):
		gtk.Label.__init__(self, "")  # invoke the super constructor
		gtk.timeout_add(1000, self.__update)  # a tick every second
		self.countdown=5
	
	def run(self):
		self.countdown=5
		self.show()

	def __update(self):
		self.set_markup('<span size="120000">%s</span>' % self.countdown)
		if self.countdown == -1:
			self.countdown=5
			self.hide()
		else:
			self.countdown = self.countdown - 1
		return gtk.TRUE


class ClockControl(gtk.Label):
	def __init__(self):
		gtk.Label.__init__(self, "")  # invoke the super constructor
		gtk.timeout_add(1000, self.__update)  # a tick every second
	
	def __update(self):
		date = strftime("%H:%M:%S")
		self.set_markup('<span size="72000">%s</span>' % date)
		return gtk.TRUE


class rebootStatus:
	def close_application(self, widget):
		gtk.mainquit()

	def __update(self):
		if not int(strftime("%S")) % 11:
			self.countdown.run()	
		return gtk.TRUE
	
	def __init__(self):
		gtk.timeout_add(1000, self.__update)  # a tick every second
		window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		window.set_resizable(gtk.TRUE)  
		window.connect("destroy", self.close_application)
		window.set_title("reboot.fm Statusmonitor")
		window.set_border_width(0)
		#Try to make the window full screen / needs something to put it on top.
		window.set_position(gtk.WIN_POS_CENTER)
		window.set_size_request(gtk.gdk.screen_width(),gtk.gdk.screen_height())
		
		vbox=gtk.VBox(gtk.FALSE, 0)
		vbox.show()
		window.add(vbox) 
		
		vbox.set_border_width(2)
		
		hbox1 = gtk.HBox(gtk.FALSE, 0)
		hbox2 = gtk.HBox(gtk.FALSE, 0)


		clock = ClockControl()
		clock.show()
		vbox.pack_start(clock,gtk.FALSE,gtk.FALSE,0)
		vbox.pack_start(hbox1,gtk.FALSE,gtk.FALSE,0)
		vbox.pack_end(hbox2,gtk.TRUE,gtk.TRUE,0)
		
		upcomingShows=UpcomingShowsControl()
		upcomingShows.show()
		hbox1.add(upcomingShows)
		
		self.countdown = CountdownControl()
		hbox2.add(self.countdown)


		hbox1.show()
		hbox2.show()
		window.show()


if __name__ == "__main__":
	rebootStatus()
	gtk.main()
