//
// Copyright (C) 2003-2004 Grzegorz Jaskiewicz 	<gj at pointblue.com.pl>
// Copyright (C) 2002-2003	 Zack Rusin 	<zack@kde.org>
//
// gaducommands.cpp - all basic, and not-session dependent commands
// (meaning you don't have to be logged in for any of these).
// These delete themselves, meaning you don't
//  have to/can't delete them explicitly and have to create
//  them dynamically (via the 'new' call).
//
// 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.
//
// 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.

#include "gaducommands.h"
#include "gadusession.h"

#include <tqsocketnotifier.h>
#include <tqregexp.h>
#include <tqtextcodec.h>
#include <tqimage.h>

#include <tdelocale.h>
#include <kdebug.h>

#include <errno.h>

GaduCommand::GaduCommand( TQObject* parent, const char* name )
: TQObject( parent, name ), read_( 0 ), write_( 0 )
{
}

GaduCommand::~GaduCommand()
{
	//TQSocketNotifiers are children and will
	//be deleted anyhow
}

bool
GaduCommand::done() const
{
	return done_;
}

void
GaduCommand::checkSocket( int fd, int checkWhat )
{
	read_ = new TQSocketNotifier( fd, TQSocketNotifier::Read, this );
	read_->setEnabled( false );
	TQObject::connect( read_, TQ_SIGNAL( activated(int) ), TQ_SLOT( forwarder() ) );

	write_ = new TQSocketNotifier( fd, TQSocketNotifier::Write, this );
	write_->setEnabled( false );
	TQObject::connect( write_, TQ_SIGNAL( activated(int) ), TQ_SLOT( forwarder() ) );

	enableNotifiers( checkWhat );
}

void
GaduCommand::enableNotifiers( int checkWhat )
{
	if ( read_ ) {
		if( checkWhat & GG_CHECK_READ ) {
			read_->setEnabled( true );
		}
	}

	if ( write_ ) {
		if( checkWhat & GG_CHECK_WRITE ) {
			write_->setEnabled( true );
		}
	}
}

void
GaduCommand::disableNotifiers()
{
	if ( read_ ) {
		read_->setEnabled( false );
	}
	if ( write_ ) {
		write_->setEnabled( false );
	}
}

void
GaduCommand::deleteNotifiers()
{
	if ( read_ ) {
		delete read_;
		read_ = NULL;
	}
	if ( write_ ) {
		delete write_;
		write_ = NULL;
	}
}

void
GaduCommand::forwarder()
{
	emit socketReady();
}

RegisterCommand::RegisterCommand( TQObject* parent, const char* name )
:GaduCommand( parent, name ), state( RegisterStateNoToken ), session_( 0 ), uin( 0 )
{
}

RegisterCommand::RegisterCommand( const TQString& email, const TQString& password, TQObject* parent, const char* name )
:GaduCommand( parent, name ), state( RegisterStateNoToken ), email_( email ), password_( password ), session_( 0 ), uin( 0 )
{
}

RegisterCommand::~RegisterCommand()
{
}

unsigned int RegisterCommand::newUin()
{
	if ( state == RegisterStateDone ) {
		return uin;
	}
//	else
	return 0;
}
void
RegisterCommand::requestToken()
{
	kdDebug( 14100 ) << "requestToken Initialisation" << endl;
	state = RegisterStateWaitingForToken;

	if ( !( session_ = gg_token( 1 ) ) ) {
		emit error( i18n( "Gadu-Gadu" ), i18n( "Unable to retrieve token." ) );
		state = RegisterStateNoToken;
		return;
	}

	connect( this, TQ_SIGNAL( socketReady() ), TQ_SLOT( watcher() ) );
	checkSocket( session_->fd, session_->check );

	return;
}

void
RegisterCommand::setUserinfo( const TQString& email, const TQString& password, const TQString& token )
{
	email_ = email;
	password_ = password;
	tokenString = token;
}

void
RegisterCommand::cancel()
{
	deleteNotifiers();
	session_ = NULL;

}

