/***************************************************************************
                   searchspymodel.h  -  Search Spy Model header
                             -------------------
    begin                : Sat Nov 3 2007
    copyright            : (C) 2007 by Edward Sheldrake
    email                : ejs1920@yahoo.co.uk
 ***************************************************************************/

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

#ifndef SEARCHSPYMODEL_H
#define SEARCHSPYMODEL_H

#include <QAbstractItemModel>
#include <QString>
#include <QList>
#include <QMap>

/**
 * @author Edward Sheldrake - inspired by dc-qt 0.2.0 alpha
 */

class SearchSpyItem {

public:
	/** Constructor */
	SearchSpyItem( QString search );
	/** Destructor */
	~SearchSpyItem();
	
	/** Increment count and date */
	void hit();
	
	/** Search text / TTH **/
	QString text;
	/** Time of search **/
	QString time;
	/** Number of times this search has been seen */
	int count;
	/** Position in list */
	int index;
};

class SearchSpyModel : public QAbstractItemModel {
Q_OBJECT

public:
	/** Constructor */
	SearchSpyModel();
	/** Destructor */
	~SearchSpyModel();
	
	/** number of rows */
	int rowCount( const QModelIndex & index ) const;
	/** number of columns */
	int columnCount( const QModelIndex & index ) const;
	/** gets data */
	QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const;
	/** gets headings */
	QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
	/** sort list */
	void sort( int column, Qt::SortOrder order = Qt::AscendingOrder );
	/** */
	QModelIndex index( int row, int column, const QModelIndex & parent ) const;
	/** */
	QModelIndex parent( const QModelIndex & parent ) const;
	
	/** got search string */
	void search( QString query );
	/** delete all data */
	void clear();

private:
	/** Map of items */
	QMap<QString, SearchSpyItem*> itemMap;
	/** Sorted list of items */
	QList<SearchSpyItem*> itemList;
	/** Column sorted by */
	int sortColumn;
	/** Sort order */
	Qt::SortOrder sortOrder;
};

#endif // SEARCHSPYMODEL_H
