gloox  1.0.1
messageeventfilter.cpp
1 /*
2  Copyright (c) 2005-2012 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 #include "messageeventfilter.h"
15 #include "messageeventhandler.h"
16 #include "messagesession.h"
17 #include "message.h"
18 #include "messageevent.h"
19 #include "error.h"
20 
21 namespace gloox
22 {
23 
25  : MessageFilter( parent ), m_messageEventHandler( 0 ), m_requestedEvents( 0 ),
26  m_lastSent( MessageEventCancel ), m_disable( false )
27  {
28  }
29 
31  {
32  }
33 
35  {
36  if( m_disable || !m_messageEventHandler )
37  return;
38 
39  if( msg.subtype() == Message::Error )
40  {
41  if( msg.error() && msg.error()->error() == StanzaErrorFeatureNotImplemented )
42  m_disable = true;
43 
44  return;
45  }
46 
48  if( !me )
49  {
50  m_requestedEvents = 0;
51  m_lastID = EmptyString;
52  return;
53  }
54 
55  if( msg.body().empty() )
56  m_messageEventHandler->handleMessageEvent( msg.from(), (MessageEventType)me->event() );
57  else
58  {
59  m_lastID = msg.id();
60  m_requestedEvents = 0;
61  m_requestedEvents = me->event();
62  }
63  }
64 
66  {
67  if( m_disable || ( !( m_requestedEvents & event ) && ( event != MessageEventCancel ) ) )
68  return;
69 
70  switch( event )
71  {
75  m_requestedEvents &= ~event;
76  break;
78  if( m_lastSent == MessageEventComposing )
79  return;
80  break;
81  case MessageEventCancel:
82  default:
83  break;
84  }
85 
86  m_lastSent = event;
87  Message m( Message::Normal, m_parent->target() );
88  m.addExtension( new MessageEvent( event, m_lastID ) );
89  send( m );
90  }
91 
93  {
94  if( m_disable )
95  return;
96 
99  m_lastSent = MessageEventCancel;
100  }
101 
103  {
104  m_messageEventHandler = meh;
105  }
106 
108  {
109  m_messageEventHandler = 0;
110  }
111 
112 }