gloox  1.0.9
presence.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 "presence.h"
14 #include "capabilities.h"
15 #include "util.h"
16 
17 #include <cstdlib>
18 
19 namespace gloox
20 {
21 
22  static const char* msgTypeStringValues[] =
23  {
24  "available", "", "", "", "", "unavailable", "probe", "error"
25  };
26 
27  static inline const std::string typeString( Presence::PresenceType type )
28  {
29  return util::lookup( type, msgTypeStringValues );
30  }
31 
32  static const char* msgShowStringValues[] =
33  {
34  "", "chat", "away", "dnd", "xa", "", "", ""
35  };
36 
37  static inline const std::string showString( Presence::PresenceType type )
38  {
39  return util::lookup( type, msgShowStringValues );
40  }
41 
42  Presence::Presence( Tag* tag )
43  : Stanza( tag ), m_subtype( Invalid ), m_stati( 0 ), m_priority( 0 )
44  {
45  if( !tag || tag->name() != "presence" )
46  return;
47 
48  const std::string& type = tag->findAttribute( TYPE );
49  if( type.empty() )
50  m_subtype = Available;
51  else
52  m_subtype = (PresenceType)util::lookup( type, msgTypeStringValues );
53 
54  if( m_subtype == Available )
55  {
56  Tag* t = tag->findChild( "show" );
57  if( t )
58  m_subtype = (PresenceType)util::lookup( t->cdata(), msgShowStringValues );
59  }
60 
61  const TagList& c = tag->children();
62  TagList::const_iterator it = c.begin();
63  for( ; it != c.end(); ++it )
64  {
65  if( (*it)->name() == "status" )
66  setLang( &m_stati, m_status, (*it) );
67  else if( (*it)->name() == "priority" )
68  m_priority = atoi( (*it)->cdata().c_str() );
69  }
70  }
71 
72  Presence::Presence( PresenceType type, const JID& to, const std::string& status,
73  int priority, const std::string& xmllang )
74  : Stanza( to ), m_subtype( type ), m_stati( 0 )
75  {
76  setLang( &m_stati, m_status, status, xmllang );
77 
78  setPriority( priority );
79  }
80 
82  {
83  delete m_stati;
84  }
85 
87  {
88  delete m_stati;
89  m_stati = 0;
90  m_status = "";
91  }
92 
93  void Presence::setPriority( int priority )
94  {
95  if( priority < -128 )
96  m_priority = -128;
97  else if( priority > 127 )
98  m_priority = 127;
99  else
100  m_priority = priority;
101  }
102 
104  {
105  return findExtension<Capabilities>( ExtCaps );
106  }
107 
109  {
110  if( m_subtype == Invalid )
111  return 0;
112 
113  Tag* t = new Tag( "presence" );
114  if( m_to )
115  t->addAttribute( "to", m_to.full() );
116  if( m_from )
117  t->addAttribute( "from", m_from.full() );
118 
119  const std::string& type = typeString( m_subtype );
120  if( !type.empty() )
121  {
122  if( type != "available" )
123  t->addAttribute( "type", type );
124  }
125  else
126  {
127  const std::string& show = showString( m_subtype );
128  if( !show.empty() )
129  new Tag( t, "show", show );
130  }
131 
132  new Tag( t, "priority", util::int2string( m_priority ) );
133 
134  getLangs( m_stati, m_status, "status", t );
135 
136  StanzaExtensionList::const_iterator it = m_extensionList.begin();
137  for( ; it != m_extensionList.end(); ++it )
138  t->addChild( (*it)->tag() );
139 
140  return t;
141  }
142 
143 }