gloox  0.9.9.12
vcardmanager.cpp
1 /*
2  Copyright (c) 2006-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 "vcardmanager.h"
15 #include "vcardhandler.h"
16 #include "vcard.h"
17 #include "clientbase.h"
18 #include "disco.h"
19 
20 namespace gloox
21 {
22 
24  : m_parent( parent )
25  {
26  if( m_parent )
27  {
28  m_parent->registerIqHandler( this, XMLNS_VCARD_TEMP );
29  m_parent->disco()->addFeature( XMLNS_VCARD_TEMP );
30  }
31  }
32 
34  {
35  if( m_parent )
36  {
37  m_parent->disco()->removeFeature( XMLNS_VCARD_TEMP );
38  m_parent->removeIqHandler( XMLNS_VCARD_TEMP );
39  m_parent->removeIDHandler( this );
40  }
41  }
42 
43  void VCardManager::fetchVCard( const JID& jid, VCardHandler *vch )
44  {
45  if( !m_parent || !vch )
46  return;
47 
48  TrackMap::const_iterator it = m_trackMap.find( jid.bare() );
49  if( it != m_trackMap.end() )
50  return;
51 
52  const std::string& id = m_parent->getID();
53  Tag *iq = new Tag( "iq" );
54  iq->addAttribute( "type", "get" );
55  iq->addAttribute( "id", id );
56  iq->addAttribute( "to", jid.bare() );
57  Tag *v = new Tag( iq, "vCard" );
58  v->addAttribute( "xmlns", XMLNS_VCARD_TEMP );
59 
60  m_parent->trackID( this, id, VCardHandler::FetchVCard );
61  m_trackMap[id] = vch;
62  m_parent->send( iq );
63  }
64 
66  {
67  TrackMap::iterator t;
68  TrackMap::iterator it = m_trackMap.begin();
69  while( it != m_trackMap.end() )
70  {
71  t = it;
72  ++it;
73  if( (*t).second == vch )
74  m_trackMap.erase( t );
75  }
76  }
77 
78  void VCardManager::storeVCard( const VCard *vcard, VCardHandler *vch )
79  {
80  if( !m_parent || !vch )
81  return;
82 
83  const std::string& id = m_parent->getID();
84  Tag *iq = new Tag( "iq" );
85  iq->addAttribute( "type", "set" );
86  iq->addAttribute( "id", id );
87  iq->addChild( vcard->tag() );
88 
89  m_parent->trackID( this, id, VCardHandler::StoreVCard );
90  m_trackMap[id] = vch;
91  m_parent->send( iq );
92  }
93 
94  bool VCardManager::handleIq( Stanza * /*stanza*/ )
95  {
96  return false;
97  }
98 
99  bool VCardManager::handleIqID( Stanza *stanza, int context )
100  {
101  TrackMap::iterator it = m_trackMap.find( stanza->id() );
102  if( it != m_trackMap.end() )
103  {
104  switch( stanza->subtype() )
105  {
106  case StanzaIqResult:
107  {
108  switch( context )
109  {
111  {
112  Tag *v = stanza->findChild( "vCard", "xmlns", XMLNS_VCARD_TEMP );
113  if( v )
114  (*it).second->handleVCard( stanza->from(), new VCard( v ) );
115  else
116  (*it).second->handleVCard( stanza->from(), 0 );
117  break;
118  }
120  (*it).second->handleVCardResult( VCardHandler::StoreVCard, stanza->from() );
121  break;
122  }
123  }
124  break;
125  case StanzaIqError:
126  {
127  switch( context )
128  {
130  (*it).second->handleVCardResult( VCardHandler::FetchVCard, stanza->from(), stanza->error() );
131  break;
133  (*it).second->handleVCardResult( VCardHandler::StoreVCard, stanza->from(), stanza->error() );
134  break;
135  }
136  break;
137  }
138  default:
139  return false;
140  }
141 
142  m_trackMap.erase( it );
143  }
144  return false;
145  }
146 
147 }