00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "stanza.h"
00015 #include "error.h"
00016 #include "jid.h"
00017 #include "util.h"
00018 #include "stanzaextension.h"
00019 #include "stanzaextensionfactory.h"
00020
00021 #include <cstdlib>
00022
00023 namespace gloox
00024 {
00025
00026 Stanza::Stanza( const JID& to )
00027 : m_xmllang( "default" ), m_to( to )
00028 {
00029 }
00030
00031 Stanza::Stanza( Tag* tag )
00032 : m_xmllang( "default" )
00033 {
00034 if( !tag )
00035 return;
00036
00037 m_from.setJID( tag->findAttribute( "from" ) );
00038 m_to.setJID( tag->findAttribute( "to" ) );
00039 m_id = tag->findAttribute( "id" );
00040 }
00041
00042 Stanza::~Stanza()
00043 {
00044 removeExtensions();
00045 }
00046
00047 const Error* Stanza::error() const
00048 {
00049 return findExtension<Error>( ExtError );
00050 }
00051
00052 void Stanza::addExtension( const StanzaExtension* se )
00053 {
00054 m_extensionList.push_back( se );
00055 }
00056
00057 const StanzaExtension* Stanza::findExtension( int type ) const
00058 {
00059 StanzaExtensionList::const_iterator it = m_extensionList.begin();
00060 for( ; it != m_extensionList.end() && (*it)->extensionType() != type; ++it ) ;
00061 return it != m_extensionList.end() ? (*it) : 0;
00062 }
00063
00064 void Stanza::removeExtensions()
00065 {
00066
00067
00068 StanzaExtensionList::iterator it = m_extensionList.begin();
00069 StanzaExtensionList::iterator it2;
00070 while( it != m_extensionList.end() )
00071 {
00072 it2 = it++;
00073 delete (*it2);
00074 m_extensionList.erase( it2 );
00075 }
00076
00077 }
00078
00079 void Stanza::setLang( StringMap** map,
00080 std::string& defaultLang,
00081 const Tag* tag )
00082 {
00083 const std::string& lang = tag->findAttribute( "xml:lang" );
00084 setLang( map, defaultLang, tag ? tag->cdata() : EmptyString, lang );
00085 }
00086
00087 void Stanza::setLang( StringMap** map,
00088 std::string& defaultLang,
00089 const std::string& data,
00090 const std::string& xmllang )
00091 {
00092 if( data.empty() )
00093 return;
00094
00095 if( xmllang.empty() )
00096 defaultLang = data;
00097 else
00098 {
00099 if( !*map )
00100 *map = new StringMap();
00101 (**map)[xmllang] = data;
00102 }
00103 }
00104
00105 const std::string& Stanza::findLang( const StringMap* map,
00106 const std::string& defaultData,
00107 const std::string& lang )
00108 {
00109 if( map && lang != "default" )
00110 {
00111 StringMap::const_iterator it = map->find( lang );
00112 if( it != map->end() )
00113 return (*it).second;
00114 }
00115 return defaultData;
00116 }
00117
00118 void Stanza::getLangs( const StringMap* map,
00119 const std::string& defaultData,
00120 const std::string& name,
00121 Tag* tag )
00122 {
00123 if( !defaultData.empty() )
00124 new Tag( tag, name, defaultData );
00125
00126 if( !map )
00127 return;
00128
00129 StringMap::const_iterator it = map->begin();
00130 for( ; it != map->end(); ++it )
00131 {
00132 Tag* t = new Tag( tag, name, "xml:lang", (*it).first );
00133 t->setCData( (*it).second );
00134 }
00135 }
00136
00137 }