chatstatefilter.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 #include "chatstatefilter.h"
00015 #include "chatstatehandler.h"
00016 #include "messageeventhandler.h"
00017 #include "messagesession.h"
00018 
00019 namespace gloox
00020 {
00021 
00022   ChatStateFilter::ChatStateFilter( MessageSession *parent )
00023   : MessageFilter( parent ), m_chatStateHandler( 0 ), m_lastSent( ChatStateGone ),
00024       m_enableChatStates( true )
00025   {
00026   }
00027 
00028   ChatStateFilter::~ChatStateFilter()
00029   {
00030   }
00031 
00032   void ChatStateFilter::filter( Stanza *stanza )
00033   {
00034     if( m_chatStateHandler )
00035     {
00036       if( stanza->body().empty() )
00037       {
00038         if( stanza->hasChild( "active" ) )
00039           m_chatStateHandler->handleChatState( stanza->from(), ChatStateActive );
00040         else if( stanza->hasChild( "composing" ) )
00041           m_chatStateHandler->handleChatState( stanza->from(), ChatStateComposing );
00042         else if( stanza->hasChild( "paused" ) )
00043           m_chatStateHandler->handleChatState( stanza->from(), ChatStatePaused );
00044         else if( stanza->hasChild( "inactive" ) )
00045           m_chatStateHandler->handleChatState( stanza->from(), ChatStateInactive );
00046         else if( stanza->hasChild( "gone" ) )
00047           m_chatStateHandler->handleChatState( stanza->from(), ChatStateGone );
00048         else
00049           m_enableChatStates = false;
00050       }
00051       else
00052       {
00053         if( stanza->hasChild( "active", "xmlns", XMLNS_CHAT_STATES )
00054             || stanza->hasChild( "composing", "xmlns", XMLNS_CHAT_STATES )
00055             || stanza->hasChild( "paused", "xmlns", XMLNS_CHAT_STATES )
00056             || stanza->hasChild( "inactive", "xmlns", XMLNS_CHAT_STATES )
00057             || stanza->hasChild( "gone", "xmlns", XMLNS_CHAT_STATES ) )
00058           m_enableChatStates = true;
00059         else
00060           m_enableChatStates = false;
00061       }
00062     }
00063     else
00064     {
00065       m_enableChatStates = false;
00066     }
00067   }
00068 
00069   void ChatStateFilter::setChatState( ChatStateType state )
00070   {
00071     if( !m_enableChatStates )
00072       return;
00073 
00074     Tag *m = new Tag( "message" );
00075     m->addAttribute( "to", m_parent->target().full() );
00076 
00077     Tag *s = 0;
00078     switch( state )
00079     {
00080       case ChatStateActive:
00081         s = new Tag( m, "active" );
00082         s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
00083         break;
00084       case ChatStateComposing:
00085         s = new Tag( m, "composing" );
00086         s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
00087         break;
00088       case ChatStatePaused:
00089         s = new Tag( m, "paused" );
00090         s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
00091         break;
00092       case ChatStateInactive:
00093         s = new Tag( m, "inactive" );
00094         s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
00095         break;
00096       case ChatStateGone:
00097         s = new Tag( m, "gone" );
00098         s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
00099         break;
00100     }
00101 
00102     m_lastSent = state;
00103 
00104     m_parent->send( m );
00105   }
00106 
00107   void ChatStateFilter::decorate( Tag *tag )
00108   {
00109     if( !m_enableChatStates )
00110       return;
00111 
00112     Tag *s = new Tag( tag, "active" );
00113     s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
00114   }
00115 
00116   void ChatStateFilter::registerChatStateHandler( ChatStateHandler *csh )
00117   {
00118     m_chatStateHandler = csh;
00119   }
00120 
00121   void ChatStateFilter::removeChatStateHandler()
00122   {
00123     m_chatStateHandler = 0;
00124   }
00125 
00126 }

Generated on Wed Dec 20 18:25:28 2006 for gloox by  doxygen 1.5.1