00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "dataform.h"
00015 #include "dataformfield.h"
00016 #include "tag.h"
00017
00018 namespace gloox
00019 {
00020
00021 DataForm::DataForm( DataFormType type, const StringList& instructions, const std::string& title )
00022 : m_instructions( instructions ), m_type( type ), m_title( title )
00023 {
00024 }
00025
00026 DataForm::DataForm( Tag *tag )
00027 : m_type( FORM_TYPE_INVALID )
00028 {
00029 if( !tag->hasAttribute( "xmlns", XMLNS_DATA_FORMS ) || tag->name() != "x" )
00030 return;
00031
00032 if( tag->hasAttribute( "type", "form" ) )
00033 m_type = FORM_TYPE_FORM;
00034 else if( tag->hasAttribute( "type", "submit" ) )
00035 m_type = FORM_TYPE_SUBMIT;
00036 else if( tag->hasAttribute( "type", "cancel" ) )
00037 m_type = FORM_TYPE_CANCEL;
00038 else if( tag->hasAttribute( "type", "result" ) )
00039 m_type = FORM_TYPE_RESULT;
00040 else
00041 return;
00042
00043 Tag::TagList l = tag->children();
00044 Tag::TagList::const_iterator it = l.begin();
00045 for( ; it != l.end(); ++it )
00046 {
00047 if( (*it)->name() == "title" )
00048 m_title = (*it)->cdata();
00049 else if( (*it)->name() == "instructions" )
00050 m_instructions.push_back( (*it)->cdata() );
00051 else if( (*it)->name() == "field" )
00052 {
00053 DataFormField f( (*it) );
00054 m_fields.push_back( f );
00055 }
00056 }
00057 }
00058
00059 DataForm::~DataForm()
00060 {
00061 }
00062
00063 Tag* DataForm::tag()
00064 {
00065 if( m_type == FORM_TYPE_INVALID )
00066 return 0;
00067
00068 Tag *x = new Tag( "x" );
00069 x->addAttrib( "xmlns", XMLNS_DATA_FORMS );
00070 if( !m_title.empty() )
00071 x->addChild( new Tag( "title", m_title ) );
00072
00073 StringList::const_iterator it_i = m_instructions.begin();
00074 for( ; it_i != m_instructions.end(); ++it_i )
00075 {
00076 x->addChild( new Tag( "instructions", (*it_i) ) );
00077 }
00078
00079 FieldList::const_iterator it = m_fields.begin();
00080 for( ; it != m_fields.end(); ++it )
00081 {
00082 x->addChild( (*it).tag() );
00083 }
00084
00085 switch( m_type )
00086 {
00087 case FORM_TYPE_FORM:
00088 x->addAttrib( "type", "form" );
00089 break;
00090 case FORM_TYPE_SUBMIT:
00091 x->addAttrib( "type", "submit" );
00092 break;
00093 case FORM_TYPE_CANCEL:
00094 x->addAttrib( "type", "cancel" );
00095 break;
00096 case FORM_TYPE_RESULT:
00097 x->addAttrib( "type", "result" );
00098 break;
00099 default:
00100 break;
00101 }
00102
00103 return x;
00104 }
00105
00106 }