gloox
1.0
|
00001 /* 00002 Copyright (c) 2005-2009 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 "clientbase.h" 00019 #include "error.h" 00020 #include "lastactivityhandler.h" 00021 00022 #include <cstdlib> 00023 00024 namespace gloox 00025 { 00026 00027 // ---- LastActivity::Query ---- 00028 LastActivity::Query::Query( const Tag* tag ) 00029 : StanzaExtension( ExtLastActivity ), m_seconds( -1 ) 00030 { 00031 if( !tag || tag->name() != "query" || tag->xmlns() != XMLNS_LAST ) 00032 return; 00033 00034 if( tag->hasAttribute( "seconds" ) ) 00035 m_seconds = atoi( tag->findAttribute( "seconds" ).c_str() ); 00036 00037 m_status = tag->cdata(); 00038 } 00039 00040 LastActivity::Query::Query( const std::string& _status, long _seconds ) 00041 : StanzaExtension( ExtLastActivity ), m_seconds( _seconds ), 00042 m_status( _status ) 00043 { 00044 } 00045 00046 LastActivity::Query::~Query() 00047 { 00048 } 00049 00050 const std::string& LastActivity::Query::filterString() const 00051 { 00052 static const std::string filter = "/iq/query[@xmlns='" + XMLNS_LAST + "']" 00053 "|/presence/query[@xmlns='" + XMLNS_LAST + "']"; 00054 return filter; 00055 } 00056 00057 Tag* LastActivity::Query::tag() const 00058 { 00059 Tag* t = new Tag( "query" ); 00060 t->setXmlns( XMLNS_LAST ); 00061 t->addAttribute( "seconds", m_seconds ); 00062 t->setCData( m_status ); 00063 return t; 00064 } 00065 // ---- ~LastActivity::Query ---- 00066 00067 // ---- LastActivity ---- 00068 LastActivity::LastActivity( ClientBase* parent ) 00069 : m_lastActivityHandler( 0 ), m_parent( parent ), 00070 m_active( time ( 0 ) ) 00071 { 00072 if( m_parent ) 00073 { 00074 m_parent->registerStanzaExtension( new Query() ); 00075 m_parent->registerIqHandler( this, ExtLastActivity ); 00076 m_parent->disco()->addFeature( XMLNS_LAST ); 00077 } 00078 } 00079 00080 LastActivity::~LastActivity() 00081 { 00082 if( m_parent ) 00083 { 00084 m_parent->disco()->removeFeature( XMLNS_LAST ); 00085 m_parent->removeIqHandler( this, ExtLastActivity ); 00086 m_parent->removeIDHandler( this ); 00087 } 00088 } 00089 00090 void LastActivity::query( const JID& jid ) 00091 { 00092 IQ iq( IQ::Get, jid, m_parent->getID() ); 00093 iq.addExtension( new Query() ); 00094 m_parent->send( iq, this, 0 ); 00095 } 00096 00097 bool LastActivity::handleIq( const IQ& iq ) 00098 { 00099 const Query* q = iq.findExtension<Query>( ExtLastActivity ); 00100 if( !q || iq.subtype() != IQ::Get ) 00101 return false; 00102 00103 IQ re( IQ::Result, iq.from(), iq.id() ); 00104 re.addExtension( new Query( EmptyString, (long)( time( 0 ) - m_active ) ) ); 00105 m_parent->send( re ); 00106 00107 return true; 00108 } 00109 00110 void LastActivity::handleIqID( const IQ& iq, int /*context*/ ) 00111 { 00112 if( !m_lastActivityHandler ) 00113 return; 00114 00115 if( iq.subtype() == IQ::Result ) 00116 { 00117 const Query* q = iq.findExtension<Query>( ExtLastActivity ); 00118 if( !q || q->seconds() < 0 ) 00119 return; 00120 00121 m_lastActivityHandler->handleLastActivityResult( iq.from(), q->seconds(), q->status() ); 00122 } 00123 else if( iq.subtype() == IQ::Error && iq.error() ) 00124 m_lastActivityHandler->handleLastActivityError( iq.from(), iq.error()->error() ); 00125 } 00126 00127 void LastActivity::resetIdleTimer() 00128 { 00129 m_active = time( 0 ); 00130 } 00131 00132 }