00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "lastactivity.h"
00016 #include "disco.h"
00017 #include "discohandler.h"
00018 #include "clientbase.h"
00019 #include "error.h"
00020 #include "lastactivityhandler.h"
00021
00022 #include <cstdlib>
00023
00024 namespace gloox
00025 {
00026
00027 LastActivity::LastActivity( ClientBase* parent )
00028 : m_lastActivityHandler( 0 ), m_parent( parent ),
00029 m_active( time ( 0 ) )
00030 {
00031 if( m_parent )
00032 m_parent->disco()->addFeature( XMLNS_LAST );
00033 }
00034
00035 LastActivity::~LastActivity()
00036 {
00037 if( m_parent )
00038 {
00039 m_parent->disco()->removeFeature( XMLNS_LAST );
00040 m_parent->removeIDHandler( this );
00041 }
00042 }
00043
00044 void LastActivity::query( const JID& jid )
00045 {
00046 IQ iq( IQ::Get, jid, m_parent->getID(), XMLNS_LAST );
00047 m_parent->send( iq, this, 0 );
00048 }
00049
00050 bool LastActivity::handleIq( const IQ& iq )
00051 {
00052 switch( iq.subtype() )
00053 {
00054 case IQ::Get:
00055 {
00056 time_t now = time( 0 );
00057
00058 IQ t( IQ::Result, iq.from(), iq.id(), XMLNS_LAST );
00059 t.query()->addAttribute( "seconds", (long)( now - m_active ) );
00060
00061 m_parent->send( t );
00062 break;
00063 }
00064
00065 case IQ::Set:
00066 {
00067 IQ t( IQ::Error, iq.from(), iq.id() );
00068 t.addExtension( new Error( StanzaErrorTypeCancel, StanzaErrorFeatureNotImplemented ) );
00069 m_parent->send( t );
00070 break;
00071 }
00072
00073 default:
00074 break;
00075 }
00076
00077 return true;
00078 }
00079
00080 void LastActivity::handleIqID( const IQ& iq, int )
00081 {
00082 if( !m_lastActivityHandler )
00083 return;
00084
00085 switch( iq.subtype() )
00086 {
00087 case IQ::Result:
00088 {
00089 Tag* q = iq.query();
00090 if( q )
00091 {
00092 const std::string& seconds = q->findAttribute( "seconds" );
00093 if( !seconds.empty() )
00094 {
00095 int secs = atoi( seconds.c_str() );
00096 m_lastActivityHandler->handleLastActivityResult( iq.from(), secs );
00097 }
00098 }
00099 break;
00100 }
00101 case IQ::Error:
00102 m_lastActivityHandler->handleLastActivityError( iq.from(), iq.error()->error() );
00103 break;
00104 default:
00105 break;
00106 }
00107 }
00108
00109 void LastActivity::resetIdleTimer()
00110 {
00111 m_active = time( 0 );
00112 }
00113
00114 }