gloox  1.0.9
pubsubmanager.h
1 /*
2  Copyright (c) 2007-2013 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 #ifndef PUBSUBMANAGER_H__
14 #define PUBSUBMANAGER_H__
15 
16 #include "pubsub.h"
17 #include "dataform.h"
18 #include "iqhandler.h"
19 #include "mutex.h"
20 
21 #include <map>
22 #include <string>
23 
24 namespace gloox
25 {
26 
27  class ClientBase;
28 
29  namespace PubSub
30  {
31 
32  class ResultHandler;
33 
78  class GLOOX_API Manager : public IqHandler
79  {
80  public:
81 
86  Manager( ClientBase* parent );
87 
91  virtual ~Manager() {}
92 
108  const std::string subscribe( const JID& service, const std::string& node,
109  ResultHandler* handler, const JID& jid = JID(),
111  int depth = 1, const std::string& expire = EmptyString );
112 
130  const std::string subscribe( const JID& service, const std::string& node,
131  ResultHandler* handler, const JID& jid,
132  DataForm* options );
133 
147  const std::string unsubscribe( const JID& service,
148  const std::string& node,
149  const std::string& subid,
150  ResultHandler* handler,
151  const JID& jid = JID() );
152 
162  const std::string getSubscriptions( const JID& service,
163  ResultHandler* handler )
164  {
165  return getSubscriptionsOrAffiliations( service,
166  handler,
167  GetSubscriptionList );
168  }
169 
179  const std::string getAffiliations( const JID& service,
180  ResultHandler* handler )
181  {
182  return getSubscriptionsOrAffiliations( service,
183  handler,
184  GetAffiliationList );
185  }
186 
199  const std::string getSubscriptionOptions( const JID& service,
200  const JID& jid,
201  const std::string& node,
202  ResultHandler* handler,
203  const std::string& subid = EmptyString)
204  { return subscriptionOptions( GetSubscriptionOptions, service, jid, node, handler, 0, subid ); }
205 
219  const std::string setSubscriptionOptions( const JID& service,
220  const JID& jid,
221  const std::string& node,
222  DataForm* df,
223  ResultHandler* handler,
224  const std::string& subid = EmptyString )
225  { return subscriptionOptions( SetSubscriptionOptions, service, jid, node, handler, df, subid ); }
226 
236  void getAffiliations( const JID& service,
237  const std::string& node,
238  ResultHandler* handler );
239 
249  const std::string requestItems( const JID& service,
250  const std::string& node,
251  const std::string& subid,
252  int maxItems,
253  ResultHandler* handler);
254 
264  const std::string requestItems( const JID& service,
265  const std::string& node,
266  const std::string& subid,
267  const ItemList& items,
268  ResultHandler* handler);
269 
284  const std::string publishItem( const JID& service,
285  const std::string& node,
286  ItemList& items,
287  DataForm* options,
288  ResultHandler* handler );
289 
302  const std::string deleteItem( const JID& service,
303  const std::string& node,
304  const ItemList& items,
305  bool notify,
306  ResultHandler* handler );
307 
320  const std::string createNode( const JID& service,
321  const std::string& node,
322  DataForm* config,
323  ResultHandler* handler );
324 
335  const std::string deleteNode( const JID& service,
336  const std::string& node,
337  ResultHandler* handler );
338 
349  const std::string getDefaultNodeConfig( const JID& service,
350  NodeType type,
351  ResultHandler* handler );
352 
363  const std::string purgeNode( const JID& service,
364  const std::string& node,
365  ResultHandler* handler );
366 
377  const std::string getSubscribers( const JID& service,
378  const std::string& node,
379  ResultHandler* handler )
380  { return subscriberList( GetSubscriberList, service,
381  node, SubscriberList(),
382  handler ); }
383 
396  const std::string setSubscribers( const JID& service,
397  const std::string& node,
398  const SubscriberList& list,
399  ResultHandler* handler )
400  { return subscriberList( SetSubscriberList, service,
401  node, list, handler ); }
402 
413  const std::string getAffiliates( const JID& service,
414  const std::string& node,
415  ResultHandler* handler )
416  { return affiliateList( GetAffiliateList, service,
417  node, AffiliateList(),
418  handler ); }
419 
431  const std::string setAffiliates( const JID& service,
432  const std::string& node,
433  const AffiliateList& list,
434  ResultHandler* handler )
435  { return affiliateList( SetAffiliateList, service,
436  node, list, handler ); }
437 
448  const std::string getNodeConfig( const JID& service,
449  const std::string& node,
450  ResultHandler* handler )
451  { return nodeConfig( service, node, 0, handler ); }
452 
464  const std::string setNodeConfig( const JID& service,
465  const std::string& node,
466  DataForm* config,
467  ResultHandler* handler )
468  { return nodeConfig( service, node, config, handler ); }
469 
475  bool removeID( const std::string& id );
476 
477  // reimplemented from DiscoHandler
478  void handleDiscoInfoResult( IQ* iq, int context );
479 
480  // reimplemented from DiscoHandler
481  void handleDiscoItemsResult( IQ* iq, int context );
482 
483  // reimplemented from DiscoHandler
484  void handleDiscoError( IQ* iq, int context );
485 
486  // reimplemented from DiscoHandler
487  bool handleDiscoSet( IQ* ) { return 0; }
488 
489  // reimplemented from IqHandler.
490  virtual bool handleIq( const IQ& iq ) { (void)iq; return false; }
491 
492  // reimplemented from IqHandler.
493  virtual void handleIqID( const IQ& iq, int context );
494 
495  private:
496 #ifdef PUBSUBMANAGER_TEST
497  public:
498 #endif
499 
500  enum TrackContext
501  {
502  Subscription,
503  Unsubscription,
504  GetSubscriptionOptions,
505  SetSubscriptionOptions,
506  GetSubscriptionList,
507  GetSubscriberList,
508  SetSubscriberList,
509  GetAffiliationList,
510  GetAffiliateList,
511  SetAffiliateList,
512  GetNodeConfig,
513  SetNodeConfig,
514  DefaultNodeConfig,
515  GetItemList,
516  PublishItem,
517  DeleteItem,
518  CreateNode,
519  DeleteNode,
520  PurgeNodeItems,
521  NodeAssociation,
522  NodeDisassociation,
523  GetFeatureList,
524  DiscoServiceInfos,
525  DiscoNodeInfos,
526  DiscoNodeItems,
527  RequestItems,
528  InvalidContext
529  };
530 
531  class PubSubOwner : public StanzaExtension
532  {
533  public:
538  PubSubOwner( TrackContext context = InvalidContext );
539 
544  PubSubOwner( const Tag* tag );
545 
549  virtual ~PubSubOwner();
550 
555  void setNode( const std::string& node ) { m_node = node; }
556 
561  const std::string& node() const { return m_node; }
562 
567  void setConfig( DataForm* config )
568  { m_form = config; }
569 
574  const DataForm* config() const { return m_form; }
575 
580  void setSubscriberList( const SubscriberList& subList )
581  { m_subList = subList; }
582 
587  void setAffiliateList( const AffiliateList& affList )
588  { m_affList = affList; }
589 
590  // reimplemented from StanzaExtension
591  virtual const std::string& filterString() const;
592 
593  // reimplemented from StanzaExtension
594  virtual StanzaExtension* newInstance( const Tag* tag ) const
595  {
596  return new PubSubOwner( tag );
597  }
598 
599  // reimplemented from StanzaExtension
600  virtual Tag* tag() const;
601 
602  // reimplemented from StanzaExtension
603  virtual StanzaExtension* clone() const
604  {
605  PubSubOwner* p = new PubSubOwner();
606  p->m_node = m_node;
607  p->m_ctx = m_ctx;
608  p->m_form = m_form ? new DataForm( *m_form ) : 0;
609  p->m_subList = m_subList;
610  p->m_affList = m_affList;
611  return p;
612  }
613 
614  private:
615  std::string m_node;
616  TrackContext m_ctx;
617  DataForm* m_form;
618  SubscriberList m_subList;
619  AffiliateList m_affList;
620  };
621 
622  class PubSub : public StanzaExtension
623  {
624  public:
629  PubSub( TrackContext context = InvalidContext );
630 
635  PubSub( const Tag* tag );
636 
640  virtual ~PubSub();
641 
646  void setJID( const JID& jid ) { m_jid = jid; }
647 
652  const JID& jid() const { return m_jid; }
653 
658  void setNode( const std::string& node ) { m_node = node; }
659 
664  const std::string& node() const { return m_node; }
665 
670  void setSubscriptionID( const std::string& subid )
671  { m_subid = subid; }
672 
677  const std::string& subscriptionID() const { return m_subid; }
678 
685  void setOptions( const std::string& node, DataForm* df )
686  {
687  m_options.node = node;
688  if( m_options.df != 0 )
689  delete m_options.df;
690  m_options.df = df;
691  }
692 
697  const DataForm* options() const
698  { return m_options.df; }
699 
704  const ItemList& items() const { return m_items; }
705 
710  void setItems( const ItemList& items )
711  { m_items = items; }
712 
717  void setMaxItems( int maxItems )
718  { m_maxItems = maxItems; }
719 
724  const SubscriptionMap& subscriptions() const
725  { return m_subscriptionMap; }
726 
731  const AffiliationMap& affiliations() const
732  { return m_affiliationMap; }
733 
738  void setNotify( bool notify ) { m_notify = notify; }
739 
740  // reimplemented from StanzaExtension
741  virtual const std::string& filterString() const;
742 
743  // reimplemented from StanzaExtension
744  virtual StanzaExtension* newInstance( const Tag* tag ) const
745  {
746  return new PubSub( tag );
747  }
748 
749  // reimplemented from StanzaExtension
750  virtual Tag* tag() const;
751 
752  // reimplemented from StanzaExtension
753  virtual StanzaExtension* clone() const;
754 
755  private:
756  AffiliationMap m_affiliationMap;
757  SubscriptionMap m_subscriptionMap;
758  TrackContext m_ctx;
759 
760  struct Options
761  {
762  std::string node;
763  DataForm* df;
764  };
765  Options m_options;
766  JID m_jid;
767  std::string m_node;
768  std::string m_subid;
769  ItemList m_items;
770  int m_maxItems;
771  bool m_notify;
772  };
773 
785  const std::string nodeConfig( const JID& service,
786  const std::string& node,
787  DataForm* config,
788  ResultHandler* handler );
789 
803  const std::string subscriberList( TrackContext ctx,
804  const JID& service,
805  const std::string& node,
806  const SubscriberList& config,
807  ResultHandler* handler );
808 
822  const std::string affiliateList( TrackContext ctx,
823  const JID& service,
824  const std::string& node,
825  const AffiliateList& config,
826  ResultHandler* handler );
827 
828  const std::string subscriptionOptions( TrackContext context,
829  const JID& service,
830  const JID& jid,
831  const std::string& node,
832  ResultHandler* handler,
833  DataForm* df,
834  const std::string& subid = EmptyString );
835 
836  const std::string getSubscriptionsOrAffiliations( const JID& service,
837  ResultHandler* handler,
838  TrackContext context );
839 
840  typedef std::map < std::string, std::string > NodeOperationTrackMap;
841  typedef std::map < std::string, ResultHandler* > ResultHandlerTrackMap;
842 
843  ClientBase* m_parent;
844 
845  NodeOperationTrackMap m_nopTrackMap;
846  ResultHandlerTrackMap m_resultHandlerTrackMap;
847 
848  util::Mutex m_trackMapMutex;
849 
850  };
851 
852  }
853 
854 }
855 
856 #endif // PUBSUBMANAGER_H__