dataform.cpp

00001 /*
00002   Copyright (c) 2005-2006 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 )
00029     : m_type( type )
00030   {
00031   }
00032 
00033   DataForm::DataForm( Tag *tag )
00034     : m_type( FORM_TYPE_INVALID )
00035   {
00036     if( !tag || !tag->hasAttribute( "xmlns", XMLNS_X_DATA ) || tag->name() != "x" )
00037       return;
00038 
00039     if( tag->hasAttribute( "type", "form" ) )
00040       m_type = FORM_TYPE_FORM;
00041     else if( tag->hasAttribute( "type", "submit" ) )
00042       m_type = FORM_TYPE_SUBMIT;
00043     else if( tag->hasAttribute( "type", "cancel" ) )
00044       m_type = FORM_TYPE_CANCEL;
00045     else if( tag->hasAttribute( "type", "result" ) )
00046       m_type = FORM_TYPE_RESULT;
00047     else
00048       return;
00049 
00050     Tag::TagList l = tag->children();
00051     Tag::TagList::const_iterator it = l.begin();
00052     for( ; it != l.end(); ++it )
00053     {
00054       if( (*it)->name() == "title" )
00055         m_title = (*it)->cdata();
00056       else if( (*it)->name() == "instructions" )
00057         m_instructions.push_back( (*it)->cdata() );
00058       else if( (*it)->name() == "field" )
00059       {
00060         DataFormField *f = new DataFormField( (*it) );
00061         m_fields.push_back( f );
00062       }
00063       else if( (*it)->name() == "reported" )
00064       {
00065         DataFormReported *r = new DataFormReported( (*it) );
00066         m_fields.push_back( r );
00067       }
00068       else if( (*it)->name() == "item" )
00069       {
00070         DataFormItem *r = new DataFormItem( (*it) );
00071         m_fields.push_back( r );
00072       }
00073     }
00074   }
00075 
00076   DataForm::~DataForm()
00077   {
00078   }
00079 
00080   Tag* DataForm::tag() const
00081   {
00082     if( m_type == FORM_TYPE_INVALID )
00083       return 0;
00084 
00085     Tag *x = new Tag( "x" );
00086     x->addAttribute( "xmlns", XMLNS_X_DATA );
00087     if( !m_title.empty() )
00088       new Tag( x, "title", m_title );
00089 
00090     StringList::const_iterator it_i = m_instructions.begin();
00091     for( ; it_i != m_instructions.end(); ++it_i )
00092       new Tag( x, "instructions", (*it_i) );
00093 
00094     FieldList::const_iterator it = m_fields.begin();
00095     for( ; it != m_fields.end(); ++it )
00096     {
00097       DataFormItem *i = dynamic_cast<DataFormItem*>( (*it) );
00098       if( i )
00099       {
00100         x->addChild( i->tag() );
00101         continue;
00102       }
00103 
00104       DataFormReported *r = dynamic_cast<DataFormReported*>( (*it) );
00105       if( r )
00106       {
00107         x->addChild( r->tag() );
00108         continue;
00109       }
00110 
00111       x->addChild( (*it)->tag() );
00112     }
00113 
00114     switch( m_type )
00115     {
00116       case FORM_TYPE_FORM:
00117         x->addAttribute( "type", "form" );
00118         break;
00119       case FORM_TYPE_SUBMIT:
00120         x->addAttribute( "type", "submit" );
00121         break;
00122       case FORM_TYPE_CANCEL:
00123         x->addAttribute( "type", "cancel" );
00124         break;
00125       case FORM_TYPE_RESULT:
00126         x->addAttribute( "type", "result" );
00127         break;
00128       default:
00129         break;
00130     }
00131 
00132     return x;
00133   }
00134 
00135 }

Generated on Tue May 1 14:20:20 2007 for gloox by  doxygen 1.5.1