/*
 * This file is part of DesktopBSD, see the README file.
 *
 * Author: Peter Hofer <ph@desktopbsd.net>
 *			(C) 2004, 2005
 *
 * Copyright: BSD, see the COPYING file included in this distribution
 *
 */

#include <qtimer.h>
#include <qprocess.h>
#include <qeventloop.h>
#include <qapplication.h>
#include <qprogressdialog.h>

#include <dbsdglobal.h>
#include <dialogs.h>
#include <fastestcvsupengine.h>
#include <unixprocess.h>
#include <pkgupgradeengine.h>

/* ATTENTION
 *	The IDs in bgrpPkgUse must correspond to PkgUpgradeEngine::PackageUse!
 */

void frmPkgSettings::fastestCVSUp() {
	editUpdateServer->setText("");

	fcprocess = FastestCVSUpEngine::getProcess();
	if(fcprocess != NULL) {
		connect(fcprocess, SIGNAL(processExited()), SLOT(fastestCVSUpExited()));
	
		if(fcprocess->start()) {
			fcpdlg = new QProgressDialog(this, NULL, true);
			disconnect(fcpdlg, SIGNAL(canceled()), fcpdlg, SLOT(cancel()));
			connect(fcpdlg, SIGNAL(canceled()), SLOT(fastestCVSUpCanceled()));
			fcpdlg->setCaption("DesktopBSD");
			fcpdlg->setTotalSteps(0); /* busy indicator */
			
			fastestCVSUpProgress();
		} else
			Dialogs::warnDlg(tr("Error: fastest_cvsup couldn't be started."));
	}
}

void frmPkgSettings::accept() {
	settings.setPackageUse(bgrpPkgUse->selectedId());
	settings.setPortsUpdateMethod(bgrpPortsUpdateMethod->selectedId());
	settings.setRecursive(chkRecursive->isChecked());
	settings.setUpwardRecursive(chkUpwardRecursive->isChecked());
	settings.setKeepGoing(chkKeepGoing->isChecked());
	settings.setMakeArguments(QStringList::split(' ', editMakeArgs->text()));
	settings.setMakeEnvironment(QStringList::split(' ', editMakeEnv->text()));
	settings.setUpdateServer(editUpdateServer->text());
	settings.setPackageDirectory(editPackageDir->text());
	settings.setIncrementalPackageFind(chkPkgFindAsYouType->isChecked());

	if(chkHideTimeout->isOn())
		settings.setNotificationTimeout(spnHideTimeout->value() * 1000);
	else
		settings.setNotificationTimeout(0);

	if(chkNotifications->isOn())
	{
		int notifications = 0;
		if(chkFinishedNotification->isOn())
			notifications |= PackageSettings::InstallationFinished;
			
		if(chkUserInputNotification->isOn())
			notifications |= PackageSettings::UserInputRequired;
			
		if(chkPkgCompletedNotification->isOn())
			notifications |= PackageSettings::PackageCompleted;

		settings.setNotifications(notifications);
	}
	else
		settings.setNotifications(0);

	QDialog::accept();
}


void frmPkgSettings::init() {
	bgrpPkgUse->setButton(settings.getPackageUse());
	chkRecursive->setChecked(settings.getRecursive());
	chkUpwardRecursive->setChecked(settings.getUpwardRecursive());
	chkKeepGoing->setChecked(settings.getKeepGoing());
	editMakeArgs->setText(settings.getMakeArguments().join(" "));
	editMakeEnv->setText(settings.getMakeEnvironment().join(" "));
	editUpdateServer->setText(settings.getUpdateServer());
	editPackageDir->setText(settings.getPackageDirectory());
	lblDefaultPackageDir->setText(tr("Default: <i>%1</i>").arg(PkgUpgradeEngine::readDefaultRepository()));
	chkPkgFindAsYouType->setChecked(settings.getIncrementalPackageFind());

	int updatemethod = settings.getPortsUpdateMethod();
	bgrpPortsUpdateMethod->setButton(updatemethod);
	grpCvsupSettings->setEnabled(updatemethod == PackageSettings::CvsUp);

	int notifytimeout = settings.getNotificationTimeout();
	chkHideTimeout->setChecked(notifytimeout > 0);
	if(notifytimeout > 0)
		spnHideTimeout->setValue(notifytimeout / 1000);

	int notifications = settings.getNotifications();
	chkNotifications->setChecked(notifications);
	if(notifications)
	{
		chkFinishedNotification->setChecked(notifications & PackageSettings::InstallationFinished);
		chkUserInputNotification->setChecked(notifications & PackageSettings::UserInputRequired);
		chkPkgCompletedNotification->setChecked(notifications & PackageSettings::PackageCompleted);
	}

	btnFastestCVSUp->setIconSet(DBSDGlobal::getIconLoader()->loadIcon("find", DBSDIconLoader::Button));
}


void frmPkgSettings::fastestCVSUpExited() {
	if(fcprocess->normalExit() && fcprocess->exitStatus() == 0)
		editUpdateServer->setText(fcprocess->readLineStdout());
	
	delete fcprocess;
	fcprocess = NULL;
}


void frmPkgSettings::fastestCVSUpProgress() {
	if(fcprocess != NULL) {
		fcpdlg->setProgress(fcpdlg->progress() + 8);
		QTimer::singleShot(30, this, SLOT(fastestCVSUpProgress()));
	} else {
		delete fcpdlg;
		fcpdlg = NULL;
	}
}


void frmPkgSettings::fastestCVSUpCanceled() {
	if(fcprocess != NULL)
		fcprocess->tryTerminate();
}



void frmPkgSettings::openServerList(const QString & url) {
	UnixProcess process(QStringList::split(' ', "/usr/local/bin/kfmclient exec"));
	process.addArgument(url);

	if(!process.start())
		Dialogs::warnDlg(tr("Error: Cannot launch browser.\n"
								  "(kfmclient couldn't be started)"));
}


void frmPkgSettings::customMakeInfo() {
	if(bgrpPkgUse->selected() == radUsePackages && !editMakeArgs->text().isEmpty())
		Dialogs::infoDlg(tr("You have selected \"Binary packages whenever possible\" as installation\n"
								  "method, but since custom make arguments are specified, software will\n"
								  "be always built from source."));
}


void frmPkgSettings::updateMethodChanged(int index)
{
	grpCvsupSettings->setEnabled(index == 1);
}
