# 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 package import *
from packageset import *
from buttonbar import *
from listbox import *
from rhdialog import *
from rhutil import rhrun
from listbox import MultifieldListbox
import string

class UninstallingWindow(RHFrame):

    def __init__(self, numPackages, totalSize, Master = None):
	RHFrame.__init__(self, Master)

class UninstallConfirmWindow(RHFrame):

    def createWidgets(self, packages):
	m = Label(self, { 'text' : 
	    "You've selected the following packages to uninstall:" } )
	m.pack({ 'anchor' : 'w' })

	l = MultifieldListbox(self, [ ( 'Name', 25, 1),
				      ( 'Version', 10, 1),
				      ( 'Release', 8, 1) ])
	for p in packages:
	    l.addItems([ ( p.getName(), p.getVersion(), p.getRelease() ) ] )
	l.pack()

	m = Label(self, { 'text': 'Are you sure you want to do this?' })
	m.pack({ 'anchor' : 'w' })

	b = ButtonBar(self)
	b.addButton('Yes', self.yes)
	b.addButton('No', self.no)
	b.pack({ 'fill' : 'x' })

    def yes(self):
	self.doit = 1
	self.quit()

    def no(self):
	self.doit = 0
	self.quit()

    def __init__(self, pset, Master = None):
	RHFrame.__init__(self, Master)
	self.createWidgets(pset)
	self.pack()
	
	if (Master):
	    Master.title("Confirm uninstall")

	self.update()
	self.grab_set()
	self.wait_window(self)

def UninstallPackages(pset, root = None, test = 0):
    win = WaitBox('Uninstalling packages...')

    rpmargs = ( "-e", )
    if (root):
	rpmargs = rpmargs + ("--root", root)
    for p in pset.getPackages():
	rpmargs = rpmargs + ( p.getIdent(), )

    (code, out, err) = rhrun("/bin/rpm", rpmargs, 
		(None, None), test)
    if (len(err)):
	scrolledMessageDialog('Errors occured during the uninstall', err)
    
    win.quit()

def ConfirmUninstall(packages):
    doit = UninstallConfirmWindow(packages, Toplevel()).doit
    return doit

def DoUninstall(pset):
    packages = pset.getPackages()

    if (len(packages) == 0):
	error("No packages have been selected to install")
	return

    if (ConfirmUninstall(packages)):
        UninstallPackages(pset)
	pset.unselectAll()

