lastactivity.cpp

00001 /*
00002   Copyright (c) 2005 by Jakob Schroeter <js@camaya.net>
00003   This file is part of the gloox library. http://camaya.net/gloox
00004 
00005   This software is distributed under a license. The full license
00006   agreement can be found in the file LICENSE in this distribution.
00007   This software may not be copied, modified, sold or distributed
00008   other than expressed in the named license agreement.
00009 
00010   This software is distributed without any warranty.
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 <sstream>
00022 
00023 namespace gloox
00024 {
00025 
00026   LastActivity::LastActivity( ClientBase *parent, Disco *disco )
00027     : m_lastActivityHandler( 0 ), m_parent( parent ), m_disco( disco )
00028   {
00029     m_disco->addFeature( XMLNS_LAST );
00030     m_active = time( 0 );
00031   }
00032 
00033   LastActivity::~LastActivity()
00034   {
00035   }
00036 
00037   void LastActivity::query( const JID& jid )
00038   {
00039     const std::string id = m_parent->getID();
00040 
00041     Tag *t = new Tag( "iq" );
00042     t->addAttrib( "type", "get" );
00043     t->addAttrib( "id", id );
00044     t->addAttrib( "to", jid.full() );
00045     Tag *q = new Tag( t, "query" );
00046     q->addAttrib( "xmlns", XMLNS_LAST );
00047 
00048     m_parent->trackID( this, id, 0 );
00049     m_parent->send( t );
00050   }
00051 
00052   bool LastActivity::handleIq( Stanza *stanza )
00053   {
00054     switch( stanza->subtype() )
00055     {
00056       case STANZA_IQ_GET:
00057       {
00058         time_t now = time( 0 );
00059 
00060         Tag *t = new Tag( "iq" );
00061         t->addAttrib( "type", "result" );
00062         t->addAttrib( "id", stanza->id() );
00063         t->addAttrib( "to", stanza->from().full() );
00064         Tag *q = new Tag( t, "query" );
00065         std::ostringstream oss;
00066         oss << (int)(now - m_active);
00067         q->addAttrib( "seconds", oss.str() );
00068         q->addAttrib( "xmlns", XMLNS_LAST );
00069 
00070         m_parent->send( t );
00071         break;
00072       }
00073 
00074       case STANZA_IQ_SET:
00075       {
00076         Tag *t = new Tag( "iq" );
00077         t->addAttrib( "id", stanza->id() );
00078         t->addAttrib( "to", stanza->from().full() );
00079         t->addAttrib( "type", "error" );
00080         Tag *e = new Tag( t, "error" );
00081         e->addAttrib( "type", "cancel" );
00082         Tag *f = new Tag( e, "feature-not-implemented" );
00083         f->addAttrib( "xmlns", XMLNS_XMPP_STANZAS );
00084 
00085         m_parent->send( t );
00086         break;
00087       }
00088 
00089       default:
00090         break;
00091     }
00092 
00093     return true;
00094   }
00095 
00096   bool LastActivity::handleIqID( Stanza *stanza, int /*context*/ )
00097   {
00098     if( !m_lastActivityHandler )
00099       return false;
00100 
00101     int secs = 0;
00102     const std::string seconds = stanza->findChild( "query" )->findAttribute( "seconds" );
00103     if( !seconds.empty() )
00104       secs = atoi( seconds.c_str() );
00105 
00106     switch( stanza->subtype() )
00107     {
00108       case STANZA_IQ_RESULT:
00109         m_lastActivityHandler->handleLastActivityResult( stanza->from(), secs );
00110         break;
00111       case STANZA_IQ_ERROR:
00112         m_lastActivityHandler->handleLastActivityError( stanza->from(), stanza->error() );
00113         break;
00114       default:
00115         break;
00116     }
00117 
00118     return false;
00119   }
00120 
00121   void LastActivity::resetIdleTimer()
00122   {
00123     m_active = time( 0 );
00124   }
00125 
00126 }

Generated on Mon Jan 16 16:19:54 2006 for gloox by  doxygen 1.4.6