privatexml.cpp

00001 /*
00002   Copyright (c) 2004-2005 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::PrivateXML( ClientBase *parent )
00022     : m_parent( parent )
00023   {
00024     if( m_parent )
00025       m_parent->registerIqHandler( this, XMLNS_PRIVATE_XML );
00026   }
00027 
00028   PrivateXML::~PrivateXML()
00029   {
00030     if( m_parent )
00031       m_parent->removeIqHandler( XMLNS_PRIVATE_XML );
00032 
00033   }
00034 
00035   std::string PrivateXML::requestXML( const std::string& tag, const std::string& xmlns,
00036                                       PrivateXMLHandler *pxh )
00037   {
00038     const std::string id = m_parent->getID();
00039 
00040     Tag *iq = new Tag( "iq" );
00041     iq->addAttrib( "id", id );
00042     iq->addAttrib( "type", "get" );
00043     Tag *query = new Tag( "query" );
00044     query->addAttrib( "xmlns", XMLNS_PRIVATE_XML );
00045     Tag *x = new Tag( tag );
00046     x->addAttrib( "xmlns", xmlns );
00047     query->addChild( x );
00048     iq->addChild( query );
00049 
00050     m_track[id] = pxh;
00051     m_parent->trackID( this, id, REQUEST_XML );
00052     m_parent->send( iq );
00053 
00054     return id;
00055   }
00056 
00057   std::string PrivateXML::storeXML( Tag *tag, PrivateXMLHandler *pxh )
00058   {
00059     const std::string id = m_parent->getID();
00060 
00061     Tag *iq = new Tag( "iq" );
00062     iq->addAttrib( "id", id );
00063     iq->addAttrib( "type", "set" );
00064     Tag *query = new Tag( "query" );
00065     query->addAttrib( "xmlns", XMLNS_PRIVATE_XML );
00066     query->addChild( tag );
00067     iq->addChild( query );
00068 
00069     m_track[id] = pxh;
00070     m_parent->trackID( this, id, STORE_XML );
00071     m_parent->send( iq );
00072 
00073     return id;
00074   }
00075 
00076   bool PrivateXML::handleIqID( Stanza *stanza, int context )
00077   {
00078     TrackMap::iterator t = m_track.find( stanza->id() );
00079     if( t != m_track.end() )
00080     {
00081       switch( stanza->subtype() )
00082       {
00083         case STANZA_IQ_RESULT:
00084         {
00085           switch( context )
00086           {
00087             case REQUEST_XML:
00088             {
00089               Tag *q = stanza->findChild( "query" );
00090               if( q )
00091               {
00092                 Tag::TagList l = q->children();
00093                 Tag::TagList::const_iterator it = l.begin();
00094                 if( it != l.end() )
00095                 {
00096                   (*t).second->handlePrivateXML( (*it)->name(), (*it) );
00097                 }
00098               }
00099               break;
00100             }
00101 
00102             case STORE_XML:
00103             {
00104               (*t).second->handlePrivateXMLResult( stanza->id(), PrivateXMLHandler::PXML_STORE_OK );
00105               break;
00106             }
00107           }
00108           return true;
00109           break;
00110         }
00111         case STANZA_IQ_ERROR:
00112         {
00113           switch( context )
00114           {
00115             case REQUEST_XML:
00116             {
00117               (*t).second->handlePrivateXMLResult( stanza->id(), PrivateXMLHandler::PXML_REQUEST_ERROR );
00118               break;
00119             }
00120 
00121             case STORE_XML:
00122             {
00123               (*t).second->handlePrivateXMLResult( stanza->id(), PrivateXMLHandler::PXML_STORE_ERROR );
00124               break;
00125             }
00126           }
00127           break;
00128         }
00129         default:
00130           break;
00131       }
00132 
00133       m_track.erase( t );
00134     }
00135     return false;
00136   }
00137 
00138 }

Generated on Mon Jan 16 16:19:54 2006 for gloox by  doxygen 1.4.6