gloox  1.0
vcardmanager.cpp
00001 /*
00002   Copyright (c) 2006-2009 by Jakob Schroeter <js@camaya.net>
00003   This file is part of the gloox library. http://camaya.net/gloox
00004 
00005   This software is distributed under a license. The full license
00006   agreement can be found in the file LICENSE in this distribution.
00007   This software may not be copied, modified, sold or distributed
00008   other than expressed in the named license agreement.
00009 
00010   This software is distributed without any warranty.
00011 */
00012 
00013 
00014 #include "vcardmanager.h"
00015 #include "vcardhandler.h"
00016 #include "vcard.h"
00017 #include "clientbase.h"
00018 #include "disco.h"
00019 #include "error.h"
00020 
00021 namespace gloox
00022 {
00023 
00024   VCardManager::VCardManager( ClientBase* parent )
00025     : m_parent( parent )
00026   {
00027     if( m_parent )
00028     {
00029       m_parent->registerIqHandler( this, ExtVCard );
00030       m_parent->disco()->addFeature( XMLNS_VCARD_TEMP );
00031       m_parent->registerStanzaExtension( new VCard() );
00032     }
00033   }
00034 
00035   VCardManager::~VCardManager()
00036   {
00037     if( m_parent )
00038     {
00039       m_parent->disco()->removeFeature( XMLNS_VCARD_TEMP );
00040       m_parent->removeIqHandler( this, ExtVCard );
00041       m_parent->removeIDHandler( this );
00042     }
00043   }
00044 
00045   void VCardManager::fetchVCard( const JID& jid, VCardHandler* vch )
00046   {
00047     if( !m_parent || !vch )
00048       return;
00049 
00050     TrackMap::const_iterator it = m_trackMap.find( jid.bare() );
00051     if( it != m_trackMap.end() )
00052       return;
00053 
00054     const std::string& id = m_parent->getID();
00055     IQ iq ( IQ::Get, jid, id );
00056     iq.addExtension( new VCard() );
00057 
00058     m_trackMap[id] = vch;
00059     m_parent->send( iq, this,VCardHandler::FetchVCard  );
00060   }
00061 
00062   void VCardManager::cancelVCardOperations( VCardHandler* vch )
00063   {
00064     TrackMap::iterator t;
00065     TrackMap::iterator it = m_trackMap.begin();
00066     while( it != m_trackMap.end() )
00067     {
00068       t = it;
00069       ++it;
00070       if( (*t).second == vch )
00071         m_trackMap.erase( t );
00072     }
00073   }
00074 
00075   void VCardManager::storeVCard( VCard* vcard, VCardHandler* vch )
00076   {
00077     if( !m_parent || !vch )
00078       return;
00079 
00080     const std::string& id = m_parent->getID();
00081     IQ iq( IQ::Set, JID(), id );
00082     iq.addExtension( vcard );
00083 
00084     m_trackMap[id] = vch;
00085     m_parent->send( iq, this, VCardHandler::StoreVCard );
00086   }
00087 
00088   void VCardManager::handleIqID( const IQ& iq, int context )
00089   {
00090     TrackMap::iterator it = m_trackMap.find( iq.id() );
00091     if( it != m_trackMap.end() )
00092     {
00093       switch( iq.subtype() )
00094       {
00095         case IQ::Result:
00096         {
00097           switch( context )
00098           {
00099             case VCardHandler::FetchVCard:
00100             {
00101               const VCard* v = iq.findExtension<VCard>( ExtVCard );
00102               (*it).second->handleVCard( iq.from(), v );
00103               break;
00104             }
00105             case VCardHandler::StoreVCard:
00106               (*it).second->handleVCardResult( VCardHandler::StoreVCard, iq.from() );
00107               break;
00108           }
00109         }
00110         break;
00111         case IQ::Error:
00112         {
00113           (*it).second->handleVCardResult( (VCardHandler::VCardContext)context,
00114                                            iq.from(),
00115                                            iq.error() ? iq.error()->error()
00116                                                        : StanzaErrorUndefined );
00117           break;
00118         }
00119         default:
00120           break;
00121       }
00122 
00123       m_trackMap.erase( it );
00124     }
00125   }
00126 
00127 }