00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "dataform.h"
00015 #include "dataformfield.h"
00016 #include "dataformitem.h"
00017 #include "dataformreported.h"
00018 #include "util.h"
00019 #include "tag.h"
00020
00021 namespace gloox
00022 {
00023
00024 DataForm::DataForm( FormType type, const StringList& instructions, const std::string& title )
00025 : StanzaExtension( ExtDataForm ),
00026 m_type( type ), m_instructions( instructions ), m_title( title )
00027 {
00028 }
00029
00030 DataForm::DataForm( FormType type, const std::string& title )
00031 : StanzaExtension( ExtDataForm ),
00032 m_type( type ), m_title( title )
00033 {
00034 }
00035
00036 DataForm::DataForm( const Tag* tag )
00037 : StanzaExtension( ExtDataForm ),
00038 m_type( TypeInvalid )
00039 {
00040 parse( tag );
00041 }
00042
00043 DataForm::DataForm( const DataForm& form )
00044 : StanzaExtension( ExtDataForm ), DataFormFieldContainer( form ),
00045 m_type( form.m_type ), m_instructions( form.m_instructions ),
00046 m_title( form.m_title )
00047 {
00048 }
00049
00050 DataForm::~DataForm()
00051 {
00052 }
00053
00054 static const char* dfTypeValues[] =
00055 {
00056 "form", "submit", "cancel", "result"
00057 };
00058
00059 bool DataForm::parse( const Tag* tag )
00060 {
00061 if( !tag || tag->xmlns() != XMLNS_X_DATA || tag->name() != "x" )
00062 return false;
00063
00064 m_type = (FormType)util::lookup(tag->findAttribute( TYPE ), dfTypeValues );
00065 if( m_type == TypeInvalid )
00066 return false;
00067
00068 const TagList& l = tag->children();
00069 TagList::const_iterator it = l.begin();
00070 for( ; it != l.end(); ++it )
00071 {
00072 if( (*it)->name() == "title" )
00073 m_title = (*it)->cdata();
00074 else if( (*it)->name() == "instructions" )
00075 m_instructions.push_back( (*it)->cdata() );
00076 else if( (*it)->name() == "field" )
00077 m_fields.push_back( new DataFormField( (*it) ) );
00078 }
00079
00080 return true;
00081 }
00082
00083 const std::string& DataForm::filterString() const
00084 {
00085 static const std::string filter = "/message/x[@xmlns='" + XMLNS_X_DATA + "']";
00086 return filter;
00087 }
00088
00089 Tag* DataForm::tag() const
00090 {
00091 if( m_type == TypeInvalid )
00092 return 0;
00093
00094 Tag* x = new Tag( "x" );
00095 x->setXmlns( XMLNS_X_DATA );
00096 x->addAttribute( TYPE, util::lookup( m_type, dfTypeValues ) );
00097 if( !m_title.empty() )
00098 new Tag( x, "title", m_title );
00099
00100 StringList::const_iterator it_i = m_instructions.begin();
00101 for( ; it_i != m_instructions.end(); ++it_i )
00102 new Tag( x, "instructions", (*it_i) );
00103
00104 FieldList::const_iterator it = m_fields.begin();
00105 for( ; it != m_fields.end(); ++it )
00106 x->addChild( (*it)->tag() );
00107
00108 return x;
00109 }
00110
00111 }