/***************************************************************************
                   userlistmodel.h  -  User List Model header
                             -------------------
    begin                : Sun Nov 18 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 USERLISTMODEL_H
#define USERLISTMODEL_H

#include <QAbstractItemModel>
#include <QString>
#include <QPixmap>
#include <QList>
#include <QHash>
#include <QStringList>
#include <QRegExp>

#include <dclib/dcos.h>

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

class UserListItem {

public:
	/** Constructor */
	UserListItem();
	/** Destructor */
	~UserListItem();
	
	/** Icon in user list */
	QPixmap pixmap;
	/** Nick */
	QString nick;
	/** Comment/Description */
	QString comment;
	/** Tag */
	QString tag;
	/** Speed/Connection */
	QString speed;
	/** Email address */
	QString email;
	/** Share size in bytes */
	ulonglong shared;
	/** IP address */
	QString ip;
	/** Lock/PK */
	QString lockpk;
	/** $Supports */
	QString supports;
	/** Position in list */
	int index;
	/** Sort on top (is operator) */
	bool sorttop;
	/** Highlight nick in red (unopened message) */
	bool highlight;
	
	/** Lower cased nick for sorting */
	QString nick_lc;
	/** Lower cased nick with [TAG] s removed for tab completion */
	QString nick_stripped;
	/** Lower cased comment for sorting */
	QString comment_lc;
	/** Lower cased tag for sorting */
	QString tag_lc;
	/** Lower cased speed for sorting */
	QString speed_lc;
	/** Lower cased email for sorting */
	QString email_lc;
};

#define COLUMN_NICK     0
#define COLUMN_COMMENT  1
#define COLUMN_TAG      2
#define COLUMN_SPEED    3
#define COLUMN_EMAIL    4
#define COLUMN_SHARE    5
#define COLUMN_IP       6
#define COLUMN_LOCKPK   7
#define COLUMN_SUPPORTS 8

class UserListModel : public QAbstractItemModel {
Q_OBJECT

public:
	/** Constructor */
	UserListModel();
	/** Destructor */
	~UserListModel();
	
	/** 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;
	
	/** delete all data */
	void clear();
	/** Tests if the nick is in the list */
	bool hasNick( const QString & nick ) const;
	/** Remove a user from the list */
	void removeUser( const QString & nick );
	/** Add a user to the list */
	void addUser( const QString nick,
	              const QPixmap icon = QPixmap(),
		      const QString comment = "",
		      const QString tag = "",
		      const QString speed = "",
		      const QString email = "",
		      const ulonglong share = 0,
		      const QString ip = "",
		      const QString lockpk = "",
		      const QString supports = "" );
	/** Update icon */
	void updateIcon( const QString & nick, const QPixmap icon );
	/** Update comment */
	void updateComment( const QString & nick, const QString comment );
	/** Update tag */
	void updateTag( const QString & nick, const QString tag );
	/** Update speed */
	void updateSpeed( const QString & nick, const QString speed );
	/** Update email */
	void updateEmail( const QString & nick, const QString email );
	/** Update share */
	void updateShare( const QString & nick, const ulonglong share );
	/** Update IP address */
	void updateIP( const QString & nick, const QString ip );
	/** Update Lock/PK */
	void updateLockPK( const QString & nick, const QString lockpk );
	/** Update Supports */
	void updateSupports( const QString & nick, const QString supports );
	/** Set sorttop */
	void setSortTop( const QString & nick, const bool top );
	/** Set highlight */
	void setHighlight( const QString & nick, const bool highlight );
	/** Get index for nick */
	QModelIndex indexForNick( const QString & nick ) const;
	/** Get sort column */
	int getSortColumn() const { return sortColumn; };
	/** Get sort order */
	Qt::SortOrder getSortOrder() const { return sortOrder; };
	/** Get the list of nicks that match the given string
	 *  stripTags means anything inside [] will be removed before matching
	 *  e.g. [BBB]xyzzy = xyzzy, but the nicks returned will include any tags
	 */
	QStringList matchNicksContaining( const QString & part, bool stripTags = FALSE ) const;
	/** The same, but using startsWith not contains */
	QStringList matchNicksStartingWith( const QString & part, bool stripTags = FALSE ) const;

private:
	/** Hash of items keyed on nick */
	QHash<QString, UserListItem*> itemHash;
	/** Sorted list of items */
	QList<UserListItem*> itemList;
	/** Column sorted by */
	int sortColumn;
	/** Sort order */
	Qt::SortOrder sortOrder;
	/** regular expression to remove tags from nicks */
	QRegExp stripper;
};

#endif // USERLISTMODEL_H
