dataformfield.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "dataformfield.h"
00014 #include "util.h"
00015 #include "tag.h"
00016
00017 namespace gloox
00018 {
00019
00020 static const char* fieldTypeValues[] =
00021 {
00022 "boolean", "fixed", "hidden", "jid-multi", "jid-single",
00023 "list-multi", "list-single", "text-multi", "text-private", "text-single", ""
00024 };
00025
00026 DataFormField::DataFormField( FieldType type )
00027 : m_type( type ), m_required( false )
00028 {
00029 }
00030
00031 DataFormField::DataFormField( const std::string& name, const std::string& value,
00032 const std::string& label, FieldType type )
00033 : m_type( type ), m_name( name ), m_label( label ), m_required( false )
00034 {
00035 m_values.push_back( value );
00036 }
00037
00038 DataFormField::DataFormField( const Tag* tag )
00039 : m_type( TypeInvalid ), m_required( false )
00040 {
00041 if( !tag )
00042 return;
00043
00044 const std::string& type = tag->findAttribute( TYPE );
00045 if( type.empty() )
00046 {
00047 if( !tag->name().empty() )
00048 m_type = TypeNone;
00049 }
00050 else
00051 m_type = (FieldType)util::lookup( type, fieldTypeValues );
00052
00053 if( tag->hasAttribute( "var" ) )
00054 m_name = tag->findAttribute( "var" );
00055
00056 if( tag->hasAttribute( "label" ) )
00057 m_label = tag->findAttribute( "label" );
00058
00059 const TagList& l = tag->children();
00060 TagList::const_iterator it = l.begin();
00061 for( ; it != l.end(); ++it )
00062 {
00063 if( (*it)->name() == "desc" )
00064 m_desc = (*it)->cdata();
00065 else if( (*it)->name() == "required" )
00066 m_required = true;
00067 else if( (*it)->name() == "value" )
00068 {
00069 if( m_type == TypeTextMulti || m_type == TypeListMulti || m_type == TypeJidMulti )
00070 addValue( (*it)->cdata() );
00071 else
00072 setValue( (*it)->cdata() );
00073 }
00074 else if( (*it)->name() == "option" )
00075 {
00076 Tag* v = (*it)->findChild( "value" );
00077 if( v )
00078 m_options.insert( std::make_pair( (*it)->findAttribute( "label" ), v->cdata() ) );
00079 }
00080 }
00081
00082 }
00083
00084 DataFormField::~DataFormField()
00085 {
00086 }
00087
00088 Tag* DataFormField::tag() const
00089 {
00090 if( m_type == TypeInvalid )
00091 return 0;
00092
00093 Tag* field = new Tag( "field" );
00094 field->addAttribute( TYPE, util::lookup( m_type, fieldTypeValues ) );
00095 field->addAttribute( "var", m_name );
00096 field->addAttribute( "label", m_label );
00097 if( m_required )
00098 new Tag( field, "required" );
00099
00100 if( !m_desc.empty() )
00101 new Tag( field, "desc", m_desc );
00102
00103 if( m_type == TypeListSingle || m_type == TypeListMulti )
00104 {
00105 StringMultiMap::const_iterator it = m_options.begin();
00106 for( ; it != m_options.end(); ++it )
00107 {
00108 Tag* option = new Tag( field, "option", "label", (*it).first );
00109 new Tag( option, "value", (*it).second );
00110 }
00111 }
00112 else if( m_type == TypeBoolean )
00113 {
00114 if( m_values.size() == 0 || m_values.front() == "false" || m_values.front() == "0" )
00115 new Tag( field, "value", "0" );
00116 else
00117 new Tag( field, "value", "1" );
00118 }
00119
00120 if( m_type == TypeTextMulti || m_type == TypeListMulti || m_type == TypeJidMulti )
00121 {
00122 StringList::const_iterator it = m_values.begin();
00123 for( ; it != m_values.end() ; ++it )
00124 new Tag( field, "value", (*it) );
00125 }
00126
00127 if( m_values.size() && !( m_type == TypeTextMulti || m_type == TypeListMulti
00128 || m_type == TypeBoolean || m_type == TypeJidMulti ) )
00129 new Tag( field, "value", m_values.front() );
00130
00131 return field;
00132 }
00133
00134 }