<commit_before>/*
    jabberchatsession.cpp - Jabber Chat Session

    Copyright (c) 2004 by <NAME>            <<EMAIL>>

    Kopete    (c) 2004 by the Kopete developers  <<EMAIL>>

    *************************************************************************
    *                                                                       *
    * 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 "jabberchatsession.h"

#include <qlabel.h>
#include <qimage.h>

#include <qfile.h>
#include <qicon.h>
#include <config-kopete.h>

#include <kconfig.h>
#include <kdebug.h>
#include <klocale.h>
#include <kicon.h>
#include <kaction.h>
#include <kactioncollection.h>
#include "kopetechatsessionmanager.h"
#include "kopetemessage.h"
#include "kopeteviewplugin.h"
#include "kopeteview.h"
#include "kopetemetacontact.h"

#include "jabberprotocol.h"
#include "jabberaccount.h"
#include "jabberclient.h"
#include "jabbercontact.h"
#include "jabberresource.h"
#include "jabberresourcepool.h"
#include "kioslave/jabberdisco.h"


JabberChatSession::JabberChatSession ( JabberProtocol *protocol, const JabberBaseContact *user,
											 Kopete::ContactPtrList others, const QString &resource )
											 : Kopete::ChatSession ( user, others, protocol ),
											 mTypingNotificationSent ( false )
{
	kDebug ( JABBER_DEBUG_GLOBAL ) << "New message manager for " << user->contactId ();

	setComponentData(protocol->componentData());

	// make sure Kopete knows about this instance
	Kopete::ChatSessionManager::self()->registerChatSession ( this );

	connect ( this, SIGNAL ( messageSent ( Kopete::Message &, Kopete::ChatSession * ) ),
			  this, SLOT ( slotMessageSent ( Kopete::Message &, Kopete::ChatSession * ) ) );

	connect ( this, SIGNAL ( myselfTyping ( bool ) ), this, SLOT ( slotSendTypingNotification ( bool ) ) );

	connect ( this, SIGNAL ( onlineStatusChanged(Kopete::Contact*, const Kopete::OnlineStatus&, const Kopete::OnlineStatus& ) ), this, SLOT ( slotUpdateDisplayName () ) );

	// check if the user ID contains a hardwired resource,
	// we'll have to use that one in that case
	XMPP::Jid jid = user->rosterItem().jid() ;

	mResource = jid.resource().isEmpty () ? resource : jid.resource ();
	slotUpdateDisplayName ();

#ifdef JINGLE_SUPPORT
	KAction *jingleSessionGui = new KAction(i18n("Show audio calls"), members().first());
	jingleSessionGui->setIcon(KIcon("voicecall"));
	connect(jingleSessionGui, SIGNAL(triggered(bool)), SLOT (slotJingleSessionGui() ));
	setComponentData(protocol->componentData());

	KAction *jingleSession = new KAction(i18n("Start audio call"), members().first());
	jingleSession->setIcon(KIcon("voicecall"));
	connect(jingleSession, SIGNAL(triggered(bool)), SLOT (slotJingleSession() ));
	setComponentData(protocol->componentData());

	Kopete::ContactPtrList chatMembers = members();
	if (chatMembers.first())
	{
		JabberResource *bestResource = account()->resourcePool()->bestJabberResource( static_cast<JabberBaseContact*>(chatMembers.first())->rosterItem().jid() );
		if (bestResource)
		{
			jingleSessionGui->setEnabled( bestResource->features().canJingle() );
			jingleSession->setEnabled( bestResource->features().canJingle() );
		}
		else
		{
			jingleSessionGui->setEnabled(false);
			jingleSession->setEnabled(false);
		}
	}


	/*KAction *jingleaudiocall = new KAction(i18n("Jingle Audio call" ), members().first());
	connect(jingleaudiocall, SIGNAL(triggered(bool)), SLOT (slotJingleAudioCall() ));
	setComponentData(protocol->componentData());
	jingleaudiocall->setEnabled( false );

	KAction *jinglevideocall = new KAction(i18n("Jingle Video call" ), members().first());
	connect(jinglevideocall, SIGNAL(triggered(bool)), SLOT (slotJingleVideoCall() ));
	setComponentData(protocol->componentData());
	jinglevideocall->setEnabled( false );*/

	//Kopete::ContactPtrList chatMembers = members ();
	//if ( chatMembers.first () )
	//{
		// Check if the current contact support Audio calls, also honor lock by default.
		// FIXME: we should use the active ressource
		//jingleaudiocall->setEnabled( bestResource->features().canJingleAudio() );
		//jinglevideocall->setEnabled( bestResource->features().canJingleVideo() );
	//}

	//FIXME : Toolbar does not show any action (either for MSN or XMPP)
	//	  It should be corrected in trunk.
	//actionCollection()->addAction( "jabberJingleaudiocall", jingleaudiocall );
	//actionCollection()->addAction( "jabberJinglevideocall", jinglevideocall );
	actionCollection()->addAction( "jingleSession", jingleSession );
	actionCollection()->addAction( "jingleSessionsGui", jingleSessionGui );

