gloox  1.0.28
dataformfield.cpp
1 /*
2  Copyright (c) 2005-2023 by Jakob Schröter <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 "dataformfield.h"
14 #include "util.h"
15 #include "tag.h"
16 
17 namespace gloox
18 {
19 
20  static const char* fieldTypeValues[] =
21  {
22  "boolean", "fixed", "hidden", "jid-multi", "jid-single",
23  "list-multi", "list-single", "text-multi", "text-private", "text-single", ""
24  };
25 
27  : m_type( type ), m_required( false )
28  {
29  }
30 
31  DataFormField::DataFormField( const std::string& name, const std::string& value,
32  const std::string& label, FieldType type )
33  : m_type( type ), m_name( name ), m_label( label ), m_required( false )
34  {
35  m_values.push_back( value );
36  }
37 
39  : m_type( TypeInvalid ), m_required( false )
40  {
41  if( !tag )
42  return;
43 
44  const std::string& type = tag->findAttribute( TYPE );
45  if( type.empty() )
46  {
47  if( !tag->name().empty() )
48  m_type = TypeNone;
49  }
50  else
51  m_type = static_cast<FieldType>( util::lookup( type, fieldTypeValues ) );
52 
53  if( tag->hasAttribute( "var" ) )
54  m_name = tag->findAttribute( "var" );
55 
56  if( tag->hasAttribute( "label" ) )
57  m_label = tag->findAttribute( "label" );
58 
59  const TagList& l = tag->children();
60  TagList::const_iterator it = l.begin();
61  for( ; it != l.end(); ++it )
62  {
63  if( (*it)->name() == "desc" )
64  m_desc = (*it)->cdata();
65  else if( (*it)->name() == "required" )
66  m_required = true;
67  else if( (*it)->name() == "value" )
68  {
69  if( m_type == TypeTextMulti || m_type == TypeListMulti || m_type == TypeJidMulti )
70  addValue( (*it)->cdata() );
71  else
72  setValue( (*it)->cdata() );
73  }
74  else if( (*it)->name() == "option" )
75  {
76  Tag* v = (*it)->findChild( "value" );
77  if( v )
78  m_options.insert( std::make_pair( (*it)->findAttribute( "label" ), v->cdata() ) );
79  }
80  }
81 
82  }
83 
85  {
86  }
87 
89  {
90  if( m_type == TypeInvalid )
91  return 0;
92 
93  Tag* field = new Tag( "field" );
94  field->addAttribute( TYPE, util::lookup( m_type, fieldTypeValues ) );
95  field->addAttribute( "var", m_name );
96  field->addAttribute( "label", m_label );
97  if( m_required )
98  new Tag( field, "required" );
99 
100  if( !m_desc.empty() )
101  new Tag( field, "desc", m_desc );
102 
103  if( m_type == TypeListSingle || m_type == TypeListMulti )
104  {
105  StringMultiMap::const_iterator it = m_options.begin();
106  for( ; it != m_options.end(); ++it )
107  {
108  Tag* option = new Tag( field, "option", "label", (*it).first );
109  new Tag( option, "value", (*it).second );
110  }
111  }
112  else if( m_type == TypeBoolean )
113  {
114  if( m_values.size() == 0 || m_values.front() == "false" || m_values.front() == "0" )
115  new Tag( field, "value", "0" );
116  else
117  new Tag( field, "value", "1" );
118  }
119 
120  if( m_type == TypeTextMulti || m_type == TypeListMulti || m_type == TypeJidMulti )
121  {
122  StringList::const_iterator it = m_values.begin();
123  for( ; it != m_values.end() ; ++it )
124  new Tag( field, "value", (*it) );
125  }
126 
127  if( m_values.size() && !( m_type == TypeTextMulti || m_type == TypeListMulti
128  || m_type == TypeBoolean || m_type == TypeJidMulti ) )
129  new Tag( field, "value", m_values.front() );
130 
131  return field;
132  }
133 
134 }
DataFormField(FieldType type=TypeTextSingle)
FieldType type() const
void addValue(const std::string &value)
const std::string & value() const
void setValue(const std::string &value)
virtual Tag * tag() const
This is an abstraction of an XML element.
Definition: tag.h:47
Tag * findChild(const std::string &name) const
Definition: tag.cpp:624
bool addAttribute(Attribute *attr)
Definition: tag.cpp:354
bool hasAttribute(const std::string &name, const std::string &value=EmptyString) const
Definition: tag.cpp:602
const std::string cdata() const
Definition: tag.cpp:497
const std::string & findAttribute(const std::string &name) const
Definition: tag.cpp:589
const std::string & name() const
Definition: tag.h:394
const TagList & children() const
Definition: tag.cpp:510
The namespace for the gloox library.
Definition: adhoc.cpp:28
std::list< Tag * > TagList
Definition: tag.h:26
const std::string TYPE
Definition: gloox.cpp:123
@ TypeInvalid
Definition: dataform.h:44