/*
 * application.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 "application.h"
#include "mainwindow.h"
#include "portmgr.h"

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

#include <qeventloop.h>

using namespace Barry;

Application::Application(): KUniqueApplication()
{
	KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
	if ( args->isSet( "rebuild-cache" ) ) {
		PortMgr::self().load();
		rebuildCache();
	}

	MainWindow *mainWindow = new MainWindow;
	setMainWidget( mainWindow );

	mainWindow->show();

	QCString term = args->getOption( "search" );
	if ( !term.isEmpty() )
		mainWindow->setup( SearchQuery( term ) );
	else
		mainWindow->setup();
}

void Application::rebuildCache()
{
	KProgressDialog dlg( 0, "dlg", i18n( "Rebuilding cache..." ),
	                     i18n( "Please wait while the cache gets rebuilt..." ),
	                     true /* modal */ );
	dlg.setAllowCancel( false );
	dlg.setAutoClose( true );
	dlg.show();

	const PortList ports = PortMgr::self().installedPorts();
	PortList::ConstIterator it = ports.begin();
	PortList::ConstIterator end = ports.end();

	dlg.progressBar()->setTotalSteps( ports.count() );
	dlg.setLabel( i18n( "Please wait while the cache gets rebuilt..." ) );
	for ( ; it != end; ++it ) {
		const Port port = *it;
		if ( !port.isNull() )
			port.files();                    // Hack to trigger cache
		dlg.progressBar()->advance( 1 );
		kapp->eventLoop()->processEvents( QEventLoop::ExcludeUserInput );
	}
}

int Application::newInstance()
{
	if ( isRestored() )
		RESTORE( MainWindow )

	return KUniqueApplication::newInstance();
}

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


syntax highlighted by Code2HTML, v. 0.9.1