privatexml.cpp

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

Generated on Sat Nov 10 08:50:27 2007 for gloox by  doxygen 1.5.3-20071008