void
RegisterCommand::execute()
{
	if ( state != RegisterStateGotToken || email_.isEmpty() || password_.isEmpty() || tokenString.isEmpty() ) {
		// get token first || fill information
		kdDebug(14100) << "not enough info to run execute, state: " << state << " , email: " << email_ << ", password present " << !password_.isEmpty() << ", token string:" << tokenString << endl;
		return;
	}
	session_ = gg_register3( email_.ascii(), password_.ascii(), tokenId.ascii(), tokenString.ascii(), 1 );
	if ( !session_ ) {
		error( i18n( "Gadu-Gadu" ), i18n( "Registration FAILED" ) );
		return;
	}
	state = RegisterStateWaitingForNumber;
	connect( this, TQ_SIGNAL( socketReady() ), TQ_SLOT( watcher() ) );
	checkSocket( session_->fd, session_->check );
}

void RegisterCommand::watcher()
{
	gg_pubdir* pubDir;

	if ( state == RegisterStateWaitingForToken  ) {
		disableNotifiers();
		if ( gg_token_watch_fd( session_ ) == -1 ) {
			deleteNotifiers();
			emit error( i18n( "Gadu-Gadu" ), i18n( "Unknown connection error while retrieving token." ) );
			gg_token_free( session_ );
			session_ = NULL;
			state = RegisterStateNoToken;
			return;
		}

		pubDir = (struct gg_pubdir *)session_->data;
		emit operationStatus( i18n( "Token retrieving status: %1" ).arg( GaduSession::stateDescription( session_->state ) ) );
		switch ( session_->state ) {
			case GG_STATE_CONNECTING:
				kdDebug( 14100 ) << "Recreating notifiers " << endl;
				deleteNotifiers();
				checkSocket( session_->fd, 0);
				break;
			case GG_STATE_ERROR:
				deleteNotifiers();
				emit error( i18n( "Gadu-Gadu token retrieve problem" ), GaduSession::errorDescription(  session_->error )  );
				gg_token_free( session_ );
				session_ = NULL;
				state = RegisterStateNoToken;
				return;
				break;
			case GG_STATE_DONE:
				struct gg_token* sp = ( struct gg_token* )session_->data;
				tokenId = (char *)sp->tokenid;
				kdDebug( 14100 ) << "got Token!, ID: " << tokenId << endl;
				deleteNotifiers();
				if ( pubDir->success ) {
					TQPixmap tokenImg;
					tokenImg.loadFromData( (const unsigned char *)session_->body, session_->body_size );
					state = RegisterStateGotToken;
					emit tokenRecieved( tokenImg, tokenId );
				}
				else {
					emit error( i18n( "Gadu-Gadu" ), i18n( "Unable to retrieve token." ) );
					state = RegisterStateNoToken;
					deleteLater();
				}
				gg_token_free( session_ );
				session_ = NULL;
				disconnect( this, TQ_SLOT( watcher() ) );
				return;
				break;
		}
		enableNotifiers( session_->check );
	}
	if ( state == RegisterStateWaitingForNumber ) {
		disableNotifiers();
		if ( gg_register_watch_fd( session_ ) == -1 ) {
			deleteNotifiers();
			emit error( i18n( "Gadu-Gadu" ), i18n( "Unknown connection error while registering." ) );
			gg_free_register( session_ );
			session_ = NULL;
			state = RegisterStateGotToken;
			return;
		}
		pubDir = (gg_pubdir*) session_->data;
		emit operationStatus( i18n( "Registration status: %1" ).arg( GaduSession::stateDescription( session_->state ) ) );
		switch ( session_->state ) {
			case GG_STATE_CONNECTING:
				kdDebug( 14100 ) << "Recreating notifiers " << endl;
				deleteNotifiers();
				checkSocket( session_->fd, 0);
				break;
			case GG_STATE_ERROR:
				deleteNotifiers();
				emit error( i18n( "Gadu-Gadu Registration Error" ), GaduSession::errorDescription(  session_->error ) );
				gg_free_register( session_ );
				session_ = NULL;
				state = RegisterStateGotToken;
				return;
				break;

			case GG_STATE_DONE:
				deleteNotifiers();
				if ( pubDir->success && pubDir->uin ) {
					uin= pubDir->uin;
					state = RegisterStateDone;
					emit done( i18n( "Registration Finished" ), i18n( "Registration has completed successfully." ) );
				}
				else {
					emit error( i18n( "Registration Error" ), i18n( "Incorrect data sent to server." ) );
					state = RegisterStateGotToken;
				}
				gg_free_register( session_ );
				session_ = NULL;
				disconnect( this, TQ_SLOT( watcher() ) );
				deleteLater();
				return;
				break;
		}
		enableNotifiers( session_->check );
		return;
	}
}

