/***************************************************************************
 *   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.             *
 ***************************************************************************/


#ifndef KWESTDAEMON_H
#define KWESTDAEMON_H

#include <dcopclient.h>
#include <qthread.h>

#include <qmutex.h>

#include "kwest_search_daemon_dcopiface.h"

#ifdef HAS_CLUCENE
#include "kclucenethread.h"
#include "kclucenedaemon.h"
#endif

#ifdef HAS_LIBBEAGLE
#include "beaglethread.h"
#endif

#include "searchthread.h"


// define SEARCH AND QUERY form  
// WARNING defined in kio_kwest/kwestimpl.h too !!
// define backend namespace
#define BACKEND_BEAGLE "beagle"
#define BACKEND_CLUCENE "lucene"
// define query form
#define QUERY_FORM "?query="
// define SEARCH AND QUERY form  
#define SEARCH_AND_QUERY_FORM_URL_BEAGLE        BACKEND_BEAGLE QUERY_FORM
#define SEARCH_AND_QUERY_FORM_URL_CLUCENE     BACKEND_CLUCENE QUERY_FORM


/**
 *
 * @author xavier helluy
 **/
typedef QDict<int> ClientList;
//typedef lucene::search::IndexSearcher * pointerToSearcher;

class KwestDaemon : public QObject, virtual public KwestSearchDaemonDCOPIface 
{
	Q_OBJECT
	public:
		KwestDaemon();

		~KwestDaemon();

    		// Here is the implementation of the example DCOP interface methods.
		virtual int  askForNewQuery( const QString newQuery ,const  QString SocketPath , 
					      QStringList indexesPathList , const int MaxResults,
					     const bool html ,  const bool peer2peer);
		
		void customEvent(QCustomEvent *);
	
	public slots:
		int startQuery( QString  newQuery  , QStringList indexesPathList, 
				    QString SocketPath , bool  peer2peer);
		
	signals:
		void newQueryRequestIsValid(QString query);
		

	private:
		QString m_socketPath;
		//QStringList m_indexesPathList;
		QString m_strValue;
		int m_intValue;
		// parseSearchEngine should return 1 if successfull.
		// it sets up a list of search-engines from the remote query-string
		// it sets up the requested query for each searchengine 
		// WARNING TODO so far only one searchengine per query is supported
		// To support querying multiple searchengines at the same time, one should 
		// create a new client with its own sockets, ....
		bool parseSearchEnginesAndAssignQueries(const QString &);
		QString m_requestedSearchEngine;
		QString m_requestedQuery;
		QStringList m_knownSearchEngineList;
		bool isQueryValid(const QString & Query);
		bool isSearchEngineSupported(const QString & SearchEngineString);
		QString isRequestStartingWithSupportedSearchEngine(const QString & requestedSearchEngine);

		int  initCLuceneIndexes(QStringList indexesPathList);
		int NumberOfQueriesRunning;
		int MaxLimitOfNumberOfQueriesRunning;
		
		QMutex mutex;
		QDict <SearchThread>    SearchThreadList;  // list of thread running queries Key=query
		QDict <ClientList>    QueryList;  //Key = of type Query
#ifdef HAS_CLUCENE
		KCLuceneDaemon kclucenedaemon;
#endif
};

#endif
