gloox  1.0
privatexml.cpp
1 /*
2  Copyright (c) 2004-2009 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 #include "privatexml.h"
15 #include "clientbase.h"
16 #include "stanza.h"
17 
18 namespace gloox
19 {
20 
21  // ---- PrivateXML::Query ----
22  PrivateXML::Query::Query( const Tag* tag )
23  : StanzaExtension( ExtPrivateXML ), m_privateXML( 0 )
24  {
25  if( !tag )
26  return;
27 
28  if( tag->name() == "query" && tag->xmlns() == XMLNS_PRIVATE_XML )
29  {
30  if( tag->children().size() )
31  m_privateXML = tag->children().front()->clone();
32  }
33  else
34  m_privateXML = tag;
35  }
36 
37  const std::string& PrivateXML::Query::filterString() const
38  {
39  static const std::string filter = "/iq/query[@xmlns='" + XMLNS_PRIVATE_XML + "']";
40  return filter;
41  }
42 
43  Tag* PrivateXML::Query::tag() const
44  {
45  Tag* t = new Tag( "query" );
46  t->setXmlns( XMLNS_PRIVATE_XML );
47  if( m_privateXML )
48  t->addChild( m_privateXML->clone() );
49  return t;
50  }
51  // ---- ~PrivateXML::Query ----
52 
53  // ---- PrivateXML ----
55  : m_parent( parent )
56  {
57  if( !m_parent )
58  return;
59 
60  m_parent->registerIqHandler( this, ExtPrivateXML );
61  m_parent->registerStanzaExtension( new Query() );
62  }
63 
65  {
66  if( !m_parent )
67  return;
68 
69  m_parent->removeIqHandler( this, ExtPrivateXML );
70  m_parent->removeIDHandler( this );
72  }
73 
74  std::string PrivateXML::requestXML( const std::string& tag, const std::string& xmlns,
75  PrivateXMLHandler* pxh )
76  {
77  const std::string& id = m_parent->getID();
78 
79  IQ iq( IQ::Get, JID(), id );
80  iq.addExtension( new Query( tag, xmlns ) );
81 
82  m_track[id] = pxh;
83  m_parent->send( iq, this, RequestXml );
84 
85  return id;
86  }
87 
88  std::string PrivateXML::storeXML( const Tag* tag, PrivateXMLHandler* pxh )
89  {
90  const std::string& id = m_parent->getID();
91 
92  IQ iq( IQ::Set, JID(), id );
93  iq.addExtension( new Query( tag ) );
94 
95  m_track[id] = pxh;
96  m_parent->send( iq, this, StoreXml );
97 
98  return id;
99  }
100 
101  void PrivateXML::handleIqID( const IQ& iq, int context )
102  {
103  TrackMap::iterator t = m_track.find( iq.id() );
104  if( t == m_track.end() )
105  return;
106 
107  if( iq.subtype() == IQ::Result )
108  {
109  if( context == RequestXml )
110  {
111  const Query* q = iq.findExtension<Query>( ExtPrivateXML );
112  if( q )
113  (*t).second->handlePrivateXML( q->privateXML() );
114  }
115  else if( context == StoreXml )
116  (*t).second->handlePrivateXMLResult( iq.id(), PrivateXMLHandler::PxmlStoreOk );
117  }
118  else if( iq.subtype() == IQ::Error )
119  {
120  if( context == RequestXml )
121  (*t).second->handlePrivateXMLResult( iq.id(), PrivateXMLHandler::PxmlRequestError );
122  else if( context == StoreXml )
123  (*t).second->handlePrivateXMLResult( iq.id(), PrivateXMLHandler::PxmlStoreError );
124  }
125 
126  m_track.erase( t );
127  }
128 
129 }