gloox  1.0.28
vcardmanager.cpp
1 /*
2  Copyright (c) 2006-2023 by Jakob Schröter <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( static_cast<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 }
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition: clientbase.h:79
const std::string getID()
virtual Disco * disco() const
Definition: clientbase.h:233
void removeIqHandler(IqHandler *ih, int exttype)
void removeIDHandler(IqHandler *ih)
void send(Tag *tag)
void registerIqHandler(IqHandler *ih, int exttype)
void registerStanzaExtension(StanzaExtension *ext)
void removeFeature(const std::string &feature)
Definition: disco.h:430
void addFeature(const std::string &feature)
Definition: disco.h:422
StanzaError error() const
Definition: error.h:75
An abstraction of an IQ stanza.
Definition: iq.h:34
IqType subtype() const
Definition: iq.h:74
@ Set
Definition: iq.h:46
@ Error
Definition: iq.h:49
@ Result
Definition: iq.h:48
@ Get
Definition: iq.h:45
An abstraction of a JID.
Definition: jid.h:31
const std::string & bare() const
Definition: jid.h:67
void addExtension(const StanzaExtension *se)
Definition: stanza.cpp:52
const Error * error() const
Definition: stanza.cpp:47
const std::string & id() const
Definition: stanza.h:63
const JID & from() const
Definition: stanza.h:51
const StanzaExtension * findExtension(int type) const
Definition: stanza.cpp:57
A virtual interface that helps requesting Jabber VCards.
Definition: vcardhandler.h:37
virtual void handleIqID(const IQ &iq, int context)
void fetchVCard(const JID &jid, VCardHandler *vch)
void storeVCard(VCard *vcard, VCardHandler *vch)
void cancelVCardOperations(VCardHandler *vch)
VCardManager(ClientBase *parent)
A VCard abstraction.
Definition: vcard.h:35
The namespace for the gloox library.
Definition: adhoc.cpp:28
const std::string XMLNS_VCARD_TEMP
Definition: gloox.cpp:56
@ StanzaErrorUndefined
Definition: gloox.h:952