gloox  1.0.28
dataform.cpp
1 /*
2  Copyright (c) 2005-2023 by Jakob Schröter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 #include "dataform.h"
15 #include "dataformfield.h"
16 #include "dataformitem.h"
17 #include "dataformreported.h"
18 #include "util.h"
19 #include "tag.h"
20 
21 namespace gloox
22 {
23 
24  DataForm::DataForm( FormType type, const StringList& instructions, const std::string& title )
26  m_type( type ), m_instructions( instructions ), m_title( title ), m_reported( 0 )
27  {
28  }
29 
30  DataForm::DataForm( FormType type, const std::string& title )
32  m_type( type ), m_title( title ), m_reported( 0 )
33  {
34  }
35 
36  DataForm::DataForm( const Tag* tag )
38  m_type( TypeInvalid ), m_reported( 0 )
39  {
40  parse( tag );
41  }
42 
45  m_type( form.m_type ), m_instructions( form.m_instructions ),
46  m_title( form.m_title ), m_reported( form.m_reported ? new DataFormReported( form.m_reported->tag() ) : 0 )
47  {
48  }
49 
51  {
52  util::clearList( m_items );
53  delete m_reported;
54  m_reported = NULL;
55  }
56 
57  static const char* dfTypeValues[] =
58  {
59  "form", "submit", "cancel", "result"
60  };
61 
62  bool DataForm::parse( const Tag* tag )
63  {
64  if( !tag || tag->xmlns() != XMLNS_X_DATA || tag->name() != "x" )
65  return false;
66 
67  const std::string& type = tag->findAttribute( TYPE );
68  if( type.empty() )
69  m_type = TypeForm;
70  else
71  {
72  m_type = static_cast<FormType>( util::lookup( type, dfTypeValues ) );
73  if( m_type == TypeInvalid )
74  return false;
75  }
76 
77  const TagList& l = tag->children();
78  TagList::const_iterator it = l.begin();
79  for( ; it != l.end(); ++it )
80  {
81  if( (*it)->name() == "title" )
82  m_title = (*it)->cdata();
83  else if( (*it)->name() == "instructions" )
84  m_instructions.push_back( (*it)->cdata() );
85  else if( (*it)->name() == "field" )
86  m_fields.push_back( new DataFormField( (*it) ) );
87  else if( (*it)->name() == "reported" )
88  {
89  if( m_reported == NULL )
90  m_reported = new DataFormReported( (*it) );
91  // else - Invalid data form - only one "reported" is allowed
92  }
93  else if( (*it)->name() == "item" )
94  m_items.push_back( new DataFormItem( (*it) ) );
95  }
96 
97  return true;
98  }
99 
100  const std::string& DataForm::filterString() const
101  {
102  static const std::string filter = "/message/x[@xmlns='" + XMLNS_X_DATA + "']";
103  return filter;
104  }
105 
107  {
108  if( m_type == TypeInvalid )
109  return 0;
110 
111  Tag* x = new Tag( "x" );
112  x->setXmlns( XMLNS_X_DATA );
113  x->addAttribute( TYPE, util::lookup( m_type, dfTypeValues ) );
114  if( !m_title.empty() )
115  new Tag( x, "title", m_title );
116 
117  StringList::const_iterator it_i = m_instructions.begin();
118  for( ; it_i != m_instructions.end(); ++it_i )
119  new Tag( x, "instructions", (*it_i) );
120 
121  FieldList::const_iterator it = m_fields.begin();
122  for( ; it != m_fields.end(); ++it )
123  x->addChild( (*it)->tag() );
124 
125  if( m_reported != NULL )
126  {
127  x->addChild( m_reported->tag() );
128  }
129 
130  ItemList::const_iterator iti = m_items.begin();
131  for( ; iti != m_items.end(); ++iti )
132  x->addChild( (*iti)->tag() );
133 
134  return x;
135  }
136 
137 }
A base class for Adhoc Command plugins (DataForm, IO Data, ...).
Definition: adhocplugin.h:39
An abstract base class for a XEP-0004 Data Form.
An abstraction of a single field in a XEP-0004 Data Form.
Definition: dataformfield.h:34
An abstraction of an <item> element in a XEP-0004 Data Form of type result.
Definition: dataformitem.h:32
An abstraction of a <reported> element in a XEP-0004 Data Form of type result.
virtual Tag * tag() const
An abstraction of a XEP-0004 Data Form.
Definition: dataform.h:57
bool parse(const Tag *tag)
Definition: dataform.cpp:62
virtual ~DataForm()
Definition: dataform.cpp:50
virtual const std::string & filterString() const
Definition: dataform.cpp:100
DataForm(FormType type, const StringList &instructions, const std::string &title=EmptyString)
Definition: dataform.cpp:24
FormType type() const
Definition: dataform.h:144
virtual Tag * tag() const
Definition: dataform.cpp:106
This is an abstraction of an XML element.
Definition: tag.h:47
const std::string xmlns() const
Definition: tag.cpp:543
bool addAttribute(Attribute *attr)
Definition: tag.cpp:354
void addChild(Tag *child)
Definition: tag.cpp:424
const std::string & findAttribute(const std::string &name) const
Definition: tag.cpp:589
const std::string & name() const
Definition: tag.h:394
const TagList & children() const
Definition: tag.cpp:510
bool setXmlns(const std::string &xmlns, const std::string &prefix=EmptyString)
Definition: tag.cpp:522
void clearList(std::list< T * > &L)
Definition: util.h:152
The namespace for the gloox library.
Definition: adhoc.cpp:28
std::list< Tag * > TagList
Definition: tag.h:26
std::list< std::string > StringList
Definition: gloox.h:1251
const std::string XMLNS_X_DATA
Definition: gloox.cpp:49
const std::string TYPE
Definition: gloox.cpp:123
FormType
Definition: dataform.h:34
@ TypeInvalid
Definition: dataform.h:44
@ TypeForm
Definition: dataform.h:35