gloox  1.0
pubsubmanager.h
1 /*
2  Copyright (c) 2007-2009 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 
126  const std::string unsubscribe( const JID& service,
127  const std::string& node,
128  const std::string& subid,
129  ResultHandler* handler,
130  const JID& jid = JID() );
131 
141  const std::string getSubscriptions( const JID& service,
142  ResultHandler* handler )
143  {
144  return getSubscriptionsOrAffiliations( service,
145  handler,
146  GetSubscriptionList );
147  }
148 
158  const std::string getAffiliations( const JID& service,
159  ResultHandler* handler )
160  {
161  return getSubscriptionsOrAffiliations( service,
162  handler,
163  GetAffiliationList );
164  }
165 
177  const std::string getSubscriptionOptions( const JID& service,
178  const JID& jid,
179  const std::string& node,
180  ResultHandler* handler)
181  { return subscriptionOptions( GetSubscriptionOptions, service, jid, node, handler, 0 ); }
182 
195  const std::string setSubscriptionOptions( const JID& service,
196  const JID& jid,
197  const std::string& node,
198  DataForm* df,
199  ResultHandler* handler )
200  { return subscriptionOptions( SetSubscriptionOptions, service, jid, node, handler, df ); }
201 
211  void getAffiliations( const JID& service,
212  const std::string& node,
213  ResultHandler* handler );
214 
224  const std::string requestItems( const JID& service,
225  const std::string& node,
226  const std::string& subid,
227  int maxItems,
228  ResultHandler* handler);
229 
239  const std::string requestItems( const JID& service,
240  const std::string& node,
241  const std::string& subid,
242  const ItemList& items,
243  ResultHandler* handler);
244 
259  const std::string publishItem( const JID& service,
260  const std::string& node,
261  ItemList& items,
262  DataForm* options,
263  ResultHandler* handler );
264 
277  const std::string deleteItem( const JID& service,
278  const std::string& node,
279  const ItemList& items,
280  bool notify,
281  ResultHandler* handler );
282 
295  const std::string createNode( const JID& service,
296  const std::string& node,
297  DataForm* config,
298  ResultHandler* handler );
299 
310  const std::string deleteNode( const JID& service,
311  const std::string& node,
312  ResultHandler* handler );
313 
324  const std::string getDefaultNodeConfig( const JID& service,
325  NodeType type,
326  ResultHandler* handler );
327 
338  const std::string purgeNode( const JID& service,
339  const std::string& node,
340  ResultHandler* handler );
341 
352  const std::string getSubscribers( const JID& service,
353  const std::string& node,
354  ResultHandler* handler )
355  { return subscriberList( GetSubscriberList, service,
356  node, SubscriberList(),
357  handler ); }
358 
371  const std::string setSubscribers( const JID& service,
372  const std::string& node,
373  const SubscriberList& list,
374  ResultHandler* handler )
375  { return subscriberList( SetSubscriberList, service,
376  node, list, handler ); }
377 
388  const std::string getAffiliates( const JID& service,
389  const std::string& node,
390  ResultHandler* handler )
391  { return affiliateList( GetAffiliateList, service,
392  node, AffiliateList(),
393  handler ); }
394 
406  const std::string setAffiliates( const JID& service,
407  const std::string& node,
408  const AffiliateList& list,
409  ResultHandler* handler )
410  { return affiliateList( SetAffiliateList, service,
411  node, list, handler ); }
412 
423  const std::string getNodeConfig( const JID& service,
424  const std::string& node,
425  ResultHandler* handler )
426  { return nodeConfig( service, node, 0, handler ); }
427 
439  const std::string setNodeConfig( const JID& service,
440  const std::string& node,
441  DataForm* config,
442  ResultHandler* handler )
443  { return nodeConfig( service, node, config, handler ); }
444 
450  bool removeID( const std::string& id );
451 
452  // reimplemented from DiscoHandler
453  void handleDiscoInfoResult( IQ* iq, int context );
454 
455  // reimplemented from DiscoHandler
456  void handleDiscoItemsResult( IQ* iq, int context );
457 
458  // reimplemented from DiscoHandler
459  void handleDiscoError( IQ* iq, int context );
460 
461  // reimplemented from DiscoHandler
462  bool handleDiscoSet( IQ* ) { return 0; }
463 
464  // reimplemented from IqHandler.
465  virtual bool handleIq( const IQ& iq ) { (void)iq; return false; }
466 
467  // reimplemented from IqHandler.
468  virtual void handleIqID( const IQ& iq, int context );
469 
470  private:
471 #ifdef PUBSUBMANAGER_TEST
472  public:
473 #endif
474 
475  enum TrackContext
476  {
477  Subscription,
478  Unsubscription,
479  GetSubscriptionOptions,
480  SetSubscriptionOptions,
481  GetSubscriptionList,
482  GetSubscriberList,
483  SetSubscriberList,
484  GetAffiliationList,
485  GetAffiliateList,
486  SetAffiliateList,
487  GetNodeConfig,
488  SetNodeConfig,
489  DefaultNodeConfig,
490  GetItemList,
491  PublishItem,
492  DeleteItem,
493  CreateNode,
494  DeleteNode,
495  PurgeNodeItems,
496  NodeAssociation,
497  NodeDisassociation,
498  GetFeatureList,
499  DiscoServiceInfos,
500  DiscoNodeInfos,
501  DiscoNodeItems,
502  RequestItems,
503  InvalidContext
504  };
505 
506  class PubSubOwner : public StanzaExtension
507  {
508  public:
513  PubSubOwner( TrackContext context = InvalidContext );
514 
519  PubSubOwner( const Tag* tag );
520 
524  virtual ~PubSubOwner();
525 
530  void setNode( const std::string& node ) { m_node = node; }
531 
536  const std::string& node() const { return m_node; }
537 
542  void setConfig( DataForm* config )
543  { m_form = config; }
544 
549  const DataForm* config() const { return m_form; }
550 
555  void setSubscriberList( const SubscriberList& subList )
556  { m_subList = subList; }
557 
562  void setAffiliateList( const AffiliateList& affList )
563  { m_affList = affList; }
564 
565  // reimplemented from StanzaExtension
566  virtual const std::string& filterString() const;
567 
568  // reimplemented from StanzaExtension
569  virtual StanzaExtension* newInstance( const Tag* tag ) const
570  {
571  return new PubSubOwner( tag );
572  }
573 
574  // reimplemented from StanzaExtension
575  virtual Tag* tag() const;
576 
577  // reimplemented from StanzaExtension
578  virtual StanzaExtension* clone() const
579  {
580  PubSubOwner* p = new PubSubOwner();
581  p->m_node = m_node;
582  p->m_ctx = m_ctx;
583  p->m_form = m_form ? new DataForm( *m_form ) : 0;
584  p->m_subList = m_subList;
585  p->m_affList = m_affList;
586  return p;
587  }
588 
589  private:
590  std::string m_node;
591  TrackContext m_ctx;
592  DataForm* m_form;
593  SubscriberList m_subList;
594  AffiliateList m_affList;
595  };
596 
597  class PubSub : public StanzaExtension
598  {
599  public:
604  PubSub( TrackContext context = InvalidContext );
605 
610  PubSub( const Tag* tag );
611 
615  virtual ~PubSub();
616 
621  void setJID( const JID& jid ) { m_jid = jid; }
622 
627  const JID& jid() const { return m_jid; }
628 
633  void setNode( const std::string& node ) { m_node = node; }
634 
639  const std::string& node() const { return m_node; }
640 
645  void setSubscriptionID( const std::string& subid )
646  { m_subid = subid; }
647 
652  const std::string& subscriptionID() const { return m_subid; }
653 
660  void setOptions( const std::string& node, DataForm* df )
661  {
662  m_options.node = node;
663  m_options.df = df;
664  }
665 
670  const DataForm* options() const
671  { return m_options.df; }
672 
677  const ItemList& items() const { return m_items; }
678 
683  void setItems( const ItemList& items )
684  { m_items = items; }
685 
690  void setMaxItems( int maxItems )
691  { m_maxItems = maxItems; }
692 
697  const SubscriptionMap& subscriptions() const
698  { return m_subscriptionMap; }
699 
704  const AffiliationMap& affiliations() const
705  { return m_affiliationMap; }
706 
711  void setNotify( bool notify ) { m_notify = notify; }
712 
713  // reimplemented from StanzaExtension
714  virtual const std::string& filterString() const;
715 
716  // reimplemented from StanzaExtension
717  virtual StanzaExtension* newInstance( const Tag* tag ) const
718  {
719  return new PubSub( tag );
720  }
721 
722  // reimplemented from StanzaExtension
723  virtual Tag* tag() const;
724 
725  // reimplemented from StanzaExtension
726  virtual StanzaExtension* clone() const;
727 
728  private:
729  AffiliationMap m_affiliationMap;
730  SubscriptionMap m_subscriptionMap;
731  TrackContext m_ctx;
732 
733  struct Options
734  {
735  std::string node;
736  DataForm* df;
737  };
738  Options m_options;
739  JID m_jid;
740  std::string m_node;
741  std::string m_subid;
742  ItemList m_items;
743  int m_maxItems;
744  bool m_notify;
745  };
746 
758  const std::string nodeConfig( const JID& service,
759  const std::string& node,
760  DataForm* config,
761  ResultHandler* handler );
762 
776  const std::string subscriberList( TrackContext ctx,
777  const JID& service,
778  const std::string& node,
779  const SubscriberList& config,
780  ResultHandler* handler );
781 
795  const std::string affiliateList( TrackContext ctx,
796  const JID& service,
797  const std::string& node,
798  const AffiliateList& config,
799  ResultHandler* handler );
800 
801  const std::string subscriptionOptions( TrackContext context,
802  const JID& service,
803  const JID& jid,
804  const std::string& node,
805  ResultHandler* handler,
806  DataForm* df );
807 
808  const std::string getSubscriptionsOrAffiliations( const JID& service,
809  ResultHandler* handler,
810  TrackContext context );
811 
812  typedef std::map < std::string, std::string > NodeOperationTrackMap;
813  typedef std::map < std::string, ResultHandler* > ResultHandlerTrackMap;
814 
815  ClientBase* m_parent;
816 
817  NodeOperationTrackMap m_nopTrackMap;
818  ResultHandlerTrackMap m_resultHandlerTrackMap;
819 
820  util::Mutex m_trackMapMutex;
821 
822  };
823 
824  }
825 
826 }
827 
828 #endif // PUBSUBMANAGER_H__