/***************************************************************************
 *   Copyright (C) 2005 Novell, Inc.                                       *
 *                                                                         *
 *   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 Street, Fifth Floor, Boston, MA 02110-1301, USA           *
 ***************************************************************************/

#ifndef SEARCHDLG_H
#define SEARCHDLG_H

#include <searchdlg_layout.h>
#include "kerryapp.h"
#include "beaglesearch.h"
#include "query.h"
#include <tdeio/previewjob.h>
#include <tdefileitem.h>
#include <dcopinterface.h>
#include <tdeabc/addressbook.h>
#include <tdeabc/stdaddressbook.h>

class KFileItem;
class TQCheckBox;
class KBookmarkManager;
class KBookmarkGroup;
class HitWidget;

class SearchDlg : public HitsLayout, virtual public dcopIface
{
    Q_OBJECT
  

	enum ScopeType {
		Everywhere,
		Applications,
		Contacts,
		Documents,
		Conversations,
		Images,
		Media,
		Website,
		FilePathName
	};

	enum SortType {
                Type,
		Modified,
		Name,
		Relevance
	};

	enum DateRange {
		AnyDate,
		Today,
		SinceYesterday,
		ThisWeek,
		ThisMonth,
		ThisYear
	};

  public:
    SearchDlg(TQWidget *parent = 0, const char *name = 0);
    ~SearchDlg();

  public slots:
    void search(const TQString& text);
    void search(const TQString& text, const TQString& scope);
    void showSearchDialog();
    void configChanged();

  protected:
    void setDisplayAmount(int);
    void setSortOrder(int);

    void keyPressEvent(TQKeyEvent *);
    void showEvent(TQShowEvent *);
    bool eventFilter(TQObject *obj, TQEvent *e); 

  signals:
    void configure();
    void readConfiguration();

  private slots:
    void slotStartBeagle();
    void slotButtonClear();
    void slotClear();
    void slotPrevious();
    void slotNext();
    void search();
    void searchChanged(const TQString &);
    void searchHasOutput(BeagleSearch::BeagleResultList &results);
    void searchLostOutput(BeagleSearch::VanishedURIList &results);
    void searchFinished();
    void searchError (const TQString& error);
    void sortFilterResults();
    void slotOpen();
    void slotOpenDir();
    void slotMailTo(const TQString&);
    void slotOpenEvolution(const TQString&);
    void slotOpenThunderbird(const TQString&);
    void slotOpenKAddressBook(const TQString&);
    void slotOpenKNotes(const TQString&);
    void slotOpenURL(const TQString&);
    void slotContextMenu( int row, int col, const TQPoint & pos );
    void slotPreview(const KFileItem *, const TQPixmap &);
    void slotPreviewResult();
    void slotCleanClientList ();
    void itemUncollapsed(HitWidget*);

  private:
    BeagleSearch* beagle_search;
    Query current_query;

    TQPtrList<BeagleSearch> toclean_client_list;
    TQMutex toclean_list_mutex;
    int current_beagle_client_id;

    BeagleSearch::BeagleResultList results, displayed_results, new_results;
    int displayOffset;
    int displayAmount;
    int defaultSortOrder,currentSortOrder;
    void insertResult(BeagleSearch::beagle_result_struct *result,int index);
    void fillTableHits();
    void updateStatus();
    void showQuickTips();
    TQString formatBytes(TQString& bytesStr) const;
    TQCheckBox* cb_beagleStart;
    bool beagleJustStarted;

    void customEvent (TQCustomEvent *e);
    bool pending_showQuickTips;
    void displayResults(BeagleSearch::BeagleResultList &items);
    bool mimeTypeMatch( const TQString& mimeType, const TQStringList& mimeList ) const;
    bool canPreview( KFileItem* item );
    void updatePreviewMimeTypes();
    TQStringList* pPreviewMimeTypes;
    TDEIO::PreviewJob *pPreviewJob;
    void startPreview( const KFileItemList& items );
    void stopPreview();
    KFileItemList previewItems;

    TQString takeProperty( const TQString& property, TQStringList& propertyList);
    TQDateTime datetimeFromString( const TQString& );

    TQRegExp encodingRegexp;

    void searchProgramList(TQString relPath);
    bool checkUriInResults(const TQString& path) const;

    KBookmarkManager *bookmarkManager;
    TDEABC::AddressBook* m_addressBook;
    void searchAddressbook();
    void searchBookmarks(KBookmarkGroup);

    ScopeType showMode;
    DateRange dateRange;
    bool showBigTiles;

    bool ensureServiceRunning(const TQString & name);
    bool still_searching;
};

#endif
