gloox  1.0
pubsubmanager.h
00001 /*
00002   Copyright (c) 2007-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 #ifndef PUBSUBMANAGER_H__
00014 #define PUBSUBMANAGER_H__
00015 
00016 #include "pubsub.h"
00017 #include "dataform.h"
00018 #include "iqhandler.h"
00019 #include "mutex.h"
00020 
00021 #include <map>
00022 #include <string>
00023 
00024 namespace gloox
00025 {
00026 
00027   class ClientBase;
00028 
00029   namespace PubSub
00030   {
00031 
00032     class ResultHandler;
00033 
00078     class GLOOX_API Manager : public IqHandler
00079     {
00080       public:
00081 
00086         Manager( ClientBase* parent );
00087 
00091         virtual ~Manager() {}
00092 
00108         const std::string subscribe( const JID& service, const std::string& node,
00109                                      ResultHandler* handler, const JID& jid = JID(),
00110                                      SubscriptionObject type = SubscriptionNodes,
00111                                      int depth = 1, const std::string& expire = EmptyString );
00112 
00126         const std::string unsubscribe( const JID& service,
00127                                        const std::string& node,
00128                                        const std::string& subid,
00129                                        ResultHandler* handler,
00130                                        const JID& jid = JID() );
00131 
00141         const std::string getSubscriptions( const JID& service,
00142                                             ResultHandler* handler )
00143         {
00144           return getSubscriptionsOrAffiliations( service,
00145                                                  handler,
00146                                                  GetSubscriptionList );
00147         }
00148 
00158         const std::string getAffiliations( const JID& service,
00159                                            ResultHandler* handler )
00160         {
00161           return getSubscriptionsOrAffiliations( service,
00162                                                  handler,
00163                                                  GetAffiliationList );
00164         }
00165 
00177         const std::string getSubscriptionOptions( const JID& service,
00178                                                   const JID& jid,
00179                                                   const std::string& node,
00180                                                   ResultHandler* handler)
00181           { return subscriptionOptions( GetSubscriptionOptions, service, jid, node, handler, 0 ); }
00182 
00195         const std::string setSubscriptionOptions( const JID& service,
00196                                                   const JID& jid,
00197                                                   const std::string& node,
00198                                                   DataForm* df,
00199                                                   ResultHandler* handler )
00200           { return subscriptionOptions( SetSubscriptionOptions, service, jid, node, handler, df ); }
00201 
00211         void getAffiliations( const JID& service,
00212                               const std::string& node,
00213                               ResultHandler* handler );
00214 
00224         const std::string requestItems( const JID& service,
00225                                         const std::string& node,
00226                                         const std::string& subid,
00227                                         int maxItems,
00228                                         ResultHandler* handler);
00229 
00239         const std::string requestItems( const JID& service,
00240                                         const std::string& node,
00241                                         const std::string& subid,
00242                                         const ItemList& items,
00243                                         ResultHandler* handler);
00244 
00259         const std::string publishItem( const JID& service,
00260                                        const std::string& node,
00261                                        ItemList& items,
00262                                        DataForm* options,
00263                                        ResultHandler* handler );
00264 
00277         const std::string deleteItem( const JID& service,
00278                                       const std::string& node,
00279                                       const ItemList& items,
00280                                       bool notify,
00281                                       ResultHandler* handler );
00282 
00295         const std::string createNode( const JID& service,
00296                                       const std::string& node,
00297                                       DataForm* config,
00298                                       ResultHandler* handler );
00299 
00310         const std::string deleteNode( const JID& service,
00311                                       const std::string& node,
00312                                       ResultHandler* handler );
00313 
00324         const std::string getDefaultNodeConfig( const JID& service,
00325                                                 NodeType type,
00326                                                 ResultHandler* handler );
00327 
00338         const std::string purgeNode( const JID& service,
00339                                      const std::string& node,
00340                                      ResultHandler* handler );
00341 
00352         const std::string getSubscribers( const JID& service,
00353                                           const std::string& node,
00354                                           ResultHandler* handler )
00355           { return subscriberList( GetSubscriberList, service,
00356                                    node, SubscriberList(),
00357                                    handler ); }
00358 
00371         const std::string setSubscribers( const JID& service,
00372                                           const std::string& node,
00373                                           const SubscriberList& list,
00374                                           ResultHandler* handler )
00375           { return subscriberList( SetSubscriberList, service,
00376                                    node, list, handler ); }
00377 
00388         const std::string getAffiliates( const JID& service,
00389                                          const std::string& node,
00390                                          ResultHandler* handler )
00391           { return affiliateList( GetAffiliateList, service,
00392                                   node, AffiliateList(),
00393                                   handler ); }
00394 
00406         const std::string setAffiliates( const JID& service,
00407                                          const std::string& node,
00408                                          const AffiliateList& list,
00409                                          ResultHandler* handler )
00410           { return affiliateList( SetAffiliateList, service,
00411                                   node, list, handler ); }
00412 
00423         const std::string getNodeConfig( const JID& service,
00424                                          const std::string& node,
00425                                          ResultHandler* handler )
00426           { return nodeConfig( service, node, 0, handler ); }
00427 
00439         const std::string setNodeConfig( const JID& service,
00440                                          const std::string& node,
00441                                          DataForm* config,
00442                                          ResultHandler* handler  )
00443           { return nodeConfig( service, node, config, handler ); }
00444 
00450         bool removeID( const std::string& id );
00451 
00452         // reimplemented from DiscoHandler
00453         void handleDiscoInfoResult( IQ* iq, int context );
00454 
00455         // reimplemented from DiscoHandler
00456         void handleDiscoItemsResult( IQ* iq, int context );
00457 
00458         // reimplemented from DiscoHandler
00459         void handleDiscoError( IQ* iq, int context );
00460 
00461         // reimplemented from DiscoHandler
00462         bool handleDiscoSet( IQ* ) { return 0; }
00463 
00464         // reimplemented from IqHandler.
00465         virtual bool handleIq( const IQ& iq ) { (void)iq; return false; }
00466 
00467         // reimplemented from IqHandler.
00468         virtual void handleIqID( const IQ& iq, int context );
00469 
00470       private:
00471 #ifdef PUBSUBMANAGER_TEST
00472       public:
00473 #endif
00474 
00475         enum TrackContext
00476         {
00477           Subscription,
00478           Unsubscription,
00479           GetSubscriptionOptions,
00480           SetSubscriptionOptions,
00481           GetSubscriptionList,
00482           GetSubscriberList,
00483           SetSubscriberList,
00484           GetAffiliationList,
00485           GetAffiliateList,
00486           SetAffiliateList,
00487           GetNodeConfig,
00488           SetNodeConfig,
00489           DefaultNodeConfig,
00490           GetItemList,
00491           PublishItem,
00492           DeleteItem,
00493           CreateNode,
00494           DeleteNode,
00495           PurgeNodeItems,
00496           NodeAssociation,
00497           NodeDisassociation,
00498           GetFeatureList,
00499           DiscoServiceInfos,
00500           DiscoNodeInfos,
00501           DiscoNodeItems,
00502           RequestItems,
00503           InvalidContext
00504         };
00505 
00506         class PubSubOwner : public StanzaExtension
00507         {
00508           public:
00513             PubSubOwner( TrackContext context = InvalidContext );
00514 
00519             PubSubOwner( const Tag* tag );
00520 
00524             virtual ~PubSubOwner();
00525 
00530             void setNode( const std::string& node ) { m_node = node; }
00531 
00536             const std::string& node() const { return m_node; }
00537 
00542             void setConfig( DataForm* config )
00543               { m_form = config; }
00544 
00549             const DataForm* config() const { return m_form; }
00550 
00555             void setSubscriberList( const SubscriberList& subList )
00556               { m_subList = subList; }
00557 
00562             void setAffiliateList( const AffiliateList& affList )
00563               { m_affList = affList; }
00564 
00565             // reimplemented from StanzaExtension
00566             virtual const std::string& filterString() const;
00567 
00568             // reimplemented from StanzaExtension
00569             virtual StanzaExtension* newInstance( const Tag* tag ) const
00570             {
00571               return new PubSubOwner( tag );
00572             }
00573 
00574             // reimplemented from StanzaExtension
00575             virtual Tag* tag() const;
00576 
00577             // reimplemented from StanzaExtension
00578             virtual StanzaExtension* clone() const
00579             {
00580               PubSubOwner* p = new PubSubOwner();
00581               p->m_node = m_node;
00582               p->m_ctx = m_ctx;
00583               p->m_form = m_form ? new DataForm( *m_form ) : 0;
00584               p->m_subList = m_subList;
00585               p->m_affList = m_affList;
00586               return p;
00587             }
00588 
00589           private:
00590             std::string m_node;
00591             TrackContext m_ctx;
00592             DataForm* m_form;
00593             SubscriberList m_subList;
00594             AffiliateList m_affList;
00595         };
00596 
00597         class PubSub : public StanzaExtension
00598         {
00599           public:
00604             PubSub( TrackContext context = InvalidContext );
00605 
00610             PubSub( const Tag* tag );
00611 
00615             virtual ~PubSub();
00616 
00621             void setJID( const JID& jid ) { m_jid = jid; }
00622 
00627             const JID& jid() const { return m_jid; }
00628 
00633             void setNode( const std::string& node ) { m_node = node; }
00634 
00639             const std::string& node() const { return m_node; }
00640 
00645             void setSubscriptionID( const std::string& subid )
00646               { m_subid = subid; }
00647 
00652             const std::string& subscriptionID() const { return m_subid; }
00653 
00660             void setOptions( const std::string& node, DataForm* df )
00661             {
00662               m_options.node = node;
00663               m_options.df = df;
00664             }
00665 
00670             const DataForm* options() const
00671               { return m_options.df; }
00672 
00677             const ItemList& items() const { return m_items; }
00678 
00683             void setItems( const ItemList& items )
00684              { m_items = items; }
00685 
00690             void setMaxItems( int maxItems )
00691               { m_maxItems = maxItems; }
00692 
00697             const SubscriptionMap& subscriptions() const
00698               { return m_subscriptionMap; }
00699 
00704             const AffiliationMap& affiliations() const
00705               { return m_affiliationMap; }
00706 
00711             void setNotify( bool notify ) { m_notify = notify; }
00712 
00713             // reimplemented from StanzaExtension
00714             virtual const std::string& filterString() const;
00715 
00716             // reimplemented from StanzaExtension
00717             virtual StanzaExtension* newInstance( const Tag* tag ) const
00718             {
00719               return new PubSub( tag );
00720             }
00721 
00722             // reimplemented from StanzaExtension
00723             virtual Tag* tag() const;
00724 
00725             // reimplemented from StanzaExtension
00726             virtual StanzaExtension* clone() const;
00727 
00728           private:
00729             AffiliationMap m_affiliationMap;
00730             SubscriptionMap m_subscriptionMap;
00731             TrackContext m_ctx;
00732 
00733             struct Options
00734             {
00735               std::string node;
00736               DataForm* df;
00737             };
00738             Options m_options;
00739             JID m_jid;
00740             std::string m_node;
00741             std::string m_subid;
00742             ItemList m_items;
00743             int m_maxItems;
00744             bool m_notify;
00745         };
00746 
00758         const std::string nodeConfig( const JID& service,
00759                                       const std::string& node,
00760                                       DataForm* config,
00761                                       ResultHandler* handler );
00762 
00776         const std::string subscriberList( TrackContext ctx,
00777                                           const JID& service,
00778                                           const std::string& node,
00779                                           const SubscriberList& config,
00780                                           ResultHandler* handler );
00781 
00795         const std::string affiliateList( TrackContext ctx,
00796                                          const JID& service,
00797                                          const std::string& node,
00798                                          const AffiliateList& config,
00799                                          ResultHandler* handler );
00800 
00801         const std::string subscriptionOptions( TrackContext context,
00802                                                const JID& service,
00803                                                const JID& jid,
00804                                                const std::string& node,
00805                                                ResultHandler* handler,
00806                                                DataForm* df );
00807 
00808         const std::string getSubscriptionsOrAffiliations( const JID& service,
00809             ResultHandler* handler,
00810             TrackContext context );
00811 
00812         typedef std::map < std::string, std::string > NodeOperationTrackMap;
00813         typedef std::map < std::string, ResultHandler* > ResultHandlerTrackMap;
00814 
00815         ClientBase* m_parent;
00816 
00817         NodeOperationTrackMap  m_nopTrackMap;
00818         ResultHandlerTrackMap  m_resultHandlerTrackMap;
00819 
00820         util::Mutex m_trackMapMutex;
00821 
00822     };
00823 
00824   }
00825 
00826 }
00827 
00828 #endif // PUBSUBMANAGER_H__