dataform.cpp

00001 /*
00002   Copyright (c) 2005-2008 by Jakob Schroeter <js@camaya.net>
00003   This file is part of the gloox library. http://camaya.net/gloox
00004 
00005   This software is distributed under a license. The full license
00006   agreement can be found in the file LICENSE in this distribution.
00007   This software may not be copied, modified, sold or distributed
00008   other than expressed in the named license agreement.
00009 
00010   This software is distributed without any warranty.
00011 */
00012 
00013 
00014 #include "dataform.h"
00015 #include "dataformfield.h"
00016 #include "dataformreported.h"
00017 #include "dataformitem.h"
00018 #include "tag.h"
00019 
00020 namespace gloox
00021 {
00022 
00023   DataForm::DataForm( DataFormType type, const StringList& instructions, const std::string& title )
00024     : m_instructions( instructions ), m_type( type ), m_title( title )
00025   {
00026   }
00027 
00028   DataForm::DataForm( DataFormType type, const std::string& title )
00029     : m_type( type ), m_title( title )
00030   {
00031   }
00032 
00033   DataForm::DataForm( Tag *tag )
00034     : m_type( FormTypeInvalid )
00035   {
00036     parse( tag );
00037   }
00038 
00039   DataForm::DataForm()
00040   : m_type( FormTypeInvalid )
00041   {
00042   }
00043 
00044   DataForm::~DataForm()
00045   {
00046   }
00047 
00048   bool DataForm::parse( Tag *tag )
00049   {
00050     if( !tag || !tag->hasAttribute( "xmlns", XMLNS_X_DATA ) || tag->name() != "x" )
00051       return false;
00052 
00053     if( tag->hasAttribute( "type", "form" ) )
00054       m_type = FormTypeForm;
00055     else if( tag->hasAttribute( "type", "submit" ) )
00056       m_type = FormTypeSubmit;
00057     else if( tag->hasAttribute( "type", "cancel" ) )
00058       m_type = FormTypeCancel;
00059     else if( tag->hasAttribute( "type", "result" ) )
00060       m_type = FormTypeResult;
00061     else
00062       return false;
00063 
00064     const Tag::TagList& l = tag->children();
00065     Tag::TagList::const_iterator it = l.begin();
00066     for( ; it != l.end(); ++it )
00067     {
00068       if( (*it)->name() == "title" )
00069         m_title = (*it)->cdata();
00070       else if( (*it)->name() == "instructions" )
00071         m_instructions.push_back( (*it)->cdata() );
00072       else if( (*it)->name() == "field" )
00073       {
00074         DataFormField *f = new DataFormField( (*it) );
00075         m_fields.push_back( f );
00076       }
00077       else if( (*it)->name() == "reported" )
00078       {
00079         DataFormReported *r = new DataFormReported( (*it) );
00080         m_fields.push_back( r );
00081       }
00082       else if( (*it)->name() == "item" )
00083       {
00084         DataFormItem *i = new DataFormItem( (*it) );
00085         m_fields.push_back( i );
00086       }
00087     }
00088 
00089     return true;
00090   }
00091 
00092   Tag* DataForm::tag() const
00093   {
00094     if( m_type == FormTypeInvalid )
00095       return 0;
00096 
00097     Tag *x = new Tag( "x" );
00098     x->addAttribute( "xmlns", XMLNS_X_DATA );
00099     if( !m_title.empty() )
00100       new Tag( x, "title", m_title );
00101 
00102     StringList::const_iterator it_i = m_instructions.begin();
00103     for( ; it_i != m_instructions.end(); ++it_i )
00104       new Tag( x, "instructions", (*it_i) );
00105 
00106     FieldList::const_iterator it = m_fields.begin();
00107     for( ; it != m_fields.end(); ++it )
00108     {
00109       DataFormItem *i = dynamic_cast<DataFormItem*>( (*it) );
00110       if( i )
00111       {
00112         x->addChild( i->tag() );
00113         continue;
00114       }
00115 
00116       DataFormReported *r = dynamic_cast<DataFormReported*>( (*it) );
00117       if( r )
00118       {
00119         x->addChild( r->tag() );
00120         continue;
00121       }
00122 
00123       x->addChild( (*it)->tag() );
00124     }
00125 
00126     switch( m_type )
00127     {
00128       case FormTypeForm:
00129         x->addAttribute( "type", "form" );
00130         break;
00131       case FormTypeSubmit:
00132         x->addAttribute( "type", "submit" );
00133         break;
00134       case FormTypeCancel:
00135         x->addAttribute( "type", "cancel" );
00136         break;
00137       case FormTypeResult:
00138         x->addAttribute( "type", "result" );
00139         break;
00140       default:
00141         break;
00142     }
00143 
00144     return x;
00145   }
00146 
00147 }

Generated on Sun Apr 27 11:08:13 2008 for gloox by  doxygen 1.5.5