#!/usr/bin/python

import os
import sys
import string
import gtk
import mix
import time

# main class for the gui, other classes handle diffrent sub functions such as Mail and Contacts
class Announce:
	
	def OnTimeout(self):
		old_volume = mix.getvolume()
		if os.system("test -x /usr/bin/bl") == 0:
			os.system("/usr/bin/bl off")
		
		vol = 90
		mix.setvolume(vol)
		
		if os.system("test -x /usr/bin/madplay") == 0 and os.system("test -e /usr/share/sounds/alarm.mp3") == 0:
			os.system("/usr/bin/madplay /usr/share/sounds/alarm.mp3")
		
		
		if os.system("test -x /usr/bin/bl") == 0:
			os.system("/usr/bin/bl on")
		
		mix.setvolume(old_volume)
		return 1
      	
      	def ok_clicked(self, *args):
		if os.system("test -x /usr/bin/bl") == 0:
			os.system("/usr/bin/bl on")
		gtk.mainquit()
		return gtk.FALSE

	def snooze_clicked(self, *args):
		if os.system("test -x /usr/bin/bl") == 0:
			os.system("/usr/bin/bl on")
		alarm_text=self.label.get()
		alarm_time_weird = tuple(time.localtime(time.time()))
		alarm_hour = str(time.strftime("%H", alarm_time_weird))
		alarm_minute = int(time.strftime("%M", alarm_time_weird))
		if (alarm_minute+5) <= 60:
			alarm_minute = str(alarm_minute+5)
		else:
			alarm_hour = str(int(alarm_hour)+1)
			alarm_minute = str(alarm_minute-55)
		alarm_second = str(time.strftime("%S", alarm_time_weird))
		alarm_year = str(time.strftime("%Y", alarm_time_weird))
		alarm_month = str(time.strftime("%m", alarm_time_weird))
		alarm_day = str(time.strftime("%d", alarm_time_weird))
		alarm_date = alarm_year + '-' + alarm_month + '-' + alarm_day
		alarm_time = alarm_hour + ':' + alarm_minute + ':' + alarm_second
		alarm_cmd = '\"export DISPLAY=:0.0;/usr/bin/announce ' + '\'' + alarm_text + '\' \"'
		uscheduleidcmd = '/usr/bin/uschedulecmd ' + alarm_cmd
		print(uscheduleidcmd)
		uschedule_id_list = string.split(os.popen(uscheduleidcmd).read())
		print(uschedule_id_list)
		uschedule_id = str(uschedule_id_list[7])
		uschedule_cmd = '/usr/bin/uschedule ' + uschedule_id  + ' \'' + alarm_date + ' ' + alarm_time + '\''
		os.system(uschedule_cmd)
		gtk.mainquit()
		return gtk.FALSE

	def __init__(self,announcetext):
		
		# the main gtk window
	        window=gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
	        window.set_title("Announce")
		window.set_usize(240,100)
		window.set_policy(gtk.FALSE, gtk.TRUE, gtk.FALSE)
		
		#the main vbox
		vbox=gtk.GtkVBox()
		window.add(vbox)
		
		#the announcement label
		label=gtk.GtkLabel()
		vbox.pack_start(label, gtk.FALSE, gtk.FALSE, 0)
		label.set_text(announcetext)
		label.show()
		self.label=label
		
		#the button hbox
		hbox=gtk.GtkHBox()
		vbox.pack_start(hbox, gtk.FALSE, gtk.FALSE, 0)
		
		okbutton=gtk.GtkButton("OK")
		hbox.pack_start(okbutton, gtk.TRUE, gtk.TRUE, 0)
		okbutton.connect("clicked", self.ok_clicked, window)
		okbutton.show()
		self.okbutton=okbutton
		
		snoozebutton=gtk.GtkButton("Snooze")
		hbox.pack_start(snoozebutton, gtk.TRUE, gtk.TRUE, 0)
		snoozebutton.connect("clicked", self.snooze_clicked, window)
		snoozebutton.show()
		self.snoozebutton=snoozebutton
		
		hbox.show()
		self.hbox=hbox
		
		vbox.show()
		self.vbox=vbox
		
		window.show()
		self.window=window
		
		self.timer = gtk.timeout_add(1000, self.OnTimeout)
		
	def quit(self, *args):
		gtk.mainquit()

def main(argv):
	if len(argv) >= 2:
        	announcetext = str(argv[1])
    	else:
        	announcetext = "You have an appointment!"
	announce = Announce(announcetext)
	gtk.mainloop()
	
if __name__ == '__main__':
	main(sys.argv)
