# GLINT graphical package management
# Copyright (C) 1995 Red Hat, Inc
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

from Tkinter import *
from rhtkinter import *
from buttonbar import ButtonBar

import rhdialog

class ConfigWindow(RHFrame):

    def createWidgets(self, currPath):
	m = Message(self, { 'text' : "You have to close all of your " +
				"available windows and reopen one before " +
				"changing your package directory will " +
				"have any affect.", 'aspect' : '800' })
	m.pack()
	f = Frame(self)
	l = Label(f, { 'text' : 'Package path:' })
	l.pack({ 'side' : 'left'})
	self.path = RHEntry(f, { 'relief' : 'sunken' } )
	self.path.insert('0', currPath)
	self.path.pack({ 'side' : 'left', 'fill' : 'x', 'expand' : '1'})
	self.path.focus_set()
	self.path.bind('<Return>', self.okay)
	f.pack({'fill' : 'x', 'expand' : '1' })

	b = ButtonBar(self)
	b.addButton('Ok', self.okay)
	b.addButton('Save', self.save)
	b.addButton('Default', self.default)
	b.addButton('Cancel', self.cancel)
	b.pack({ 'fill' : 'x' })	

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

    def okay(self, e = None):
	self.canceled = 0
	self.answer = self.path.get()
	self.quit()

    def default(self):
	self.path.delete('0', 'end')
	self.path.insert('0', self.defPath)

    def save(self):
	try:
	    rcfile = open("/root/.glintrc", "w")
	except default:
	    rhdialog.error("I can't open /root/.glintrc")

	rcfile.write("rpmpath:" + self.path.get() + "\n")
	rcfile.close()
	self.okay()

    def cancel(self):
	self.canceled = 1	
	self.quit()

    def wasCanceled(self):
	return self.canceled

    def newPath(self):
	return self.answer

    def __init__(self, defPath, currPath):
	master = Toplevel()
	master.title("Configuration")
	RHFrame.__init__(self, master)

	self.defPath = defPath
	self.createWidgets(currPath)
	

def DoConfig(defPath, currPath):
    win = ConfigWindow(defPath, currPath)
    win.grab_set()
    win.update()
    win.wait_window()

    if (win.canceled):
	return (currPath, )
    else:
	return (win.newPath(), )
