<commit_before>/*
    kopeteprotocol.cpp - Kopete Protocol

    Copyright (c) 2002      by <NAME> <<EMAIL>>
    Copyright (c) 2002-2003 by <NAME>       <<EMAIL>>
    Copyright (c) 2002-2005 by <NAME>        <<EMAIL>>

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

    *************************************************************************
    *                                                                       *
    * This library is free software; you can redistribute it and/or         *
    * modify it under the terms of the GNU Lesser General Public            *
    * License as published by the Free Software Foundation; either          *
    * version 2 of the License, or (at your option) any later version.      *
    *                                                                       *
    *************************************************************************
*/

#include "kopeteprotocol.h"

#include <kdebug.h>
#include <kaction.h>
#include <klocale.h>

#include "kopeteaccountmanager.h"
#include "kopeteaccount.h"
#include "kopetecontact.h"
#include "kopeteglobal.h"
#include "kopetecontactproperty.h"
#include "kopetemetacontact.h"

namespace Kopete
{

class Protocol::Private
{
public:
	bool unloading;
	Protocol::Capabilities capabilities;
	/*
	 * Make sure we always have a lastSeen and a fullname property as long as
	 * a protocol is loaded
	 */
	ContactPropertyTmpl mStickLastSeen;
	ContactPropertyTmpl mStickFullName;

	Kopete::OnlineStatus accountNotConnectedStatus;
};

Protocol::Protocol( const KComponentData &instance, QObject *parent )
: Plugin( instance, parent )
{
	d = new Private;
	d->mStickLastSeen = Global::Properties::self()->lastSeen();
	d->mStickFullName = Global::Properties::self()->fullName();
	d->unloading = false;
	d->accountNotConnectedStatus = Kopete::OnlineStatus( Kopete::OnlineStatus::Unknown, 0, this, Kopete::OnlineStatus::AccountOffline, QStringList(QString::fromLatin1( "account_offline_overlay" )), i18n( "Account Offline" ) );
}

Protocol::~Protocol()
{
	// Remove all active accounts
	foreach( Account *a , AccountManager::self()->accounts() )
	{
		if( a->protocol() == this )
		{
			kWarning( 14010 ) << k_funcinfo << "Deleting protocol with existing accounts! Did the account unloading go wrong?  account: " 
					<< a->accountId() << endl;
		
			delete a;
		}
	}
	delete d;
}

Protocol::Capabilities Protocol::capabilities() const
{
	return d->capabilities;
}

void Protocol::setCapabilities( Protocol::Capabilities capabilities )
{
	d->capabilities = capabilities;
}


Kopete::OnlineStatus Protocol::accountOfflineStatus() const
{
	return d->accountNotConnectedStatus;
}

void Protocol::slotAccountOnlineStatusChanged( Contact *self )
{//slot connected in aboutToUnload
	if ( !self || !self->account() || self->account()->isConnected())
		return;
	// some protocols change status several times during shutdown.  We should only call deleteLater() once
	disconnect( self, 0, this, 0 );

	connect( self->account(), SIGNAL(destroyed( )),
		this, SLOT( slotAccountDestroyed( ) ) );

	self->account()->deleteLater();
}

void Protocol::slotAccountDestroyed( )
{
	foreach( Account *a , AccountManager::self()->accounts() )
	{
		if( a->protocol() == this )
		{
			//all accounts has not been deleted yet
			return;
		}
	}

	// While at this point we are still in a stack trace from the destroyed
	// account it's safe to emit readyForUnload already, because it uses a
	// deleteLater rather than a delete for exactly this reason, to keep the
	// API managable
	emit( readyForUnload() );
}

void Protocol::aboutToUnload()
{

	d->unloading = true;

	bool accountcountcount=0;
	// Disconnect all accounts
	foreach( Account *a , AccountManager::self()->accounts() )
	{
		if( a->protocol() == this )
		{
			accountcountcount++;
			
			if ( a->myself() && a->myself()->isOnline() )
			{
				kDebug( 14010 ) << k_funcinfo << a->accountId() <<
						" is still connected, disconnecting..." << endl;

				QObject::connect( a->myself(),
								  SIGNAL( onlineStatusChanged( Kopete::Contact *, const Kopete::OnlineStatus &, const Kopete::OnlineStatus & ) ),
								  this, SLOT( slotAccountOnlineStatusChanged( Kopete::Contact * ) ) );
				a->disconnect();
			}
			else
			{
				// Remove account, it's already disconnected
				kDebug( 14010 ) << k_funcinfo << a->accountId() <<
						" is already disconnected, deleting..." << endl;

				QObject::connect( a, SIGNAL( destroyed( ) ),
								  this, SLOT( slotAccountDestroyed( ) ) );
				a->deleteLater();
			}
		}
	}<commit_msg>this should be an int... :)<commit_after>/*
    kopeteprotocol.cpp - Kopete Protocol

    Copyright (c) 2002      by <NAME> <<EMAIL>>
    Copyright (c) 2002-2003 by <NAME>       <<EMAIL>>
    Copyright (c) 2002-2005 by <NAME>        <<EMAIL>>

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

    *************************************************************************
    *                                                                       *
    * This library is free software; you can redistribute it and/or         *
    * modify it under the terms of the GNU Lesser General Public            *
    * License as published by the Free Software Foundation; either          *
    * version 2 of the License, or (at your option) any later version.      *
    *                                                                       *
    *************************************************************************
*/

#include "kopeteprotocol.h"

#include <kdebug.h>
#include <kaction.h>
#include <klocale.h>

#include "kopeteaccountmanager.h"
#include "kopeteaccount.h"
#include "kopetecontact.h"
#include "kopeteglobal.h"
#include "kopetecontactproperty.h"
#include "kopetemetacontact.h"

namespace Kopete
{

class Protocol::Private
{
public:
	bool unloading;
	Protocol::Capabilities capabilities;
	/*
	 * Make sure we always have a lastSeen and a fullname property as long as
	 * a protocol is loaded
	 */
	ContactPropertyTmpl mStickLastSeen;
	ContactPropertyTmpl mStickFullName;

