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 || !tag->hasAttribute( "xmlns", XMLNS_X_DATA ) || 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 const Tag* DataForm::tag() const
00064 {
00065 if( m_type == FORM_TYPE_INVALID )
00066 return 0;
00067
00068 Tag *x = new Tag( "x" );
00069 x->addAttribute( "xmlns", XMLNS_X_DATA );
00070 if( !m_title.empty() )
00071 new Tag( x, "title", m_title );
00072
00073 StringList::const_iterator it_i = m_instructions.begin();
00074 for( ; it_i != m_instructions.end(); ++it_i )
00075 new Tag( x, "instructions", (*it_i) );
00076
00077 FieldList::const_iterator it = m_fields.begin();
00078 for( ; it != m_fields.end(); ++it )
00079 {
00080 x->addChild( (*it).tag() );
00081 }
00082
00083 switch( m_type )
00084 {
00085 case FORM_TYPE_FORM:
00086 x->addAttribute( "type", "form" );
00087 break;
00088 case FORM_TYPE_SUBMIT:
00089 x->addAttribute( "type", "submit" );
00090 break;
00091 case FORM_TYPE_CANCEL:
00092 x->addAttribute( "type", "cancel" );
00093 break;
00094 case FORM_TYPE_RESULT:
00095 x->addAttribute( "type", "result" );
00096 break;
00097 default:
00098 break;
00099 }
00100
00101 return x;
00102 }
00103
00104 }