/*
 * actionprogressdialog.cc
 *
 * Copyright (c) 2002, 2003 Frerich Raabe <raabe@kde.org>
 *
 * 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. For licensing and distribution details, check the
 * accompanying file 'COPYING'.
 */
#include "actionprogressdialog.h"
#include "portlistview.h"

#include <kdebug.h>
#include <klocale.h>
#include <kprogress.h>

#include <qlabel.h>
#include <qtimer.h>
#include <qvbox.h>

using namespace Barry;

ActionProgressDialog::ActionProgressDialog( QWidget *parent,
	const char *name, bool showProgress )
	: KDialogBase( parent, name, true, i18n( "Searching..." ),
	               Close, Close, true ),
	m_progressBar( 0 ),
	m_searchTimer( 0 )
{
	QVBox *mainWidget = makeVBoxMainWidget();

	m_statusLabel = new QLabel( i18n( "Searching..." ), mainWidget );

	if ( showProgress ) {
		m_progressBar = new KProgress( mainWidget, "m_progressBar" );
		m_progressBar->setCenterIndicator( true );
	}

	new QLabel( i18n( "Found packages:" ), mainWidget );
	m_foundPackages = new KListView( mainWidget, "m_foundPackages" );
	m_foundPackages->setAllColumnsShowFocus( true );
	m_foundPackages->setShowSortIndicator( true );
	connect( m_foundPackages, SIGNAL( executed( QListViewItem * ) ),
	         this, SLOT( itemSelected( QListViewItem * ) ) );
	connect( m_foundPackages, SIGNAL( returnPressed( QListViewItem * ) ),
	         this, SLOT( itemSelected( QListViewItem * ) ) );
	
	resize( 400, 300 );
}

void ActionProgressDialog::search( const PortList &portList )
{
	if ( portList.isEmpty() ) {
		kdWarning() << "ActionProgressDialog::search() called even though "
		               "port list is empty." << endl;
		return;
	}

	m_ports = portList;

	stopSearch();
	if ( m_progressBar ) {
		m_progressBar->setProgress( 0 );
		m_progressBar->setTotalSteps( portList.count() );
	}
	m_portIt = m_ports.begin();
	m_portEnd = m_ports.end();

	setStatus( i18n( "Searching..." ) );

	m_searchTimer = new QTimer( this );
	connect( m_searchTimer, SIGNAL( timeout() ), this, SLOT( getNextPort() ) );
	m_searchTimer->start( 0 );
}

void ActionProgressDialog::stopSearch()
{
	if ( m_searchTimer )
		disconnect( m_searchTimer, SIGNAL( timeout() ),
		            this, SLOT( getNextPort() ) );
	delete m_searchTimer;
	m_searchTimer = 0;
}

void ActionProgressDialog::getNextPort()
{
	if ( m_portIt == m_portEnd ) {
		m_statusLabel->setText( i18n( "Searching... done." ) );
		if ( m_progressBar )
			m_progressBar->setProgress( m_progressBar->totalSteps() );
		stopSearch();
		return;
	}

	if ( m_progressBar )
		m_progressBar->advance( 1 );

	processPort( *m_portIt++ );
}

void ActionProgressDialog::itemSelected( QListViewItem *item )
{
	PortItem *portItem = dynamic_cast<PortItem *>( item );
	if ( !portItem )
		return;

	emit portSelected( portItem->port() );
}

void ActionProgressDialog::enlistPort( const Port &port )
{
	PortItem *item = new PortItem( m_foundPackages, port );
	setupItem( item );
}

void ActionProgressDialog::setStatus( const QString &s )
{
	m_statusLabel->setText( s );
}

void ActionProgressDialog::addColumn( const QString &s )
{
	m_foundPackages->addColumn( s );
}

void ActionProgressDialog::setupItem( PortItem * /*item*/ )
{
	// no default implementation
}

#include "actionprogressdialog.moc"
// vim:ts=4:sw=4:noet:list
