/*
    yahooeditaccount.cpp - UI Page to edit a Yahoo account

    Copyright (c) 2003 by Matt Rogers <mattrogers@sbcglobal.net>
    Copyright (c) 2002 by Gav Wood <gav@kde.org>

    Copyright (c) 2002 by the Kopete developers  <kopete-devel@kde.org>

    *************************************************************************
    *                                                                       *
    * 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.                                   *
    *                                                                       *
    *************************************************************************
*/

// QT Includes
#include <tqcheckbox.h>
#include <tqgroupbox.h>
#include <tqimage.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlineedit.h>
#include <tqpushbutton.h>
#include <tqspinbox.h>

// KDE Includes
#include <tdelocale.h>
#include <kdebug.h>
#include <tdemessagebox.h>
#include <krun.h>
#include <kurl.h>
#include <tdefiledialog.h>
#include <kpassdlg.h>
#include <tdeconfig.h>
#include <tdestandarddirs.h>
#include <kpixmapregionselectordialog.h>

// Kopete Includes
#include <addcontactpage.h>

// Local Includes
#include "yahooaccount.h"
#include "yahoocontact.h"
#include "yahooeditaccount.h"

// Yahoo Add Contact page
YahooEditAccount::YahooEditAccount(YahooProtocol *protocol, Kopete::Account *theAccount, TQWidget *parent, const char* /*name*/): YahooEditAccountBase(parent), KopeteEditAccountWidget(theAccount)
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;

	theProtocol = protocol;

	mPasswordWidget = new Kopete::UI::PasswordWidget( mAccountInfo );
	mAccountInfoLayout->add( mPasswordWidget );

	if(YahooAccount *acct = dynamic_cast<YahooAccount*>(account()))
	{	mScreenName->setText(acct->accountId());
		mScreenName->setReadOnly(true); //the accountId is Constant FIXME: remove soon!
		mScreenName->setDisabled(true);
		mAutoConnect->setChecked(acct->excludeConnect());
		mPasswordWidget->load( &acct->password() );

		TQString pagerServer = account()->configGroup()->readEntry("Server", "scs.msg.yahoo.com");
		int pagerPort = account()->configGroup()->readNumEntry("Port", 5050);
		if( pagerServer != "scs.msg.yahoo.com" || pagerPort != 5050 )
			optionOverrideServer->setChecked( true );
		else
			optionOverrideServer->setChecked( false );
		editServerAddress->setText( pagerServer );
		sbxServerPort->setValue( pagerPort );

		TQString iconUrl = account()->configGroup()->readEntry("pictureUrl", "");
		bool sendPicture = account()->configGroup()->readBoolEntry("sendPicture", false);
		optionSendBuddyIcon->setChecked( sendPicture );
    buttonSelectPicture->setEnabled( sendPicture );  
    connect( optionSendBuddyIcon, TQ_SIGNAL( toggled( bool ) ), buttonSelectPicture, TQ_SLOT( setEnabled( bool ) ) ); 
		editPictureUrl->setText( iconUrl );
		if( !iconUrl.isEmpty() )
			m_Picture->setPixmap( KURL( iconUrl ).path() );
		editPictureUrl->setEnabled( sendPicture );

		// Global Identity
		mGlobalIdentity->setChecked( account()->configGroup()->readBoolEntry("ExcludeGlobalIdentity", false) );
	}

	TQObject::connect(buttonRegister, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotOpenRegister()));
	TQObject::connect(buttonSelectPicture, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotSelectPicture()));

	optionSendBuddyIcon->setEnabled( account() );

	/* Set tab order to password custom widget correctly */
	TQWidget::setTabOrder( mAutoConnect, mPasswordWidget->mRemembered );
	TQWidget::setTabOrder( mPasswordWidget->mRemembered, mPasswordWidget->mPassword );
	TQWidget::setTabOrder( mPasswordWidget->mPassword, buttonRegister );

	show();
}

bool YahooEditAccount::validateData()
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;

	if(mScreenName->text().isEmpty())
	{	KMessageBox::queuedMessageBox(this, KMessageBox::Sorry,
			i18n("<qt>You must enter a valid screen name.</qt>"), i18n("Yahoo"));
		return false;
	}
	if(!mPasswordWidget->validate())
	{	KMessageBox::queuedMessageBox(this, KMessageBox::Sorry,
			i18n("<qt>You must enter a valid password.</qt>"), i18n("Yahoo"));
		return false;
	}
	return true;
}

Kopete::Account *YahooEditAccount::apply()
{
	kdDebug(YAHOO_GEN_DEBUG) << k_funcinfo << endl;

	if ( !account() )
		setAccount( new YahooAccount( theProtocol, mScreenName->text().lower() ) );

	YahooAccount *yahooAccount = static_cast<YahooAccount *>( account() );

	yahooAccount->setExcludeConnect( mAutoConnect->isChecked() );

	mPasswordWidget->save( &yahooAccount->password() );

	if ( optionOverrideServer->isChecked() )
	{
		yahooAccount->setServer( editServerAddress->text() );
		yahooAccount->setPort( sbxServerPort->value() );
	}
	else
	{
		yahooAccount->setServer( "scs.msg.yahoo.com" );
		yahooAccount->setPort( 5050 );
	}

	account()->configGroup()->writeEntry("pictureUrl", editPictureUrl->text() );
	account()->configGroup()->writeEntry("sendPicture", optionSendBuddyIcon->isChecked() );
	if ( optionSendBuddyIcon->isChecked() )
	{
		yahooAccount->setBuddyIcon( editPictureUrl->text() );
	}
	else
	{
		yahooAccount->setBuddyIcon( KURL( TQString() ) );
	}
	
	// Global Identity
	account()->configGroup()->writeEntry("ExcludeGlobalIdentity", mGlobalIdentity->isChecked() );

	return yahooAccount;
}

void YahooEditAccount::slotOpenRegister()
{
    KRun::runURL( "http://edit.yahoo.com/config/eval_register?new=1", "text/html" );
}

void YahooEditAccount::slotSelectPicture()
{
	KURL file = KFileDialog::getImageOpenURL( TQString(), this, i18n( "Yahoo Buddy Icon" ) );

	if ( file.isEmpty() )
		return;

	TQImage picture(file.path());
	if( !picture.isNull() )
	{
		picture = KPixmapRegionSelectorDialog::getSelectedImage( TQPixmap(picture), 96, 96, this );
		TQString newlocation( locateLocal( "appdata", "yahoopictures/"+ file.fileName().lower() ) ) ;
		file = KURL(newlocation);
		if( !picture.save( newlocation, "PNG" ))
		{
			KMessageBox::sorry( this, i18n( "An error occurred when trying to change the display picture." ), i18n( "Yahoo Plugin" ) );
			return;
		}
	}
	else
	{
		KMessageBox::sorry( this, i18n( "<qt>The selected buddy icon could not be opened. <br>Please set a new buddy icon.</qt>" ), i18n( "Yahoo Plugin" ) );
		return;
	}
	editPictureUrl->setText( file.path() );
	
	m_Picture->setPixmap( file.path() );
}

#include "yahooeditaccount.moc"
