/***************************************************************************
 *   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 CLUCENEDAEMON_H
#define CLUCENEDAEMON_H

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

#include <qmutex.h>

#include "kclucene_search_daemon_dcopiface.h"
#include "mythread2.h"

// #include <CLucene/StdHeader.h>
#include <CLucene.h>
#include <CLucene/search/IndexSearcher.h>





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

class CLuceneDaemon : public QObject, virtual public KCLuceneSearchDaemonDCOPIface 
{
	Q_OBJECT
	public:
		CLuceneDaemon();

		~CLuceneDaemon();

    // 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;
		
		QStringList getValidSubList(QStringList indexesPathList);
		
		bool isQueryValid(const QString & Query);
		bool isIndexesListValid(QStringList &indexesPathList);
		bool isIndexesPathListValid(QStringList indexesPathList);
		int  initIndexes(QStringList indexesPathList);
		int NumberOfQueriesRunning;
		int MaxLimitOfNumberOfQueriesRunning;
		
		int initSingleIndex( QStringList indexesPathList );
		int initMultiIndex( QStringList  indexesPathList);
		bool InitAllSingleSearchers(QStringList & indexesPathList);
		bool InitAllMultiSearchers(QStringList & indexesPathList);
		
		QMutex mutex;
		QDict <MyThread2>    MyThreadsList;  // list of thread running queries Key=query
		QDict <ClientList>    QueryList;  //Key = of type Query
		
		QDict <lucene::search::IndexSearcher>  m_indexSingleSearchersList;
		//QDict <lucene::search::IndexSearcher * * >  m_indexMultiSearchersList;
		QDict <QStringList   >  m_indexMultiSearchersList;
		
		QMap <QString, int> m_indexMultiSearchersListCounter;
		QMap <QString, int> m_indexSingleSearchersListCounter;
};

#endif
