gloox  1.0.9
subscription.cpp
1 /*
2  Copyright (c) 2007-2013 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 #include "subscription.h"
14 #include "util.h"
15 
16 namespace gloox
17 {
18 
19  static const char* msgTypeStringValues[] =
20  {
21  "subscribe", "subscribed", "unsubscribe", "unsubscribed"
22  };
23 
24  static inline const std::string typeString( Subscription::S10nType type )
25  {
26  return util::lookup( type, msgTypeStringValues );
27  }
28 
29  Subscription::Subscription( Tag* tag )
30  : Stanza( tag ), m_subtype( Invalid ), m_stati( 0 )
31  {
32  if( !tag || tag->name() != "presence" )
33  return;
34 
35  m_subtype = (S10nType)util::lookup( tag->findAttribute( TYPE ), msgTypeStringValues );
36 
37  const ConstTagList& c = tag->findTagList( "/presence/status" );
38  ConstTagList::const_iterator it = c.begin();
39  for( ; it != c.end(); ++it )
40  setLang( &m_stati, m_status, (*it) );
41  }
42 
43  Subscription::Subscription( S10nType type, const JID& to, const std::string& status,
44  const std::string& xmllang )
45  : Stanza( to ), m_subtype( type ), m_stati( 0 )
46  {
47  setLang( &m_stati, m_status, status, xmllang );
48  }
49 
51  {
52  delete m_stati;
53  }
54 
56  {
57  if( m_subtype == Invalid )
58  return 0;
59 
60  Tag* t = new Tag( "presence" );
61  if( m_to )
62  t->addAttribute( "to", m_to.full() );
63  if( m_from )
64  t->addAttribute( "from", m_from.full() );
65 
66  t->addAttribute( "type", typeString( m_subtype ) );
67 
68  getLangs( m_stati, m_status, "status", t );
69 
70  StanzaExtensionList::const_iterator it = m_extensionList.begin();
71  for( ; it != m_extensionList.end(); ++it )
72  t->addChild( (*it)->tag() );
73 
74  return t;
75  }
76 
77 }