	Kopete::OnlineStatus accountNotConnectedStatus;
};

Protocol::Protocol( const KComponentData &instance, QObject *parent )
: Plugin( instance, parent )
{
	d = new Private;
	d->mStickLastSeen = Global::Properties::self()->lastSeen();
	d->mStickFullName = Global::Properties::self()->fullName();
	d->unloading = false;
	d->accountNotConnectedStatus = Kopete::OnlineStatus( Kopete::OnlineStatus::Unknown, 0, this, Kopete::OnlineStatus::AccountOffline, QStringList(QString::fromLatin1( "account_offline_overlay" )), i18n( "Account Offline" ) );
}

Protocol::~Protocol()
{
	// Remove all active accounts
	foreach( Account *a , AccountManager::self()->accounts() )
	{
		if( a->protocol() == this )
		{
			kWarning( 14010 ) << k_funcinfo << "Deleting protocol with existing accounts! Did the account unloading go wrong?  account: " 
					<< a->accountId() << endl;
		
			delete a;
		}
	}
	delete d;
}

Protocol::Capabilities Protocol::capabilities() const
{
	return d->capabilities;
}

void Protocol::setCapabilities( Protocol::Capabilities capabilities )
{
	d->capabilities = capabilities;
}


Kopete::OnlineStatus Protocol::accountOfflineStatus() const
{
	return d->accountNotConnectedStatus;
}

void Protocol::slotAccountOnlineStatusChanged( Contact *self )
{//slot connected in aboutToUnload
	if ( !self || !self->account() || self->account()->isConnected())
		return;
	// some protocols change status several times during shutdown.  We should only call deleteLater() once
	disconnect( self, 0, this, 0 );

	connect( self->account(), SIGNAL(destroyed( )),
		this, SLOT( slotAccountDestroyed( ) ) );

	self->account()->deleteLater();
}

void Protocol::slotAccountDestroyed( )
{
	foreach( Account *a , AccountManager::self()->accounts() )
	{
		if( a->protocol() == this )
		{
			//all accounts has not been deleted yet
			return;
		}
	}

	// While at this point we are still in a stack trace from the destroyed
	// account it's safe to emit readyForUnload already, because it uses a
	// deleteLater rather than a delete for exactly this reason, to keep the
	// API managable
	emit( readyForUnload() );
}

void Protocol::aboutToUnload()
{

	d->unloading = true;

	int accountcountcount=0;
	// Disconnect all accounts
	foreach( Account *a , AccountManager::self()->accounts() )
	{
		if( a->protocol() == this )
		{
			accountcountcount++;
			
			if ( a->myself() && a->myself()->isOnline() )
			{
				kDebug( 14010 ) << k_funcinfo << a->accountId() <<
						" is still connected, disconnecting..." << endl;

				QObject::connect( a->myself(),
								  SIGNAL( onlineStatusChanged( Kopete::Contact *, const Kopete::OnlineStatus &, const Kopete::OnlineStatus & ) ),
								  this, SLOT( slotAccountOnlineStatusChanged( Kopete::Contact * ) ) );
				a->disconnect();
			}
			else
			{
				// Remove account, it's already disconnected
				kDebug( 14010 ) << k_funcinfo << a->accountId() <<
						" is already disconnected, deleting..." << endl;

				QObject::connect( a, SIGNAL( destroyed( ) ),
								  this, SLOT( slotAccountDestroyed( ) ) );
				a->deleteLater();
			}
		}
	}
{'fraction_non_alphanumeric': 0.15847114226262185, 'fraction_numerical': 0.011170737049611214, 'mean_word_length': 3.4009638554216868, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1978351', 'n_tokens_mistral': 3057, 'n_tokens_neox': 2677, 'n_words': 737}