/***************************************************************************
 *   Copyright (C) 2005 by xavier helluy   *
 *   xavier.helluy@web.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/

#include "queryfieldmedia.h"
#include <kdebug.h>


QueryFieldMedia::QueryFieldMedia( QWidget* parent, const char* name, WFlags fl )
	: QWidget( parent, name, fl )
{
	action = new KComboBox( FALSE, this, "action" );
//	action->setMaximumSize( QSize( 140, 32767 ) );
	action->setAutoCompletion( FALSE );

	
	
	//query is media-name query
	query = new KLineEdit( this, "query" );

	query->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)1, 0, 0, FALSE ) );
	action->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)1, (QSizePolicy::SizeType)1, 0, 0, FALSE ) );


	
	languageChange();
//	resize( QSize(490, 40).expandedTo(minimumSizeHint()) );
	clearWState( WState_Polished );
	layoutA = new QHBoxLayout(this);
	layoutA->addWidget(action);
	layoutA->addWidget(query,3);
	
	
	
// 	this->setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::MinimumExpanding, FALSE);
// 	this->query->setFocus();
	
	this->setShown(TRUE);
}


void QueryFieldMedia::languageChange()
{
	setCaption( tr2i18n( "Form1" ) );
	m_mediaTypeList.append(tr2i18n( "CD" ));
	m_mediaTypeList.append(tr2i18n( "DVD" ));
	m_mediaTypeList.append(tr2i18n( "HD" ));
	m_mediaTypeList.append(tr2i18n( "Tape" ));
	action->clear();
	action->insertStringList(tr2i18n( "Any type" ));
	action->insertStringList(m_mediaTypeList,-1);
// 	query->setText( tr2i18n( "Any name" ) );
}

QString QueryFieldMedia::getQuery()
{
	
	QString mediaType = this->action->currentText();
	QString mediaName = this->query->text();
	mediaName = mediaName.stripWhiteSpace();
	
	kdDebug() << "QueryFieldMedia::getQuery() mediaType: "<< mediaType << endl;
	kdDebug() << "QueryFieldMedia::getQuery() mediaName: "<< mediaName << endl;

	QString query(QString::null);
	
	if (mediaType == tr2i18n( "Any type" ) ) 
	{
		if ((mediaName ==  tr2i18n( "Any name" )) || mediaName.isEmpty() )
		{
			query="";
		/*		for (QStringList::Iterator it=m_mediaTypeList.begin();
				   it!=m_mediaTypeList.end();it++)
		{
			query.append("mediaType:" + *it + " ");
		}*/
		}
		else 	if ((mediaName !=  tr2i18n( "Any name" )) && (!mediaName.isEmpty()) )
		{
			query.append("mediaName:(" + mediaName +")");
// 			for (QStringList::Iterator it=m_mediaTypeList.begin();
// 					   it!=m_mediaTypeList.end();it++)
// 			{
// 				query.append("(mediaType:" + *it + " AND mediaName:" + mediaName +") ");
// 			}
		}
		
	}
	else if ( ( mediaName ==  tr2i18n( "Any name" ) ) || ( mediaName.isEmpty() ) )
	{
		query.append("mediaType:" + mediaType);
	}
	else if (!mediaName.isEmpty())
	{
		query.append("(mediaType:" + mediaType + " AND mediaName:" + mediaName +") ");
	}
	
// 	QString str2 = parseQuery(str1);
	return query;
}

 #include "queryfieldmedia.moc"
