gloox
1.0
|
00001 /* 00002 Copyright (c) 2007-2009 by Jakob Schroeter <js@camaya.net> 00003 This file is part of the gloox library. http://camaya.net/gloox 00004 00005 This software is distributed under a license. The full license 00006 agreement can be found in the file LICENSE in this distribution. 00007 This software may not be copied, modified, sold or distributed 00008 other than expressed in the named license agreement. 00009 00010 This software is distributed without any warranty. 00011 */ 00012 00013 #include "presence.h" 00014 #include "capabilities.h" 00015 #include "util.h" 00016 00017 #include <cstdlib> 00018 00019 namespace gloox 00020 { 00021 00022 static const char* msgTypeStringValues[] = 00023 { 00024 "available", "", "", "", "", "unavailable", "probe", "error" 00025 }; 00026 00027 static inline const std::string typeString( Presence::PresenceType type ) 00028 { 00029 return util::lookup( type, msgTypeStringValues ); 00030 } 00031 00032 static const char* msgShowStringValues[] = 00033 { 00034 "", "chat", "away", "dnd", "xa", "", "", "" 00035 }; 00036 00037 static inline const std::string showString( Presence::PresenceType type ) 00038 { 00039 return util::lookup( type, msgShowStringValues ); 00040 } 00041 00042 Presence::Presence( Tag* tag ) 00043 : Stanza( tag ), m_subtype( Invalid ), m_stati( 0 ), m_priority( 0 ) 00044 { 00045 if( !tag || tag->name() != "presence" ) 00046 return; 00047 00048 const std::string& type = tag->findAttribute( TYPE ); 00049 if( type.empty() ) 00050 m_subtype = Available; 00051 else 00052 m_subtype = (PresenceType)util::lookup( type, msgTypeStringValues ); 00053 00054 if( m_subtype == Available ) 00055 { 00056 Tag* t = tag->findChild( "show" ); 00057 if( t ) 00058 m_subtype = (PresenceType)util::lookup( t->cdata(), msgShowStringValues ); 00059 } 00060 00061 const TagList& c = tag->children(); 00062 TagList::const_iterator it = c.begin(); 00063 for( ; it != c.end(); ++it ) 00064 { 00065 if( (*it)->name() == "status" ) 00066 setLang( &m_stati, m_status, (*it) ); 00067 else if( (*it)->name() == "priority" ) 00068 m_priority = atoi( (*it)->cdata().c_str() ); 00069 } 00070 } 00071 00072 Presence::Presence( PresenceType type, const JID& to, const std::string& status, 00073 int priority, const std::string& xmllang ) 00074 : Stanza( to ), m_subtype( type ), m_stati( 0 ) 00075 { 00076 setLang( &m_stati, m_status, status, xmllang ); 00077 00078 setPriority( priority ); 00079 } 00080 00081 Presence::~Presence() 00082 { 00083 delete m_stati; 00084 } 00085 00086 void Presence::resetStatus() 00087 { 00088 delete m_stati; 00089 m_stati = 0; 00090 m_status = ""; 00091 } 00092 00093 void Presence::setPriority( int priority ) 00094 { 00095 if( priority < -128 ) 00096 m_priority = -128; 00097 else if( priority > 127 ) 00098 m_priority = 127; 00099 else 00100 m_priority = priority; 00101 } 00102 00103 const Capabilities* Presence::capabilities() const 00104 { 00105 return findExtension<Capabilities>( ExtCaps ); 00106 } 00107 00108 Tag* Presence::tag() const 00109 { 00110 if( m_subtype == Invalid ) 00111 return 0; 00112 00113 Tag* t = new Tag( "presence" ); 00114 if( m_to ) 00115 t->addAttribute( "to", m_to.full() ); 00116 if( m_from ) 00117 t->addAttribute( "from", m_from.full() ); 00118 00119 const std::string& type = typeString( m_subtype ); 00120 if( !type.empty() ) 00121 { 00122 if( type != "available" ) 00123 t->addAttribute( "type", type ); 00124 } 00125 else 00126 { 00127 const std::string& show = showString( m_subtype ); 00128 if( !show.empty() ) 00129 new Tag( t, "show", show ); 00130 } 00131 00132 new Tag( t, "priority", util::int2string( m_priority ) ); 00133 00134 getLangs( m_stati, m_status, "status", t ); 00135 00136 StanzaExtensionList::const_iterator it = m_extensionList.begin(); 00137 for( ; it != m_extensionList.end(); ++it ) 00138 t->addChild( (*it)->tag() ); 00139 00140 return t; 00141 } 00142 00143 }