#ifndef _KVI_IRCCONNECTION_H_
#define _KVI_IRCCONNECTION_H_
//=============================================================================
//
//   File : kvi_ircconnection.h
//   Created on Mon 03 May 2004 01:45:42 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2004 Szymon Stefanek <pragma at kvirc dot net>
//
//   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 opinion) 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. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//=============================================================================

#include "kvi_settings.h"
#include "kvi_qstring.h"
#include <qobject.h>

class KviConsole;
class KviIrcServer;
class KviProxy;
class KviIrcLink;
class KviDns;

/*
	WARNING: This class is work in progress and is not yet hardwired in the core
		It should take care of all the socket related stuff that
		actually is handled by KviConsole

	USAGE:
		KviConsole::x()
		{
			...
			KviIrcConnection * c = new KviIrcConnection(this,szNetworkName,pServer,pProxy);
			c->setBindAddress(szBindAddress);
			connect(c,SIGNAL(connectionFailed()),this,SLOT(connectionFailed()));
			c->start();
			...
		}
*/

class KVIRC_API KviIrcConnection : public QObject
{
	friend class KviConosle;
	Q_OBJECT
protected:
	// only KviConsole can create this
	// pConsole must NOT be null
	// pServer is a shallow pointer: Connection makes a copy of it internally, must NOT be null
	// pProxy may be null if a proxy is not desicred. Connection makes a copy of it internally
	KviIrcConnection(KviConsole * pConsole,
				const QString &szNetworkName,
				KviIrcServer * pServer,
				KviProxy * pProxy = 0);
	~KviIrcConnection();
private:
	KviConsole                   * m_pConsole;       // shallow, never null
	KviIrcServer                 * m_pServer;        // owned, never null
	KviProxy                     * m_pProxy;         // owned, may be null if proxy is not used
	QString                        m_szNetworkName;
	KviIrcLink                   * m_pLink;          // owned, may be null!
	
	
	QString                        m_szBindAddress;  // force binding to this address

	// Auxiliary stuff
	QTimer                       * m_pStartTimer;    // timer used to start the connection
	KviDns                       * m_pProxyDns;      // the dns object for the proxy hostnames
	KviDns                       * m_pServerDns;     // the dns object for the server hostnames
public:
	// returns a pointer to the owning console
	// the pointer is NEVER null
	KviConsole * console(){ return m_pConsole; };
	// never null
	KviIrcServer * server(){ return m_pServer; };
	// may be null
	KviProxy * proxy(){ return m_pProxy; };
	// always non-empty string
	const QString &networkName(){ return m_szNetworkName; };
	// da link
	KviIrcLink * link(){ return m_pLink; };
	
	// force binding to this address/interface
	void setBindAddress(const QString &szBindAddress){ m_szBindAddress = szBindAddress; };
protected:
	// this starts an asynchronous connection attempt
	// you must return control to the qt core for the connection
	// operations to be initiated
	void start();
private slots:
	void startConnection();
	void linkDestroyed();
	void serverLookupTerminated(KviDns *);
	void proxyLookupTerminated(KviDns *);
private:
	void destroyLink();
	void createLink(const QString &szLinkFilterName);
	void lookupProxyHostname();
	void lookupServerHostname();
	void haveServerIp();
	bool validateLocalAddress(const QString &szAddress,QString &szBuffer);
signals:
	void connectionFailed(); // the connection attempt has failed
};



// TODO: KviIdentity


#endif //!_KVI_IRCCONNECTION_H_
