privatexml.cpp
/*
Copyright (c) 2004-2023 by Jakob Schröter <js@camaya.net>
This file is part of the gloox library. http://camaya.net/gloox
This software is distributed under a license. The full license
agreement can be found in the file LICENSE in this distribution.
This software may not be copied, modified, sold or distributed
other than expressed in the named license agreement.
This software is distributed without any warranty.
*/
#include "privatexml.h"
#include "clientbase.h"
#include "stanza.h"
namespace gloox
{
// ---- PrivateXML::Query ----
PrivateXML::Query::Query( const Tag* tag )
: StanzaExtension( ExtPrivateXML ), m_privateXML( 0 )
{
if( !tag )
return;
if( tag->name() == "query" && tag->xmlns() == XMLNS_PRIVATE_XML )
{
if( tag->children().size() )
m_privateXML = tag->children().front()->clone();
}
else
m_privateXML = tag;
}
const std::string& PrivateXML::Query::filterString() const
{
static const std::string filter = "/iq/query[@xmlns='" + XMLNS_PRIVATE_XML + "']";
return filter;
}
Tag* PrivateXML::Query::tag() const
{
Tag* t = new Tag( "query" );
t->setXmlns( XMLNS_PRIVATE_XML );
if( m_privateXML )
t->addChild( m_privateXML->clone() );
return t;
}
// ---- ~PrivateXML::Query ----
// ---- PrivateXML ----
PrivateXML::PrivateXML( ClientBase* parent )
: m_parent( parent )
{
if( !m_parent )
return;
m_parent->registerIqHandler( this, ExtPrivateXML );
m_parent->registerStanzaExtension( new Query() );
}
PrivateXML::~PrivateXML()
{
if( !m_parent )
return;
m_parent->removeIqHandler( this, ExtPrivateXML );
m_parent->removeIDHandler( this );
m_parent->removeStanzaExtension( ExtPrivateXML );
}
std::string PrivateXML::requestXML( const std::string& tag, const std::string& xmlns,
PrivateXMLHandler* pxh )
{
const std::string& id = m_parent->getID();
IQ iq( IQ::Get, JID(), id );
iq.addExtension( new Query( tag, xmlns ) );
m_track[id] = pxh;
m_parent->send( iq, this, RequestXml );
return id;
}
std::string PrivateXML::storeXML( const Tag* tag, PrivateXMLHandler* pxh )
{
const std::string& id = m_parent->getID();
IQ iq( IQ::Set, JID(), id );
iq.addExtension( new Query( tag ) );
m_track[id] = pxh;
m_parent->send( iq, this, StoreXml );
return id;
}
void PrivateXML::handleIqID( const IQ& iq, int context )
{
TrackMap::iterator t = m_track.find( iq.id() );
if( t == m_track.end() )
return;
if( iq.subtype() == IQ::Result )
{
if( context == RequestXml )
{
const Query* q = iq.findExtension<Query>( ExtPrivateXML );
if( q )
(*t).second->handlePrivateXML( q->privateXML() );
}
else if( context == StoreXml )
(*t).second->handlePrivateXMLResult( iq.id(), PrivateXMLHandler::PxmlStoreOk );
}
else if( iq.subtype() == IQ::Error )
{
if( context == RequestXml )
(*t).second->handlePrivateXMLResult( iq.id(), PrivateXMLHandler::PxmlRequestError );
else if( context == StoreXml )
(*t).second->handlePrivateXMLResult( iq.id(), PrivateXMLHandler::PxmlStoreError );
}
m_track.erase( t );
}
}
gloox::ClientBase
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition clientbase.h:79
gloox::IQ
An abstraction of an IQ stanza.
Definition iq.h:34
gloox::IQ::subtype
IqType subtype() const
Definition iq.h:74
gloox::IQ::Set
@ Set
Definition iq.h:46
gloox::IQ::Error
@ Error
Definition iq.h:49
gloox::IQ::Result
@ Result
Definition iq.h:48
gloox::IQ::Get
@ Get
Definition iq.h:45
gloox::JID
An abstraction of a JID.
Definition jid.h:31
gloox::PrivateXMLHandler
A virtual interface which can be reimplemented to store and receive private XML data.
Definition privatexmlhandler.h:36
gloox::PrivateXMLHandler::PxmlStoreOk
@ PxmlStoreOk
Definition privatexmlhandler.h:43
gloox::PrivateXMLHandler::PxmlStoreError
@ PxmlStoreError
Definition privatexmlhandler.h:44
gloox::PrivateXMLHandler::PxmlRequestError
@ PxmlRequestError
Definition privatexmlhandler.h:45
gloox::PrivateXML::PrivateXML
PrivateXML(ClientBase *parent)
Definition privatexml.cpp:54
gloox::PrivateXML::requestXML
std::string requestXML(const std::string &tag, const std::string &xmlns, PrivateXMLHandler *pxh)
Definition privatexml.cpp:74
gloox::PrivateXML::handleIqID
virtual void handleIqID(const IQ &iq, int context)
Definition privatexml.cpp:101
gloox::PrivateXML::storeXML
std::string storeXML(const Tag *tag, PrivateXMLHandler *pxh)
Definition privatexml.cpp:88
gloox::PrivateXML::~PrivateXML
virtual ~PrivateXML()
Definition privatexml.cpp:64
gloox::StanzaExtension
This class abstracts a stanza extension, which is usually an XML child element in a specific namespac...
Definition stanzaextension.h:204
gloox::Stanza::addExtension
void addExtension(const StanzaExtension *se)
Definition stanza.cpp:52
gloox::Stanza::id
const std::string & id() const
Definition stanza.h:63
gloox::Stanza::findExtension
const StanzaExtension * findExtension(int type) const
Definition stanza.cpp:57
gloox::Tag
This is an abstraction of an XML element.
Definition tag.h:47
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::XMLNS_PRIVATE_XML
const std::string XMLNS_PRIVATE_XML
Definition gloox.cpp:44
gloox::ExtPrivateXML
@ ExtPrivateXML
Definition stanzaextension.h:63