privatexml.cpp

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