//
// C++ Implementation: cmoduleindexdialog
//
// Description: 
//
//
// Author: The BibleTime team <info@bibletime.info>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include "cmoduleindexdialog.h"

#include "util/scoped_resource.h"

//TQt includes
#include <tqstring.h>

//TDE includes
#include <tdeapplication.h>
#include <kprogress.h>
#include <tdelocale.h>

CModuleIndexDialog* CModuleIndexDialog::getInstance() {
	static CModuleIndexDialog* instance = 0;
	if (instance == 0) {
		instance = new CModuleIndexDialog();
	}

	return instance;
}

void CModuleIndexDialog::indexAllModules( const ListCSwordModuleInfo& modules ) {
	tqDebug("indexAllModules");
	if (modules.count() == 0) {
		return;
	}
	
	m_currentModuleIndex = 0;
	progress = new KProgressDialog(0, "progressDialog", i18n("Preparing instant search"), TQString(), true);
	progress->setAllowCancel(false);
	progress->progressBar()->setTotalSteps( modules.count() * 100 );
	progress->setMinimumDuration(0);
// 	progress->show();
// 	progress->raise();

	ListCSwordModuleInfo::const_iterator end_it = modules.end();
	for( ListCSwordModuleInfo::const_iterator it = modules.begin(); it != end_it; ++it) {
		(*it)->connectIndexingFinished(this, TQ_SLOT(slotFinished()));
		(*it)->connectIndexingProgress(this, TQ_SLOT(slotModuleProgress(int)));

		progress->setLabel(i18n("Creating index for work %1").arg((*it)->name()));
		tqDebug("Building index for work %s", (*it)->name().latin1());
		
		(*it)->buildIndex();

		m_currentModuleIndex++;
		(*it)->disconnectIndexingSignals(this);
	}

	delete progress;
	progress = 0;
}

void CModuleIndexDialog::indexUnindexedModules( const ListCSwordModuleInfo& modules ) {
	tqDebug("indexUnindexedModules");
	ListCSwordModuleInfo unindexedMods;
	
	ListCSwordModuleInfo::const_iterator end_it = modules.end();
	for( ListCSwordModuleInfo::const_iterator it = modules.begin(); it != end_it; ++it) {
		if ((*it)->hasIndex()) {
			continue;
		}

		unindexedMods << (*it);
	}
	
	indexAllModules(unindexedMods);
}


/*!
    \fn CModuleIndexDialog::slotModuleProgress( int percentage )
 */
void CModuleIndexDialog::slotModuleProgress( int percentage ) {
//	tqDebug("progress %d", percentage);
	
	progress->progressBar()->setProgress( m_currentModuleIndex * 100 + percentage );
	tdeApp->processEvents( 10 ); //10 ms only
}

void CModuleIndexDialog::slotFinished( ) {
	tqDebug("indexing finished()");
	
	progress->progressBar()->setProgress( progress->progressBar()->totalSteps() );
	tdeApp->processEvents( 1 ); //1 ms only
}

#include "cmoduleindexdialog.moc"
