message.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 "util.h"
00014 #include "message.h"
00015 
00016 namespace gloox
00017 {
00018 
00019   static const char* msgTypeStringValues[] =
00020   {
00021     "chat", "error", "groupchat", "headline", "normal"
00022   };
00023 
00024   static inline const std::string typeString( Message::MessageType type )
00025   {
00026     return util::lookup2( type, msgTypeStringValues );
00027   }
00028 
00029   Message::Message( Tag* tag )
00030     : Stanza( tag ), m_subtype( Invalid ), m_bodies( 0 ), m_subjects( 0 )
00031   {
00032     if( !tag || tag->name() != "message" )
00033       return;
00034 
00035     const std::string& typestring = tag->findAttribute( TYPE );
00036     if( typestring.empty() )
00037       m_subtype = Normal;
00038     else
00039       m_subtype = (MessageType)util::lookup2( typestring, msgTypeStringValues );
00040 
00041     const TagList& c = tag->children();
00042     TagList::const_iterator it = c.begin();
00043     for( ; it != c.end(); ++it )
00044     {
00045       if( (*it)->name() == "body" )
00046         setLang( &m_bodies, m_body, (*it) );
00047       else if( (*it)->name() == "subject" )
00048         setLang( &m_subjects, m_subject, (*it) );
00049       else if( (*it)->name() == "thread" )
00050         m_thread = (*it)->cdata();
00051     }
00052   }
00053 
00054   Message::Message( MessageType type, const JID& to,
00055                     const std::string& body, const std::string& subject,
00056                     const std::string& thread, const std::string& xmllang )
00057     : Stanza( to ), m_subtype( type ), m_bodies( 0 ), m_subjects( 0 ), m_thread( thread )
00058   {
00059     setLang( &m_bodies, m_body, body, xmllang );
00060     setLang( &m_subjects, m_subject, subject, xmllang );
00061   }
00062 
00063   Message::~Message()
00064   {
00065     delete m_bodies;
00066     delete m_subjects;
00067   }
00068 
00069   Tag* Message::tag() const
00070   {
00071     if( m_subtype == Invalid )
00072       return 0;
00073 
00074     Tag* t = new Tag( "message" );
00075     if( m_to )
00076       t->addAttribute( "to", m_to.full() );
00077     if( m_from )
00078       t->addAttribute( "from", m_from.full() );
00079     if( !m_id.empty() )
00080       t->addAttribute( "id", m_id );
00081     t->addAttribute( TYPE, typeString( m_subtype ) );
00082 
00083     getLangs( m_bodies, m_body, "body", t );
00084     getLangs( m_subjects, m_subject, "subject", t );
00085 
00086     if( !m_thread.empty() )
00087       new Tag( t, "thread", m_thread );
00088 
00089     StanzaExtensionList::const_iterator it = m_extensionList.begin();
00090     for( ; it != m_extensionList.end(); ++it )
00091       t->addChild( (*it)->tag() );
00092 
00093     return t;
00094   }
00095 
00096 }

Generated by  doxygen 1.6.2