gloox 1.0
chatstatefilter.cpp
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 #include "chatstatefilter.h"
00015 #include "chatstatehandler.h"
00016 #include "messageeventhandler.h"
00017 #include "messagesession.h"
00018 #include "message.h"
00019 #include "chatstate.h"
00020 
00021 namespace gloox
00022 {
00023 
00024   ChatStateFilter::ChatStateFilter( MessageSession* parent )
00025     : MessageFilter( parent ), m_chatStateHandler( 0 ), m_lastSent( ChatStateGone ),
00026       m_enableChatStates( true )
00027   {
00028   }
00029 
00030   ChatStateFilter::~ChatStateFilter()
00031   {
00032   }
00033 
00034   void ChatStateFilter::filter( Message& msg )
00035   {
00036     if( m_enableChatStates && m_chatStateHandler )
00037     {
00038       const ChatState* state = msg.findExtension<ChatState>( ExtChatState );
00039 
00040       m_enableChatStates = state && state->state() != ChatStateInvalid;
00041       if( m_enableChatStates && msg.body().empty() )
00042         m_chatStateHandler->handleChatState( msg.from(), state->state() );
00043     }
00044   }
00045 
00046   void ChatStateFilter::setChatState( ChatStateType state )
00047   {
00048     if( !m_enableChatStates || state == m_lastSent || state == ChatStateInvalid )
00049       return;
00050 
00051     Message m( Message::Chat, m_parent->target() );
00052     m.addExtension( new ChatState( state ) );
00053 
00054     m_lastSent = state;
00055 
00056     send( m );
00057   }
00058 
00059   void ChatStateFilter::decorate( Message& msg )
00060   {
00061     if( m_enableChatStates )
00062       msg.addExtension( new ChatState( ChatStateActive ) );
00063   }
00064 
00065 }