dataform.cpp
/*
Copyright (c) 2005-2023 by Jakob Schröter <js@camaya.net>
This file is part of the gloox library. http://camaya.net/gloox
This software is distributed under a license. The full license
agreement can be found in the file LICENSE in this distribution.
This software may not be copied, modified, sold or distributed
other than expressed in the named license agreement.
This software is distributed without any warranty.
*/
#include "dataform.h"
#include "dataformfield.h"
#include "dataformitem.h"
#include "dataformreported.h"
#include "util.h"
#include "tag.h"
namespace gloox
{
DataForm::DataForm( FormType type, const StringList& instructions, const std::string& title )
: AdhocPlugin( ExtDataForm ),
m_type( type ), m_instructions( instructions ), m_title( title ), m_reported( 0 )
{
}
DataForm::DataForm( FormType type, const std::string& title )
: AdhocPlugin( ExtDataForm ),
m_type( type ), m_title( title ), m_reported( 0 )
{
}
DataForm::DataForm( const Tag* tag )
: AdhocPlugin( ExtDataForm ),
m_type( TypeInvalid ), m_reported( 0 )
{
parse( tag );
}
DataForm::DataForm( const DataForm& form )
: AdhocPlugin( ExtDataForm ), DataFormFieldContainer( form ),
m_type( form.m_type ), m_instructions( form.m_instructions ),
m_title( form.m_title ), m_reported( form.m_reported ? new DataFormReported( form.m_reported->tag() ) : 0 )
{
}
DataForm::~DataForm()
{
util::clearList( m_items );
delete m_reported;
m_reported = NULL;
}
static const char* dfTypeValues[] =
{
"form", "submit", "cancel", "result"
};
bool DataForm::parse( const Tag* tag )
{
if( !tag || tag->xmlns() != XMLNS_X_DATA || tag->name() != "x" )
return false;
const std::string& type = tag->findAttribute( TYPE );
if( type.empty() )
m_type = TypeForm;
else
{
m_type = static_cast<FormType>( util::lookup( type, dfTypeValues ) );
if( m_type == TypeInvalid )
return false;
}
const TagList& l = tag->children();
TagList::const_iterator it = l.begin();
for( ; it != l.end(); ++it )
{
if( (*it)->name() == "title" )
m_title = (*it)->cdata();
else if( (*it)->name() == "instructions" )
m_instructions.push_back( (*it)->cdata() );
else if( (*it)->name() == "field" )
m_fields.push_back( new DataFormField( (*it) ) );
else if( (*it)->name() == "reported" )
{
if( m_reported == NULL )
m_reported = new DataFormReported( (*it) );
// else - Invalid data form - only one "reported" is allowed
}
else if( (*it)->name() == "item" )
m_items.push_back( new DataFormItem( (*it) ) );
}
return true;
}
const std::string& DataForm::filterString() const
{
static const std::string filter = "/message/x[@xmlns='" + XMLNS_X_DATA + "']";
return filter;
}
Tag* DataForm::tag() const
{
if( m_type == TypeInvalid )
return 0;
Tag* x = new Tag( "x" );
x->setXmlns( XMLNS_X_DATA );
x->addAttribute( TYPE, util::lookup( m_type, dfTypeValues ) );
if( !m_title.empty() )
new Tag( x, "title", m_title );
StringList::const_iterator it_i = m_instructions.begin();
for( ; it_i != m_instructions.end(); ++it_i )
new Tag( x, "instructions", (*it_i) );
FieldList::const_iterator it = m_fields.begin();
for( ; it != m_fields.end(); ++it )
x->addChild( (*it)->tag() );
if( m_reported != NULL )
{
x->addChild( m_reported->tag() );
}
ItemList::const_iterator iti = m_items.begin();
for( ; iti != m_items.end(); ++iti )
x->addChild( (*iti)->tag() );
return x;
}
}
gloox::AdhocPlugin::AdhocPlugin
AdhocPlugin(int type)
Definition adhocplugin.h:47
gloox::DataFormFieldContainer::DataFormFieldContainer
DataFormFieldContainer()
Definition dataformfieldcontainer.cpp:21
gloox::DataFormField
An abstraction of a single field in a XEP-0004 Data Form.
Definition dataformfield.h:34
gloox::DataFormItem
An abstraction of an <item> element in a XEP-0004 Data Form of type result.
Definition dataformitem.h:32
gloox::DataFormReported
An abstraction of a <reported> element in a XEP-0004 Data Form of type result.
Definition dataformreported.h:34
gloox::DataForm::instructions
const StringList & instructions() const
Definition dataform.h:116
gloox::DataForm::title
const std::string & title() const
Definition dataform.h:103
gloox::DataForm::parse
bool parse(const Tag *tag)
Definition dataform.cpp:62
gloox::DataForm::~DataForm
virtual ~DataForm()
Definition dataform.cpp:50
gloox::DataForm::filterString
virtual const std::string & filterString() const
Definition dataform.cpp:100
gloox::DataForm::DataForm
DataForm(FormType type, const StringList &instructions, const std::string &title=EmptyString)
Definition dataform.cpp:24
gloox::DataForm::type
FormType type() const
Definition dataform.h:144
gloox::DataForm::tag
virtual Tag * tag() const
Definition dataform.cpp:106
gloox::Tag
This is an abstraction of an XML element.
Definition tag.h:47
gloox::Tag::addAttribute
bool addAttribute(Attribute *attr)
Definition tag.cpp:354
gloox::Tag::addChild
void addChild(Tag *child)
Definition tag.cpp:424
gloox::Tag::setXmlns
bool setXmlns(const std::string &xmlns, const std::string &prefix=EmptyString)
Definition tag.cpp:522
gloox::util::clearList
void clearList(std::list< T * > &L)
Definition util.h:152
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::TagList
std::list< Tag * > TagList
Definition tag.h:31
gloox::StringList
std::list< std::string > StringList
Definition gloox.h:1251
gloox::XMLNS_X_DATA
const std::string XMLNS_X_DATA
Definition gloox.cpp:49
gloox::TYPE
const std::string TYPE
Definition gloox.cpp:123
gloox::ExtDataForm
@ ExtDataForm
Definition stanzaextension.h:52
gloox::FormType
FormType
Definition dataform.h:34
gloox::TypeInvalid
@ TypeInvalid
Definition dataform.h:44
gloox::TypeForm
@ TypeForm
Definition dataform.h:35