/*
    jinglevoicesessiondialog.cpp - GUI for a voice session.

    Copyright (c) 2006      by Michaƫl Larouche     <michael.larouche@kdemail.net>

    Kopete    (c) 2001-2006 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.                                   *
    *                                                                       *
    *************************************************************************
*/
#include "jinglevoicesessiondialog.h"

// TQt includes
#include <tqlabel.h>
#include <tqpixmap.h>
#include <tqimage.h>

// Jingle includes
// #include "jinglevoicesession.h"
// #include "jinglesessionmanager.h"
#include "voicecaller.h"

// KDE includes
#include <tdelocale.h>
#include <kpushbutton.h>

// Kopete includes
#include "jabberaccount.h"
#include "jabbercontact.h"
#include "jabbercontactpool.h"

#include "kopeteglobal.h"
#include "kopetemetacontact.h"

using namespace XMPP;

JingleVoiceSessionDialog::JingleVoiceSessionDialog(const Jid &peerJid, VoiceCaller *caller, TQWidget *parent, const char *name)
 : JingleVoiceSessionDialogBase(parent, name), m_session(caller), m_peerJid(peerJid), m_sessionState(Incoming)
{
	TQString contactJid = m_peerJid.full();
	setCaption( i18n("Voice session with %1").arg(contactJid) );

	connect(buttonAccept, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotAcceptClicked()));
	connect(buttonDecline, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotDeclineClicked()));
	connect(buttonTerminate, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotTerminateClicked()));

// NOTE: Disabled for 0.12
#if 0
	connect(m_session, TQT_SIGNAL(sessionStarted()), this, TQT_SLOT(sessionStarted()));
	connect(m_session, TQT_SIGNAL(accepted()), this, TQT_SLOT(sessionAccepted()));
	connect(m_session, TQT_SIGNAL(declined()), this, TQT_SLOT(sessionDeclined()));
	connect(m_session, TQT_SIGNAL(terminated()), this, TQT_SLOT(sessionTerminated()));
#endif
	connect(m_session, TQT_SIGNAL(accepted( const Jid & )), this, TQT_SLOT( sessionAccepted(const Jid &) ));
	connect(m_session, TQT_SIGNAL(in_progress( const Jid & )), this, TQT_SLOT( sessionStarted(const Jid &) ));
	connect(m_session, TQT_SIGNAL(rejected( const Jid& )), this, TQT_SLOT( sessionDeclined(const Jid &) ));
	connect(m_session, TQT_SIGNAL(terminated( const Jid& )), this, TQT_SLOT( sessionTerminated(const Jid &) ));

	// Find JabberContact for the peer and fill this dialog with contact information.
	JabberContact *peerContact = static_cast<JabberContact*>( m_session->account()->contactPool()->findRelevantRecipient( m_peerJid ) );
	if( peerContact )
	{
		setContactInformation( peerContact );
	}
	
	labelSessionStatus->setText( i18n("Incoming Session...") );
	buttonAccept->setEnabled(true);
	buttonDecline->setEnabled(true);
}

JingleVoiceSessionDialog::~JingleVoiceSessionDialog()
{
	//m_session->account()->sessionManager()->removeSession(m_session);
}

void JingleVoiceSessionDialog::setContactInformation(JabberContact *contact)
{
	if( contact->metaContact() )
	{
		labelDisplayName->setText( contact->metaContact()->displayName() );
		labelContactPhoto->setPixmap( TQPixmap(contact->metaContact()->photo()) );
	}
	else
	{
		labelDisplayName->setText( contact->nickName() );
		labelDisplayName->setPixmap( TQPixmap(contact->property(Kopete::Global::Properties::self()->photo()).value().toString()) );
	}
}

void JingleVoiceSessionDialog::start()
{
	labelSessionStatus->setText( i18n("Waiting for other peer...") );
	buttonAccept->setEnabled(false);
	buttonDecline->setEnabled(false);
	buttonTerminate->setEnabled(true);
	//m_session->start();
	m_session->call( m_peerJid );
	m_sessionState = Waiting;
}

void JingleVoiceSessionDialog::slotAcceptClicked()
{
	labelSessionStatus->setText( i18n("Session accepted.") );
	buttonAccept->setEnabled(false);
	buttonDecline->setEnabled(false);
	buttonTerminate->setEnabled(true);
	
	//m_session->accept();
	m_session->accept( m_peerJid );
	m_sessionState = Accepted;
}

void JingleVoiceSessionDialog::slotDeclineClicked()
{
	labelSessionStatus->setText( i18n("Session declined.") );
	buttonAccept->setEnabled(false);
	buttonDecline->setEnabled(false);
	buttonTerminate->setEnabled(false);

	//m_session->decline();
	m_session->reject( m_peerJid );
	m_sessionState = Declined;
	finalize();
}

void JingleVoiceSessionDialog::slotTerminateClicked()
{
	labelSessionStatus->setText( i18n("Session terminated.") );
	buttonAccept->setEnabled(false);
	buttonDecline->setEnabled(false);
	buttonTerminate->setEnabled(false);

	//m_session->terminate();
	m_session->terminate( m_peerJid );
	m_sessionState = Terminated;
	finalize();
	close();
}

void JingleVoiceSessionDialog::sessionStarted(const Jid &jid)
{
	if( m_peerJid.compare(jid) )
	{
		labelSessionStatus->setText( i18n("Session in progress.") );
		buttonAccept->setEnabled(false);
		buttonDecline->setEnabled(false);
		buttonTerminate->setEnabled(true);
		m_sessionState = Started;
	}
}

void JingleVoiceSessionDialog::sessionAccepted(const Jid &jid)
{
	if( m_peerJid.compare(jid) )
	{
		labelSessionStatus->setText( i18n("Session accepted.") );
		buttonAccept->setEnabled(false);
		buttonDecline->setEnabled(false);
		buttonTerminate->setEnabled(true);
		m_sessionState = Accepted;
	}
}

void JingleVoiceSessionDialog::sessionDeclined(const Jid &jid)
{
	if( m_peerJid.compare(jid) )
	{
		labelSessionStatus->setText( i18n("Session declined.") );
		buttonAccept->setEnabled(false);
		buttonDecline->setEnabled(false);
		buttonTerminate->setEnabled(false);
		m_sessionState = Declined;
	}
}

void JingleVoiceSessionDialog::sessionTerminated(const Jid &jid)
{
	if( m_peerJid.compare(jid) )
	{
		labelSessionStatus->setText( i18n("Session terminated.") );
		buttonAccept->setEnabled(false);
		buttonDecline->setEnabled(false);
		buttonTerminate->setEnabled(false);
		m_sessionState = Terminated;
	}
}

void JingleVoiceSessionDialog::reject()
{
	finalize();
	TQDialog::reject();
}

void JingleVoiceSessionDialog::finalize()
{
	disconnect(m_session, TQT_SIGNAL(accepted( const Jid & )), this, TQT_SLOT( sessionAccepted(const Jid &) ));
	disconnect(m_session, TQT_SIGNAL(in_progress( const Jid & )), this, TQT_SLOT( sessionStarted(const Jid &) ));
	disconnect(m_session, TQT_SIGNAL(rejected( const Jid& )), this, TQT_SLOT( sessionDeclined(const Jid &) ));
	disconnect(m_session, TQT_SIGNAL(terminated( const Jid& )), this, TQT_SLOT( sessionTerminated(const Jid &) ));
}

#include "jinglevoicesessiondialog.moc"