dataform.cpp
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 ), m_reported( 0 )
00027 {
00028 }
00029
00030 DataForm::DataForm( FormType type, const std::string& title )
00031 : StanzaExtension( ExtDataForm ),
00032 m_type( type ), m_title( title ), m_reported( 0 )
00033 {
00034 }
00035
00036 DataForm::DataForm( const Tag* tag )
00037 : StanzaExtension( ExtDataForm ),
00038 m_type( TypeInvalid ), m_reported( 0 )
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 ), m_reported( form.m_reported ? new DataFormReported( form.m_reported->tag() ) : 0 )
00047 {
00048 }
00049
00050 DataForm::~DataForm()
00051 {
00052 util::clearList( m_items );
00053 delete m_reported;
00054 m_reported = NULL;
00055 }
00056
00057 static const char* dfTypeValues[] =
00058 {
00059 "form", "submit", "cancel", "result"
00060 };
00061
00062 bool DataForm::parse( const Tag* tag )
00063 {
00064 if( !tag || tag->xmlns() != XMLNS_X_DATA || tag->name() != "x" )
00065 return false;
00066
00067 const std::string& type = tag->findAttribute( TYPE );
00068 if( type.empty() )
00069 m_type = TypeForm;
00070 else
00071 {
00072 m_type = (FormType)util::lookup( type, dfTypeValues );
00073 if( m_type == TypeInvalid )
00074 return false;
00075 }
00076
00077 const TagList& l = tag->children();
00078 TagList::const_iterator it = l.begin();
00079 for( ; it != l.end(); ++it )
00080 {
00081 if( (*it)->name() == "title" )
00082 m_title = (*it)->cdata();
00083 else if( (*it)->name() == "instructions" )
00084 m_instructions.push_back( (*it)->cdata() );
00085 else if( (*it)->name() == "field" )
00086 m_fields.push_back( new DataFormField( (*it) ) );
00087 else if( (*it)->name() == "reported" )
00088 {
00089 if( m_reported == NULL )
00090 m_reported = new DataFormReported( (*it) );
00091
00092 }
00093 else if( (*it)->name() == "item" )
00094 m_items.push_back( new DataFormItem( (*it) ) );
00095 }
00096
00097 return true;
00098 }
00099
00100 const std::string& DataForm::filterString() const
00101 {
00102 static const std::string filter = "/message/x[@xmlns='" + XMLNS_X_DATA + "']";
00103 return filter;
00104 }
00105
00106 Tag* DataForm::tag() const
00107 {
00108 if( m_type == TypeInvalid )
00109 return 0;
00110
00111 Tag* x = new Tag( "x" );
00112 x->setXmlns( XMLNS_X_DATA );
00113 x->addAttribute( TYPE, util::lookup( m_type, dfTypeValues ) );
00114 if( !m_title.empty() )
00115 new Tag( x, "title", m_title );
00116
00117 StringList::const_iterator it_i = m_instructions.begin();
00118 for( ; it_i != m_instructions.end(); ++it_i )
00119 new Tag( x, "instructions", (*it_i) );
00120
00121 FieldList::const_iterator it = m_fields.begin();
00122 for( ; it != m_fields.end(); ++it )
00123 x->addChild( (*it)->tag() );
00124
00125 if( m_reported != NULL )
00126 {
00127 x->addChild( m_reported->tag() );
00128 }
00129
00130 ItemList::const_iterator iti = m_items.begin();
00131 for( ; iti != m_items.end(); ++iti )
00132 x->addChild( (*iti)->tag() );
00133
00134 return x;
00135 }
00136
00137 }