00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "dataformfield.h"
00014 #include "tag.h"
00015
00016 namespace gloox
00017 {
00018
00019 DataFormField::DataFormField( DataFormFieldType type )
00020 : m_type( type ), m_required( false )
00021 {
00022 }
00023
00024 DataFormField::DataFormField( Tag *tag )
00025 : m_type( FIELD_TYPE_INVALID ), m_required( false )
00026 {
00027 if( tag->hasAttribute( "type", "boolean" ) )
00028 m_type = FIELD_TYPE_BOOLEAN;
00029 else if( tag->hasAttribute( "type", "fixed" ) )
00030 m_type = FIELD_TYPE_FIXED;
00031 else if( tag->hasAttribute( "type", "hidden" ) )
00032 m_type = FIELD_TYPE_HIDDEN;
00033 else if( tag->hasAttribute( "type", "jid-multi" ) )
00034 m_type = FIELD_TYPE_JID_MULTI;
00035 else if( tag->hasAttribute( "type", "jid-single" ) )
00036 m_type = FIELD_TYPE_JID_SINGLE;
00037 else if( tag->hasAttribute( "type", "list-multi" ) )
00038 m_type = FIELD_TYPE_LIST_MULTI;
00039 else if( tag->hasAttribute( "type", "list-single" ) )
00040 m_type = FIELD_TYPE_LIST_SINGLE;
00041 else if( tag->hasAttribute( "type", "text-multi" ) )
00042 m_type = FIELD_TYPE_TEXT_MULTI;
00043 else if( tag->hasAttribute( "type", "text-private" ) )
00044 m_type = FIELD_TYPE_TEXT_PRIVATE;
00045 else if( tag->hasAttribute( "type", "text-single" ) )
00046 m_type = FIELD_TYPE_TEXT_SINGLE;
00047
00048 Tag::TagList l = tag->children();
00049 Tag::TagList::const_iterator it = l.begin();
00050 for( ; it != l.end(); ++it )
00051 {
00052 if( (*it)->name() == "desc" )
00053 m_desc = (*it)->cdata();
00054 else if( (*it)->name() == "required" )
00055 m_required = true;
00056 else if( (*it)->name() == "value" )
00057 {
00058 if( m_type == FIELD_TYPE_TEXT_MULTI )
00059 m_values.push_back( (*it)->cdata() );
00060 else
00061 m_value = (*it)->cdata();
00062 }
00063 else if( (*it)->name() == "option" )
00064 m_options[(*it)->findAttribute( "label" )] = (*it)->findChild( "value" )->cdata();
00065 }
00066
00067 if( tag->hasAttribute( "var" ) )
00068 m_name = tag->findAttribute( "var" );
00069
00070 if( tag->hasAttribute( "label" ) )
00071 m_label = tag->findAttribute( "label" );
00072 }
00073
00074 DataFormField::~DataFormField()
00075 {
00076 }
00077
00078 Tag* DataFormField::tag() const
00079 {
00080 if( m_type == FIELD_TYPE_INVALID )
00081 return 0;
00082
00083 Tag *field = new Tag( "field" );
00084 field->addAttrib( "var", m_name );
00085 field->addAttrib( "label", m_label );
00086 if( m_required )
00087 field->addChild( new Tag( "required" ) );
00088 if( !m_desc.empty() )
00089 field->addChild( new Tag( "desc", m_desc ) );
00090
00091 switch( m_type )
00092 {
00093 case FIELD_TYPE_BOOLEAN:
00094 field->addAttrib( "type", "boolean" );
00095 break;
00096 case FIELD_TYPE_FIXED:
00097 field->addAttrib( "type", "fixed" );
00098 break;
00099 case FIELD_TYPE_HIDDEN:
00100 field->addAttrib( "type", "hidden" );
00101 break;
00102 case FIELD_TYPE_JID_MULTI:
00103 field->addAttrib( "type", "jid-multi" );
00104 break;
00105 case FIELD_TYPE_JID_SINGLE:
00106 field->addAttrib( "type", "jid-single" );
00107 break;
00108 case FIELD_TYPE_LIST_MULTI:
00109 field->addAttrib( "type", "list-multi" );
00110 break;
00111 case FIELD_TYPE_LIST_SINGLE:
00112 field->addAttrib( "type", "list-single" );
00113 break;
00114 case FIELD_TYPE_TEXT_MULTI:
00115 field->addAttrib( "type", "text-multi" );
00116 break;
00117 case FIELD_TYPE_TEXT_PRIVATE:
00118 field->addAttrib( "type", "text-private" );
00119 break;
00120 case FIELD_TYPE_TEXT_SINGLE:
00121 field->addAttrib( "type", "text-single" );
00122 break;
00123 default:
00124 break;
00125 }
00126
00127 if( ( m_type == FIELD_TYPE_LIST_SINGLE ) || ( m_type == FIELD_TYPE_LIST_MULTI ) )
00128 {
00129 StringMap::const_iterator it = m_options.begin();
00130 for( ; it != m_options.end(); ++it )
00131 {
00132 Tag *option = new Tag( field, "option" );
00133 option->addAttrib( "label", (*it).first );
00134 option->addChild( new Tag( "value", (*it).second ) );
00135 }
00136 }
00137 else if( m_type == FIELD_TYPE_BOOLEAN )
00138 {
00139 if( m_value.empty() || m_value == "false" || m_value == "0" )
00140 field->addChild( new Tag( "value", "0" ) );
00141 else
00142 field->addChild( new Tag( "value", "1" ) );
00143 }
00144 else if( m_type == FIELD_TYPE_TEXT_MULTI )
00145 {
00146 StringList::const_iterator it = m_values.begin();
00147 for( ; it != m_values.end() ; ++it )
00148 {
00149 field->addChild( new Tag( "value", (*it) ) );
00150 }
00151 }
00152 else if( !m_value.empty() )
00153 field->addChild( new Tag( "value", m_value ) );
00154
00155 return field;
00156 }
00157
00158 }