/*
 * searchquery.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 "port.h"
#include "searchquery.h"

using namespace Barry;

SearchQuery::SearchQuery()
{
}

SearchQuery::SearchQuery( const QString &searchTerm, Flags flags, DataCollections collections ):
	m_searchTerm( searchTerm ),
	m_flags( flags ),
	m_collections( collections )
{
	if ( m_flags & RegExp ) {
		m_regexp.setPattern( m_searchTerm );
		m_regexp.setCaseSensitive( m_flags & CaseSensitive );
	}
}

bool SearchQuery::operator==( const SearchQuery &rhs ) const
{
	return m_searchTerm == rhs.m_searchTerm &&
	       m_flags == rhs.m_flags &&
	       m_collections == rhs.m_collections;
}

bool SearchQuery::matches( const Port &port ) const
{
	bool found = false;
	if ( m_flags & RegExp ) {
		found = m_regexp.search( port.name() ) > -1;
		if ( m_collections & Comments )
			found |= m_regexp.search( port.comment() ) > -1;
		if ( m_collections & Descriptions )
			found |= m_regexp.search( port.description() ) > -1;
	} else {
		const bool caseSensitive = m_flags & CaseSensitive;
		found = port.name().find( m_searchTerm, 0, caseSensitive ) != -1;
		if ( m_collections & Comments )
			found |= port.comment().find( m_searchTerm, 0, caseSensitive ) != -1;
		if ( m_collections & Descriptions )
			found |= port.description().find( m_searchTerm, 0, caseSensitive ) != -1;
	}
	return found;
}
// vim:ts=4:sw=4:noet:list


syntax highlighted by Code2HTML, v. 0.9.1