chatstatefilter.cpp

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

Generated on Sat Nov 10 08:50:26 2007 for gloox by  doxygen 1.5.3-20071008