Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | Related Pages

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

Generated on Sun Oct 12 16:25:15 2008 for gloox by  doxygen 1.4.1