#!/usr/bin/env python

import gtk
import os
import sys
import datetime

# Internationalization
import locale
import gettext
import gtk.glade
gettext.bindtextdomain("gtkclocksetup", "/usr/share/locale")
gettext.textdomain("gtkclocksetup")
gettext.install("gtkclocksetup", "/usr/share/locale", unicode=1)
gtk.glade.bindtextdomain("gtkclocksetup", "/usr/share/locale")
gtk.glade.textdomain("gtkclocksetup")

def continents():
	continents = ['Africa', 'America', 'Antarctica', 'Asia', \
			'Atlantic', 'Australia', 'Europe', 'Indian', \
			'Pacific', 'US', 'Mexico', 'Chile', 'Mideast',
			'Canada', 'Brazil', 'Arctic', 'Etc']
	continents.sort()
	return continents

def alltimezones():
	zoneinfo = '/usr/share/zoneinfo/'
	availablecontinents = []
	timezones = []
	for i in continents():
		if os.path.isdir(zoneinfo+i):
			availablecontinents.append(i)
	availablecontinents.sort()
	
	for i in availablecontinents:
		for file in os.listdir(zoneinfo+i):
			if os.path.isdir(zoneinfo+i+'/'+file):
				for file2 in os.listdir(zoneinfo+i+'/'+file):
					timezones.append([i, file+'/'+file2])
			else:
				timezones.append([i, file])

	return timezones

def cities(continent):
	cities = []
	for i in alltimezones():
		if i[0] == continent:
			cities.append(i[1])
	cities.sort()
	return cities

def currenttimezone():
	tz = os.readlink('/etc/localtime-copied-from').replace('/usr/share/zoneinfo/', '')
	continent = tz.partition('/')[0]
	location = tz.partition('/')[2]
	return continent, location

def settimezone(continent, location):
	if not continent == currenttimezone()[0]:
		if not location == currenttimezone()[1]:
			os.system('ln -sf /usr/share/zoneinfo/'+continent+'/'+location+' /etc/localtime-copied-from')
			os.system('rm -f /etc/localtime')
			os.system('cp /etc/localtime-copied-from /etc/localtime')

def ntpstate():
	if os.access('/etc/rc.d/rc.ntpd', os.X_OK):
		state = True
	else:
		state = False
	return state

def ntppresent():
	if os.path.isfile('/etc/rc.d/rc.ntpd'):
		state = True
	else:
		state = False
	return state

def utcstate():
	utc = False
	try:
		f = file('/etc/hardwareclock')
		while True:
			line = f.readline()
			if len(line) == 0:
				break
			elif line.rstrip("\n") == 'localtime':
				utc = False
				break
			elif line.rstrip("\n") == 'UTC':
				utc = True
				break
		f.close()
	except IOError:
		setutc(False)
	return utc

def setutc(state):
	f = file('/etc/hardwareclock', 'w')
	if state == True:
		time = 'UTC'
	else:
		time = 'localtime'
	f.write('# /etc/hardwareclock\n')
	f.write('#\n')
	f.write('# Tells how the hardware clock time is stored.\n')
	f.write('# You should run (gtk)clocksetup or timeconfig to edit this file.\n\n')
	f.write(time+'\n')
	f.close()

def ntp_sync_now():
	os.system('sh /etc/rc.d/rc.ntpd sync')

def setntp(state):
	if state == True:
		os.system('service start ntpd')
	else:
		os.system('service stop ntpd')

