privatexml.cpp

00001 /*
00002   Copyright (c) 2004-2008 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     {
00032       m_parent->removeIqHandler( XMLNS_PRIVATE_XML );
00033       m_parent->removeIDHandler( this );
00034     }
00035   }
00036 
00037   std::string PrivateXML::requestXML( const std::string& tag, const std::string& xmlns,
00038                                       PrivateXMLHandler *pxh )
00039   {
00040     const std::string& id = m_parent->getID();
00041 
00042     Tag *iq = new Tag( "iq" );
00043     iq->addAttribute( "id", id );
00044     iq->addAttribute( "type", "get" );
00045     Tag *query = new Tag( iq, "query" );
00046     query->addAttribute( "xmlns", XMLNS_PRIVATE_XML );
00047     Tag *x = new Tag( query, tag );
00048     x->addAttribute( "xmlns", xmlns );
00049 
00050     m_track[id] = pxh;
00051     m_parent->trackID( this, id, RequestXml );
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->addAttribute( "id", id );
00063     iq->addAttribute( "type", "set" );
00064     Tag *query = new Tag( iq, "query" );
00065     query->addAttribute( "xmlns", XMLNS_PRIVATE_XML );
00066     query->addChild( tag );
00067 
00068     m_track[id] = pxh;
00069     m_parent->trackID( this, id, StoreXml );
00070     m_parent->send( iq );
00071 
00072     return id;
00073   }
00074 
00075   bool PrivateXML::handleIqID( Stanza *stanza, int context )
00076   {
00077     TrackMap::iterator t = m_track.find( stanza->id() );
00078     if( t != m_track.end() )
00079     {
00080       switch( stanza->subtype() )
00081       {
00082         case StanzaIqResult:
00083         {
00084           switch( context )
00085           {
00086             case RequestXml:
00087             {
00088               Tag *q = stanza->findChild( "query" );
00089               if( q )
00090               {
00091                 const Tag::TagList& l = q->children();
00092                 Tag::TagList::const_iterator it = l.begin();
00093                 if( it != l.end() )
00094                 {
00095                   (*t).second->handlePrivateXML( (*it)->name(), (*it) );
00096                 }
00097               }
00098               break;
00099             }
00100 
00101             case StoreXml:
00102             {
00103               (*t).second->handlePrivateXMLResult( stanza->id(), PrivateXMLHandler::PxmlStoreOk );
00104               break;
00105             }
00106           }
00107           m_track.erase( t );
00108           return true;
00109           break;
00110         }
00111         case StanzaIqError:
00112         {
00113           switch( context )
00114           {
00115             case RequestXml:
00116             {
00117               (*t).second->handlePrivateXMLResult( stanza->id(), PrivateXMLHandler::PxmlRequestError );
00118               break;
00119             }
00120 
00121             case StoreXml:
00122             {
00123               (*t).second->handlePrivateXMLResult( stanza->id(), PrivateXMLHandler::PxmlStoreError );
00124               break;
00125             }
00126           }
00127           break;
00128         }
00129         default:
00130           break;
00131       }
00132 
00133       m_track.erase( t );
00134     }
00135 
00136     return false;
00137   }
00138 
00139   bool PrivateXML::handleIq( Stanza * /*stanza*/ )
00140   {
00141     return false;
00142   }
00143 
00144 }

Generated on Sun Apr 27 11:08:13 2008 for gloox by  doxygen 1.5.5