privatexml.cpp

00001 /*
00002   Copyright (c) 2004-2006 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, REQUEST_XML );
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, STORE_XML );
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 REQUEST_XML:
00085             {
00086               Tag *q = stanza->findChild( "query" );
00087               if( q )
00088               {
00089                 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 STORE_XML:
00100             {
00101               (*t).second->handlePrivateXMLResult( stanza->id(), PrivateXMLHandler::PXML_STORE_OK );
00102               break;
00103             }
00104           }
00105           return true;
00106           break;
00107         }
00108         case StanzaIqError:
00109         {
00110           switch( context )
00111           {
00112             case REQUEST_XML:
00113             {
00114               (*t).second->handlePrivateXMLResult( stanza->id(), PrivateXMLHandler::PXML_REQUEST_ERROR );
00115               break;
00116             }
00117 
00118             case STORE_XML:
00119             {
00120               (*t).second->handlePrivateXMLResult( stanza->id(), PrivateXMLHandler::PXML_STORE_ERROR );
00121               break;
00122             }
00123           }
00124           break;
00125         }
00126         default:
00127           break;
00128       }
00129 
00130       m_track.erase( t );
00131     }
00132     return false;
00133   }
00134 
00135   bool PrivateXML::handleIq( Stanza * /*stanza*/ )
00136   {
00137     return false;
00138   }
00139 
00140 }

Generated on Tue May 1 14:20:20 2007 for gloox by  doxygen 1.5.1