gloox  1.0.1
vcardmanager.cpp
1 /*
2  Copyright (c) 2006-2012 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 #include "error.h"
20 
21 namespace gloox
22 {
23 
25  : m_parent( parent )
26  {
27  if( m_parent )
28  {
29  m_parent->registerIqHandler( this, ExtVCard );
30  m_parent->disco()->addFeature( XMLNS_VCARD_TEMP );
31  m_parent->registerStanzaExtension( new VCard() );
32  }
33  }
34 
36  {
37  if( m_parent )
38  {
39  m_parent->disco()->removeFeature( XMLNS_VCARD_TEMP );
40  m_parent->removeIqHandler( this, ExtVCard );
41  m_parent->removeIDHandler( this );
42  }
43  }
44 
45  void VCardManager::fetchVCard( const JID& jid, VCardHandler* vch )
46  {
47  if( !m_parent || !vch )
48  return;
49 
50  TrackMap::const_iterator it = m_trackMap.find( jid.bare() );
51  if( it != m_trackMap.end() )
52  return;
53 
54  const std::string& id = m_parent->getID();
55  IQ iq ( IQ::Get, jid, id );
56  iq.addExtension( new VCard() );
57 
58  m_trackMap[id] = vch;
59  m_parent->send( iq, this,VCardHandler::FetchVCard );
60  }
61 
63  {
64  TrackMap::iterator t;
65  TrackMap::iterator it = m_trackMap.begin();
66  while( it != m_trackMap.end() )
67  {
68  t = it;
69  ++it;
70  if( (*t).second == vch )
71  m_trackMap.erase( t );
72  }
73  }
74 
76  {
77  if( !m_parent || !vch )
78  return;
79 
80  const std::string& id = m_parent->getID();
81  IQ iq( IQ::Set, JID(), id );
82  iq.addExtension( vcard );
83 
84  m_trackMap[id] = vch;
85  m_parent->send( iq, this, VCardHandler::StoreVCard );
86  }
87 
88  void VCardManager::handleIqID( const IQ& iq, int context )
89  {
90  TrackMap::iterator it = m_trackMap.find( iq.id() );
91  if( it != m_trackMap.end() )
92  {
93  switch( iq.subtype() )
94  {
95  case IQ::Result:
96  {
97  switch( context )
98  {
100  {
101  const VCard* v = iq.findExtension<VCard>( ExtVCard );
102  (*it).second->handleVCard( iq.from(), v );
103  break;
104  }
106  (*it).second->handleVCardResult( VCardHandler::StoreVCard, iq.from() );
107  break;
108  }
109  }
110  break;
111  case IQ::Error:
112  {
113  (*it).second->handleVCardResult( (VCardHandler::VCardContext)context,
114  iq.from(),
115  iq.error() ? iq.error()->error()
117  break;
118  }
119  default:
120  break;
121  }
122 
123  m_trackMap.erase( it );
124  }
125  }
126 
127 }