/***************************************************************************
                          setpageservers.cpp  -  description
                             -------------------
    begin                : Thu Aug 16 2001
    copyright            : (C) 2003 by Troy Corbin Jr.
    email                : tcorbin@users.sourceforge.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 option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "setpageservers.moc"
#include <tqstyle.h>
#include <kicontheme.h>

setPageServers::setPageServers(TQWidget *parent, resource *Rsrc  ) : TQVBoxLayout(parent)
{
	TQStyle& Style = TQApplication::style();
	margin = Style.defaultFrameWidth();
	Resource = Rsrc;
	Servers_ListView = NULL;
	resetServer = FALSE;

	TabParent = new TQTabWidget( parent, "setPageDisplay" );
	initTab1();
	initTab2();
	addWidget( TabParent );
}
setPageServers::~setPageServers()
{
	Servers_ListView->saveLayout( kapp->config(), "Servers_ListView" );
	delete Servers_ButtonBox;
	delete Servers_ListView;
	delete GROUP_Servers;
	delete GROUP_Current;
}
///////////////////////////////////////
//
//	setPageServers::initTab1
//
///////////////////////////////////////
void setPageServers::initTab1( void )
{
	Tab1 = new TQVBox( TabParent );
	Tab1->setMargin( margin );

	/* Current Server */
	GROUP_Current = new TQGroupBox( 	1,
																	TQt::Vertical,
																	i18n( "Current Server" ),
																	Tab1 );
	
	Servers = new KComboBox ( GROUP_Current );
	Servers->setEditable( FALSE );

	/* Chess Servers ListView */
	GROUP_Servers = new TQGroupBox(	2,
																	TQt::Horizontal,
																	i18n( "Chess Servers" ),
																	Tab1 );
	
	Servers_ListView = new TDEListView( GROUP_Servers );
	Servers_ListView->addColumn( i18n( "Name" ) );
	Servers_ListView->addColumn( i18n( "URL" ) );
	Servers_ListView->setAllColumnsShowFocus( TRUE );
	Servers_ListView->setMultiSelection( FALSE );
	Servers_ListView->setShowSortIndicator( TRUE );
	Servers_ListView->restoreLayout( kapp->config(), "Servers_ListView" );

	Servers_ButtonBox = new KButtonBox( GROUP_Servers,TQt::Vertical );
		Servers_Button_Add = Servers_ButtonBox->addButton( i18n( "&Add..." ) );
		Servers_Button_Change = Servers_ButtonBox->addButton( i18n( "&Modify..." ) );
		Servers_Button_Delete = Servers_ButtonBox->addButton( i18n( "&Delete..." ) );
		Servers_ButtonBox->layout();

	BuildServerData();

	connect( Servers_Button_Add, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotServer_Add() ) );
	connect( Servers_Button_Change, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotServer_Modify() ) );
	connect( Servers_Button_Delete, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotServer_Delete() ) );
	connect( Servers, TQ_SIGNAL( activated(int) ), this, TQ_SLOT( slotCurrent(int) ) );
	connect( Servers_ListView, TQ_SIGNAL( selectionChanged() ), this, TQ_SLOT( selectionChanged() ) );

	TabParent->addTab( Tab1, i18n("Servers") );
}
///////////////////////////////////////
//
//	setPageServers::initTab2
//
///////////////////////////////////////
void setPageServers::initTab2( void )
{
	TQStringList FilterLevels;
	Tab2 = new TQVBox( TabParent );
	Tab2->setMargin( margin );

	/* Current Profanity Filter */
	BOX_Profanity = new TQGroupBox( 	1,
																	TQt::Vertical,
																	i18n( "Profanity Filter" ),
																	Tab2 );
	COMBO_Profanity = new KComboBox ( BOX_Profanity );
	COMBO_Profanity->setEditable( FALSE );
	FilterLevels << i18n("Filter Everything") << i18n("Default") << "---" << "---" << "---" << i18n("No Filtering");
	COMBO_Profanity->insertStringList( FilterLevels );
	COMBO_Profanity->setCurrentItem( Resource->OPTION_Profanity );
	connect( COMBO_Profanity, TQ_SIGNAL( activated(int) ), this, TQ_SLOT( slot_Profanity(int) ) );

	BUTTON_Auto_Close_ICS = new TQCheckBox( i18n( "Automatically Close Previous ICS Match" ), Tab2 );
	BUTTON_Auto_Close_ICS->setChecked( Resource->OPTION_Auto_Close_Last_ICS );
	connect(	BUTTON_Auto_Close_ICS, TQ_SIGNAL( toggled(bool) ),
						this, TQ_SLOT( slot_Auto_Close_ICS(bool) ) );

	BUTTON_Private = new TQCheckBox( i18n( "Private Matches" ), Tab2 );
	BUTTON_Private->setChecked( Resource->OPTION_Private );
	connect( BUTTON_Private, TQ_SIGNAL( toggled(bool) ), this, TQ_SLOT( slot_Private(bool) ) );

	BUTTON_Premove = new TQCheckBox( i18n( "Enable Premove" ), Tab2 );
	BUTTON_Premove->setChecked( Resource->OPTION_Premove );
	connect( BUTTON_Premove, TQ_SIGNAL( toggled(bool) ), this, TQ_SLOT( slot_Premove(bool) ) );

	BUTTON_Kibitz = new TQCheckBox( i18n( "Enable Kibitzes" ), Tab2 );
	BUTTON_Kibitz->setChecked( Resource->OPTION_Kibitz );
	connect( BUTTON_Kibitz, TQ_SIGNAL( toggled(bool) ), this, TQ_SLOT( slot_Kibitz(bool) ) );

	BUTTON_Tell = new TQCheckBox( i18n( "Enable Unregistered Tells" ), Tab2 );
	BUTTON_Tell->setChecked( Resource->OPTION_Tell );
	connect( BUTTON_Tell, TQ_SIGNAL( toggled(bool) ), this, TQ_SLOT( slot_Tell(bool) ) );

	BUTTON_Shout = new TQCheckBox( i18n( "Enable Shouts" ), Tab2 );
	BUTTON_Shout->setChecked( Resource->OPTION_Shout );
	connect( BUTTON_Shout, TQ_SIGNAL( toggled(bool) ), this, TQ_SLOT( slot_Shout(bool) ) );

	BUTTON_Seek = new TQCheckBox( i18n( "Enable Seeks" ), Tab2 );
	BUTTON_Seek->setChecked( Resource->OPTION_Seek );
	connect( BUTTON_Seek, TQ_SIGNAL( toggled(bool) ), this, TQ_SLOT( slot_Seek(bool) ) );

	BOX_SeekTimer = new TQHBox( Tab2 );
		BUTTON_SeekTimer = new TQSpinBox( 3, 120, 1, BOX_SeekTimer );
		BUTTON_SeekTimer->setSuffix( i18n(" sec.") );
		BUTTON_SeekTimer->setValue( Resource->Seek_Timer / 10 );
		connect(	BUTTON_SeekTimer, TQ_SIGNAL( valueChanged(int) ),
							this, TQ_SLOT( slot_SeekTimer(int) ) );
		LABEL_SeekTimer = new TQLabel( i18n("Seconds Between Seek Updates"), BOX_SeekTimer );

	TabParent->addTab( Tab2, i18n("Options") );
}
///////////////////////////////////////
//
//	setPageServers::BuildServerData
//
///////////////////////////////////////
void setPageServers::BuildServerData( void )
{
	bool 	SERVER_FLAG(FALSE);
	TQStringList ServerList;
	int Index(0);
	serverList::Iterator	serversIT;

	/* Clear Comboboxes */
	Servers->clear();
	Servers_ListView->clear();
	/* Read the server list */
	if( Resource->servers.isEmpty() ) return;
	for ( serversIT = Resource->servers.begin(); serversIT != Resource->servers.end(); ++serversIT )
	{
		(void) new TQListViewItem( Servers_ListView,
															(*serversIT).Name,
															(*serversIT).URL );
		ServerList.append( (*serversIT).Name );
	}
	/* Insert engines into comboboxes */
	ServerList.sort();
	Servers->insertStringList( ServerList );
	/* Now run the list again, setting the current engine for each combobox */
	for ( serversIT = Resource->servers.begin(); serversIT != Resource->servers.end(); ++serversIT )
	{
 		if( (*serversIT).CurrentRef )
			for( Index = 0; Index < Servers->count(); Index++ )
				if( Servers->text(Index) == (*serversIT).Name )
				{
		 			Servers->setCurrentItem(Index);
					SERVER_FLAG = TRUE;
				}
	}
	/* This prevents a bug where you had to modify the current_server_combobox
		before you could get any servers to run. */
	if( ( !SERVER_FLAG ) && ( Servers->count() ) ) slotCurrent( Servers->currentItem() );

	Servers_Button_Change->setEnabled( FALSE );
	Servers_Button_Delete->setEnabled( FALSE );
}
///////////////////////////////////////
//
//	setPageServers::slotServer_Add
//
///////////////////////////////////////
void setPageServers::slotServer_Add( void )
{
	Server_Dialog = new dlg_server( TabParent, "ServerDialog", Resource );
	connect( Server_Dialog, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( BuildServerData() ) );
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageServers::slotServer_Modify
//
///////////////////////////////////////
void setPageServers::slotServer_Modify( void )
{
	TQListViewItem *Item;
	TQPtrList<TQListViewItem> Select = Servers_ListView->selectedItems();

	if( Select.isEmpty() ) return;
	Item = Select.first();
	Server_Dialog = new dlg_server( TabParent, "ServerDialog", Resource, Item->text(0) );
	connect( Server_Dialog, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( BuildServerData() ) );
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageServers::slotServer_Delete
//
///////////////////////////////////////
void setPageServers::slotServer_Delete( void )
{
	serverList::Iterator	serversIT;
	TQListViewItem *Item;
	TQPtrList<TQListViewItem> Select = Servers_ListView->selectedItems();

	if( Select.isEmpty() ) return;
	Item = Select.first();

	for ( serversIT = Resource->servers.begin(); serversIT != Resource->servers.end(); ++serversIT )
	{
		if( (*serversIT).Name == Item->text(0) ) break;
	}
	Resource->servers.remove(serversIT);
	BuildServerData();
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageServers::slotCurrent
//
///////////////////////////////////////
void setPageServers::slotCurrent( int Index )
{
	TQString Name;
	serverList::Iterator	serversIT;
	
	Name = Servers->text(Index);
	for ( serversIT = Resource->servers.begin(); serversIT != Resource->servers.end(); ++serversIT )
	{
		if( (*serversIT).CurrentRef ) (*serversIT).CurrentRef = FALSE;
		if( (*serversIT).Name == Name ) (*serversIT).CurrentRef = TRUE;
	}
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageServers::slot_Profanity
//
///////////////////////////////////////
void setPageServers::slot_Profanity( int Index )
{
	Resource->OPTION_Profanity = Index;
	emit enableApply();
	resetServer = TRUE;
}
///////////////////////////////////////
//
//	setPageServers::slot_Premove
//
///////////////////////////////////////
void setPageServers::slot_Premove( bool state )
{
	Resource->OPTION_Premove = state;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageServers::slot_Shout
//
///////////////////////////////////////
void setPageServers::slot_Shout( bool state )
{
	Resource->OPTION_Shout = state;
	emit enableApply();
	resetServer = TRUE;
}
///////////////////////////////////////
//
//	setPageServers::slot_Private
//
///////////////////////////////////////
void setPageServers::slot_Private( bool state )
{
	Resource->OPTION_Private = state;
	emit enableApply();
	resetServer = TRUE;
}
///////////////////////////////////////
//
//	setPageServers::slot_Kibitz
//
///////////////////////////////////////
void setPageServers::slot_Kibitz( bool state )
{
	Resource->OPTION_Kibitz = state;
	emit enableApply();
	resetServer = TRUE;
}
///////////////////////////////////////
//
//	setPageServers::slot_Tell
//
///////////////////////////////////////
void setPageServers::slot_Tell( bool state )
{
	Resource->OPTION_Tell = state;
	emit enableApply();
	resetServer = TRUE;
}
///////////////////////////////////////
//
//	setPageServers::slot_Seek
//
///////////////////////////////////////
void setPageServers::slot_Seek( bool state )
{
	Resource->OPTION_Seek = state;
	emit enableApply();
	resetServer = TRUE;
}
///////////////////////////////////////
//
//	setPageServers::slot_Auto_Close_ICS
//
///////////////////////////////////////
void setPageServers::slot_Auto_Close_ICS( bool state )
{
	Resource->OPTION_Auto_Close_Last_ICS = state;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageServers::slot_SeekTimer
//
///////////////////////////////////////
void setPageServers::slot_SeekTimer( int value )
{
	Resource->Seek_Timer = value * 10;
	emit enableApply();
}
///////////////////////////////////////
//
//	setPageServers::selectionChanged
//
///////////////////////////////////////
void setPageServers::selectionChanged( void )
{
	if( Servers_ListView->selectedItem() == 0 )
	{
		Servers_Button_Change->setEnabled( FALSE );
		Servers_Button_Delete->setEnabled( FALSE );
	}
	else
	{
		Servers_Button_Change->setEnabled( TRUE );
		Servers_Button_Delete->setEnabled( TRUE );
	}
}
