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

#include <kapplication.h>
#include <kcombobox.h>
#include <kconfig.h>
#include <kiconloader.h>
#include <klocale.h>
#include <knotifyclient.h>
#include <kparts/componentfactory.h>
#include <kregexpeditorinterface.h>

#include <qcheckbox.h>
#include <qgroupbox.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qtoolbutton.h>

using namespace Barry;

SearchDialog::SearchDialog( QWidget *parent )
	: KDialogBase( parent, "SearchDialog", true, i18n( "Find" ),
			User1 | Cancel, User1, false, i18n( "&Find" ) )
{
	QWidget *mainWidget = new QWidget( this );
	QGridLayout *layout = new QGridLayout( mainWidget );
	layout->setMargin( KDialog::marginHint() );
	layout->setSpacing( KDialog::spacingHint() );
	
	QLabel *lSearch = new QLabel( mainWidget );
	lSearch->setText( i18n( "Find:" ) );
	layout->addMultiCellWidget( lSearch, 0, 0, 0, 2 );

	m_searchBox = new KComboBox( true, mainWidget );
	m_searchBox->setFocus();
	KCompletion *comp = m_searchBox->completionObject();
	connect( m_searchBox, SIGNAL( returnPressed( const QString & ) ),
	         comp, SLOT( addItem( const QString & ) ) );

	KConfig *cfg = kapp->config();
	cfg->setGroup( "Search dialog" );
	QStringList items = cfg->readListEntry( QString::fromLatin1( "Recent search terms" ) );
	m_searchBox->insertStringList( items );
	comp->setItems( items );
	
	if ( regexpEditorAvailable() ) {
		QToolButton *bRegexpDlg = new QToolButton( mainWidget );
		bRegexpDlg->setFixedSize( QSize( 32, 32 ) );
		QIconSet iconSet = kapp->iconLoader()->loadIconSet( QString::fromLatin1( "find" ), KIcon::Small );
		bRegexpDlg->setIconSet( iconSet );
		connect( bRegexpDlg, SIGNAL( clicked() ),
		         this, SLOT( openRegexpDlg() ) );
		layout->addMultiCellWidget( m_searchBox, 1, 1, 0, 1 );
		layout->addWidget( bRegexpDlg, 1, 2 );
	} else
		layout->addMultiCellWidget( m_searchBox, 1, 1, 0, 2 );

	QGroupBox *groupBox = new QGroupBox( mainWidget );
	
	QGridLayout *gbLayout = new QGridLayout( groupBox );
	gbLayout->setMargin( KDialog::marginHint() );
	gbLayout->setSpacing( KDialog::spacingHint() );
	
	m_caseSensitive = new QCheckBox( i18n( "Case &sensitive" ), groupBox );
	gbLayout->addWidget( m_caseSensitive, 0, 0 );

	m_isRegExp = new QCheckBox( i18n( "Treat as &regular expression" ), groupBox );
	gbLayout->addWidget( m_isRegExp, 0, 1 );
	
	m_searchComments = new QCheckBox( i18n( "Search port &comments" ), groupBox );
	gbLayout->addWidget( m_searchComments, 1, 0 );
	
	m_searchDescriptions = new QCheckBox( i18n( "Search port &descriptions" ), groupBox );
	gbLayout->addWidget( m_searchDescriptions, 1, 1 );
	
	layout->addMultiCellWidget( groupBox, 2, 3, 0, 2 );

	setMainWidget( mainWidget );
}

bool SearchDialog::regexpEditorAvailable() const
{
	return !KTrader::self()->query( QString::fromLatin1( "KRegExpEditor/KRegExpEditor" ) ).isEmpty();
}

void SearchDialog::openRegexpDlg()
{
	QDialog *dlg = KParts::ComponentFactory::createInstanceFromQuery<QDialog>( QString::fromLatin1( "KRegExpEditor/KRegExpEditor" ) );
	if ( !dlg )
		return;
	
	KRegExpEditorInterface *editor = static_cast<KRegExpEditorInterface *>( dlg->qt_cast( "KRegExpEditorInterface" ) );
	editor->setRegExp( m_searchBox->currentText() );

	if ( dlg->exec() == QDialog::Accepted ) {
		m_searchBox->setCurrentText( editor->regExp() );
		m_isRegExp->setChecked( true );
	}
}

SearchQuery SearchDialog::searchQuery() const
{
	const QString term = m_searchBox->currentText();

	int flags = 0;
	if ( m_isRegExp->isChecked() )
		flags |= SearchQuery::RegExp;
	if ( m_caseSensitive->isChecked() )
		flags |= SearchQuery::CaseSensitive;

	int collections = 0;
	if ( m_searchComments->isChecked() )
		collections |= SearchQuery::Comments;
	if ( m_searchDescriptions->isChecked() )
		collections |= SearchQuery::Descriptions;

	return SearchQuery( term,
	                    static_cast<SearchQuery::Flags>( flags ),
	                    static_cast<SearchQuery::DataCollections>( collections )
	                  );
}

void SearchDialog::slotUser1()
{
	if ( m_searchBox->currentText().isEmpty() ) {
		KNotifyClient::event( QString::fromLatin1( "NoSearchTerm" ),
			i18n( "No search term specified!" ) );
		return;
	}

	KConfig *cfg = kapp->config();
	cfg->setGroup( "Search dialog" );
	cfg->writeEntry( QString::fromLatin1( "Recent search terms" ), m_searchBox->compObj()->items() );
	cfg->sync();

	accept();
}

SearchProgressDialog::SearchProgressDialog( const SearchQuery &query,
                                            QWidget *parent,
                                            const char *name )
	: ActionProgressDialog( parent, name, false ),
	m_query( query )
{
	addColumn( i18n( "Name" ) );
	addColumn( i18n( "Version" ) );

	search( PortMgr::self().allPorts() );
}

void SearchProgressDialog::processPort( const Port &port )
{
	if ( !port.isNull() && m_query.matches( port ) )
		enlistPort( port );
}

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