14#include "clientbase.h"
15#include "rostermanager.h"
17#include "rosteritem.h"
18#include "rosteritemdata.h"
19#include "rosterlistener.h"
20#include "privatexml.h"
22#include "stanzaextension.h"
23#include "capabilities.h"
30 RosterManager::Query::Query(
const JID& jid,
const std::string& name,
const StringList& groups )
33 m_roster.push_back(
new RosterItemData( jid, name, groups ) );
36 RosterManager::Query::Query(
const JID& jid )
39 m_roster.push_back(
new RosterItemData( jid ) );
42 RosterManager::Query::Query(
const Tag* tag )
45 if( !tag || tag->name() !=
"query" || tag->xmlns() != XMLNS_ROSTER )
48 const ConstTagList& l = tag->findTagList(
"query/item" );
49 ConstTagList::const_iterator it = l.begin();
50 for( ; it != l.end(); ++it )
53 const ConstTagList& g = (*it)->findTagList(
"item/group" );
54 ConstTagList::const_iterator it_g = g.begin();
55 for( ; it_g != g.end(); ++it_g )
56 groups.push_back( (*it_g)->cdata() );
58 const std::string sub = (*it)->findAttribute(
"subscription" );
60 m_roster.push_back( new RosterItemData( JID( (*it)->findAttribute(
"jid" ) ) ) );
63 RosterItemData* rid = new RosterItemData( JID( (*it)->findAttribute(
"jid" ) ),
64 (*it)->findAttribute(
"name" ),
66 rid->setSubscription( sub, (*it)->findAttribute(
"ask" ) );
67 m_roster.push_back( rid );
72 RosterManager::Query::~Query()
74 util::clearList( m_roster );
77 const std::string& RosterManager::Query::filterString()
const
79 static const std::string filter =
"/iq/query[@xmlns='" +
XMLNS_ROSTER +
"']";
83 Tag* RosterManager::Query::tag()
const
85 Tag* t =
new Tag(
"query" );
86 t->setXmlns( XMLNS_ROSTER );
88 RosterData::const_iterator it = m_roster.begin();
89 for( ; it != m_roster.end(); ++it )
90 t->addChild( (*it)->tag() );
95 StanzaExtension* RosterManager::Query::clone()
const
97 Query* q =
new Query();
98 RosterData::const_iterator it = m_roster.begin();
99 for( ; it != m_roster.end(); ++it )
101 q->m_roster.push_back(
new RosterItemData( *(*it) ) );
109 : m_rosterListener( 0 ), m_parent( parent ), m_privateXML( 0 ),
110 m_syncSubscribeReq( false )
114 m_parent->registerIqHandler(
this,
ExtRoster );
115 m_parent->registerPresenceHandler(
this );
116 m_parent->registerSubscriptionHandler(
this );
117 m_parent->registerStanzaExtension(
new Query() );
119 m_self =
new RosterItem( m_parent->jid().bare() );
128 m_parent->removeIqHandler(
this,
ExtRoster );
129 m_parent->removeIDHandler(
this );
130 m_parent->removePresenceHandler(
this );
131 m_parent->removeSubscriptionHandler(
this );
132 m_parent->removeStanzaExtension(
ExtRoster );
154 m_parent->send( iq,
this, RequestRoster );
164 if( q && q->roster().size() )
165 mergePush( q->roster() );
168 m_parent->send( re );
178 mergeRoster( q->roster() );
180 if( context == RequestRoster )
183 m_parent->rosterFilled();
185 if( m_rosterListener )
186 m_rosterListener->handleRoster( m_roster );
191 if( context == RequestRoster && m_parent )
192 m_parent->rosterFilled();
194 if( m_rosterListener )
195 m_rosterListener->handleRosterError( iq );
205 Roster::iterator it = m_roster.find( presence.
from().
bare() );
206 if( it != m_roster.end() || ( self = ( presence.
from().
bareJID() == m_self->jidJID() ) ) )
208 RosterItem* ri = self ? m_self : (*it).second;
209 const std::string& resource = presence.
from().
resource();
221 if( m_rosterListener && !self )
222 m_rosterListener->handleRosterPresence( *ri, resource,
224 else if( m_rosterListener && self )
225 m_rosterListener->handleSelfPresence( *ri, resource,
230 if( m_rosterListener )
231 m_rosterListener->handleNonrosterPresence( presence );
236 const StringList& groups,
const std::string& msg )
241 add( jid, name, groups );
256 m_parent->send( iq,
this, AddRosterItem );
279 m_parent->send( iq,
this, RemoveRosterItem );
284 Roster::const_iterator it = m_roster.begin();
285 for( ; it != m_roster.end(); ++it )
287 if( !(*it).second->changed() )
291 iq.
addExtension(
new Query( (*it).second->jidJID(), (*it).second->name(), (*it).second->groups() ) );
292 m_parent->send( iq,
this, SynchronizeRoster );
305 if( !m_rosterListener )
312 bool answer = m_rosterListener->handleSubscriptionRequest( s10n.
from(), s10n.
status() );
313 if( m_syncSubscribeReq )
321 m_rosterListener->handleItemSubscribed( s10n.
from() );
330 bool answer = m_rosterListener->handleUnsubscriptionRequest( s10n.
from(), s10n.
status() );
331 if( m_syncSubscribeReq && answer )
338 m_rosterListener->handleItemUnsubscribed( s10n.
from() );
349 m_syncSubscribeReq = syncSubscribeReq;
350 m_rosterListener = rl;
355 m_syncSubscribeReq =
false;
356 m_rosterListener = 0;
362 Tag* t =
new Tag(
"roster", m_delimiter );
364 m_privateXML->storeXML( t,
this );
370 m_delimiter = xml->
cdata();
379 Roster::const_iterator it = m_roster.find( jid.
bare() );
380 return it != m_roster.end() ? (*it).second : 0;
383 void RosterManager::mergePush(
const RosterData& data )
385 RosterData::const_iterator it = data.begin();
386 for( ; it != data.end(); ++it )
388 Roster::iterator itr = m_roster.find( (*it)->jidJID().full() );
389 if( itr != m_roster.end() )
391 if( (*it)->remove() )
393 if( m_rosterListener )
395 delete (*itr).second;
396 m_roster.erase( itr );
400 (*itr).second->setData( *(*it) );
401 if( m_rosterListener )
405 else if( !(*it)->remove() )
407 m_roster.insert( std::make_pair( (*it)->jidJID().full(),
new RosterItem( *(*it) ) ) );
408 if( m_rosterListener )
409 m_rosterListener->handleItemAdded( (*it)->jidJID().full() );
414 void RosterManager::mergeRoster(
const RosterData& data )
416 RosterData::const_iterator it = data.begin();
417 for( ; it != data.end(); ++it )
418 m_roster.insert( std::make_pair( (*it)->jidJID().full(),
new RosterItem( *(*it) ) ) );
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
An abstraction of an IQ stanza.
const std::string & resource() const
const std::string & bare() const
An abstraction of a presence stanza.
PresenceType presence() const
PresenceType subtype() const
const std::string status(const std::string &lang="default") const
This class implements XEP-0049 (Private XML Storage).
An abstraction of a roster item.
void setPriority(const std::string &resource, int priority)
void removeResource(const std::string &resource)
void setStatus(const std::string &resource, const std::string &msg)
void setExtensions(const std::string &resource, const StanzaExtensionList &exts)
void setPresence(const std::string &resource, Presence::PresenceType presence)
A virtual interface which can be reimplemented to receive roster updates.
virtual void handleItemRemoved(const JID &jid)=0
virtual void handleItemUpdated(const JID &jid)=0
RosterManager(ClientBase *parent)
void add(const JID &jid, const std::string &name, const StringList &groups)
void subscribe(const JID &jid, const std::string &name=EmptyString, const StringList &groups=StringList(), const std::string &msg=EmptyString)
void unsubscribe(const JID &jid, const std::string &msg=EmptyString)
virtual void handleSubscription(const Subscription &subscription)
virtual void handleIqID(const IQ &iq, int context)
const std::string & delimiter() const
virtual bool handleIq(const IQ &iq)
void setDelimiter(const std::string &delimiter)
virtual void handlePrivateXML(const Tag *xml)
virtual void handlePresence(const Presence &presence)
void cancel(const JID &jid, const std::string &msg=EmptyString)
RosterItem * getRosterItem(const JID &jid)
virtual void handlePrivateXMLResult(const std::string &uid, PrivateXMLResult pxResult)
void ackSubscriptionRequest(const JID &to, bool ack)
void remove(const JID &jid)
void registerRosterListener(RosterListener *rl, bool syncSubscribeReq=true)
void removeRosterListener()
This class abstracts a stanza extension, which is usually an XML child element in a specific namespac...
void addExtension(const StanzaExtension *se)
const std::string & id() const
const StanzaExtensionList & extensions() const
const StanzaExtension * findExtension(int type) const
An abstraction of a subscription stanza.
const std::string status(const std::string &lang="default") const
This is an abstraction of an XML element.
bool addAttribute(Attribute *attr)
const std::string cdata() const
void clearMap(std::map< Key, T * > &M)
The namespace for the gloox library.
std::list< std::string > StringList
const std::string XMLNS_ROSTER
std::map< const std::string, RosterItem * > Roster
const std::string XMLNS_ROSTER_DELIMITER