<commit_before>/*
    kopetemetacontactlvi.cpp - Kopete Meta Contact KListViewItem

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

    Kopete    (c) 2002      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 <qapplication.h>
#include <qpainter.h>
#include <qtimer.h>

#include <kdebug.h>
#include <klocale.h>
#include <kpopupmenu.h>
#include <kiconloader.h>
#include <kmessagebox.h>
#include <klineeditdlg.h>
#include <kiconeffect.h>
#include <kpassivepopup.h>
#include "kopetenotifyclient.h"

#include "kopetecontactlistview.h"
#include "kopeteaccountmanager.h"
#include "kopetemetacontactlvi.h"
#include "kopetegroupviewitem.h"
#include "kopetecontactlist.h"
#include "kopetemetacontact.h"
#include "kopetestdaction.h"
#include "addcontactpage.h"
#include "kopetepluginmanager.h"
#include "kopeteaccount.h"
#include "kopeteprefs.h"
#include "systemtray.h"

#include <kdeversion.h>


KopeteMetaContactLVI::KopeteMetaContactLVI( KopeteMetaContact *contact, KopeteGroupViewItem *parent )
	: QObject( contact, "KopeteMetaContactLVI" ), KListViewItem( parent )
{
	m_metaContact = contact;
	m_isTopLevel = false;
	m_parentGroup = parent;
	m_parentView = 0L;

	initLVI();
	parent->refreshDisplayName();
}

KopeteMetaContactLVI::KopeteMetaContactLVI( KopeteMetaContact *contact, QListViewItem *parent )
: QObject( contact, "KopeteMetaContactLVI" ), KListViewItem( parent )
{
	m_metaContact = contact;

	m_isTopLevel = true;
	m_parentGroup = 0L;
	m_parentView = 0L;

	initLVI();
}

KopeteMetaContactLVI::KopeteMetaContactLVI( KopeteMetaContact *contact, QListView *parent )
: QObject( contact, "KopeteMetaContactLVI" ), KListViewItem( parent )
{
	m_metaContact = contact;

	m_isTopLevel = true;
	m_parentGroup = 0L;
	m_parentView = parent;

	initLVI();
}

void KopeteMetaContactLVI::initLVI()
{
	m_actionMove = 0L;
	m_actionCopy = 0L;

	m_oldStatus = m_metaContact->status();
	m_oldStatusIcon = m_metaContact->statusIcon();

	connect( m_metaContact, SIGNAL( displayNameChanged( const QString &, const QString & ) ),
		SLOT( slotDisplayNameChanged() ) );

	connect( m_metaContact, SIGNAL( onlineStatusChanged( KopeteMetaContact *, KopeteOnlineStatus::OnlineStatus ) ),
		SLOT( slotUpdateIcons() ) );

	connect( m_metaContact, SIGNAL( contactStatusChanged( KopeteContact *, const KopeteOnlineStatus & ) ),
		SLOT( slotContactStatusChanged(KopeteContact *) ) );

	connect( m_metaContact, SIGNAL( contactAdded( KopeteContact * ) ),
		SLOT( slotUpdateIcons() ) );

	connect( m_metaContact, SIGNAL( contactRemoved( KopeteContact * ) ),
		SLOT( slotUpdateIcons() ) );

	connect( m_metaContact, SIGNAL( contactIdleStateChanged( KopeteContact * ) ),
		SLOT( slotIdleStateChanged() ) );

	connect( KopetePrefs::prefs(), SIGNAL( saved() ),
		SLOT( slotConfigChanged() ) );

	mBlinkTimer = new QTimer( this, "mBlinkTimer" );
	QObject::connect( mBlinkTimer, SIGNAL(timeout()), this, SLOT(slotBlink()) );
	mIsBlinkIcon=false;
	m_event=0L;

	//if(!mBlinkIcon) mBlinkIcon = new QPixmap(KGlobal::iconLoader()->loadIcon( QString::fromLatin1( "newmsg" ), KIcon::Small));

	slotUpdateIcons();
	slotDisplayNameChanged();
}

KopeteMetaContactLVI::~KopeteMetaContactLVI()
{
//	if(m_parentGroup)
//		m_parentGroup->refreshDisplayName();
}

void KopeteMetaContactLVI::movedToGroup(KopeteGroup *to)
{
	KopeteContactListView *lv =
		dynamic_cast<KopeteContactListView *>( listView() );
	if( !lv )
		return;

	if(m_parentGroup)
		m_parentGroup->refreshDisplayName();

	KopeteGroupViewItem *group_item = lv->getGroup( to );
	if(group_item)
	{
		m_isTopLevel = false;
		m_parentGroup = group_item;
		m_parentView = 0L;
		group_item->refreshDisplayName();
	}
	else
	{
		m_isTopLevel = true;
		m_parentGroup = 0L;
		m_parentView = lv;
	}
}

void KopeteMetaContactLVI::rename( const QString& newName )
{
	if( newName.isEmpty() )
	{
		//reset the last display name
		QListViewItem::setText(0, m_metaContact->displayName());
		m_metaContact->setTrackChildNameChanges( true );
	}
	else // user changed name manually, disable tracking of contact nickname and update displayname
	{
		m_metaContact->setTrackChildNameChanges( false );
		m_metaContact->setDisplayName( newName );
	}

	kdDebug( 14000 ) << k_funcinfo << "newName=" << newName <<
		", TrackChildNameChanges=" << m_metaContact->trackChildNameChanges() << endl;
}

void KopeteMetaContactLVI::slotContactStatusChanged(KopeteContact *c)
{
	// FIXME: All this code should be in kopetemetacontact.cpp.. having it in the LVI makes it all fire
	// multiple times if the user is in multiple groups - Jason

	// NOTE: this assume that the use don't want a notification when the contact was away and goes online
	QString event =
		( m_metaContact->status() == KopeteOnlineStatus::Online &&
			( m_oldStatus == KopeteOnlineStatus::Offline ||
			m_oldStatus == KopeteOnlineStatus::Unknown )
		) ? "kopete_online" : "kopete_status_change";

	m_oldStatus = m_metaContact->status();

	if(c->account()->myself()->onlineStatus().status() != KopeteOnlineStatus::Connecting)
	{
		int winId = KopeteSystemTray::systemTray() ?
			KopeteSystemTray::systemTray()->winId() : 0;

		KopeteNotifyClient::event(winId, event,
			i18n("%2 is now %1!").arg(m_metaContact->statusString()).arg(m_metaContact->displayName()),
			i18n("Chat") , this, SLOT(execute()));

		if( !mBlinkTimer->isActive() &&
			( m_metaContact->statusIcon() != m_oldStatusIcon ) )<commit_msg>Fix Bug 63799:   do not play sound when a contact goes online when the user don't want to hear sound when he is away<commit_after>/*
    kopetemetacontactlvi.cpp - Kopete Meta Contact KListViewItem

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

    Kopete    (c) 2002      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 <qapplication.h>
#include <qpainter.h>
#include <qtimer.h>

#include <kdebug.h>
#include <klocale.h>
#include <kpopupmenu.h>
#include <kiconloader.h>
#include <kmessagebox.h>
#include <klineeditdlg.h>
#include <kiconeffect.h>
#include <kpassivepopup.h>
#include "kopetenotifyclient.h"

#include "kopetecontactlistview.h"
#include "kopeteaccountmanager.h"
#include "kopetemetacontactlvi.h"
#include "kopetegroupviewitem.h"
#include "kopetecontactlist.h"
#include "kopetemetacontact.h"
#include "kopetestdaction.h"
#include "addcontactpage.h"
#include "kopetepluginmanager.h"
#include "kopeteaccount.h"
#include "kopeteprefs.h"
#include "systemtray.h"

#include <kdeversion.h>


KopeteMetaContactLVI::KopeteMetaContactLVI( KopeteMetaContact *contact, KopeteGroupViewItem *parent )
	: QObject( contact, "KopeteMetaContactLVI" ), KListViewItem( parent )
{
	m_metaContact = contact;
	m_isTopLevel = false;
	m_parentGroup = parent;
	m_parentView = 0L;

	initLVI();
	parent->refreshDisplayName();
}

KopeteMetaContactLVI::KopeteMetaContactLVI( KopeteMetaContact *contact, QListViewItem *parent )
: QObject( contact, "KopeteMetaContactLVI" ), KListViewItem( parent )
{
	m_metaContact = contact;

	m_isTopLevel = true;
	m_parentGroup = 0L;
	m_parentView = 0L;

	initLVI();
}

KopeteMetaContactLVI::KopeteMetaContactLVI( KopeteMetaContact *contact, QListView *parent )
: QObject( contact, "KopeteMetaContactLVI" ), KListViewItem( parent )
{
	m_metaContact = contact;

	m_isTopLevel = true;
	m_parentGroup = 0L;
	m_parentView = parent;

	initLVI();
}

void KopeteMetaContactLVI::initLVI()
{
	m_actionMove = 0L;
	m_actionCopy = 0L;

	m_oldStatus = m_metaContact->status();
	m_oldStatusIcon = m_metaContact->statusIcon();

	connect( m_metaContact, SIGNAL( displayNameChanged( const QString &, const QString & ) ),
		SLOT( slotDisplayNameChanged() ) );

	connect( m_metaContact, SIGNAL( onlineStatusChanged( KopeteMetaContact *, KopeteOnlineStatus::OnlineStatus ) ),
		SLOT( slotUpdateIcons() ) );

	connect( m_metaContact, SIGNAL( contactStatusChanged( KopeteContact *, const KopeteOnlineStatus & ) ),
		SLOT( slotContactStatusChanged(KopeteContact *) ) );

	connect( m_metaContact, SIGNAL( contactAdded( KopeteContact * ) ),
		SLOT( slotUpdateIcons() ) );

	connect( m_metaContact, SIGNAL( contactRemoved( KopeteContact * ) ),
		SLOT( slotUpdateIcons() ) );

	connect( m_metaContact, SIGNAL( contactIdleStateChanged( KopeteContact * ) ),
		SLOT( slotIdleStateChanged() ) );

	connect( KopetePrefs::prefs(), SIGNAL( saved() ),
		SLOT( slotConfigChanged() ) );

	mBlinkTimer = new QTimer( this, "mBlinkTimer" );
	QObject::connect( mBlinkTimer, SIGNAL(timeout()), this, SLOT(slotBlink()) );
	mIsBlinkIcon=false;
	m_event=0L;

	//if(!mBlinkIcon) mBlinkIcon = new QPixmap(KGlobal::iconLoader()->loadIcon( QString::fromLatin1( "newmsg" ), KIcon::Small));

	slotUpdateIcons();
	slotDisplayNameChanged();
}

KopeteMetaContactLVI::~KopeteMetaContactLVI()
{
//	if(m_parentGroup)
//		m_parentGroup->refreshDisplayName();
}

void KopeteMetaContactLVI::movedToGroup(KopeteGroup *to)
{
	KopeteContactListView *lv =
		dynamic_cast<KopeteContactListView *>( listView() );
	if( !lv )
		return;

	if(m_parentGroup)
		m_parentGroup->refreshDisplayName();

	KopeteGroupViewItem *group_item = lv->getGroup( to );
	if(group_item)
	{
		m_isTopLevel = false;
		m_parentGroup = group_item;
		m_parentView = 0L;
		group_item->refreshDisplayName();
	}
	else
	{
		m_isTopLevel = true;
		m_parentGroup = 0L;
		m_parentView = lv;
	}
}

void KopeteMetaContactLVI::rename( const QString& newName )
{
	if( newName.isEmpty() )
	{
		//reset the last display name
		QListViewItem::setText(0, m_metaContact->displayName());
		m_metaContact->setTrackChildNameChanges( true );
	}
	else // user changed name manually, disable tracking of contact nickname and update displayname
	{
		m_metaContact->setTrackChildNameChanges( false );
		m_metaContact->setDisplayName( newName );
	}

	kdDebug( 14000 ) << k_funcinfo << "newName=" << newName <<
		", TrackChildNameChanges=" << m_metaContact->trackChildNameChanges() << endl;
}

void KopeteMetaContactLVI::slotContactStatusChanged(KopeteContact *c)
{
	// FIXME: All this code should be in kopetemetacontact.cpp.. having it in the LVI makes it all fire
	// multiple times if the user is in multiple groups - Jason

	// NOTE: this assume that the use don't want a notification when the contact was away and goes online
	QString event =
		( m_metaContact->status() == KopeteOnlineStatus::Online &&
			( m_oldStatus == KopeteOnlineStatus::Offline ||
			m_oldStatus == KopeteOnlineStatus::Unknown )
		) ? "kopete_online" : "kopete_status_change";

	m_oldStatus = m_metaContact->status();

	if(c->account()->myself()->onlineStatus().status() != KopeteOnlineStatus::Connecting && ( !c->account()->isAway() || KopetePrefs::prefs()->soundIfAway()  ))
	{
		int winId = KopeteSystemTray::systemTray() ?
			KopeteSystemTray::systemTray()->winId() : 0;

		KopeteNotifyClient::event(winId, event,
			i18n("%2 is now %1!").arg(m_metaContact->statusString()).arg(m_metaContact->displayName()),
			i18n("Chat") , this, SLOT(execute()));

		if( !mBlinkTimer->isActive() &&
			( m_metaContact->statusIcon() != m_oldStatusIcon ) )
{'fraction_non_alphanumeric': 0.14557371743000486, 'fraction_numerical': 0.008172843502184819, 'mean_word_length': 4.522341376228776, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 100, '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': '1358585', 'n_tokens_mistral': 4412, 'n_tokens_neox': 4131, 'n_words': 867}