dataformfield.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 #include "dataformfield.h"
00014 #include "tag.h"
00015 
00016 namespace gloox
00017 {
00018 
00019   DataFormField::DataFormField( DataFormFieldType type )
00020     : m_type( type ), m_required( false )
00021   {
00022   }
00023 
00024   DataFormField::DataFormField( Tag *tag )
00025     : m_type( FIELD_TYPE_INVALID ), m_required( false )
00026   {
00027     if( !tag )
00028       return;
00029 
00030     if( tag->hasAttribute( "type", "boolean" ) )
00031       m_type = FIELD_TYPE_BOOLEAN;
00032     else if( tag->hasAttribute( "type", "fixed" ) )
00033       m_type = FIELD_TYPE_FIXED;
00034     else if( tag->hasAttribute( "type", "hidden" ) )
00035       m_type = FIELD_TYPE_HIDDEN;
00036     else if( tag->hasAttribute( "type", "jid-multi" ) )
00037       m_type = FIELD_TYPE_JID_MULTI;
00038     else if( tag->hasAttribute( "type", "jid-single" ) )
00039       m_type = FIELD_TYPE_JID_SINGLE;
00040     else if( tag->hasAttribute( "type", "list-multi" ) )
00041       m_type = FIELD_TYPE_LIST_MULTI;
00042     else if( tag->hasAttribute( "type", "list-single" ) )
00043       m_type = FIELD_TYPE_LIST_SINGLE;
00044     else if( tag->hasAttribute( "type", "text-multi" ) )
00045       m_type = FIELD_TYPE_TEXT_MULTI;
00046     else if( tag->hasAttribute( "type", "text-private" ) )
00047       m_type = FIELD_TYPE_TEXT_PRIVATE;
00048     else if( tag->hasAttribute( "type", "text-single" ) )
00049       m_type = FIELD_TYPE_TEXT_SINGLE;
00050 
00051     if( tag->hasAttribute( "var" ) )
00052       m_name = tag->findAttribute( "var" );
00053 
00054     if( tag->hasAttribute( "label" ) )
00055       m_label = tag->findAttribute( "label" );
00056 
00057     Tag::TagList l = tag->children();
00058     Tag::TagList::const_iterator it = l.begin();
00059     for( ; it != l.end(); ++it )
00060     {
00061       if( (*it)->name() == "desc" )
00062         m_desc = (*it)->cdata();
00063       else if( (*it)->name() == "required" )
00064         m_required = true;
00065       else if( (*it)->name() == "value" )
00066       {
00067         if( m_type == FIELD_TYPE_TEXT_MULTI || m_type == FIELD_TYPE_LIST_MULTI )
00068           m_values.push_back( (*it)->cdata() );
00069         else
00070           m_value = (*it)->cdata();
00071       }
00072       else if( (*it)->name() == "option" )
00073       {
00074         Tag *v = (*it)->findChild( "value" );
00075         if( v )
00076           m_options[(*it)->findAttribute( "label" )] = v->cdata();
00077       }
00078     }
00079 
00080   }
00081 
00082   DataFormField::~DataFormField()
00083   {
00084   }
00085 
00086   Tag* DataFormField::tag() const
00087   {
00088     if( m_type == FIELD_TYPE_INVALID )
00089       return 0;
00090 
00091     Tag *field = new Tag( "field" );
00092     field->addAttribute( "var", m_name );
00093     field->addAttribute( "label", m_label );
00094     if( m_required )
00095       new Tag( field, "required" );
00096 
00097     if( !m_desc.empty() )
00098       new Tag( field, "desc", m_desc );
00099 
00100     switch( m_type )
00101     {
00102       case FIELD_TYPE_BOOLEAN:
00103         field->addAttribute( "type", "boolean" );
00104         break;
00105       case FIELD_TYPE_FIXED:
00106         field->addAttribute( "type", "fixed" );
00107         break;
00108       case FIELD_TYPE_HIDDEN:
00109         field->addAttribute( "type", "hidden" );
00110         break;
00111       case FIELD_TYPE_JID_MULTI:
00112         field->addAttribute( "type", "jid-multi" );
00113         break;
00114       case FIELD_TYPE_JID_SINGLE:
00115         field->addAttribute( "type", "jid-single" );
00116         break;
00117       case FIELD_TYPE_LIST_MULTI:
00118         field->addAttribute( "type", "list-multi" );
00119         break;
00120       case FIELD_TYPE_LIST_SINGLE:
00121         field->addAttribute( "type", "list-single" );
00122         break;
00123       case FIELD_TYPE_TEXT_MULTI:
00124         field->addAttribute( "type", "text-multi" );
00125         break;
00126       case FIELD_TYPE_TEXT_PRIVATE:
00127         field->addAttribute( "type", "text-private" );
00128         break;
00129       case FIELD_TYPE_TEXT_SINGLE:
00130         field->addAttribute( "type", "text-single" );
00131         break;
00132       default:
00133         break;
00134     }
00135 
00136     if( m_type == FIELD_TYPE_LIST_SINGLE || m_type == FIELD_TYPE_LIST_MULTI )
00137     {
00138       StringMap::const_iterator it = m_options.begin();
00139       for( ; it != m_options.end(); ++it )
00140       {
00141         Tag *option = new Tag( field, "option" );
00142         option->addAttribute( "label", (*it).first );
00143         new Tag( option, "value", (*it).second );
00144       }
00145     }
00146     else if( m_type == FIELD_TYPE_BOOLEAN )
00147     {
00148       if( m_value.empty() || m_value == "false" || m_value == "0" )
00149         new Tag( field, "value", "0" );
00150       else
00151         new Tag( field, "value", "1" );
00152     }
00153     
00154     if( m_type == FIELD_TYPE_TEXT_MULTI || m_type == FIELD_TYPE_LIST_MULTI )
00155     {
00156       StringList::const_iterator it = m_values.begin();
00157       for( ; it != m_values.end() ; ++it )
00158         new Tag( field, "value", (*it) );
00159     }
00160 
00161     if( !m_value.empty() )
00162       new Tag( field, "value", m_value );
00163 
00164     return field;
00165   }
00166 
00167 }

Generated on Sun Sep 24 21:57:31 2006 for gloox by  doxygen 1.4.7