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

#include "pkgupgradetraynotifier.h"

#include <pkgupgradecontroller.h>
#include <packagemgrtray.h>
#include <packagesettings.h>

PkgUpgradeTrayNotifier::PkgUpgradeTrayNotifier(PkgUpgradeController *ctrl,
												PackageMgrTray *tray, const char *name)
	: QObject(ctrl, name)
{
	this->tray = tray;
	tray->setState(PackageMgrTray::Working);
	
	connect(ctrl, SIGNAL(currentPackageChanged(const QString &)), SLOT(currentPackageChanged(const QString &)));
	connect(ctrl, SIGNAL(overallProgressChanged(int)), SLOT(overallProgressChanged(int)));

	if(PackageSettings().getNotifications() & PackageSettings::UserInputRequired)
		connect(ctrl, SIGNAL(userInputRequired(const QString&)), SLOT(userInputRequired(const QString &)));
}


PkgUpgradeTrayNotifier::~PkgUpgradeTrayNotifier()
{
	tray->clearTooltip();
	tray->setState(PackageMgrTray::Normal);

	if(PackageSettings().getNotifications() & PackageSettings::InstallationFinished)
		tray->displayMessage(tr("All operations completed."));
}


void PkgUpgradeTrayNotifier::currentPackageChanged(const QString & pkg)
{
	if(PackageSettings().getNotifications() & PackageSettings::PackageCompleted)
	{
		QString msg;

		if(!currentPackage.isEmpty())
			msg += tr("<i>%1</i> completed.<br>").arg(currentPackage);

		msg += tr("Now processing <i>%2</i>").arg(pkg);
		
		tray->displayMessage(msg);
	}

	currentPackage = pkg;
	updateToolTip();
}


void PkgUpgradeTrayNotifier::overallProgressChanged(int percent)
{
	overallProgress = percent;
	updateToolTip();
}


void PkgUpgradeTrayNotifier::userInputRequired(const QString & str)
{
	tray->displayMessage(tr("User interaction required: <i>%1</i>").arg(str));
}


/**
  * Updates the tray icon's tooltip to match the status 
  * of current installation/upgrade processes.
  */
void PkgUpgradeTrayNotifier::updateToolTip()
{
	tray->setTooltip(tr("Overall progress: %1 %\n"
							  "Currently processing: %2\n")
							 .arg(overallProgress)
							 .arg(currentPackage));
}
