gloox  0.9.9.12
dataform.cpp
1 /*
2  Copyright (c) 2005-2008 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 "dataformreported.h"
17 #include "dataformitem.h"
18 #include "tag.h"
19 
20 namespace gloox
21 {
22 
23  DataForm::DataForm( DataFormType type, const StringList& instructions, const std::string& title )
24  : m_instructions( instructions ), m_type( type ), m_title( title )
25  {
26  }
27 
28  DataForm::DataForm( DataFormType type, const std::string& title )
29  : m_type( type ), m_title( title )
30  {
31  }
32 
34  : m_type( FormTypeInvalid )
35  {
36  parse( tag );
37  }
38 
40  : m_type( FormTypeInvalid )
41  {
42  }
43 
45  {
46  }
47 
48  bool DataForm::parse( Tag *tag )
49  {
50  if( !tag || !tag->hasAttribute( "xmlns", XMLNS_X_DATA ) || tag->name() != "x" )
51  return false;
52 
53  if( tag->hasAttribute( "type", "form" ) )
54  m_type = FormTypeForm;
55  else if( tag->hasAttribute( "type", "submit" ) )
56  m_type = FormTypeSubmit;
57  else if( tag->hasAttribute( "type", "cancel" ) )
58  m_type = FormTypeCancel;
59  else if( tag->hasAttribute( "type", "result" ) )
60  m_type = FormTypeResult;
61  else
62  return false;
63 
64  const Tag::TagList& l = tag->children();
65  Tag::TagList::const_iterator it = l.begin();
66  for( ; it != l.end(); ++it )
67  {
68  if( (*it)->name() == "title" )
69  m_title = (*it)->cdata();
70  else if( (*it)->name() == "instructions" )
71  m_instructions.push_back( (*it)->cdata() );
72  else if( (*it)->name() == "field" )
73  {
74  DataFormField *f = new DataFormField( (*it) );
75  m_fields.push_back( f );
76  }
77  else if( (*it)->name() == "reported" )
78  {
79  DataFormReported *r = new DataFormReported( (*it) );
80  m_fields.push_back( r );
81  }
82  else if( (*it)->name() == "item" )
83  {
84  DataFormItem *i = new DataFormItem( (*it) );
85  m_fields.push_back( i );
86  }
87  }
88 
89  return true;
90  }
91 
92  Tag* DataForm::tag() const
93  {
94  if( m_type == FormTypeInvalid )
95  return 0;
96 
97  Tag *x = new Tag( "x" );
98  x->addAttribute( "xmlns", XMLNS_X_DATA );
99  if( !m_title.empty() )
100  new Tag( x, "title", m_title );
101 
102  StringList::const_iterator it_i = m_instructions.begin();
103  for( ; it_i != m_instructions.end(); ++it_i )
104  new Tag( x, "instructions", (*it_i) );
105 
106  FieldList::const_iterator it = m_fields.begin();
107  for( ; it != m_fields.end(); ++it )
108  {
109  DataFormItem *i = dynamic_cast<DataFormItem*>( (*it) );
110  if( i )
111  {
112  x->addChild( i->tag() );
113  continue;
114  }
115 
116  DataFormReported *r = dynamic_cast<DataFormReported*>( (*it) );
117  if( r )
118  {
119  x->addChild( r->tag() );
120  continue;
121  }
122 
123  x->addChild( (*it)->tag() );
124  }
125 
126  switch( m_type )
127  {
128  case FormTypeForm:
129  x->addAttribute( "type", "form" );
130  break;
131  case FormTypeSubmit:
132  x->addAttribute( "type", "submit" );
133  break;
134  case FormTypeCancel:
135  x->addAttribute( "type", "cancel" );
136  break;
137  case FormTypeResult:
138  x->addAttribute( "type", "result" );
139  break;
140  default:
141  break;
142  }
143 
144  return x;
145  }
146 
147 }