gloox  1.0.20
presence.cpp
1 /*
2  Copyright (c) 2007-2017 by Jakob Schröter <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 = static_cast<PresenceType>( util::lookup( type, msgTypeStringValues ) );
53 
54  if( m_subtype == Available )
55  {
56  Tag* t = tag->findChild( "show" );
57  if( t )
58  m_subtype = static_cast<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 
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 }
virtual Tag * tag() const
Definition: presence.cpp:108
This is the base class for XMPP stanza abstractions.
Definition: stanza.h:33
std::list< Tag * > TagList
Definition: tag.h:26
const std::string TYPE
Definition: gloox.cpp:123
virtual ~Presence()
Definition: presence.cpp:81
void addChild(Tag *child)
Definition: tag.cpp:424
int priority() const
Definition: presence.h:131
const Capabilities * capabilities() const
Definition: presence.cpp:103
void setPriority(int priority)
Definition: presence.cpp:93
The namespace for the gloox library.
Definition: adhoc.cpp:27
void resetStatus()
Definition: presence.cpp:86
const std::string & full() const
Definition: jid.h:61
An abstraction of a JID.
Definition: jid.h:30
bool addAttribute(Attribute *attr)
Definition: tag.cpp:354
This is an implementation of XEP-0115 (Entity Capabilities).
Definition: capabilities.h:36
Presence(PresenceType type, const JID &to, const std::string &status=EmptyString, int priority=0, const std::string &xmllang=EmptyString)
Definition: presence.cpp:72
This is an abstraction of an XML element.
Definition: tag.h:46