Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | Related Pages

presence.cpp

00001 /*
00002   Copyright (c) 2007-2008 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 #include <sstream>
00019 
00020 namespace gloox
00021 {
00022 
00023   static const char* msgTypeStringValues[] =
00024   {
00025     "available", "", "", "", "", "unavailable", "probe", "error"
00026   };
00027 
00028   static inline const std::string typeString( Presence::PresenceType type )
00029   {
00030     return util::lookup( type, msgTypeStringValues );
00031   }
00032 
00033   static const char* msgShowStringValues[] =
00034   {
00035     "", "chat", "away", "dnd", "xa", "", "", ""
00036   };
00037 
00038   static inline const std::string showString( Presence::PresenceType type )
00039   {
00040     return util::lookup( type, msgShowStringValues );
00041   }
00042 
00043   Presence::Presence( Tag* tag )
00044     : Stanza( tag ), m_subtype( Invalid ), m_stati( 0 ), m_priority( 0 )
00045   {
00046     if( !tag || tag->name() != "presence" )
00047       return;
00048 
00049     const std::string& type = tag->findAttribute( TYPE );
00050     if( type.empty() )
00051       m_subtype = Available;
00052     else
00053       m_subtype = (PresenceType)util::lookup( type, msgTypeStringValues );
00054 
00055     if( m_subtype == Available )
00056     {
00057       Tag* t = tag->findChild( "show" );
00058       if( t )
00059         m_subtype = (PresenceType)util::lookup( t->cdata(), msgShowStringValues );
00060     }
00061 
00062     const TagList& c = tag->children();
00063     TagList::const_iterator it = c.begin();
00064     for( ; it != c.end(); ++it )
00065     {
00066       if( (*it)->name() == "status" )
00067         setLang( &m_stati, m_status, (*it) );
00068       else if( (*it)->name() == "priority" )
00069         m_priority = atoi( (*it)->cdata().c_str() );
00070     }
00071   }
00072 
00073   Presence::Presence( PresenceType type, const JID& to, const std::string& status,
00074                       int priority, const std::string& xmllang )
00075     : Stanza( to ), m_subtype( type ), m_stati( 0 )
00076   {
00077     setLang( &m_stati, m_status, status, xmllang );
00078 
00079     setPriority( priority );
00080   }
00081 
00082   Presence::~Presence()
00083   {
00084     delete m_stati;
00085   }
00086 
00087   void Presence::resetStatus()
00088   {
00089     delete m_stati;
00090     m_stati = 0;
00091     m_status = "";
00092   }
00093 
00094   void Presence::setPriority( int priority )
00095   {
00096     if( priority < -128 )
00097       m_priority = -128;
00098     else if( priority > 127 )
00099       m_priority = 127;
00100     else
00101       m_priority = priority;
00102   }
00103 
00104   const Capabilities* Presence::capabilities() const
00105   {
00106     return findExtension<Capabilities>( ExtCaps );
00107   }
00108 
00109   Tag* Presence::tag() const
00110   {
00111     if( m_subtype == Invalid )
00112       return 0;
00113 
00114     Tag* t = new Tag( "presence" );
00115     t->setXmlns( XMLNS_CLIENT );
00116     if( m_to )
00117       t->addAttribute( "to", m_to.full() );
00118     if( m_from )
00119       t->addAttribute( "from", m_from.full() );
00120 
00121     const std::string& type = typeString( m_subtype );
00122     if( !type.empty() )
00123     {
00124       if( type != "available" )
00125         t->addAttribute( "type", type );
00126     }
00127     else
00128     {
00129       const std::string& show = showString( m_subtype );
00130       if( !show.empty() )
00131         new Tag( t, "show", show );
00132     }
00133 
00134 //     const int len = 4 + (int)std::log10( m_priority ? m_priority : 1 ) + 1;
00135 //     char* tmp = new char[len];
00136 //     sprintf( tmp, "%d", m_priority );
00137 //     std::string ret( tmp, len );
00138 //     new Tag( t, "priority", ret );
00139 //     delete[] tmp;
00140 
00141     std::ostringstream oss;
00142     oss << m_priority;
00143     new Tag( t, "priority", oss.str() );
00144 
00145     getLangs( m_stati, m_status, "status", t );
00146 
00147     StanzaExtensionList::const_iterator it = m_extensionList.begin();
00148     for( ; it != m_extensionList.end(); ++it )
00149       t->addChild( (*it)->tag() );
00150 
00151     return t;
00152   }
00153 
00154 }

Generated on Tue Apr 22 15:10:11 2008 for gloox by  doxygen 1.4.1