lastactivity.cpp

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

Generated on Mon Dec 7 13:28:19 2009 for gloox by  doxygen 1.6.1