/***************************************************************************
                          dcspy.cpp  -  description
                             -------------------
    begin                : Fre Nov 8 2002
    copyright            : (C) 2002 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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <qcheckbox.h>
#include <qpushbutton.h>
#include <qlabel.h>
#include <qdatetime.h>
#include <qapplication.h>
#include <qcursor.h>
#include <qclipboard.h>

#include <QMdiArea>
#include <QMdiSubWindow>

#include "dcconfig.h"
#include "dciconloader.h"
#include "dcmenuhandler.h"

#include "dcspy.h"

#include <dclib/cquerymanager.h>

/** */
DCSpy * g_pSpy = 0;

/** */
DCSpy::DCSpy(QWidget *parent, const char *name, Qt::WindowFlags wflags) : QWidget(parent,name,wflags)
{
	setupUi(this);

	// set default icon
	setIcon( g_pIconLoader->GetPixmap(eiSPY) );
	
	model = new SearchSpyModel();
	TreeView_SPY->setModel( model );
	
	if ( (parent != 0) && (qobject_cast<QMdiArea*>(parent) != 0) )
	{
		m_pContainerWindow = new QMdiSubWindow();
		m_pContainerWindow->setWidget(this);
		(qobject_cast<QMdiArea*>(parent))->addSubWindow(m_pContainerWindow);
		m_pContainerWindow->setIcon( g_pIconLoader->GetPixmap(eiSPY) );
	}
	else
	{
		m_pContainerWindow = 0;
	}
	
	InitDocument();

	g_pSpy = this;
}

/** */
DCSpy::~DCSpy()
{
	g_pSpy = NULL;
	
	delete model;
	
	m_Timer.stop();
}

/** */
void DCSpy::InitDocument()
{
	StringMap * map;

	// restore settings
	if ( g_pConfig->GetMap("SPYVIEW",map) == TRUE )
	{
		if ( ((*map)["WIDTH"].toInt() > 0) && ((*map)["HEIGHT"].toInt() > 0) )
		{
			if ( m_pContainerWindow != 0 )
			{
				m_pContainerWindow->setGeometry( (*map)["X"].toInt(), (*map)["Y"].toInt(), (*map)["WIDTH"].toInt(), (*map)["HEIGHT"].toInt() );
			}
		}
		
		if ( (*map)["CHECKED"] == "1" )
		{
			CheckBox_ENABLESPY->setChecked(TRUE);
		}
		
		if ( (*map)["HIDETTH"] == "1" )
		{
			CheckBox_HIDETTH->setChecked(TRUE);
		}
	}

	connect(PushButton_CLEAR, SIGNAL(clicked()), this, SLOT(slotClickedClear()) );
	
	connect( &m_Timer, SIGNAL(timeout()), this, SLOT(timerDone()) );
	
	connect(TreeView_SPY, SIGNAL(customContextMenuRequested( const QPoint & )), this, SLOT(slotContextMenuSpy( const QPoint & )) );
	
	m_Timer.start( 1000, TRUE );
}

/** */
void DCSpy::DeInitDocument()
{
	StringMap * map;

	// save search view settings
	g_pConfig->GetMap("SPYVIEW",map);

	if (m_pContainerWindow != 0 )
	{
		(*map)["X"]         = QString().setNum(m_pContainerWindow->x());
		(*map)["Y"]         = QString().setNum(m_pContainerWindow->y());
		(*map)["WIDTH"]     = QString().setNum(m_pContainerWindow->width());
		(*map)["HEIGHT"]    = QString().setNum(m_pContainerWindow->height());
		(*map)["VISIBLE"]   = QString().setNum(m_pContainerWindow->isVisible());
		(*map)["MAXIMIZED"] = QString().setNum(m_pContainerWindow->isMaximized());
		(*map)["MINIMIZED"] = QString().setNum(m_pContainerWindow->isMinimized());
		(*map)["ENABLED"]   = QString().setNum(m_pContainerWindow->isEnabled());
	}
	(*map)["CHECKED"]   = QString().setNum(CheckBox_ENABLESPY->isChecked());
	(*map)["HIDETTH"]   = QString().setNum(CheckBox_HIDETTH->isChecked());
}

/** */
void DCSpy::slotClickedClear()
{
	model->clear();
}

/** */
void DCSpy::timerDone()
{
	if ( CQueryManager::Instance() )
	{
		// update search stat
		TextLabel_SEARCHCOUNTACTIVE->setText( CString().setNum( CQueryManager::Instance()->GetStat(essCOUNTACTIVE)).Data() );
		TextLabel_SEARCHCOUNTPASSIVE->setText( CString().setNum( CQueryManager::Instance()->GetStat(essCOUNTPASSIVE)).Data() );
		TextLabel_SEARCHCOUNTREJECT->setText( CString().setNum( CQueryManager::Instance()->GetStat(essCOUNTREJECT)).Data() );
		TextLabel_SEARCHCOUNTERROR->setText( CString().setNum( CQueryManager::Instance()->GetStat(essCOUNTERROR)).Data() );
		TextLabel_RESULTCOUNT->setText( CString().setNum( CQueryManager::Instance()->GetStat(essRESULTCOUNT)).Data() );
		TextLabel_RESULTCOUNTERROR->setText( CString().setNum( CQueryManager::Instance()->GetStat(essRESULTCOUNTERROR)).Data() );
	}

	m_Timer.start( 1000, TRUE );
}

/** search */
void DCSpy::DC_Search( CMessageSearch * MessageSearch )
{
	if ( CheckBox_ENABLESPY->isChecked() )
	{
		QString s = MessageSearch->m_sString.Data();
		
		if ( (s.left(4) == "TTH:") && (CheckBox_HIDETTH->isChecked()) )
		{
			return;
		}
		
		if ( s.left(4) != "TTH:" )
		{
			s = s.lower();
		}
		
		model->search( s );
	}
}

/** */
void DCSpy::slotContextMenuSpy( const QPoint & /* point */ )
{
	QMenu * m;
	QAction * chosen = 0;
	
	QModelIndexList list = TreeView_SPY->selectionModel()->selectedIndexes();
	if ( list.isEmpty() )
	{
		return;
	}
	QModelIndex index = list.first(); 
	
	QVariant data = model->data( index );
	
	m = new QMenu(this);
	QAction * copy = DCMenuHandler().addAction( m, emiCOPY );
	
	chosen = m->exec(QCursor::pos());
	
	delete m;
	
	if ( chosen == copy )
	{
		QApplication::clipboard()->setText(data.toString());
	}
}
