gloox  0.9.9.12
chatstatefilter.cpp
1 /*
2  Copyright (c) 2005-2008 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 "chatstatefilter.h"
15 #include "chatstatehandler.h"
16 #include "messageeventhandler.h"
17 #include "messagesession.h"
18 #include "stanza.h"
19 
20 namespace gloox
21 {
22 
24  : MessageFilter( parent ), m_chatStateHandler( 0 ), m_lastSent( ChatStateGone ),
25  m_enableChatStates( true )
26  {
27  }
28 
30  {
31  }
32 
34  {
35  if( m_chatStateHandler )
36  {
37  if( stanza->body().empty() )
38  {
39  m_enableChatStates = true;
40  if( stanza->hasChild( "active" ) )
41  m_chatStateHandler->handleChatState( stanza->from(), ChatStateActive );
42  else if( stanza->hasChild( "composing" ) )
43  m_chatStateHandler->handleChatState( stanza->from(), ChatStateComposing );
44  else if( stanza->hasChild( "paused" ) )
45  m_chatStateHandler->handleChatState( stanza->from(), ChatStatePaused );
46  else if( stanza->hasChild( "inactive" ) )
47  m_chatStateHandler->handleChatState( stanza->from(), ChatStateInactive );
48  else if( stanza->hasChild( "gone" ) )
49  m_chatStateHandler->handleChatState( stanza->from(), ChatStateGone );
50  else
51  m_enableChatStates = false;
52  }
53  else
54  {
55  if( stanza->hasChild( "active", "xmlns", XMLNS_CHAT_STATES )
56  || stanza->hasChild( "composing", "xmlns", XMLNS_CHAT_STATES )
57  || stanza->hasChild( "paused", "xmlns", XMLNS_CHAT_STATES )
58  || stanza->hasChild( "inactive", "xmlns", XMLNS_CHAT_STATES )
59  || stanza->hasChild( "gone", "xmlns", XMLNS_CHAT_STATES ) )
60  m_enableChatStates = true;
61  else
62  m_enableChatStates = false;
63  }
64  }
65  else
66  {
67  m_enableChatStates = false;
68  }
69  }
70 
72  {
73  if( !m_enableChatStates || state == m_lastSent )
74  return;
75 
76  Tag *m = new Tag( "message" );
77  m->addAttribute( "to", m_parent->target().full() );
78  m->addAttribute( "type", "chat" );
79 
80  Tag *s = 0;
81  switch( state )
82  {
83  case ChatStateActive:
84  s = new Tag( m, "active" );
85  s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
86  break;
87  case ChatStateComposing:
88  s = new Tag( m, "composing" );
89  s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
90  break;
91  case ChatStatePaused:
92  s = new Tag( m, "paused" );
93  s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
94  break;
95  case ChatStateInactive:
96  s = new Tag( m, "inactive" );
97  s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
98  break;
99  case ChatStateGone:
100  s = new Tag( m, "gone" );
101  s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
102  break;
103  }
104 
105  m_lastSent = state;
106 
107  send( m );
108  }
109 
111  {
112  if( !m_enableChatStates )
113  return;
114 
115  Tag *s = new Tag( tag, "active" );
116  s->addAttribute( "xmlns", XMLNS_CHAT_STATES );
117  }
118 
120  {
121  m_chatStateHandler = csh;
122  }
123 
125  {
126  m_chatStateHandler = 0;
127  }
128 
129 }