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         m_enableChatStates = true;
00039         if( stanza->hasChild( "active" ) )
00040           m_chatStateHandler->handleChatState( stanza->from(), ChatStateActive );
00041         else if( stanza->hasChild( "composing" ) )
00042           m_chatStateHandler->handleChatState( stanza->from(), ChatStateComposing );
00043         else if( stanza->hasChild( "paused" ) )
00044           m_chatStateHandler->handleChatState( stanza->from(), ChatStatePaused );
00045         else if( stanza->hasChild( "inactive" ) )
00046           m_chatStateHandler->handleChatState( stanza->from(), ChatStateInactive );
00047         else if( stanza->hasChild( "gone" ) )
00048           m_chatStateHandler->handleChatState( stanza->from(), ChatStateGone );
00049         else
00050           m_enableChatStates = false;
00051       }
00052       else
00053       {
00054         if( stanza->hasChild( "active", "xmlns", XMLNS_CHAT_STATES )
00055             || stanza->hasChild( "composing", "xmlns", XMLNS_CHAT_STATES )
00056             || stanza->hasChild( "paused", "xmlns", XMLNS_CHAT_STATES )
00057             || stanza->hasChild( "inactive", "xmlns", XMLNS_CHAT_STATES )
00058             || stanza->hasChild( "gone", "xmlns", XMLNS_CHAT_STATES ) )
00059           m_enableChatStates = true;
00060         else
00061           m_enableChatStates = false;
00062       }
00063     }
00064     else
00065     {
00066       m_enableChatStates = false;
00067     }
00068   }
00069 
00070   void ChatStateFilter::setChatState( ChatStateType state )
00071   {
00072     if( !m_enableChatStates )
00073       return;
00074 
00075     Tag *m = new Tag( "message" );
00076     m->addAttribute( "to", m_parent->target().full() );
00077 
00078     Tag *s = 0;
00079     switch( state )
00080     {
00081       case ChatStateActive:
00082         s = new Tag( m, "active" );
00083         s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
00084         break;
00085       case ChatStateComposing:
00086         s = new Tag( m, "composing" );
00087         s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
00088         break;
00089       case ChatStatePaused:
00090         s = new Tag( m, "paused" );
00091         s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
00092         break;
00093       case ChatStateInactive:
00094         s = new Tag( m, "inactive" );
00095         s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
00096         break;
00097       case ChatStateGone:
00098         s = new Tag( m, "gone" );
00099         s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
00100         break;
00101     }
00102 
00103     m_lastSent = state;
00104 
00105     m_parent->send( m );
00106   }
00107 
00108   void ChatStateFilter::decorate( Tag *tag )
00109   {
00110     if( !m_enableChatStates )
00111       return;
00112 
00113     Tag *s = new Tag( tag, "active" );
00114     s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
00115   }
00116 
00117   void ChatStateFilter::registerChatStateHandler( ChatStateHandler *csh )
00118   {
00119     m_chatStateHandler = csh;
00120   }
00121 
00122   void ChatStateFilter::removeChatStateHandler()
00123   {
00124     m_chatStateHandler = 0;
00125   }
00126 
00127 }

Generated on Tue May 1 14:20:20 2007 for gloox by  doxygen 1.5.1