项目需要,可能实现的有点麻烦,大家如果有好方法,请告诉我!
在gloox库的基础上,要实现的功能:
当好友头像信息修改后(vcard),本地立即去获取好友头像。
1、增加一个头文件:VCardUpdateHandler.h 用来接收好友头像变更消息
class VCardUpdateHandler
{
public:
virtual ~VCardUpdateHandler() {;}
//有人更新VCard信息
virtual void VCardUpdateHash(const JID& jid , const std::string& hash) = 0;
};
转载请注明来源:blog.csdn.net/doubleuto
2、增加一个类VCardUpdatePresence,用来过滤包 ;
class GLOOX_API VCardUpdatePresence : public StanzaExtension
{
public:
VCardUpdatePresence(VCardUpdateHandler* phandler);
VCardUpdatePresence(VCardUpdateHandler* phandler , const Tag* tag );
/**
* Virtual destructor.
*/
virtual ~VCardUpdatePresence();
private:
// reimplemented from StanzaExtension
virtual const std::string& filterString() const;
// reimplemented from StanzaExtension
virtual StanzaExtension* newInstance( const Tag* tag ) const
{
return new VCardUpdatePresence( m_pHandler, tag );
}
// reimplemented from StanzaExtension
virtual Tag* tag() const;
// reimplemented from StanzaExtension
virtual StanzaExtension* clone() const
{
return new VCardUpdatePresence( *this );
}
private:
JID m_fromjid;
std::string m_hash;
bool m_valid;
VCardUpdateHandler* m_pHandler;
};
3、在主要位置注册Handler:
pVCardUpdatePresence_ = new VCardUpdatePresence(new VCardupdateHandler);
pClient_->registerStanzaExtension(pVCardUpdatePresence_);
现在就可以再需要的地方,接收好友更新头像的信息了!
附注:VCardUpdatePresence.CPP源码
VCardUpdatePresence::VCardUpdatePresence(VCardUpdateHandler* phandler)
: StanzaExtension( ExtVCardUpdate ),m_valid(false),m_porgTag(NULL),m_pHandler(phandler)
{
}
VCardUpdatePresence::VCardUpdatePresence( VCardUpdateHandler* phandler , const Tag* tag )
: StanzaExtension( ExtVCardUpdate ),m_valid(false),m_porgTag(NULL),m_pHandler(phandler)
{
if( !tag || tag->name() != "presence" )
return;
//from
const std::string& fromjidstring = tag->findAttribute("from");
if( fromjidstring.empty() )
return;
m_fromjid = fromjidstring;
//<x xmlns='vcard-temp:x:update'>
Tag* x = tag->findChild("x");
if( x && x->name() == "x" && x->hasAttribute( XMLNS, XMLNS_X_VCARD_UPDATE ) )
{
if( x->hasChild( "photo" ) )
{
m_hash = x->findChild( "photo" )->cdata();
if( !m_hash.empty() )
m_valid = true;
}
if( m_valid && m_pHandler)
{
m_pHandler->VCardUpdateHash(m_fromjid,m_hash);
}
}
}
VCardUpdatePresence::~VCardUpdatePresence()
{
}
const std::string& VCardUpdatePresence::filterString() const
{
static const std::string filter = "/presence";
return filter;
}