lastactivity.cpp
/*
Copyright (c) 2005-2023 by Jakob Schröter <js@camaya.net>
This file is part of the gloox library. http://camaya.net/gloox
This software is distributed under a license. The full license
agreement can be found in the file LICENSE in this distribution.
This software may not be copied, modified, sold or distributed
other than expressed in the named license agreement.
This software is distributed without any warranty.
*/
#include "lastactivity.h"
#include "disco.h"
#include "discohandler.h"
#include "clientbase.h"
#include "error.h"
#include "lastactivityhandler.h"
#include <cstdlib>
namespace gloox
{
// ---- LastActivity::Query ----
LastActivity::Query::Query( const Tag* tag )
: StanzaExtension( ExtLastActivity ), m_seconds( -1 )
{
if( !tag || tag->name() != "query" || tag->xmlns() != XMLNS_LAST )
return;
if( tag->hasAttribute( "seconds" ) )
m_seconds = atoi( tag->findAttribute( "seconds" ).c_str() );
m_status = tag->cdata();
}
LastActivity::Query::Query( const std::string& _status, long _seconds )
: StanzaExtension( ExtLastActivity ), m_seconds( _seconds ),
m_status( _status )
{
}
LastActivity::Query::~Query()
{
}
const std::string& LastActivity::Query::filterString() const
{
static const std::string filter = "/iq/query[@xmlns='" + XMLNS_LAST + "']"
"|/presence/query[@xmlns='" + XMLNS_LAST + "']";
return filter;
}
Tag* LastActivity::Query::tag() const
{
Tag* t = new Tag( "query" );
t->setXmlns( XMLNS_LAST );
t->addAttribute( "seconds", m_seconds );
t->setCData( m_status );
return t;
}
// ---- ~LastActivity::Query ----
// ---- LastActivity ----
LastActivity::LastActivity( ClientBase* parent )
: m_lastActivityHandler( 0 ), m_parent( parent ),
m_active( time ( 0 ) )
{
if( m_parent )
{
m_parent->registerStanzaExtension( new Query() );
m_parent->registerIqHandler( this, ExtLastActivity );
m_parent->disco()->addFeature( XMLNS_LAST );
}
}
LastActivity::~LastActivity()
{
if( m_parent )
{
m_parent->disco()->removeFeature( XMLNS_LAST );
m_parent->removeIqHandler( this, ExtLastActivity );
m_parent->removeIDHandler( this );
}
}
void LastActivity::query( const JID& jid )
{
IQ iq( IQ::Get, jid, m_parent->getID() );
iq.addExtension( new Query() );
m_parent->send( iq, this, 0 );
}
bool LastActivity::handleIq( const IQ& iq )
{
const Query* q = iq.findExtension<Query>( ExtLastActivity );
if( !q || iq.subtype() != IQ::Get )
return false;
IQ re( IQ::Result, iq.from(), iq.id() );
re.addExtension( new Query( EmptyString, static_cast<long>( time( 0 ) - m_active ) ) );
m_parent->send( re );
return true;
}
void LastActivity::handleIqID( const IQ& iq, int /*context*/ )
{
if( !m_lastActivityHandler )
return;
if( iq.subtype() == IQ::Result )
{
const Query* q = iq.findExtension<Query>( ExtLastActivity );
if( !q || q->seconds() < 0 )
return;
m_lastActivityHandler->handleLastActivityResult( iq.from(), q->seconds(), q->status() );
}
else if( iq.subtype() == IQ::Error && iq.error() )
m_lastActivityHandler->handleLastActivityError( iq.from(), iq.error()->error() );
}
void LastActivity::resetIdleTimer()
{
m_active = time( 0 );
}
}
gloox::ClientBase
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition clientbase.h:79
gloox::Error::error
StanzaError error() const
Definition error.h:75
gloox::IQ
An abstraction of an IQ stanza.
Definition iq.h:34
gloox::IQ::subtype
IqType subtype() const
Definition iq.h:74
gloox::IQ::Error
@ Error
Definition iq.h:49
gloox::IQ::Result
@ Result
Definition iq.h:48
gloox::IQ::Get
@ Get
Definition iq.h:45
gloox::JID
An abstraction of a JID.
Definition jid.h:31
gloox::LastActivity::Query
This is an abstraction of a LastActivity Query that can be used in XEP-0012 as well as XEP-0256.
Definition lastactivity.h:53
gloox::LastActivity::Query::seconds
long seconds() const
Definition lastactivity.h:78
gloox::LastActivity::Query::~Query
virtual ~Query()
Definition lastactivity.cpp:46
gloox::LastActivity::Query::Query
Query(const Tag *tag=0)
Definition lastactivity.cpp:28
gloox::LastActivity::Query::filterString
virtual const std::string & filterString() const
Definition lastactivity.cpp:50
gloox::LastActivity::Query::status
const std::string & status() const
Definition lastactivity.h:85
gloox::LastActivity::Query::tag
virtual Tag * tag() const
Definition lastactivity.cpp:57
gloox::LastActivity::handleIqID
virtual void handleIqID(const IQ &iq, int context)
Definition lastactivity.cpp:110
gloox::LastActivity::handleIq
virtual bool handleIq(const IQ &iq)
Definition lastactivity.cpp:97
gloox::LastActivity::LastActivity
LastActivity(ClientBase *parent)
Definition lastactivity.cpp:68
gloox::LastActivity::~LastActivity
virtual ~LastActivity()
Definition lastactivity.cpp:80
gloox::LastActivity::query
void query(const JID &jid)
Definition lastactivity.cpp:90
gloox::LastActivity::resetIdleTimer
void resetIdleTimer()
Definition lastactivity.cpp:127
gloox::StanzaExtension::StanzaExtension
StanzaExtension(int type)
Definition stanzaextension.h:211
gloox::Stanza::addExtension
void addExtension(const StanzaExtension *se)
Definition stanza.cpp:52
gloox::Stanza::id
const std::string & id() const
Definition stanza.h:63
gloox::Stanza::error
const Error * error() const
Definition stanza.cpp:47
gloox::Stanza::from
const JID & from() const
Definition stanza.h:51
gloox::Stanza::findExtension
const StanzaExtension * findExtension(int type) const
Definition stanza.cpp:57
gloox::Tag
This is an abstraction of an XML element.
Definition tag.h:47
gloox::Tag::setCData
bool setCData(const std::string &cdata)
Definition tag.cpp:447
gloox::Tag::addAttribute
bool addAttribute(Attribute *attr)
Definition tag.cpp:354
gloox::Tag::setXmlns
bool setXmlns(const std::string &xmlns, const std::string &prefix=EmptyString)
Definition tag.cpp:522
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::XMLNS_LAST
const std::string XMLNS_LAST
Definition gloox.cpp:45
gloox::EmptyString
const std::string EmptyString
Definition gloox.cpp:124
gloox::ExtLastActivity
@ ExtLastActivity
Definition stanzaextension.h:80