gloox  1.0
subscription.cpp
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 "subscription.h"
00014 #include "util.h"
00015 
00016 namespace gloox
00017 {
00018 
00019   static const char* msgTypeStringValues[] =
00020   {
00021     "subscribe", "subscribed", "unsubscribe", "unsubscribed"
00022   };
00023 
00024   static inline const std::string typeString( Subscription::S10nType type )
00025   {
00026     return util::lookup( type, msgTypeStringValues );
00027   }
00028 
00029   Subscription::Subscription( Tag* tag )
00030     : Stanza( tag ), m_subtype( Invalid ), m_stati( 0 )
00031   {
00032     if( !tag || tag->name() != "presence" )
00033       return;
00034 
00035     m_subtype = (S10nType)util::lookup( tag->findAttribute( TYPE ), msgTypeStringValues );
00036 
00037     const ConstTagList& c = tag->findTagList( "/presence/status" );
00038     ConstTagList::const_iterator it = c.begin();
00039     for( ; it != c.end(); ++it )
00040       setLang( &m_stati, m_status, (*it) );
00041   }
00042 
00043   Subscription::Subscription( S10nType type, const JID& to, const std::string& status,
00044                               const std::string& xmllang )
00045     : Stanza( to ), m_subtype( type ), m_stati( 0 )
00046   {
00047     setLang( &m_stati, m_status, status, xmllang );
00048   }
00049 
00050   Subscription::~Subscription()
00051   {
00052     delete m_stati;
00053   }
00054 
00055   Tag* Subscription::tag() const
00056   {
00057     if( m_subtype == Invalid )
00058       return 0;
00059 
00060     Tag* t = new Tag( "presence" );
00061     if( m_to )
00062       t->addAttribute( "to", m_to.full() );
00063     if( m_from )
00064       t->addAttribute( "from", m_from.full() );
00065 
00066     t->addAttribute( "type", typeString( m_subtype ) );
00067 
00068     getLangs( m_stati, m_status, "status", t );
00069 
00070     StanzaExtensionList::const_iterator it = m_extensionList.begin();
00071     for( ; it != m_extensionList.end(); ++it )
00072       t->addChild( (*it)->tag() );
00073 
00074     return t;
00075   }
00076 
00077 }