gloox  1.0.28
iodata.cpp
1 /*
2  Copyright (c) 2015-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 "iodata.h"
15 
16 #include "util.h"
17 
18 namespace gloox
19 {
20 
21  static const char* ioTypes[] = {
22  "io-schemata-get",
23  "input",
24  "getStatus",
25  "getOutput",
26  "io-schemata-result",
27  "output",
28  "error",
29  "status"
30  };
31 
32  static inline IOData::Type ioType( const std::string& type )
33  {
34  return static_cast<IOData::Type>( util::lookup( type, ioTypes ) );
35  }
36 
39  m_in( 0 ), m_out( 0 ), m_error( 0 ),
40  m_type( type )
41  {
42  m_status.elapsed = -1;
43  m_status.remaining = -1;
44  m_status.percentage = -1;
45  }
46 
47  IOData::IOData( const Tag* tag )
49  m_in( 0 ), m_out( 0 ), m_error( 0 ),
50  m_type( TypeInvalid )
51  {
52  if( !tag || !( tag->name() == "iodata" && tag->hasAttribute( XMLNS, XMLNS_IODATA ) ) )
53  return;
54 
55  m_status.elapsed = -1;
56  m_status.remaining = -1;
57  m_status.percentage = -1;
58 
59  m_type = ioType( tag->findAttribute( "type" ) );
60  Tag* m = 0;
61  switch( m_type )
62  {
63  case TypeInput:
64  m = tag->findChild( "in" );
65  if( m )
66  m_in = m->clone();
67  break;
69  m = tag->findChild( "desc" );
70  if( m )
71  m_desc = m->cdata();
72 
73  m = tag->findChild( "out" );
74  if( m )
75  m_out = m->clone();
76 
77  m = tag->findChild( "in" );
78  if( m )
79  m_in = m->clone();
80  break;
81  case TypeOutput:
82  m = tag->findChild( "out" );
83  if( m )
84  m_out = m->clone();
85  break;
86  case TypeError:
87  m = tag->findChild( "error" );
88  if( m )
89  m_error = m->clone();
90  break;
91  case TypeStatus:
92  m = tag->findChild( "status" );
93  if( m )
94  {
95  Tag* t = m->findChild( "elapsed" );
96  if( t )
97  m_status.elapsed = atoi( t->cdata().c_str() );
98 
99  t = m->findChild( "remaining" );
100  if( t )
101  m_status.remaining = atoi( t->cdata().c_str() );
102 
103  t = m->findChild( "percentage" );
104  if( t )
105  m_status.percentage = atoi( t->cdata().c_str() );
106 
107  t = m->findChild( "information" );
108  if( t )
109  m_status.info = t->cdata();
110  }
111  break;
112  case TypeIoSchemataGet:
113  case TypeGetStatus:
114  case TypeGetOutput:
115  default:
116  break;
117  }
118 
119  }
120 
122  {
123  delete m_in;
124  delete m_out;
125  delete m_error;
126  }
127 
128  Tag* IOData::tag() const
129  {
130  if( m_type == TypeInvalid )
131  return 0;
132 
133  Tag* i = new Tag( "iodata" );
134  i->setXmlns( XMLNS_IODATA );
135  i->addAttribute( "type", util::lookup( m_type, ioTypes ) );
136 
137  Tag* t = 0;
138  switch( m_type )
139  {
140  case TypeInput:
141  i->addChild( m_in );
142  break;
144  i->addChild( m_in );
145  i->addChild( m_out );
146  new Tag( i, "desc", m_desc );
147  break;
148  case TypeOutput:
149  i->addChild( m_out );
150  break;
151  case TypeError:
152  i->addChild( m_error );
153  break;
154  case TypeStatus:
155  t = new Tag( i, "status" );
156  if( m_status.elapsed >= 0 )
157  new Tag( t, "elapsed", util::int2string( m_status.elapsed ) );
158  if( m_status.remaining >= 0 )
159  new Tag( t, "remaining", util::int2string( m_status.remaining ) );
160  if( m_status.percentage >= 0 )
161  new Tag( t, "percentage", util::int2string( m_status.percentage ) );
162  if( m_status.info.length() )
163  new Tag( t, "information", m_status.info );
164  break;
165  case TypeIoSchemataGet:
166  case TypeGetStatus:
167  case TypeGetOutput:
168  default:
169  break;
170  }
171 
172  return i;
173  }
174 
176  {
177  IOData* i = new IOData( m_type );
178  i->m_status = m_status;
179  i->m_desc = m_desc;
180 
181  if( m_in )
182  i->m_in = m_in->clone();
183  if( m_out )
184  i->m_out = m_out->clone();
185  if( m_error )
186  i->m_error = m_error->clone();
187 
188  return i;
189  }
190 
191  void IOData::setIn( Tag* in )
192  {
193  if( !in )
194  return;
195 
196  delete m_in;
197 
198  if( in->name() == "in" && in->xmlns() == EmptyString )
199  m_in = in;
200  else
201  {
202  m_in = new Tag( "in" );
203  m_in->addChild( in );
204  }
205  }
206 
207  void IOData::setOut( Tag* out )
208  {
209  if( !out )
210  return;
211 
212  delete m_out;
213 
214  if( out->name() == "out" && out->xmlns() == EmptyString )
215  m_out = out;
216  else
217  {
218  m_out = new Tag( "out" );
219  m_out->addChild( out );
220  }
221  }
222 
223  void IOData::setError( Tag* error )
224  {
225  if( !error )
226  return;
227 
228  delete m_error;
229 
230  if( error->name() == "error" && error->xmlns() == EmptyString )
231  m_error = error;
232  else
233  {
234  m_error = new Tag( "error" );
235  m_error->addChild( error );
236  }
237  }
238 
239 
240 }
241 
A base class for Adhoc Command plugins (DataForm, IO Data, ...).
Definition: adhocplugin.h:39
This is an abstraction of the IO Data specification XEP-0244.
Definition: iodata.h:37
@ TypeStatus
Definition: iodata.h:51
@ TypeGetStatus
Definition: iodata.h:46
@ TypeInvalid
Definition: iodata.h:52
@ TypeOutput
Definition: iodata.h:49
@ TypeIoSchemataResult
Definition: iodata.h:48
@ TypeGetOutput
Definition: iodata.h:47
IOData(Type type)
Definition: iodata.cpp:37
virtual ~IOData()
Definition: iodata.cpp:121
const Tag * error() const
Definition: iodata.h:125
void setError(Tag *error)
Definition: iodata.cpp:223
const Tag * in() const
Definition: iodata.h:93
void setOut(Tag *out)
Definition: iodata.cpp:207
virtual IOData * clone() const
Definition: iodata.cpp:175
const Tag * out() const
Definition: iodata.h:109
void setIn(Tag *in)
Definition: iodata.cpp:191
virtual Tag * tag() const
Definition: iodata.cpp:128
This is an abstraction of an XML element.
Definition: tag.h:47
Tag * findChild(const std::string &name) const
Definition: tag.cpp:624
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
bool hasAttribute(const std::string &name, const std::string &value=EmptyString) const
Definition: tag.cpp:602
const std::string cdata() const
Definition: tag.cpp:497
const std::string & findAttribute(const std::string &name) const
Definition: tag.cpp:589
const std::string & name() const
Definition: tag.h:394
Tag * clone() const
Definition: tag.cpp:670
bool setXmlns(const std::string &xmlns, const std::string &prefix=EmptyString)
Definition: tag.cpp:522
The namespace for the gloox library.
Definition: adhoc.cpp:28
const std::string EmptyString
Definition: gloox.cpp:124
const std::string XMLNS
Definition: gloox.cpp:122
const std::string XMLNS_IODATA
Definition: gloox.cpp:114
@ TypeInvalid
Definition: dataform.h:44