lastactivity.cpp

00001 /*
00002   Copyright (c) 2005-2006 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 <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 /*context*/ )
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 }

Generated on Tue May 1 14:20:20 2007 for gloox by  doxygen 1.5.1