~ubuntu-branches/ubuntu/trusty/jreen/trusty

« back to all changes in this revision

Viewing changes to src/registrationqueryfactory.cpp

  • Committer: Package Import Robot
  • Author(s): Prasad Murthy
  • Date: 2013-03-08 00:00:33 UTC
  • Revision ID: package-import@ubuntu.com-20130308000033-x8thp6syo1kkh63s
Tags: upstream-1.1.1
Import upstream version 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Jreen
 
4
**
 
5
** Copyright © 2012 Ruslan Nigmatullin <euroelessar@yandex.ru>
 
6
**
 
7
*****************************************************************************
 
8
**
 
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.
 
14
**
 
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.
 
19
**
 
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$
 
23
**
 
24
****************************************************************************/
 
25
 
 
26
#include "registrationqueryfactory_p.h"
 
27
#include "jstrings.h"
 
28
 
 
29
#define NS_REGISTER QLatin1String("jabber:iq:register")
 
30
 
 
31
namespace Jreen
 
32
{
 
33
 
 
34
const char *emptyNames[] = {
 
35
        "registered",
 
36
        "remove"
 
37
};
 
38
 
 
39
const char *valueNames[] = {
 
40
        "username",
 
41
        "nick",
 
42
        "password",
 
43
        "name",
 
44
        "first",
 
45
        "last",
 
46
        "email",
 
47
        "address",
 
48
        "city",
 
49
        "state",
 
50
        "zip",
 
51
        "phone",
 
52
        "url",
 
53
        "date"
 
54
        "misc",
 
55
        "text",
 
56
        "key"
 
57
};
 
58
 
 
59
 
 
60
RegistrationQueryFactory::RegistrationQueryFactory() : m_state(Nowhere), m_depth(0)
 
61
{
 
62
}
 
63
 
 
64
QStringList RegistrationQueryFactory::features() const
 
65
{
 
66
        return QStringList(); // << NS_REGISTER;
 
67
}
 
68
 
 
69
bool RegistrationQueryFactory::canParse(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes)
 
70
{
 
71
        Q_UNUSED(attributes);
 
72
        m_state = Nowhere;
 
73
        m_depth = 0;
 
74
        m_query.reset(new RegistrationQuery);
 
75
        return name == QLatin1String("query") && uri == NS_REGISTER;
 
76
}
 
77
 
 
78
void RegistrationQueryFactory::handleStartElement(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes)
 
79
{
 
80
        ++m_depth;
 
81
        if (m_depth == 2) {
 
82
                if (m_formFactory.canParse(name, uri, attributes)) {
 
83
                        m_state = AtForm;
 
84
                } else if (m_bobFactory.canParse(name, uri, attributes)) {
 
85
                        m_state = AtBob;
 
86
                } else if (name == QLatin1String("instructions")) {
 
87
                        m_state = AtInstructions;
 
88
                } else {
 
89
                        RegistrationDataPrivate::Flag flag = strToFlag<RegistrationDataPrivate::Flag>(name, emptyNames);
 
90
                        if (flag != -1) {
 
91
                                m_query->data->flags |= flag;
 
92
                                return;
 
93
                        } 
 
94
                        int index = strToEnum(name, valueNames);
 
95
                        if (index != -1) {
 
96
                                m_query->data->valuesFlags |= (1 << index);
 
97
                                m_state = static_cast<State>(AtValue + index);
 
98
                                return;
 
99
                        }
 
100
                }
 
101
        }
 
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);
 
106
}
 
107
 
 
108
void RegistrationQueryFactory::handleEndElement(const QStringRef &name, const QStringRef &uri)
 
109
{
 
110
        if (m_state == AtForm)
 
111
                m_formFactory.handleEndElement(name, uri);
 
112
        else if (m_state == AtBob)
 
113
                m_bobFactory.handleEndElement(name, uri);
 
114
        if (m_depth == 2) {
 
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>();
 
119
                m_state = Nowhere;
 
120
        }
 
121
        --m_depth;
 
122
}
 
123
 
 
124
void RegistrationQueryFactory::handleCharacterData(const QStringRef &text)
 
125
{
 
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();
 
134
}
 
135
 
 
136
void RegistrationQueryFactory::serialize(Payload *extension, QXmlStreamWriter *writer)
 
137
{
 
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]));
 
144
        }
 
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]);
 
148
        }
 
149
        if (query->data->form)
 
150
                m_formFactory.serialize(query->data->form.data(), writer);
 
151
        writer->writeEndElement();
 
152
}
 
153
 
 
154
Payload::Ptr RegistrationQueryFactory::createPayload()
 
155
{
 
156
        return Payload::Ptr(m_query.take());
 
157
}
 
158
 
 
159
} // namespace Jreen