from Tkinter import *
import Tkinter
from parttable import *
from listbox import *
from rhtkinter import *
from buttonbar import *
from fstab import *
from rhdialog import *
import rhutil

class FormatWindow(RHFrame):

    def createWidgets(self, fstab):

	self.message = Message(self, { 'text' : 'What partitions would you '+
				'like to format?', 'aspect' : '400'})
	self.message.pack()

	self.list = MultifieldListbox(self, [ ('Device', 15, 0), 
			('Mount Point', 20, 0) ])
	self.list.allowMultiple()

	
	for mount in fstab.getList():
	    count = 0
	    (device, mntpoint, fstype, options, freq, order) = mount
	    if (fstype == "ext2"):
		self.list.addItems([ (device, mntpoint) ])
		count = count + 1
		if (mntpoint == '/'):
		    self.list.selectLine(count)

	self.list.pack({ 'expand' : '1', 'fill' : 'both' } )

	self.buttons = ButtonBar(self)
	self.buttons.addButton("Done", self.done)

	self.buttons.pack({ 'fill' : 'x' })
	

    def done(self):
	selected = self.list.getSelectedItems()
	self.formatList = []
	for thing in selected:
	    (device, mntpoint) = thing
	    self.formatList.append(device)

	if ((len(self.formatList)) == 0):
	    retry = Dialog("Warning",
		   "It's a bad idea to install Red Hat onto partitions that" +
		    " haven't been formatted. Are you sure you don't want" +
		    " to format anything?", "warning", "", 
		    [ "Continue", "Retry" ]).num
	    if (retry):
		return
	
	self.quit()

    def toFormat(self):
	return self.formatList

    def __init__(self, fstab, Master = None):
	RHFrame.__init__(self, Master)
	self.createWidgets(fstab)
	self.pack({ 'expand' : '1', 'fill' : 'both'})
	if (Master):
	    Master.title("Format Partitions")

def formatParts(fstab, test):
    win = FormatWindow(fstab, Toplevel())
    win.update()
    win.wait_window(win)

    formatlist = win.toFormat()

    for device in formatlist:
	win = WaitBox('Formatting ' + device)
	rhsystem("/sbin/mke2fs", (device,), test)
	win.quit()

    # The swap patitions should have been formatted from the text portion
