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 "client.h"
00019 #include "lastactivityhandler.h"
00020
00021 #include <cstdlib>
00022
00023 namespace gloox
00024 {
00025
00026 LastActivity::LastActivity( ClientBase *parent )
00027 : m_lastActivityHandler( 0 ), m_parent( parent ),
00028 m_active( time ( 0 ) )
00029 {
00030 if( m_parent )
00031 m_parent->disco()->addFeature( XMLNS_LAST );
00032 }
00033
00034 LastActivity::~LastActivity()
00035 {
00036 if( m_parent )
00037 m_parent->disco()->removeFeature( XMLNS_LAST );
00038 }
00039
00040 void LastActivity::query( const JID& jid )
00041 {
00042 const std::string& id = m_parent->getID();
00043
00044 Tag *t = new Tag( "iq" );
00045 t->addAttribute( "type", "get" );
00046 t->addAttribute( "id", id );
00047 t->addAttribute( "to", jid.full() );
00048 Tag *q = new Tag( t, "query" );
00049 q->addAttribute( "xmlns", XMLNS_LAST );
00050
00051 m_parent->trackID( this, id, 0 );
00052 m_parent->send( t );
00053 }
00054
00055 bool LastActivity::handleIq( Stanza *stanza )
00056 {
00057 switch( stanza->subtype() )
00058 {
00059 case StanzaIqGet:
00060 {
00061 time_t now = time( 0 );
00062
00063 Tag *t = new Tag( "iq" );
00064 t->addAttribute( "type", "result" );
00065 t->addAttribute( "id", stanza->id() );
00066 t->addAttribute( "to", stanza->from().full() );
00067 Tag *q = new Tag( t, "query" );
00068 q->addAttribute( "seconds", (long)( now - m_active ) );
00069 q->addAttribute( "xmlns", XMLNS_LAST );
00070
00071 m_parent->send( t );
00072 break;
00073 }
00074
00075 case StanzaIqSet:
00076 {
00077 Tag *t = new Tag( "iq" );
00078 t->addAttribute( "id", stanza->id() );
00079 t->addAttribute( "to", stanza->from().full() );
00080 t->addAttribute( "type", "error" );
00081 Tag *e = new Tag( t, "error" );
00082 e->addAttribute( "type", "cancel" );
00083 Tag *f = new Tag( e, "feature-not-implemented" );
00084 f->addAttribute( "xmlns", XMLNS_XMPP_STANZAS );
00085
00086 m_parent->send( t );
00087 break;
00088 }
00089
00090 default:
00091 break;
00092 }
00093
00094 return true;
00095 }
00096
00097 bool LastActivity::handleIqID( Stanza *stanza, int )
00098 {
00099 if( !m_lastActivityHandler )
00100 return false;
00101
00102 switch( stanza->subtype() )
00103 {
00104 case StanzaIqResult:
00105 {
00106 Tag *q = stanza->findChild( "query" );
00107 if( q )
00108 {
00109 const std::string& seconds = q->findAttribute( "seconds" );
00110 if( !seconds.empty() )
00111 {
00112 int secs = atoi( seconds.c_str() );
00113 m_lastActivityHandler->handleLastActivityResult( stanza->from(), secs );
00114 }
00115 }
00116 break;
00117 }
00118 case StanzaIqError:
00119 m_lastActivityHandler->handleLastActivityError( stanza->from(), stanza->error() );
00120 break;
00121 default:
00122 break;
00123 }
00124
00125 return false;
00126 }
00127
00128 void LastActivity::resetIdleTimer()
00129 {
00130 m_active = time( 0 );
00131 }
00132
00133 }