gloox  1.1-svn
iq.cpp
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 #include "iq.h"
14 #include "util.h"
15 
16 namespace gloox
17 {
18 
19  static const char * iqTypeStringValues[] =
20  {
21  "get", "set", "result", "error"
22  };
23 
24  static inline const char* typeString( IQ::IqType type )
25  {
26  return iqTypeStringValues[type];
27  }
28 
29  IQ::IQ( Tag* tag )
30  : Stanza( tag ), m_subtype( Invalid )
31  {
32  if( !tag || tag->name() != "iq" )
33  return;
34 
35  m_subtype = (IQ::IqType)util::lookup( tag->findAttribute( TYPE ), iqTypeStringValues );
36  }
37 
38  IQ::IQ( IqType type, const JID& to, const std::string& id )
39  : Stanza( to ), m_subtype( type )
40  {
41  m_id = id;
42  }
43 
45  {
46  }
47 
48  Tag* IQ::tag() const
49  {
50  if( m_subtype == Invalid )
51  return 0;
52 
53  Tag* t = new Tag( "iq" );
54  if( m_to )
55  t->addAttribute( "to", m_to.full() );
56  if( m_from )
57  t->addAttribute( "from", m_from.full() );
58  if( !m_id.empty() )
59  t->addAttribute( "id", m_id );
60  t->addAttribute( TYPE, typeString( m_subtype ) );
61 
62  StanzaExtensionList::const_iterator it = m_extensionList.begin();
63  for( ; it != m_extensionList.end(); ++it )
64  t->addChild( (*it)->tag() );
65 
66  return t;
67  }
68 
69 }