gloox  1.0
disco.h
00001 /*
00002   Copyright (c) 2004-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 
00015 #ifndef DISCO_H__
00016 #define DISCO_H__
00017 
00018 #include "gloox.h"
00019 
00020 #include "iqhandler.h"
00021 #include "jid.h"
00022 
00023 #include <string>
00024 #include <list>
00025 #include <map>
00026 
00027 namespace gloox
00028 {
00029 
00030   class ClientBase;
00031   class DataForm;
00032   class DiscoHandler;
00033   class DiscoNodeHandler;
00034   class IQ;
00035 
00045   class GLOOX_API Disco : public IqHandler
00046   {
00047     friend class ClientBase;
00048 
00049     public:
00050 
00051       class Identity; // declared below class Info
00052 
00056       typedef std::list<Identity*> IdentityList;
00057 
00065       class GLOOX_API Info : public StanzaExtension
00066       {
00067         friend class Disco;
00068 
00069         public:
00074           const std::string& node() const { return m_node; }
00075 
00080           const StringList& features() const { return m_features; }
00081 
00087           bool hasFeature( const std::string& feature ) const;
00088 
00093           const IdentityList& identities() const { return m_identities; }
00094 
00099           const DataForm* form() const { return m_form; }
00100 
00108           void setForm( DataForm* form );
00109 
00110           // reimplemented from StanzaExtension
00111           virtual const std::string& filterString() const;
00112 
00113           // reimplemented from StanzaExtension
00114           virtual StanzaExtension* newInstance( const Tag* tag ) const
00115           {
00116             return new Info( tag );
00117           }
00118 
00119           // reimplemented from StanzaExtension
00120           virtual StanzaExtension* clone() const
00121           {
00122             return new Info( *this );
00123           }
00124 
00125           // reimplemented from StanzaExtension
00126           virtual Tag* tag() const;
00127 
00128         private:
00129 #ifdef DISCO_INFO_TEST
00130         public:
00131 #endif
00132 
00139           Info( const std::string& node = EmptyString, bool defaultFeatures = false );
00140 
00146           Info( const Tag* tag );
00147 
00152           Info( const Info& info );
00153 
00157           virtual ~Info();
00158 
00163           void setNode( const std::string& node ) { m_node = node; }
00164 
00169           void setFeatures( const StringList& features )
00170           {
00171             StringList fl( features );
00172             fl.sort(); // needed on win32
00173             m_features.merge( fl );
00174           }
00175 
00182           void setIdentities( const IdentityList& identities ) { m_identities = identities; }
00183 
00184           std::string m_node;
00185           StringList m_features;
00186           IdentityList m_identities;
00187           DataForm* m_form;
00188       };
00189 
00196       class GLOOX_API Identity
00197       {
00198         friend class Info;
00199         friend class Disco;
00200 
00201         public:
00209           Identity( const std::string& category,
00210                     const std::string& type,
00211                     const std::string& name );
00212 
00217           Identity( const Identity& id );
00218 
00222           ~Identity();
00223 
00228           const std::string& category() const { return m_category; }
00229 
00234           const std::string& type() const { return m_type; }
00235 
00240           const std::string& name() const { return m_name; }
00241 
00246           Tag* tag() const;
00247 
00248         private:
00253           Identity( const Tag* tag );
00254 
00255           std::string m_category;   
00256           std::string m_type;       
00257           std::string m_name;       
00259       };
00260 
00261       class Item; // declared below class Items
00262 
00266       typedef std::list<Item*> ItemList;
00267 
00275       class GLOOX_API Items : public StanzaExtension
00276       {
00277         friend class Disco;
00278 
00279         public:
00280           // This needs to be public so one can proactively send a list of adhoc commands
00281           // see XEP-0050
00286           Items( const std::string& node = EmptyString );
00287 
00291           virtual ~Items();
00292 
00299           void setItems( const ItemList& items );
00300 
00305           const std::string& node() const { return m_node; }
00306 
00311           const ItemList& items() const { return m_items; }
00312 
00313           // reimplemented from StanzaExtension
00314           virtual const std::string& filterString() const;
00315 
00316           // reimplemented from StanzaExtension
00317           virtual StanzaExtension* newInstance( const Tag* tag ) const
00318           {
00319             return new Items( tag );
00320           }
00321 
00322           // reimplemented from StanzaExtension
00323           virtual Tag* tag() const;
00324 
00325           // reimplemented from StanzaExtension
00326           virtual StanzaExtension* clone() const
00327           {
00328             return new Items( *this );
00329           }
00330 
00331         private:
00332 #ifdef DISCO_ITEMS_TEST
00333         public:
00334 #endif
00335 
00340           Items( const Tag* tag );
00341 
00342           std::string m_node;
00343           ItemList m_items;
00344       };
00345 
00352       class GLOOX_API Item
00353       {
00354         friend class Items;
00355 
00356         public:
00363           Item( const JID& jid,
00364                 const std::string& node,
00365                 const std::string& name )
00366           : m_jid( jid ), m_node( node ), m_name( name ) {}
00367 
00371           ~Item() {}
00372 
00377           const JID& jid() const { return m_jid; }
00378 
00383           const std::string& node() const { return m_node; }
00384 
00389           const std::string& name() const { return m_name; }
00390 
00395           Tag* tag() const;
00396 
00397         private:
00402           Item( const Tag* tag );
00403 
00404           JID m_jid;                
00405           std::string m_node;       
00406           std::string m_name;       
00408       };
00409 
00422       void addFeature( const std::string& feature )
00423         { m_features.push_back( feature ); }
00424 
00430       void removeFeature( const std::string& feature )
00431         { m_features.remove( feature ); }
00432 
00438       const StringList features( bool defaultFeatures = false ) const;
00439 
00451       void getDiscoInfo( const JID& to, const std::string& node, DiscoHandler* dh, int context,
00452                          const std::string& tid = EmptyString )
00453         { getDisco( to, node, dh, context, GetDiscoInfo, tid ); }
00454 
00466       void getDiscoItems( const JID& to, const std::string& node, DiscoHandler* dh, int context,
00467                           const std::string& tid = EmptyString )
00468         { getDisco( to, node, dh, context, GetDiscoItems, tid ); }
00469 
00478       void setVersion( const std::string& name, const std::string& version,
00479                        const std::string& os = EmptyString );
00480 
00485       const std::string& name() const { return m_versionName; }
00486 
00491       const std::string& version() const { return m_versionVersion; }
00492 
00497       const std::string& os() const { return m_versionOs; }
00498 
00512       void setIdentity( const std::string& category, const std::string& type,
00513                         const std::string& name = EmptyString );
00514 
00521       void addIdentity( const std::string& category, const std::string& type,
00522                         const std::string& name = EmptyString )
00523         { m_identities.push_back( new Identity( category, type, name ) ); }
00524 
00529       const IdentityList& identities() const { return m_identities; }
00530 
00538       void setForm( DataForm* form );
00539 
00544       const DataForm* form() const { return m_form; }
00545 
00552       void registerDiscoHandler( DiscoHandler* dh )
00553         { m_discoHandlers.push_back( dh ); }
00554 
00559       void removeDiscoHandler( DiscoHandler* dh );
00560 
00569       void registerNodeHandler( DiscoNodeHandler* nh, const std::string& node );
00570 
00577       void removeNodeHandler( DiscoNodeHandler* nh, const std::string& node );
00578 
00583       void removeNodeHandlers( DiscoNodeHandler* nh );
00584 
00585       // reimplemented from IqHandler.
00586       virtual bool handleIq( const IQ& iq );
00587 
00588       // reimplemented from IqHandler.
00589       virtual void handleIqID( const IQ& iq, int context );
00590 
00591     private:
00592 #ifdef DISCO_TEST
00593     public:
00594 #endif
00595       Disco( ClientBase* parent );
00596       virtual ~Disco();
00597 
00598       enum IdType
00599       {
00600         GetDiscoInfo,
00601         GetDiscoItems
00602       };
00603 
00604       void getDisco( const JID& to, const std::string& node, DiscoHandler* dh,
00605                      int context, IdType idType, const std::string& tid );
00606 
00607       struct DiscoHandlerContext
00608       {
00609         DiscoHandler* dh;
00610         int context;
00611       };
00612 
00613       ClientBase* m_parent;
00614 
00615       typedef std::list<DiscoHandler*> DiscoHandlerList;
00616       typedef std::list<DiscoNodeHandler*> DiscoNodeHandlerList;
00617       typedef std::map<std::string, DiscoNodeHandlerList> DiscoNodeHandlerMap;
00618       typedef std::map<std::string, DiscoHandlerContext> DiscoHandlerMap;
00619 
00620       DiscoHandlerList m_discoHandlers;
00621       DiscoNodeHandlerMap m_nodeHandlers;
00622       DiscoHandlerMap m_track;
00623       IdentityList m_identities;
00624       StringList m_features;
00625       StringMap m_queryIDs;
00626       DataForm* m_form;
00627 
00628       std::string m_versionName;
00629       std::string m_versionVersion;
00630       std::string m_versionOs;
00631 
00632   };
00633 
00634 }
00635 
00636 #endif // DISCO_H__