#endif

	setXMLFile("jabberchatui.rc");

}

JabberChatSession::~JabberChatSession( )
{
	JabberAccount * a = dynamic_cast<JabberAccount *>(Kopete::ChatSession::account ());
	if( !a ) //When closing kopete, the account is partially destroyed already,  dynamic_cast return 0
		return;
	if ( a->configGroup()->readEntry ("SendEvents", true) &&
			 a->configGroup()->readEntry ("SendGoneEvent", true) )
		sendNotification( Gone );
}


void JabberChatSession::slotUpdateDisplayName ()
{
	kDebug ( JABBER_DEBUG_GLOBAL ) ;

	Kopete::ContactPtrList chatMembers = members ();

	// make sure we do have members in the chat
	if ( !chatMembers.first () )
		return;

	XMPP::Jid jid = static_cast<JabberBaseContact*>(chatMembers.first())->rosterItem().jid();<commit_msg>QList asserts when calling first() and it's empty, so use a proper isEmpty test instead.<commit_after>/*
    jabberchatsession.cpp - Jabber Chat Session

    Copyright (c) 2004 by <NAME>            <<EMAIL>>

    Kopete    (c) 2004 by the Kopete developers  <<EMAIL>>

    *************************************************************************
    *                                                                       *
    * 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 "jabberchatsession.h"

#include <qlabel.h>
#include <qimage.h>

#include <qfile.h>
#include <qicon.h>
#include <config-kopete.h>

#include <kconfig.h>
#include <kdebug.h>
#include <klocale.h>
#include <kicon.h>
#include <kaction.h>
#include <kactioncollection.h>
#include "kopetechatsessionmanager.h"
#include "kopetemessage.h"
#include "kopeteviewplugin.h"
#include "kopeteview.h"
#include "kopetemetacontact.h"

#include "jabberprotocol.h"
#include "jabberaccount.h"
#include "jabberclient.h"
#include "jabbercontact.h"
#include "jabberresource.h"
#include "jabberresourcepool.h"
#include "kioslave/jabberdisco.h"


JabberChatSession::JabberChatSession ( JabberProtocol *protocol, const JabberBaseContact *user,
											 Kopete::ContactPtrList others, const QString &resource )
											 : Kopete::ChatSession ( user, others, protocol ),
											 mTypingNotificationSent ( false )
{
	kDebug ( JABBER_DEBUG_GLOBAL ) << "New message manager for " << user->contactId ();

	setComponentData(protocol->componentData());

	// make sure Kopete knows about this instance
	Kopete::ChatSessionManager::self()->registerChatSession ( this );

	connect ( this, SIGNAL ( messageSent ( Kopete::Message &, Kopete::ChatSession * ) ),
			  this, SLOT ( slotMessageSent ( Kopete::Message &, Kopete::ChatSession * ) ) );

	connect ( this, SIGNAL ( myselfTyping ( bool ) ), this, SLOT ( slotSendTypingNotification ( bool ) ) );

	connect ( this, SIGNAL ( onlineStatusChanged(Kopete::Contact*, const Kopete::OnlineStatus&, const Kopete::OnlineStatus& ) ), this, SLOT ( slotUpdateDisplayName () ) );

	// check if the user ID contains a hardwired resource,
	// we'll have to use that one in that case
	XMPP::Jid jid = user->rosterItem().jid() ;

	mResource = jid.resource().isEmpty () ? resource : jid.resource ();
	slotUpdateDisplayName ();

#ifdef JINGLE_SUPPORT
	KAction *jingleSessionGui = new KAction(i18n("Show audio calls"), members().first());
	jingleSessionGui->setIcon(KIcon("voicecall"));
	connect(jingleSessionGui, SIGNAL(triggered(bool)), SLOT (slotJingleSessionGui() ));
	setComponentData(protocol->componentData());

	KAction *jingleSession = new KAction(i18n("Start audio call"), members().first());
	jingleSession->setIcon(KIcon("voicecall"));
	connect(jingleSession, SIGNAL(triggered(bool)), SLOT (slotJingleSession() ));
	setComponentData(protocol->componentData());

	Kopete::ContactPtrList chatMembers = members();
	if (!chatMembers.isEmpty())
	{
		JabberResource *bestResource = account()->resourcePool()->bestJabberResource( static_cast<JabberBaseContact*>(chatMembers.first())->rosterItem().jid() );
		if (bestResource)
		{
			jingleSessionGui->setEnabled( bestResource->features().canJingle() );
			jingleSession->setEnabled( bestResource->features().canJingle() );
		}
		else
		{
			jingleSessionGui->setEnabled(false);
			jingleSession->setEnabled(false);
		}
	}


	/*KAction *jingleaudiocall = new KAction(i18n("Jingle Audio call" ), members().first());
	connect(jingleaudiocall, SIGNAL(triggered(bool)), SLOT (slotJingleAudioCall() ));
	setComponentData(protocol->componentData());
	jingleaudiocall->setEnabled( false );

	KAction *jinglevideocall = new KAction(i18n("Jingle Video call" ), members().first());
	connect(jinglevideocall, SIGNAL(triggered(bool)), SLOT (slotJingleVideoCall() ));
	setComponentData(protocol->componentData());
	jinglevideocall->setEnabled( false );*/

	//Kopete::ContactPtrList chatMembers = members ();
	//if ( chatMembers.first () )
	//{
		// Check if the current contact support Audio calls, also honor lock by default.
		// FIXME: we should use the active ressource
		//jingleaudiocall->setEnabled( bestResource->features().canJingleAudio() );
		//jinglevideocall->setEnabled( bestResource->features().canJingleVideo() );
	//}

	//FIXME : Toolbar does not show any action (either for MSN or XMPP)
	//	  It should be corrected in trunk.
	//actionCollection()->addAction( "jabberJingleaudiocall", jingleaudiocall );
	//actionCollection()->addAction( "jabberJinglevideocall", jinglevideocall );
	actionCollection()->addAction( "jingleSession", jingleSession );
	actionCollection()->addAction( "jingleSessionsGui", jingleSessionGui );

#endif

	setXMLFile("jabberchatui.rc");

}

JabberChatSession::~JabberChatSession( )
{
	JabberAccount * a = dynamic_cast<JabberAccount *>(Kopete::ChatSession::account ());
	if( !a ) //When closing kopete, the account is partially destroyed already,  dynamic_cast return 0
		return;
	if ( a->configGroup()->readEntry ("SendEvents", true) &&
			 a->configGroup()->readEntry ("SendGoneEvent", true) )
		sendNotification( Gone );
}


void JabberChatSession::slotUpdateDisplayName ()
{
	kDebug ( JABBER_DEBUG_GLOBAL ) ;

	Kopete::ContactPtrList chatMembers = members ();

	// make sure we do have members in the chat
	if ( chatMembers.isEmpty() )
		return;

	XMPP::Jid jid = static_cast<JabberBaseContact*>(chatMembers.first())->rosterItem().jid();
{'fraction_non_alphanumeric': 0.1579447322970639, 'fraction_numerical': 0.0031088082901554403, 'mean_word_length': 4.434537775692164, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6256141', 'n_tokens_mistral': 3751, 'n_tokens_neox': 3506, 'n_words': 783}