1
/****************************************************************************
5
** Copyright © 2012 Ruslan Nigmatullin <euroelessar@yandex.ru>
7
*****************************************************************************
9
** $JREEN_BEGIN_LICENSE$
10
** This program is free software: you can redistribute it and/or modify
11
** it under the terms of the GNU General Public License as published by
12
** the Free Software Foundation, either version 2 of the License, or
13
** (at your option) any later version.
15
** This program is distributed in the hope that it will be useful,
16
** but WITHOUT ANY WARRANTY; without even the implied warranty of
17
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18
** See the GNU General Public License for more details.
20
** You should have received a copy of the GNU General Public License
21
** along with this program. If not, see http://www.gnu.org/licenses/.
22
** $JREEN_END_LICENSE$
24
****************************************************************************/
26
#include "registrationqueryfactory_p.h"
29
#define NS_REGISTER QLatin1String("jabber:iq:register")
34
const char *emptyNames[] = {
39
const char *valueNames[] = {
60
RegistrationQueryFactory::RegistrationQueryFactory() : m_state(Nowhere), m_depth(0)
64
QStringList RegistrationQueryFactory::features() const
66
return QStringList(); // << NS_REGISTER;
69
bool RegistrationQueryFactory::canParse(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes)
74
m_query.reset(new RegistrationQuery);
75
return name == QLatin1String("query") && uri == NS_REGISTER;
78
void RegistrationQueryFactory::handleStartElement(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes)
82
if (m_formFactory.canParse(name, uri, attributes)) {
84
} else if (m_bobFactory.canParse(name, uri, attributes)) {
86
} else if (name == QLatin1String("instructions")) {
87
m_state = AtInstructions;
89
RegistrationDataPrivate::Flag flag = strToFlag<RegistrationDataPrivate::Flag>(name, emptyNames);
91
m_query->data->flags |= flag;
94
int index = strToEnum(name, valueNames);
96
m_query->data->valuesFlags |= (1 << index);
97
m_state = static_cast<State>(AtValue + index);
102
if (m_state == AtForm)
103
m_formFactory.handleStartElement(name, uri, attributes);
104
else if (m_state == AtBob)
105
m_bobFactory.handleStartElement(name, uri, attributes);
108
void RegistrationQueryFactory::handleEndElement(const QStringRef &name, const QStringRef &uri)
110
if (m_state == AtForm)
111
m_formFactory.handleEndElement(name, uri);
112
else if (m_state == AtBob)
113
m_bobFactory.handleEndElement(name, uri);
115
if (m_state == AtForm)
116
m_query->data->form = m_formFactory.createPayload().staticCast<DataForm>();
117
else if (m_state == AtBob)
118
m_query->data->bobs << m_bobFactory.createPayload().staticCast<BitsOfBinary>();
124
void RegistrationQueryFactory::handleCharacterData(const QStringRef &text)
126
if (m_state == AtForm)
127
m_formFactory.handleCharacterData(text);
128
else if (m_state == AtBob)
129
m_bobFactory.handleCharacterData(text);
130
else if (m_state == AtInstructions)
131
m_query->data->instructions = text.toString();
132
else if (m_state >= AtValue)
133
m_query->data->values[m_state - AtValue] = text.toString();
136
void RegistrationQueryFactory::serialize(Payload *extension, QXmlStreamWriter *writer)
138
RegistrationQuery *query = se_cast<RegistrationQuery*>(extension);
139
writer->writeStartElement(QLatin1String("query"));
140
writer->writeDefaultNamespace(NS_REGISTER);
141
for (uint i = 0; i < sizeof(emptyNames) / sizeof(emptyNames[0]); ++i) {
142
if (query->data->flags & (1 << i))
143
writer->writeEmptyElement(QLatin1String(emptyNames[i]));
145
for (int i = 0; i < query->data->values.size(); ++i) {
146
if (query->data->valuesFlags & (1 << i))
147
writer->writeTextElement(QLatin1String(valueNames[i]), query->data->values[i]);
149
if (query->data->form)
150
m_formFactory.serialize(query->data->form.data(), writer);
151
writer->writeEndElement();
154
Payload::Ptr RegistrationQueryFactory::createPayload()
156
return Payload::Ptr(m_query.take());