gloox  1.0.1
disco.h
1 /*
2  Copyright (c) 2004-2012 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 
14 
15 #ifndef DISCO_H__
16 #define DISCO_H__
17 
18 #include "gloox.h"
19 
20 #include "iqhandler.h"
21 #include "jid.h"
22 
23 #include <string>
24 #include <list>
25 #include <map>
26 
27 namespace gloox
28 {
29 
30  class ClientBase;
31  class DataForm;
32  class DiscoHandler;
33  class DiscoNodeHandler;
34  class IQ;
35 
45  class GLOOX_API Disco : public IqHandler
46  {
47  friend class ClientBase;
48 
49  public:
50 
51  class Identity; // declared below class Info
52 
56  typedef std::list<Identity*> IdentityList;
57 
65  class GLOOX_API Info : public StanzaExtension
66  {
67  friend class Disco;
68 
69  public:
74  const std::string& node() const { return m_node; }
75 
80  const StringList& features() const { return m_features; }
81 
87  bool hasFeature( const std::string& feature ) const;
88 
93  const IdentityList& identities() const { return m_identities; }
94 
99  const DataForm* form() const { return m_form; }
100 
108  void setForm( DataForm* form );
109 
110  // reimplemented from StanzaExtension
111  virtual const std::string& filterString() const;
112 
113  // reimplemented from StanzaExtension
114  virtual StanzaExtension* newInstance( const Tag* tag ) const
115  {
116  return new Info( tag );
117  }
118 
119  // reimplemented from StanzaExtension
120  virtual StanzaExtension* clone() const
121  {
122  return new Info( *this );
123  }
124 
125  // reimplemented from StanzaExtension
126  virtual Tag* tag() const;
127 
128  private:
129 #ifdef DISCO_INFO_TEST
130  public:
131 #endif
132 
139  Info( const std::string& node = EmptyString, bool defaultFeatures = false );
140 
146  Info( const Tag* tag );
147 
152  Info( const Info& info );
153 
157  virtual ~Info();
158 
163  void setNode( const std::string& node ) { m_node = node; }
164 
169  void setFeatures( const StringList& features )
170  {
171  StringList fl( features );
172  fl.sort(); // needed on win32
173  m_features.merge( fl );
174  }
175 
182  void setIdentities( const IdentityList& identities ) { m_identities = identities; }
183 
184  std::string m_node;
185  StringList m_features;
186  IdentityList m_identities;
187  DataForm* m_form;
188  };
189 
196  class GLOOX_API Identity
197  {
198  friend class Info;
199  friend class Disco;
200 
201  public:
209  Identity( const std::string& category,
210  const std::string& type,
211  const std::string& name );
212 
217  Identity( const Identity& id );
218 
222  ~Identity();
223 
228  const std::string& category() const { return m_category; }
229 
234  const std::string& type() const { return m_type; }
235 
240  const std::string& name() const { return m_name; }
241 
246  Tag* tag() const;
247 
248  private:
253  Identity( const Tag* tag );
254 
255  std::string m_category;
256  std::string m_type;
257  std::string m_name;
259  };
260 
261  class Item; // declared below class Items
262 
266  typedef std::list<Item*> ItemList;
267 
275  class GLOOX_API Items : public StanzaExtension
276  {
277  friend class Disco;
278 
279  public:
280  // This needs to be public so one can proactively send a list of adhoc commands
281  // see @xep{0050}
286  Items( const std::string& node = EmptyString );
287 
291  virtual ~Items();
292 
299  void setItems( const ItemList& items );
300 
305  const std::string& node() const { return m_node; }
306 
311  const ItemList& items() const { return m_items; }
312 
313  // reimplemented from StanzaExtension
314  virtual const std::string& filterString() const;
315 
316  // reimplemented from StanzaExtension
317  virtual StanzaExtension* newInstance( const Tag* tag ) const
318  {
319  return new Items( tag );
320  }
321 
322  // reimplemented from StanzaExtension
323  virtual Tag* tag() const;
324 
325  // reimplemented from StanzaExtension
326  virtual StanzaExtension* clone() const
327  {
328  return new Items( *this );
329  }
330 
331  private:
332 #ifdef DISCO_ITEMS_TEST
333  public:
334 #endif
335 
340  Items( const Tag* tag );
341 
342  std::string m_node;
343  ItemList m_items;
344  };
345 
352  class GLOOX_API Item
353  {
354  friend class Items;
355 
356  public:
363  Item( const JID& jid,
364  const std::string& node,
365  const std::string& name )
366  : m_jid( jid ), m_node( node ), m_name( name ) {}
367 
371  ~Item() {}
372 
377  const JID& jid() const { return m_jid; }
378 
383  const std::string& node() const { return m_node; }
384 
389  const std::string& name() const { return m_name; }
390 
395  Tag* tag() const;
396 
397  private:
402  Item( const Tag* tag );
403 
404  JID m_jid;
405  std::string m_node;
406  std::string m_name;
408  };
409 
422  void addFeature( const std::string& feature )
423  { m_features.push_back( feature ); }
424 
430  void removeFeature( const std::string& feature )
431  { m_features.remove( feature ); }
432 
438  const StringList features( bool defaultFeatures = false ) const;
439 
451  void getDiscoInfo( const JID& to, const std::string& node, DiscoHandler* dh, int context,
452  const std::string& tid = EmptyString )
453  { getDisco( to, node, dh, context, GetDiscoInfo, tid ); }
454 
466  void getDiscoItems( const JID& to, const std::string& node, DiscoHandler* dh, int context,
467  const std::string& tid = EmptyString )
468  { getDisco( to, node, dh, context, GetDiscoItems, tid ); }
469 
478  void setVersion( const std::string& name, const std::string& version,
479  const std::string& os = EmptyString );
480 
485  const std::string& name() const { return m_versionName; }
486 
491  const std::string& version() const { return m_versionVersion; }
492 
497  const std::string& os() const { return m_versionOs; }
498 
512  void setIdentity( const std::string& category, const std::string& type,
513  const std::string& name = EmptyString );
514 
521  void addIdentity( const std::string& category, const std::string& type,
522  const std::string& name = EmptyString )
523  { m_identities.push_back( new Identity( category, type, name ) ); }
524 
529  const IdentityList& identities() const { return m_identities; }
530 
538  void setForm( DataForm* form );
539 
544  const DataForm* form() const { return m_form; }
545 
552  void registerDiscoHandler( DiscoHandler* dh )
553  { m_discoHandlers.push_back( dh ); }
554 
559  void removeDiscoHandler( DiscoHandler* dh );
560 
569  void registerNodeHandler( DiscoNodeHandler* nh, const std::string& node );
570 
577  void removeNodeHandler( DiscoNodeHandler* nh, const std::string& node );
578 
583  void removeNodeHandlers( DiscoNodeHandler* nh );
584 
585  // reimplemented from IqHandler.
586  virtual bool handleIq( const IQ& iq );
587 
588  // reimplemented from IqHandler.
589  virtual void handleIqID( const IQ& iq, int context );
590 
591  private:
592 #ifdef DISCO_TEST
593  public:
594 #endif
595  Disco( ClientBase* parent );
596  virtual ~Disco();
597 
598  enum IdType
599  {
600  GetDiscoInfo,
601  GetDiscoItems
602  };
603 
604  void getDisco( const JID& to, const std::string& node, DiscoHandler* dh,
605  int context, IdType idType, const std::string& tid );
606 
607  struct DiscoHandlerContext
608  {
609  DiscoHandler* dh;
610  int context;
611  };
612 
613  ClientBase* m_parent;
614 
615  typedef std::list<DiscoHandler*> DiscoHandlerList;
616  typedef std::list<DiscoNodeHandler*> DiscoNodeHandlerList;
617  typedef std::map<std::string, DiscoNodeHandlerList> DiscoNodeHandlerMap;
618  typedef std::map<std::string, DiscoHandlerContext> DiscoHandlerMap;
619 
620  DiscoHandlerList m_discoHandlers;
621  DiscoNodeHandlerMap m_nodeHandlers;
622  DiscoHandlerMap m_track;
623  IdentityList m_identities;
624  StringList m_features;
625  StringMap m_queryIDs;
626  DataForm* m_form;
627 
628  std::string m_versionName;
629  std::string m_versionVersion;
630  std::string m_versionOs;
631 
632  };
633 
634 }
635 
636 #endif // DISCO_H__