/***************************************************************************
 *   Copyright (C) 2004 by David Sansome                                   *
 *   me@davidsansome.com                                                   *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "uninstallwizard.h"
#include "headerlistitem.h"
#include "data.h"

#include <tqregexp.h>
#include <tqwidgetstack.h>
#include <tqfile.h>
#include <tqtextstream.h>
#include <tqheader.h>
#include <tqlistbox.h>
#include <tqmessagebox.h>
#include <tqpushbutton.h>
#include <tqlineedit.h>
#include <tqtextedit.h>
#include <tqlabel.h>

AppListItem::AppListItem(TQString nN, TQString n, TQListView* parent)
 : TQCheckListItem(parent, "", TQCheckListItem::CheckBox)
{
	niceName = nN;
	name = n;
	section = 2;
	
	setText(0, niceName);
}

int AppListItem::compare(TQListViewItem* i, int col, bool ascending) const
{
	switch (i->rtti())
	{
		case 1003: // App
		{
			AppListItem* item = (AppListItem*) i;
			if (section < item->section)
				return -1;
			if (section > item->section)
				return 1;
			return TQListViewItem::compare(i, col, ascending);
		}
		break;
		
		case 1002: // Header
		{
			HeaderListItem* item = (HeaderListItem*) i;
			if (section < item->section)
				return -1;
			return 1;
		}
		break;
	}
	return 0;
}


UninstallWizard::UninstallWizard(TQWidget* parent, const char* name, bool modal, WFlags fl)
: WizardBase(parent,name, modal,fl)
{
	mainStack->raiseWidget(1);
	setCaption("Uninstall Software");
	titleLabel->setText("<b>Uninstall Software</b>");
	componentInfo->setMaximumSize(32767,70);
	
	logDialog = new LogDialog(this);
	logDialog->hide();
	appList->header()->hide();
	
	globalHeader = NULL;
	
	icon.convertFromImage(qembed_findImage("misc"));
	
	externalProcess = new TQProcess(this);
	connect(externalProcess, SIGNAL(processExited()), SLOT(processExited()));
	connect(externalProcess, SIGNAL(readyReadStdout()), SLOT(readyReadStdout()));
	connect(externalProcess, SIGNAL(readyReadStderr()), SLOT(readyReadStderr()));
	
	TQFile uninstallScript("/tmp/arkollon-uninstall.sh");
	if (uninstallScript.exists())
		uninstallScript.remove();
	uninstallScript.open(IO_WriteOnly);
	TQDataStream stream(&uninstallScript);
	stream.writeRawBytes((const char*)uninstaller_sh_data, uninstaller_sh_len);
	uninstallScript.close();
	
	currentStage = ListingPackages;
	externalProcess->addArgument("/bin/sh");
	externalProcess->addArgument("/tmp/arkollon-uninstall.sh");
	externalProcess->addArgument("--list");
	externalProcess->start();
}

UninstallWizard::~UninstallWizard()
{
	TQFile uninstallScript("/tmp/arkollon-uninstall.sh");
	if (uninstallScript.exists())
		uninstallScript.remove();
}

void UninstallWizard::logPressed()
{
	logDialog->show();
}

void UninstallWizard::cancelPressed()
{
	reject();
}

void UninstallWizard::previousPressed()
{
	int currentId = uninstallStack->id(uninstallStack->visibleWidget());
	if (currentId == 0)
		return;
		
	uninstallStack->raiseWidget(--currentId);
	
	if (currentId == 0)
		previousButton->setEnabled(false);
	nextButton->setEnabled(true);
}

void UninstallWizard::nextPressed()
{
	int currentId = uninstallStack->id(uninstallStack->visibleWidget());
	if (currentId == 2)
	{
		accept();
		return;
	}
	
	if (currentId == 0)
	{
		bool checked = false;
		TQStringList argList;
		argList.append("/bin/sh");
		argList.append("/tmp/arkollon-uninstall.sh");
		for ( TQListViewItem * myChild = appList->firstChild() ; myChild != NULL ; myChild = myChild->nextSibling())
		{
			if (myChild->rtti() != 1003)
				continue;
			AppListItem* app = (AppListItem*) myChild;
			if (!app->isOn())
				continue;
			argList.append("--files");
			argList.append(app->name);
			checked = true;
		}
		if (!checked)
		{
			TQMessageBox::warning(this, "Error", "You need to select at least one package to remove", TQMessageBox::Ok, TQMessageBox::NoButton, TQMessageBox::NoButton);
			return;
		}
		fileList->clear();
		currentStage = ListingFiles;
		externalProcess->setArguments(argList);
		externalProcess->start();
		nextButton->setEnabled(false); // Set true again when the process is done
	}
	
	uninstallStack->raiseWidget(++currentId);
	
	if (currentId == 2)
		removeUserPackages();
	else
		previousButton->setEnabled(true);
}

void UninstallWizard::processExited()
{
	switch (currentStage)
	{
	case ListingPackages:
		if (appList->childCount() <= 0)
		{
			TQMessageBox::warning(NULL, "Warning", "There are no packages installed", TQMessageBox::Ok, TQMessageBox::NoButton, TQMessageBox::NoButton);
			reject();
			return;
		}
		show();
		break;
	case ListingFiles:
		nextButton->setEnabled(true);
		fileList->sort();
		break;
	case RemovingGlobal:
		finished();
		break;
	}
}

void UninstallWizard::readyReadStdout()
{
	switch (currentStage)
	{
	case ListingPackages:
	{
		while (externalProcess->canReadLineStdout())
		{
			TQString line = externalProcess->readLineStdout();
			if (line.isEmpty())
				continue;
				
			// See if it already exists
			bool exists = false;
			
			for ( TQListViewItem * myChild = appList->firstChild() ; myChild != NULL ; myChild = myChild->nextSibling())
			{
				if (myChild->rtti() != 1003)
					continue;
				AppListItem* app = (AppListItem*) myChild;
				if (app->name.lower() == line.lower())
				{
					exists = true;
					break;
				}
			}
			if (exists)
				continue;
			
			TQString niceName = line.left(line.findRev(':'));
			niceName = niceName.left(1).upper() + niceName.right(niceName.length()-1);
			new AppListItem(niceName, line, appList);
			
			if (globalHeader == NULL)
			{
				globalHeader = new HeaderListItem(appList);
				globalHeader->setText(0, "Applications available to all users");
				globalHeader->section = 1;
			}
		}
		break;
	}
	case ListingFiles:
	{
		while (externalProcess->canReadLineStdout())
		{
			TQString line = externalProcess->readLineStdout();
			if (line.isEmpty())
				continue;
			fileList->insertItem(icon, line.left(line.find("  ")));
		}
		break;
	}
	case RemovingGlobal:
		while (externalProcess->canReadLineStdout())
		{
			TQString line = externalProcess->readLineStdout();
			if (line.isEmpty())
				continue;
			line.replace(TQRegExp("\\033[^m]*m"), "");
			logDialog->logBox->append(line);
		}
		break;
	}
}

void UninstallWizard::readyReadStderr()
{
	while (externalProcess->canReadLineStdout())
	{
		TQString line = externalProcess->readLineStdout();
		if (line.isEmpty())
			continue;
		line.replace(TQRegExp("\\033[^m]*m"), "");
		logDialog->logBox->append(line);
	}
}

void UninstallWizard::removeUserPackages()
{
	nextButton->setEnabled(false); // Set true again when the process is done
	previousButton->setEnabled(false);
	
	// Find out which packages belong to the user, and uninstall them
	TQStringList argList;
	argList.append("/bin/sh");
	argList.append("/tmp/arkollon-uninstall.sh");
	for ( TQListViewItem * myChild = appList->firstChild() ; myChild != NULL ; myChild = myChild->nextSibling())
	{
		if (myChild->rtti() != 1003)
			continue;
		AppListItem* app = (AppListItem*) myChild;
		if (!app->isOn())
			continue;
		
		argList.append("--remove");
		argList.append(app->name);
	}
	currentStage = RemovingGlobal;
	externalProcess->setArguments(argList);
	externalProcess->start();
}

void UninstallWizard::finished()
{
	pleaseWaitLabel2->setText("Removal of packages complete!");
	nextButton->setText("Finish");
	nextButton->setEnabled(true);
}

#include "uninstallwizard.moc"