RemindPasswordCommand::RemindPasswordCommand( TQObject* parent, const char* name )
: GaduCommand( parent, name ), uin_( 0 ), session_( 0 )
{
}

RemindPasswordCommand::RemindPasswordCommand( uin_t uin, TQObject* parent, const char* name )
: GaduCommand( parent, name ), uin_( uin ), session_( 0 )
{
}

RemindPasswordCommand::~RemindPasswordCommand()
{
}

void
RemindPasswordCommand::setUIN( uin_t uin )
{
	uin_ = uin;
}

void
RemindPasswordCommand::execute()
{
}

void
RemindPasswordCommand::watcher()
{
	disableNotifiers();

	if ( gg_remind_passwd_watch_fd( session_ ) == -1 ) {
		gg_free_remind_passwd( session_ );
		emit error( i18n( "Connection Error" ), i18n( "Password reminding finished prematurely due to a connection error." ) );
		done_ = true;
		deleteLater();
		return;
	}

	if ( session_->state == GG_STATE_ERROR ) {
		gg_free_remind_passwd( session_ );
		emit error( i18n( "Connection Error" ), i18n( "Password reminding finished prematurely due to a connection error." ) );
		done_ = true;
		deleteLater();
		return;
	}

	if ( session_->state == GG_STATE_DONE ) {
		struct gg_pubdir* p = static_cast<struct gg_pubdir*>( session_->data );
		TQString finished = (p->success) ? i18n( "Successfully" ) : i18n( "Unsuccessful. Please retry." );
		emit done( i18n( "Remind Password" ), i18n( "Remind password finished: " ) + finished );
		gg_free_remind_passwd( session_ );
		done_ = true;
		deleteLater();
		return;
	}
	enableNotifiers( session_->check );
}

ChangePasswordCommand::ChangePasswordCommand( TQObject* parent, const char* name )
: GaduCommand( parent, name ), session_( 0 )
{
}

ChangePasswordCommand::~ChangePasswordCommand()
{
}

void
ChangePasswordCommand::setInfo( uin_t uin, const TQString& passwd, const TQString& newpasswd, const TQString& newemail )
{
	uin_			= uin;
	passwd_		= passwd;
	newpasswd_	= newpasswd;
	newemail_		= newemail;
}

void
ChangePasswordCommand::execute()
{
}

void
ChangePasswordCommand::watcher()
{
	disableNotifiers();

	if (  gg_pubdir_watch_fd( session_ ) == -1 ) {
		gg_change_passwd_free( session_ );
		emit error( i18n( "Connection Error" ), i18n( "Password changing finished prematurely due to a connection error." ) );
		done_ = true;
		deleteLater();
		return;
	}

	if ( session_->state == GG_STATE_ERROR ) {
		gg_free_change_passwd( session_ );
		emit error( i18n( "State Error" ),
				i18n( "Password changing finished prematurely due to a session related problem (try again later)." ) );
		done_ = true;
		deleteLater();
		return;
	}

	if ( session_->state == GG_STATE_DONE ) {
		emit done( i18n( "Changed Password" ),  i18n( "Your password has been changed." ) );
		gg_free_change_passwd( session_ );
		done_ = true;
		deleteLater();
		return;
	}

	enableNotifiers( session_->check );
}


#include "gaducommands.moc"
