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

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