/***************************************************************************
 *   Copyright (C) 2007 by Danny Kukawka                                   *
 *                         <dkukawka@suse.de>, <danny.kukawka@web.de>      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License     *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

/*! \file countdowndialog.cpp
 * \brief 	In this file can be found the countdown dialog related code. 
 * \author 	Danny Kukawka, <dkukawka@suse.de, danny.kukawka@web.de>
 * \date    	2007
 */

// own header
#include "countdowndialog.h"
#include "kpowersave_debug.h"

// KDE headers:
#include <tdelocale.h>
#include <kiconloader.h>
#include <kprogress.h>

// QT headers:
#include <tqdialog.h>
#include <tqlabel.h> 
#include <tqprogressbar.h>
#include <tqpushbutton.h>
#include <tqstring.h>
#include <tqtimer.h>

/*! This is the default constructor of class countDownDialog . */
countDownDialog::countDownDialog( int timeout, TQWidget *parent, const char *name)
		:countdown_Dialog(parent, name, false, TQt::WStyle_StaysOnTop | TQt::WDestructiveClose )
{
	kdDebugFuncIn(trace);
	chancel = false;
	remaining = timeout;
	timeOut = timeout;

	PROGRESS = new TQTimer(this);
	connect(PROGRESS, TQT_SIGNAL(timeout()), this, TQT_SLOT(updateProgress()));

	this->setCaption(i18n("KPowersave"));

	kdDebugFuncOut(trace);
}

/*! This is the default destructor of class countDownDialog . */
countDownDialog::~countDownDialog() {
	kdDebugFuncIn(trace);
	// no need to delete child widgets, TQt does it all for us
	emit dialogClosed(chancel);

	kdDebugFuncOut(trace);
}

/*! 
 * This used to set Icon/pixmap for the dialog.
 * \param type TQString with the type of the current suspend
 *             to set the pixmap in the dialog
 */
void countDownDialog::setPixmap( TQString type )
{
	TQPixmap pixmap = 0;

	if(type.startsWith("suspend2disk")){
		pixmap = TDEGlobal::iconLoader()->loadIcon("suspend_to_disk", TDEIcon::NoGroup, TDEIcon::SizeLarge);
	} else if (type.startsWith("suspend2ram")) {
		pixmap = TDEGlobal::iconLoader()->loadIcon("suspend_to_ram", TDEIcon::NoGroup, TDEIcon::SizeLarge);
	} else if (type.startsWith("standby")) {
		pixmap = TDEGlobal::iconLoader()->loadIcon("stand_by", TDEIcon::NoGroup, TDEIcon::SizeLarge);
	} else {
		pixmap = TDEGlobal::iconLoader()->loadIcon("kpowersave", TDEIcon::NoGroup, TDEIcon::SizeLarge);
	}
	iconPixmap->setPixmap( pixmap );
}

/*!
 * To set the message to the dialog, which should be shown to the user.
 * \param text	TQString with the message. 
 */
void countDownDialog::setMessageText(TQString text) {
	kdDebugFuncIn(trace);

	if (!text.isEmpty()) {
		textLabel->setText(text);
	}

	kdDebugFuncOut(trace);
}

/*!
 * To show the dialog and start the countdown.
 * \return boolean with the result of the operation
 * \retval true		if the dialog could get displayed
 * \retval false	if there is any problem
 */
bool countDownDialog::showDialog() {
	kdDebugFuncIn(trace);

	bool _retval = false;

	if (!textLabel->text().isEmpty() && timeOut > 0) {
		// init the progressbar
		progressBar->setFormat(i18n("%1 seconds").arg(remaining));
		progressBar->setPercentageVisible(true);
		progressBar->setProgress(100);
		progressBar->setEnabled(true);

		this->adjustSize();
		this->show();

		PROGRESS->start(1000, true);
	}

	kdDebugFuncOut(trace);
	return _retval;
}

/*!
 * \b TQT_SLOT to get the event if the 'Cancel' button was pressed.
 */
void countDownDialog::pB_cancel_pressed() {
	kdDebugFuncIn(trace);

	if (PROGRESS->isActive())
		PROGRESS->stop();

	chancel = true;
	close();

	kdDebugFuncOut(trace);
}

/*!
 * \b TQT_SLOT to handle the change of the progressbar.
 */
void countDownDialog::updateProgress() {
	kdDebugFuncIn(trace);

	if (remaining == 0 ) {
		if (PROGRESS->isActive())
			PROGRESS->stop();
		
		chancel = false;
		close();
	} else if ( remaining > 0) {
		int setTo = (int)((100.0/(float)timeOut)*(float)remaining);
		
		// set the progressBar
		progressBar->setFormat(i18n("%1 seconds").arg(remaining));
		progressBar->setPercentageVisible(true);
		progressBar->setProgress(setTo);
		progressBar->setEnabled(true);

		// increase counter
		remaining--;
		// start needed timer
		PROGRESS->start(1000, true);
	}

	kdDebugFuncOut(trace);
}

#include "countdowndialog.moc"