gloox  1.0
dataform.cpp
1 /*
2  Copyright (c) 2005-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 
14 #include "dataform.h"
15 #include "dataformfield.h"
16 #include "dataformitem.h"
17 #include "dataformreported.h"
18 #include "util.h"
19 #include "tag.h"
20 
21 namespace gloox
22 {
23 
24  DataForm::DataForm( FormType type, const StringList& instructions, const std::string& title )
26  m_type( type ), m_instructions( instructions ), m_title( title ), m_reported( 0 )
27  {
28  }
29 
30  DataForm::DataForm( FormType type, const std::string& title )
32  m_type( type ), m_title( title ), m_reported( 0 )
33  {
34  }
35 
36  DataForm::DataForm( const Tag* tag )
38  m_type( TypeInvalid ), m_reported( 0 )
39  {
40  parse( tag );
41  }
42 
45  m_type( form.m_type ), m_instructions( form.m_instructions ),
46  m_title( form.m_title ), m_reported( form.m_reported ? new DataFormReported( form.m_reported->tag() ) : 0 )
47  {
48  }
49 
51  {
52  util::clearList( m_items );
53  delete m_reported;
54  m_reported = NULL;
55  }
56 
57  static const char* dfTypeValues[] =
58  {
59  "form", "submit", "cancel", "result"
60  };
61 
62  bool DataForm::parse( const Tag* tag )
63  {
64  if( !tag || tag->xmlns() != XMLNS_X_DATA || tag->name() != "x" )
65  return false;
66 
67  const std::string& type = tag->findAttribute( TYPE );
68  if( type.empty() )
69  m_type = TypeForm;
70  else
71  {
72  m_type = (FormType)util::lookup( type, dfTypeValues );
73  if( m_type == TypeInvalid )
74  return false;
75  }
76 
77  const TagList& l = tag->children();
78  TagList::const_iterator it = l.begin();
79  for( ; it != l.end(); ++it )
80  {
81  if( (*it)->name() == "title" )
82  m_title = (*it)->cdata();
83  else if( (*it)->name() == "instructions" )
84  m_instructions.push_back( (*it)->cdata() );
85  else if( (*it)->name() == "field" )
86  m_fields.push_back( new DataFormField( (*it) ) );
87  else if( (*it)->name() == "reported" )
88  {
89  if( m_reported == NULL )
90  m_reported = new DataFormReported( (*it) );
91  // else - Invalid data form - only one "reported" is allowed
92  }
93  else if( (*it)->name() == "item" )
94  m_items.push_back( new DataFormItem( (*it) ) );
95  }
96 
97  return true;
98  }
99 
100  const std::string& DataForm::filterString() const
101  {
102  static const std::string filter = "/message/x[@xmlns='" + XMLNS_X_DATA + "']";
103  return filter;
104  }
105 
107  {
108  if( m_type == TypeInvalid )
109  return 0;
110 
111  Tag* x = new Tag( "x" );
112  x->setXmlns( XMLNS_X_DATA );
113  x->addAttribute( TYPE, util::lookup( m_type, dfTypeValues ) );
114  if( !m_title.empty() )
115  new Tag( x, "title", m_title );
116 
117  StringList::const_iterator it_i = m_instructions.begin();
118  for( ; it_i != m_instructions.end(); ++it_i )
119  new Tag( x, "instructions", (*it_i) );
120 
121  FieldList::const_iterator it = m_fields.begin();
122  for( ; it != m_fields.end(); ++it )
123  x->addChild( (*it)->tag() );
124 
125  if( m_reported != NULL )
126  {
127  x->addChild( m_reported->tag() );
128  }
129 
130  ItemList::const_iterator iti = m_items.begin();
131  for( ; iti != m_items.end(); ++iti )
132  x->addChild( (*iti)->tag() );
133 
134  return x;
135  }
136 
137 }