from Tkinter import *
from parttable import *
from listbox import *
from rhtkinter import *
from buttonbar import *
from rhutil import *
import os
import string

class GMTorLocalWin(RHFrame):

    def createWidgets(self):
	self.message = Message(self, { 'text' : 
"Is your system clock set to UTC/GMT or local time?", 'aspect' : 600 })
	self.message.pack()

	self.Buttons = ButtonBar(self)
	self.Buttons.addButton("UTC", self.isutc, 1)
	self.Buttons.addButton("Local", self.islocal, 0)
	self.Buttons.pack({ 'fill' : 'x' })
	self.pack()

    def isutc(self):
	self.setClockType("GMT")

    def islocal(self):
	self.setClockType("LOCAL")

    def setClockType(self, mode):
	if (self.test):
	    print "Would set CLOCKMODE=" + mode + " in", self.root + \
		"/etc/sysconfig/clock"
	else:
	    f = open(self.root + "/etc/sysconfig/clock", "w")
	    f.write("CLOCKMODE=" + mode + "\n")
	    f.close()
	self.quit()
    
    def __init__(self, test, root, Master = None):
	RHFrame.__init__(self, Master)
	self.root = root
	self.test = test
	self.createWidgets()
	Master.title("Clock Type")

class TimezoneWin(RHFrame):

    def getTimezones(self):
	os.chdir(self.root + "/usr/lib/zoneinfo")
	(code, out, err) = rhrun(self.root + "/usr/bin/find",  \
		('.', '-name', '[A-Z]*', '-type', 'f', '-print'), None)
	lines = string.splitfields(out, "\n")
	lines.sort()
	lines.reverse()   # to get the US timezones toward the top
	return lines

    def createWidgets(self, timezones):
	self.message = Label(self, { 'text' : "What timezone are you in?"})
	self.message.pack()

	self.listframe = Frame(self)

	self.list = RHListbox(self.listframe, { 'height' : '20', 
				'width' : '40', 
				'relief' : 'sunken' } )
	self.list.bind('<Double-1>', self.done)
	for tz in timezones:
	    self.list.insert('end', tz[2:])
	self.list.pack({ 'side' : 'left', 'expand' : '1', 'fill' : 'both' })

	self.scrollbar = Scrollbar(self.listframe, 
		{ 'command' : self.list.yview } )
	self.scrollbar.pack({ 'side' : 'right', 'fill' : 'y'})
	self.list['yscrollcommand'] = self.scrollbar.set
	self.listframe.pack({ 'expand' : '1', 'fill' : 'both' })

	self.Buttons = ButtonBar(self)
	self.Buttons.addButton("Ok", self.done, 1)
	self.Buttons.pack({ 'fill' : 'x' })
	self.pack({ 'expand' : '1', 'fill' : 'both' })

    def done(self, e = None):
	selection = self.list.curselection()
	if (not len(selection)):
	    return
	tz = self.list.get(selection)
	print "set timezone to",  tz
	fn = "/usr/lib/zoneinfo/" + tz
	if (self.test):
	    print "Making link from " + self.root + "/etc/localtime to", fn
	else:
	    if (os.path.exists(self.root + "/etc/localtime")):
		os.unlink(self.root + "/etc/localtime")
	    os.symlink(fn, self.root + "/etc/localtime")
	self.quit()

    def __init__(self, test, root, Master = None):
	RHFrame.__init__(self, Master)
	self.test = test
	if (test):
	    self.root = "/"
	else:
	    self.root = root
	lines = self.getTimezones()
	self.createWidgets(lines)
	if (Master):
	    Master.title("Time Zone")

def setupclock(test, root = "/"):
    win = GMTorLocalWin(test, root, Toplevel())
    win.update()
    win.wait_window(win)

    win = TimezoneWin(test, root, Toplevel())
    win.update()
    win.wait_window(win)

