00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "privatexml.h"
00015 #include "clientbase.h"
00016 #include "stanza.h"
00017
00018 namespace gloox
00019 {
00020
00021
00022 PrivateXML::Query::Query( const Tag* tag )
00023 : StanzaExtension( ExtPrivateXML ), m_privateXML( 0 )
00024 {
00025 if( !tag )
00026 return;
00027
00028 if( tag->name() == "query" && tag->xmlns() == XMLNS_PRIVATE_XML && tag->children().size() )
00029 m_privateXML = tag->children().front()->clone();
00030 else
00031 m_privateXML = tag;
00032 }
00033
00034 const std::string& PrivateXML::Query::filterString() const
00035 {
00036 static const std::string filter = "/iq/query[@xmlns='" + XMLNS_PRIVATE_XML + "']";
00037 return filter;
00038 }
00039
00040 Tag* PrivateXML::Query::tag() const
00041 {
00042 Tag* t = new Tag( "query" );
00043 t->setXmlns( XMLNS_PRIVATE_XML );
00044 t->addChild( m_privateXML->clone() );
00045 return t;
00046 }
00047
00048
00049
00050 PrivateXML::PrivateXML( ClientBase* parent )
00051 : m_parent( parent )
00052 {
00053 if( !m_parent )
00054 return;
00055
00056 m_parent->registerIqHandler( this, ExtPrivateXML );
00057 m_parent->registerStanzaExtension( new Query() );
00058 }
00059
00060 PrivateXML::~PrivateXML()
00061 {
00062 if( !m_parent )
00063 return;
00064
00065 m_parent->removeIqHandler( this, ExtPrivateXML );
00066 m_parent->removeIDHandler( this );
00067 m_parent->removeStanzaExtension( ExtPrivateXML );
00068 }
00069
00070 std::string PrivateXML::requestXML( const std::string& tag, const std::string& xmlns,
00071 PrivateXMLHandler* pxh )
00072 {
00073 const std::string& id = m_parent->getID();
00074
00075 IQ iq( IQ::Get, JID(), id );
00076 iq.addExtension( new Query( tag, xmlns ) );
00077
00078 m_track[id] = pxh;
00079 m_parent->send( iq, this, RequestXml );
00080
00081 return id;
00082 }
00083
00084 std::string PrivateXML::storeXML( const Tag* tag, PrivateXMLHandler* pxh )
00085 {
00086 const std::string& id = m_parent->getID();
00087
00088 IQ iq( IQ::Set, JID(), id );
00089 iq.addExtension( new Query( tag ) );
00090
00091 m_track[id] = pxh;
00092 m_parent->send( iq, this, StoreXml );
00093
00094 return id;
00095 }
00096
00097 void PrivateXML::handleIqID( const IQ& iq, int context )
00098 {
00099 TrackMap::iterator t = m_track.find( iq.id() );
00100 if( t == m_track.end() )
00101 return;
00102
00103 if( iq.subtype() == IQ::Result )
00104 {
00105 if( context == RequestXml )
00106 {
00107 const Query* q = iq.findExtension<Query>( ExtPrivateXML );
00108 if( q )
00109 (*t).second->handlePrivateXML( q->privateXML() );
00110 }
00111 else if( context == StoreXml )
00112 (*t).second->handlePrivateXMLResult( iq.id(), PrivateXMLHandler::PxmlStoreOk );
00113 }
00114 else if( iq.subtype() == IQ::Error )
00115 {
00116 if( context == RequestXml )
00117 (*t).second->handlePrivateXMLResult( iq.id(), PrivateXMLHandler::PxmlRequestError );
00118 else if( context == StoreXml )
00119 (*t).second->handlePrivateXMLResult( iq.id(), PrivateXMLHandler::PxmlStoreError );
00120 }
00121
00122 m_track.erase( t );
00123 }
00124
00125 }