vcardmanager.cpp

00001 /*
00002   Copyright (c) 2006-2008 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 
00020 namespace gloox
00021 {
00022 
00023   VCardManager::VCardManager( ClientBase *parent )
00024     : m_parent( parent )
00025   {
00026     if( m_parent )
00027     {
00028       m_parent->registerIqHandler( this, XMLNS_VCARD_TEMP );
00029       m_parent->disco()->addFeature( XMLNS_VCARD_TEMP );
00030     }
00031   }
00032 
00033   VCardManager::~VCardManager()
00034   {
00035     if( m_parent )
00036     {
00037       m_parent->disco()->removeFeature( XMLNS_VCARD_TEMP );
00038       m_parent->removeIqHandler( XMLNS_VCARD_TEMP );
00039       m_parent->removeIDHandler( this );
00040     }
00041   }
00042 
00043   void VCardManager::fetchVCard( const JID& jid, VCardHandler *vch )
00044   {
00045     if( !m_parent || !vch )
00046       return;
00047 
00048     TrackMap::const_iterator it = m_trackMap.find( jid.bare() );
00049     if( it != m_trackMap.end() )
00050       return;
00051 
00052     const std::string& id = m_parent->getID();
00053     Tag *iq = new Tag( "iq" );
00054     iq->addAttribute( "type", "get" );
00055     iq->addAttribute( "id", id );
00056     iq->addAttribute( "to", jid.bare() );
00057     Tag *v = new Tag( iq, "vCard" );
00058     v->addAttribute( "xmlns", XMLNS_VCARD_TEMP );
00059 
00060     m_parent->trackID( this, id, VCardHandler::FetchVCard );
00061     m_trackMap[id] = vch;
00062     m_parent->send( iq );
00063   }
00064 
00065   void VCardManager::cancelVCardOperations( VCardHandler *vch )
00066   {
00067     TrackMap::iterator t;
00068     TrackMap::iterator it = m_trackMap.begin();
00069     while( it != m_trackMap.end() )
00070     {
00071       t = it;
00072       ++it;
00073       if( (*t).second == vch )
00074         m_trackMap.erase( t );
00075     }
00076   }
00077 
00078   void VCardManager::storeVCard( const VCard *vcard, VCardHandler *vch )
00079   {
00080     if( !m_parent || !vch )
00081       return;
00082 
00083     const std::string& id = m_parent->getID();
00084     Tag *iq = new Tag( "iq" );
00085     iq->addAttribute( "type", "set" );
00086     iq->addAttribute( "id", id );
00087     iq->addChild( vcard->tag() );
00088 
00089     m_parent->trackID( this, id, VCardHandler::StoreVCard );
00090     m_trackMap[id] = vch;
00091     m_parent->send( iq );
00092   }
00093 
00094   bool VCardManager::handleIq( Stanza * /*stanza*/ )
00095   {
00096     return false;
00097   }
00098 
00099   bool VCardManager::handleIqID( Stanza *stanza, int context )
00100   {
00101     TrackMap::iterator it = m_trackMap.find( stanza->id() );
00102     if( it != m_trackMap.end() )
00103     {
00104       switch( stanza->subtype() )
00105       {
00106         case StanzaIqResult:
00107         {
00108           switch( context )
00109           {
00110             case VCardHandler::FetchVCard:
00111             {
00112               Tag *v = stanza->findChild( "vCard", "xmlns", XMLNS_VCARD_TEMP );
00113               if( v )
00114                 (*it).second->handleVCard( stanza->from(), new VCard( v ) );
00115               else
00116                 (*it).second->handleVCard( stanza->from(), 0 );
00117               break;
00118             }
00119             case VCardHandler::StoreVCard:
00120               (*it).second->handleVCardResult( VCardHandler::StoreVCard, stanza->from() );
00121               break;
00122           }
00123         }
00124         break;
00125         case StanzaIqError:
00126         {
00127           switch( context )
00128           {
00129             case VCardHandler::FetchVCard:
00130               (*it).second->handleVCardResult( VCardHandler::FetchVCard, stanza->from(), stanza->error() );
00131               break;
00132             case VCardHandler::StoreVCard:
00133               (*it).second->handleVCardResult( VCardHandler::StoreVCard, stanza->from(), stanza->error() );
00134               break;
00135           }
00136           break;
00137         }
00138         default:
00139           return false;
00140       }
00141 
00142       m_trackMap.erase( it );
00143     }
00144     return false;
00145   }
00146 
00147 }

Generated on Fri Oct 10 15:26:12 2008 for gloox by  doxygen 1.5.6