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 <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       m_active( time ( 0 ) )
00029   {
00030     if( m_disco )
00031       m_disco->addFeature( XMLNS_LAST );
00032   }
00033 
00034   LastActivity::~LastActivity()
00035   {
00036   }
00037 
00038   void LastActivity::query( const JID& jid )
00039   {
00040     const std::string id = m_parent->getID();
00041 
00042     Tag *t = new Tag( "iq" );
00043     t->addAttribute( "type", "get" );
00044     t->addAttribute( "id", id );
00045     t->addAttribute( "to", jid.full() );
00046     Tag *q = new Tag( t, "query" );
00047     q->addAttribute( "xmlns", XMLNS_LAST );
00048 
00049     m_parent->trackID( this, id, 0 );
00050     m_parent->send( t );
00051   }
00052 
00053   bool LastActivity::handleIq( Stanza *stanza )
00054   {
00055     switch( stanza->subtype() )
00056     {
00057       case StanzaIqGet:
00058       {
00059         time_t now = time( 0 );
00060 
00061         Tag *t = new Tag( "iq" );
00062         t->addAttribute( "type", "result" );
00063         t->addAttribute( "id", stanza->id() );
00064         t->addAttribute( "to", stanza->from().full() );
00065         Tag *q = new Tag( t, "query" );
00066         std::ostringstream oss;
00067         oss << (int)(now - m_active);
00068         q->addAttribute( "seconds", oss.str() );
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 /*context*/ )
00098   {
00099     if( !m_lastActivityHandler )
00100       return false;
00101 
00102     int secs = 0;
00103     const std::string seconds = stanza->findChild( "query" )->findAttribute( "seconds" );
00104     if( !seconds.empty() )
00105       secs = atoi( seconds.c_str() );
00106 
00107     switch( stanza->subtype() )
00108     {
00109       case StanzaIqResult:
00110         m_lastActivityHandler->handleLastActivityResult( stanza->from(), secs );
00111         break;
00112       case StanzaIqError:
00113         m_lastActivityHandler->handleLastActivityError( stanza->from(), stanza->error() );
00114         break;
00115       default:
00116         break;
00117     }
00118 
00119     return false;
00120   }
00121 
00122   void LastActivity::resetIdleTimer()
00123   {
00124     m_active = time( 0 );
00125   }
00126 
00127 }

Generated on Sun Sep 24 21:57:31 2006 for gloox by  doxygen 1.4.7