/***************************************************************************
                          dcclient.h  -  description
                             -------------------
    begin                : Thu Oct 4 2001
    copyright            : (C) 2001-2005 by Mathias Küster
    email                : mathen@users.berlios.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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef DCCLIENT_H
#define DCCLIENT_H

/**
  *@author Mathias Küster
  */

#include <qtimer.h>
#include <qstringlist.h>
//Added by qt3to4:
#include <QResizeEvent>
#include <QEvent>
#include <QHash>
#include <QMdiArea>
#include <QMdiSubWindow>

#include "DCDialogClient.h"

#include <dclib/dcobject.h>
#include <dclib/cclient.h>
#include <dclib/core/cstringlist.h>
#include <dclib/cmessagehandler.h>

#include "userlistmodel.h"

class DCChat;
class QListWidgetItem;

class DCClient : public QWidget, private Ui::DCDialogClient, public CClient {
Q_OBJECT

public:
	/** construtor */
	DCClient(QWidget* parent, const char *name, Qt::WindowFlags wflags);
	/** destructor */
	virtual ~DCClient();

	/** */
	void InitDocument();
	/**
	 * Moved out of InitDocument so it can be called by DCConnectionManager after
	 * the widget is visible, otherwise things are not sized properly
	 */
	void InitSizes();
	/** */
	void DeInitDocument();

	/** chat */
	void DC_Chat( CMessageChat * MessageChat );
	/** myinfo */
	void DC_MyInfo( CMessageMyInfo * MessageMyInfo );
	/** nicklist */
	void DC_NickList( CMessageNickList * MessageNickList );
	/** oplist */
	void DC_OpList( CMessageNickList * MessageOpList );
	/** hello */
	void DC_Hello( CMessageHello * MessageHello );
	/** quit */        	
	void DC_Quit( CMessageQuit * MessageQuit );
	/** */
	void DC_PrivateChat( CMessagePrivateChat * msg, bool bLocal = FALSE );
	/** */
	void DC_PrivateChat( QString nick, QString fromnick, QString message, bool bLocal = FALSE );

	/** */
	static bool GetOPKickMessage( QString & message );
	/** */
	static bool GetOPForceMoveMessage( QString & message, QString & host );

	/** */
	bool OPKick( QString nick, QString message );
	/** */
	bool OPForceMove( QString nick, QString message, QString host );

	/** */
	void UpdateStatusBar();

	/** */
	void CloseChat( DCChat * chat );
	/** */
	void CloseAllChats();

	/** callback function */
	virtual int DC_CallBack( CObject * Object );
	/** overridden so that the columns are resized on show() */
	virtual void show();

	/** */
	QString findNick( QString part, uint which );
	
	/** Replaces the %[tag] s in the menu command with their values */
	QString replaceCommandTags( QString command, QString remotenick );
	
	/** Checks for and sends auto responses */
	void doAutoResponse( QString nick, QString message, DCChat * chatobject );
	
	/** Jump to the nick in the userlist */
	void jumpToNick( const QString & nick );
	
	/** Enable userlist sorting */
	void enableUserListSorting();
	
	/** Disable userlist sorting */
	void disableUserListSorting();
	
	/** Centre on MDI area */
	void centreOnMdiArea();
	/** Get our mdi sub window */
	QMdiSubWindow * GetMdiSubWindow() { return m_pContainerWindow; };

private:
	/** gets the list of selected nicks */
	int selectedItems( CList<CString> * lst );
	/** */
	void addUser( QString nick );
	/** */
	void UpdateCaption();
	/** */
	void SetCrypt( bool b );
	/** */
	void SetConnection( bool b );
	/** */
	void UpdateClientColumn( bool enabled, int index, int width );
	/** */
	void SaveClientColumn();
	
	/** the private chat widgets */
	CStringList * m_pChatList;
	/** */
	CMessageChat m_ChatFloodMessage;
	/** */
	long m_nChatFloodCounter;
	/** */
	bool m_bUserPassword;
	/** the hubchat widget */
	DCChat * m_pHubChat;
	/** */
	bool m_bUseTabWidget;
	/** Nick of last auto response trigger */
	QString lastAutoNick;
	/** Time of last auto response trigger */
	long lastAutoResponseTime;
	/** Userlist model */
	UserListModel * m_pUserListModel;
	/** The MDI window that contains us */
	QMdiSubWindow * m_pContainerWindow;
	/** Last part of a nick for which tab completion requested */
	QString lastPart;
	/** List of nicks matching lastpart */
	QStringList lastMatches;

protected:
	/** */
	QMdiArea * m_pMdiArea;
	/** */
	CThread m_Thread;
	/** */
	QTimer m_Timer;
	/** */
	CList<CObject> * m_pMessageList;
	/** */
	QHash<QString, CMessageMyInfo> * m_pMyInfoHash;

	/** resize the ListView columns */
	void ResizeListViewColumn();

	/** event filter */
	virtual bool eventFilter( QObject * object, QEvent * event );
	/** close event handler */
	// virtual void closeEvent( QCloseEvent * ) {};
	/** resize event handler */
	virtual void resizeEvent( QResizeEvent * );

private slots:
	/** */
	void timerDone();
	/** */
	void slotDoubleClickedUserList( const QModelIndex & index );
	/** */
	void slotDoubleClickedChatUserList( QListWidgetItem * item );
	/** */
	void slotContextMenuUserList( const QPoint & );
	/** */
	void slotDoubleClickedUserChat( QListWidgetItem * item );
	/** */
	void slotHubConnect();
	/** */
	/* void slotSSLInfo(); */
	/** current tab widget change slot */
	void slotTabWidgetChatCurrentChange(QWidget*);

	void slotSaveColumnWidth(int section, int oldSize, int newSize);
	/** */
	void slotTabCornerCloseWidgetClicked();
};

#endif