class GTKClockSetup:
	def on_button_ok_clicked(self, widget, data=None):
		if not self.ntpcheckbutton.get_active == ntpstate():
			setntp(self.ntpcheckbutton.get_active())
			if self.ntpcheckbutton.get_active() == False:
				year = "%04d" % (self.calendar.get_date()[0])
				month = "%02d" % (self.calendar.get_date()[1]+1)
				day = "%02d" % (self.calendar.get_date()[2])
				hour = "%02d" % self.spinbutton_hrs.get_value()
				min = "%02d" % self.spinbutton_min.get_value()
				sec = "%02d" % self.spinbutton_sec.get_value()
				os.system('date +%Y%m%d -s "'+year+month+day+'"	> /dev/null')
				os.system('date +T -s "'+hour+':'+min+':'+sec+'" > /dev/null')
				os.system('hwclock --systohc')
		if not self.utccheckbutton.get_active() == utcstate():
			setutc(self.utccheckbutton.get_active())
		gtk.main_quit()

	def on_button_cancel_clicked(self, widget, data=None):
		gtk.main_quit()
	
	def gtk_main_quit(self, widget, data=None):
		gtk.main_quit()

	def on_button_timezone_clicked(self, widget, data=None):
		self.tzwindow.show()

	def on_button_sync_now_clicked(self, widget, data=None):
		ntp_sync_now()
		self.update_calendar()
		self.update_time()

	def on_tz_button_ok_clicked(self, widget, data=None):
		pos = self.continentlist.get_cursor()[0][0]
		selectedcontinent = continents()[pos]
		pos = self.locationlist.get_cursor()[0][0]
		selectedlocation = cities(selectedcontinent)[pos]
		self.update_timezone_label(selectedcontinent, selectedlocation)
		settimezone(selectedcontinent, selectedlocation)
		self.tzwindow.hide()

	def on_tz_button_cancel_clicked(self, widget, data=None):
		self.tzwindow.hide()

	def on_timezonewindow_delete_event(self, widget, event):
		self.tzwindow.hide()
		return True

	def on_continentlist_cursor_changed(self, widget, data=None):
		try:
			pos = self.continentlist.get_cursor()[0][0]
		except TypeError:
			pos = 0
		self.locationliststore.clear()
		selectedcontinent = continents()[pos]
		currenttz = currenttimezone()[1]
		count = 0
		set = False
		for i in cities(selectedcontinent):
			self.locationliststore.append([i])
			if i == currenttz:
				set = True
				self.locationlist.set_cursor(count)
				self.locationlist.scroll_to_cell(count)
			count += 1
			if set == False:
				self.locationlist.set_cursor(0)
				self.locationlist.scroll_to_cell(0)

	def update_timezone_label(self, continent, location):
		self.label_timezone.set_label(_('Time zone:')+'      '+continent+'/'+location)

	def on_ntpcheckbutton_toggled(self, widget, data=None):
		state = not self.ntpcheckbutton.get_active()
		self.label_date.set_sensitive(state)
		self.calendar.set_sensitive(state)
		self.label_time.set_sensitive(state)
		self.spinbutton_hrs.set_sensitive(state)
		self.spinbutton_min.set_sensitive(state)
		self.spinbutton_sec.set_sensitive(state)
		self.button_sync_now.set_sensitive(state)

	def update_calendar(self):
		now = datetime.datetime.now()
		self.calendar.select_month(now.month-1, now.year)
		self.calendar.select_day(now.day)

	def update_time(self):
		now = datetime.datetime.now()
		self.spinbutton_hrs.set_value(now.hour)
		self.spinbutton_min.set_value(now.minute)
		self.spinbutton_sec.set_value(now.second)

	def __init__(self):
		builder = gtk.Builder()
		if os.path.exists('gtkclocksetup.glade'):
			builder.add_from_file('gtkclocksetup.glade')
		elif os.path.exists('/usr/share/salixtools/gtkclocksetup/gtkclocksetup.glade'):
			builder.add_from_file('/usr/share/salixtools/gtkclocksetup/gtkclocksetup.glade')
		self.window = builder.get_object('gtkclocksetup')
		self.tzwindow = builder.get_object('timezonewindow')
		self.tzsyncwindow = builder.get_object('tzsyncwindow')
		self.label_timezone = builder.get_object('label_timezone')
		self.label_timezone.set_label(_('Time zone:')+'      '+currenttimezone()[0]+'/'+currenttimezone()[1])
		self.continentlist = builder.get_object('continentlist')
		self.continentcolumn = builder.get_object('continentcolumn')
		self.continentliststore = builder.get_object('continentliststore')
		self.locationlist = builder.get_object('locationlist')
		self.locationcolumn = builder.get_object('locationcolumn')
		self.locationliststore = builder.get_object('locationliststore')
		self.ntpcheckbutton = builder.get_object('ntpcheckbutton')
		self.button_sync_now = builder.get_object('button_sync_now')
		self.button_timezone = builder.get_object('button_timezone')
		self.label_date = builder.get_object('label_date')
		self.calendar = builder.get_object('calendar1')
		self.label_time = builder.get_object('label_time')
		self.spinbutton_hrs = builder.get_object('spinbutton_hrs')
		self.spinbutton_min = builder.get_object('spinbutton_min')
		self.spinbutton_sec = builder.get_object('spinbutton_sec')
		self.continentcolumn.set_title(_('General area'))
		self.locationcolumn.set_title(_('Location'))
		self.utccheckbutton = builder.get_object('utccheckbutton')
		self.utccheckbutton.set_active(utcstate())
		
		ntp = ntppresent()
		self.ntpcheckbutton.set_sensitive(ntp)
		self.button_sync_now.set_sensitive(not ntp)
		ntp = ntpstate()
		self.ntpcheckbutton.set_active(ntp)
		self.on_ntpcheckbutton_toggled(self)

		btn_sync_width = self.button_sync_now.size_request()[0]
		btn_tz_width = self.button_timezone.size_request()[0]
		if btn_sync_width < btn_tz_width:
			self.button_sync_now.set_size_request(btn_tz_width, -1)
		else:
			self.button_timezone.set_size_request(btn_sync_width, 0)

		currentcontinent = currenttimezone()[0]
		count = 0
		self.continentliststore.clear()
		for i in continents():
			self.continentliststore.append([i])
			if i == currentcontinent:
				self.continentlist.set_cursor(count)
				self.continentlist.scroll_to_cell(count)
			count += 1
		self.on_continentlist_cursor_changed(self)
		builder.connect_signals(self)

		self.update_calendar()
		self.update_time()

if __name__ == "__main__":
	app = GTKClockSetup()
	app.window.show()
	gtk.main()
