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