//=============================================================================
//
//   File : kvi_kvs_dnsmanager.cpp
//   Created on Wed 04 Aug 2004 04:38:31 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. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#define __KVIRC__

#include "kvi_kvs_dnsmanager.h"
#include "kvi_dns.h"
#include "kvi_app.h"
#include "kvi_window.h"
#include "kvi_out.h"
#include "kvi_iconmanager.h"
#include "kvi_kvs_script.h"
#include "kvi_kvs_variantlist.h"
#include "kvi_string.h"
#include "kvi_locale.h"
#include "kvi_error.h"

KviKvsDnsManager * KviKvsDnsManager::m_pInstance = 0;


KviKvsDnsObject::KviKvsDnsObject(KviDns * pDns,KviWindow * pWnd,const TQString &szQuery,bool bRebindOnWindowClose,KviKvsScript * pCallback,KviKvsVariantList * pParameterList)
{
	m_pDns = pDns;
	m_pWindow = pWnd;
	m_szQuery = szQuery;
	m_pCallback = pCallback;
	m_pParameterList = pParameterList;
	m_bRebindOnWindowClose = bRebindOnWindowClose;
}

KviKvsDnsObject::~KviKvsDnsObject()
{
	TQObject::disconnect(m_pDns,0,0,0);
	if(m_pDns->isRunning())
	{
		g_pApp->collectGarbage(m_pDns);
	} else {
		delete m_pDns;
	}
	if(m_pCallback)delete m_pCallback;
	if(m_pParameterList)delete m_pParameterList;
}


KviKvsDnsManager::KviKvsDnsManager()
: TQObject()
{
	m_pDnsObjects = 0;
}

KviKvsDnsManager::~KviKvsDnsManager()
{
	if(m_pDnsObjects)delete m_pDnsObjects;
}

void KviKvsDnsManager::init()
{
	if(KviKvsDnsManager::m_pInstance)
	{
		tqDebug("Trying to double init() the dns manager!");
		return;
	}
	KviKvsDnsManager::m_pInstance = new KviKvsDnsManager();
}

void KviKvsDnsManager::done()
{
	if(!KviKvsDnsManager::m_pInstance)
	{
		tqDebug("Trying to call done() on a non existing dns manager!");
		return;
	}
	delete KviKvsDnsManager::m_pInstance;
	KviKvsDnsManager::m_pInstance = 0;
}

void KviKvsDnsManager::addDns(KviKvsDnsObject * pObject)
{
	if(!m_pDnsObjects)
	{
		m_pDnsObjects = new KviPointerHashTable<void *,KviKvsDnsObject>;
		m_pDnsObjects->setAutoDelete(true);
	}
	m_pDnsObjects->replace(pObject->dns(),pObject);
	connect(pObject->dns(),TQ_SIGNAL(lookupDone(KviDns *)),this,TQ_SLOT(dnsLookupTerminated(KviDns *)));
}

void KviKvsDnsManager::dnsLookupTerminated(KviDns * pDns)
{
	KviKvsDnsObject * o = m_pDnsObjects->find(pDns);
	if(!o)
	{
		tqDebug("KviKvsDnsManager::dnsLookupTerminated(): can't find the KviKvsDnsObject structure");
		return;
	}

	if(!g_pApp->windowExists(o->window()))
	{
		if(o->rebindOnWindowClose())
		{
			o->setWindow(g_pActiveWindow);
		} else {
			// just kill it
			m_pDnsObjects->remove(pDns);
			return;
		}
	}

	if(o->callback())
	{
		KviKvsScript copy(*(o->callback()));
		if(!o->parameterList())
			o->setParameterList(new KviKvsVariantList());

		if(o->dns()->state() == KviDns::Failure)
		{
			// $4... is the magic data
			o->parameterList()->prepend(new KviKvsVariant()); // $3
			o->parameterList()->prepend(new KviKvsVariant(KviError::getDescription(o->dns()->error()))); // $2
			o->parameterList()->prepend(new KviKvsVariant((kvs_int_t)0)); // $1
		} else {
			TQString * fh = o->dns()->hostnameList()->first();
			TQString * fi = o->dns()->ipAddressList()->first();

			// $4... is the magic data
			o->parameterList()->prepend(new KviKvsVariant(fh ? *fh : TQString("?.?"))); // $3
			o->parameterList()->prepend(new KviKvsVariant(fi ? *fi : TQString("?.?.?.?"))); // $2
			o->parameterList()->prepend(new KviKvsVariant((kvs_int_t)1)); // $1
		}
		o->parameterList()->prepend(new KviKvsVariant(o->query())); // $0

		copy.run(o->window(),o->parameterList(),0,KviKvsScript::PreserveParams);

	} else {
		TQString szQuery = o->dns()->query();
		o->window()->output(KVI_OUT_HOSTLOOKUP,__tr2qs("DNS Lookup result for query \"%Q\""),&szQuery);
	
		if(o->dns()->state() == KviDns::Failure)
		{
			TQString szErr = KviError::getDescription(o->dns()->error());
			o->window()->output(KVI_OUT_HOSTLOOKUP,__tr2qs("Error: %Q"),&szErr);
		} else {
			int idx = 1;
			for(TQString * h = o->dns()->hostnameList()->first();h;h = o->dns()->hostnameList()->next())
			{
				o->window()->output(KVI_OUT_HOSTLOOKUP,__tr2qs("Hostname %d: %Q"),idx,h);
				idx++;
			}
			idx = 1;
			for(TQString * a = o->dns()->ipAddressList()->first();a;a = o->dns()->ipAddressList()->next())
			{
				o->window()->output(KVI_OUT_HOSTLOOKUP,__tr2qs("IP address %d: %Q"),idx,a);
				idx++;
			}
		}
	}

	m_pDnsObjects->remove(pDns);
}

#include "kvi_kvs_dnsmanager.moc"
