/* This file is part of the KDE project
   Copyright (c) 2004 Kevin Ottens <ervin ipsquad net>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#ifndef REMOTEIMPL_H
#define REMOTEIMPL_H

#include <kio/global.h>
#include <kio/job.h>
#include <kurl.h>

#include <qstring.h>

// define CONFIG_INDEXES_URL     
#define CONFIG_INDEXES_URL        "clucene:/___S___"
#define CONFIG_INDEXES_SERVICE    "kclucene_indexer_config"
#define CONFIG_INDEXES_EXEC       "kclucene-indexer-config"

// define GUI_SEARCH_URL         
#define GUI_SEARCH_URL            "clucene:/___G___"
#define GUI_SEARCH_SERVICE        "kclucene_search_dialog"
#define GUI_SEARCH_EXEC           "kclucene-search-dialog"

// define HTML_SEARCH_URL         
#define HTML_SEARCH_URL            "clucene:/___HS___"
#define HTML_SEARCH_SERVICE        ""
#define HTML_SEARCH_EXEC           ""

// define GUI_SEARCH_URL         
#define GO_HOME_URL               "clucene:/___H___"

// #efine SEARCH AND QUERY form  
#define SEARCH_AND_QUERY_FORM_URL "search?query="

// define SEARCH form            
#define SEARCH_FORM_URL            "clucene:/search"

// define STORE DIRECTORY          
#define BACKUP_URL                "clucene:/___bckp___"

#define HTML_RESULTS_URL              "results"
#define HTML_RESULTS_NAME         

// define START OF URL FIELD INSIDE INDEX 
#define START_URL 		"CLBU "



#include "clucenedcoprequest.h"
#include <kprocess.h>
#include <kfileitem.h>
#include <kconfig.h>


using namespace KIO;

class CLuceneImpl : public QObject
{
	Q_OBJECT
public:
	CLuceneImpl();

	~CLuceneImpl();
	
	QString m_buf;
	QString m_kprocess;
	bool m_isAutoCompletion;
	QString m_queryName;

	
	
	void getResultsList(QStringList& resultsLust);
	const UDSEntry pathToUDSEntry(const QString& path, const QString& display,
			const QString& mime, const QString modTime, const QString& mode_T,
			const QString& size, const QString& path2pixmap);
	
	/*			      const QString& url = QString::null, const QString& icon = QString::null);*/
	
	bool startGuiSearch();
	bool startGuiConfig();
	void urlToPattern(const KURL& url);
	


	
	void createTopLevelEntry(KIO::UDSEntry &entry) const;
		
	/*TODO: Start a GUI interface for the Querys. Query string is passed to the slave via a 
	Kprocess stdout. The slave uses this query to contact the dcopDaemon*/
	bool createGUISearchEntry(KIO::UDSEntry &entry) const;	
	bool createGoHomeEntry(KIO::UDSEntry &entry) const;	
	bool createDesktopFile(KIO::UDSEntry& e , const KURL & url , const QString & name);
	
	/*TODO:Configure lucene indexes: do indexing, updates, and so on */
	bool createConfigIndexesEntry(KIO::UDSEntry &entry) const;
	
	
	bool createHTMLSearchEntry(KIO::UDSEntry &entry) const;
	
	/*TODO: Wizard entry, will go in steps through the process of creating an index
	and configuring the search and so on*/
	bool createHTMLReportEntry(KIO::UDSEntry &entry) const;
	
	bool createHTMLSearchFormEntry(KIO::UDSEntry &entry) const;

	bool isWantedURL(const KURL &url ,QString wizardUrl) const;

	/*FIXME: pseudo-create the list of Hits UDS entries */
	void listRoot(QValueList<KIO::UDSEntry> &list) ;  //FIXME:const 
	

	
	void setQuery(QString query);
	
	
	/*start the query to the dcop daemon*/
	void startQuery();
	//check if config file has been modified on disk and reload config if so
	bool hasConfigFileChanged();


	/*display a html view of the results. 
	Can be: empty-no results yet || statistics only-no highlighting yet  || highlighting a la google*/
	int displayHTMLResults();
	
	/*FIXME: update the config of the slave, simply reloads settings*/
	KURL query_url();
	void setQuery_url( KURL&);
	QString query();
	void addQueryToList();
	void createQueryEntry(KIO::UDSEntry& e , const  KURL & url , const QString & name , 
			      const QString & iconName);
	void createBackup(KIO::UDSEntry& e);
	void setIndexesPaths(QStringList);

	void readConfig();
	
	QStringList m_queryList;
	QStringList m_query_urlList;
	
	
	CLuceneDCOPRequest m_request;

	
	public slots:
		void getOutput(KProcess *, char *, int); // KProcess related
		
		//slots activated from the dcopQueryInterface
		void processCLuceneHits();
		void processCLuceneHighlightings();
		void processAllHitsAreHere();
		
		//FIXME: is the buffer cleaned up ? and the process ID checked 
		void slotProcessExited(KProcess* );

		//slots for communication with external progs
		

		
	signals:
		//signals emitted towards the ioslave interface
		void HitsAreHere();
		void AllHitsAreHere();
		void HighLightingsAreHere();
		
		//signals for communication with external progs
		void OutPutToGUI();
		void OutPutToConfig();

private:
	
	
	KFileItem m_configItem;
	uint m_oldConfigFileModTime;
	KConfig m_cfg;
	
	QString m_query;
	int     m_html;
	QString m_display;  //could be a short notification string or path to a tmp-file
	bool    m_cleanHits;
	bool    m_hitsArrived;
	QValueList<KIO::UDSEntry> m_hits;
	
	KURL    m_query_url;
	KProcess *proc;
	
};


#endif
