gloox  0.9.9.12
privatexml.cpp
1 /*
2  Copyright (c) 2004-2008 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 
22  : m_parent( parent )
23  {
24  if( m_parent )
25  m_parent->registerIqHandler( this, XMLNS_PRIVATE_XML );
26  }
27 
29  {
30  if( m_parent )
31  {
33  m_parent->removeIDHandler( this );
34  }
35  }
36 
37  std::string PrivateXML::requestXML( const std::string& tag, const std::string& xmlns,
38  PrivateXMLHandler *pxh )
39  {
40  const std::string& id = m_parent->getID();
41 
42  Tag *iq = new Tag( "iq" );
43  iq->addAttribute( "id", id );
44  iq->addAttribute( "type", "get" );
45  Tag *query = new Tag( iq, "query" );
46  query->addAttribute( "xmlns", XMLNS_PRIVATE_XML );
47  Tag *x = new Tag( query, tag );
48  x->addAttribute( "xmlns", xmlns );
49 
50  m_track[id] = pxh;
51  m_parent->trackID( this, id, RequestXml );
52  m_parent->send( iq );
53 
54  return id;
55  }
56 
57  std::string PrivateXML::storeXML( Tag *tag, PrivateXMLHandler *pxh )
58  {
59  const std::string& id = m_parent->getID();
60 
61  Tag *iq = new Tag( "iq" );
62  iq->addAttribute( "id", id );
63  iq->addAttribute( "type", "set" );
64  Tag *query = new Tag( iq, "query" );
65  query->addAttribute( "xmlns", XMLNS_PRIVATE_XML );
66  query->addChild( tag );
67 
68  m_track[id] = pxh;
69  m_parent->trackID( this, id, StoreXml );
70  m_parent->send( iq );
71 
72  return id;
73  }
74 
75  bool PrivateXML::handleIqID( Stanza *stanza, int context )
76  {
77  TrackMap::iterator t = m_track.find( stanza->id() );
78  if( t != m_track.end() )
79  {
80  switch( stanza->subtype() )
81  {
82  case StanzaIqResult:
83  {
84  switch( context )
85  {
86  case RequestXml:
87  {
88  Tag *q = stanza->findChild( "query" );
89  if( q )
90  {
91  const Tag::TagList& l = q->children();
92  Tag::TagList::const_iterator it = l.begin();
93  if( it != l.end() )
94  {
95  (*t).second->handlePrivateXML( (*it)->name(), (*it) );
96  }
97  }
98  break;
99  }
100 
101  case StoreXml:
102  {
103  (*t).second->handlePrivateXMLResult( stanza->id(), PrivateXMLHandler::PxmlStoreOk );
104  break;
105  }
106  }
107  m_track.erase( t );
108  return true;
109  break;
110  }
111  case StanzaIqError:
112  {
113  switch( context )
114  {
115  case RequestXml:
116  {
117  (*t).second->handlePrivateXMLResult( stanza->id(), PrivateXMLHandler::PxmlRequestError );
118  break;
119  }
120 
121  case StoreXml:
122  {
123  (*t).second->handlePrivateXMLResult( stanza->id(), PrivateXMLHandler::PxmlStoreError );
124  break;
125  }
126  }
127  break;
128  }
129  default:
130  break;
131  }
132 
133  m_track.erase( t );
134  }
135 
136  return false;
137  }
138 
139  bool PrivateXML::handleIq( Stanza * /*stanza*/ )
140  {
141  return false;
142  }
143 
144 }