1
/****************************************************************************
5
** Copyright © 2011 Aleksey Sidorov <gorthauer87@yandex.ru>
6
** Copyright © 2011 Alexey Prokhin <alexey.prokhin@yandex.ru>
8
*****************************************************************************
10
** $JREEN_BEGIN_LICENSE$
11
** This program is free software: you can redistribute it and/or modify
12
** it under the terms of the GNU General Public License as published by
13
** the Free Software Foundation, either version 2 of the License, or
14
** (at your option) any later version.
16
** This program is distributed in the hope that it will be useful,
17
** but WITHOUT ANY WARRANTY; without even the implied warranty of
18
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19
** See the GNU General Public License for more details.
21
** You should have received a copy of the GNU General Public License
22
** along with this program. If not, see http://www.gnu.org/licenses/.
23
** $JREEN_END_LICENSE$
25
****************************************************************************/
26
#include <QXmlStreamWriter>
27
#include <qstringlist.h>
28
#include "activityfactory_p.h"
32
#define NS_ACTIVITY QLatin1String("http://jabber.org/protocol/activity")
38
static const char* general_types[]= {
53
static const char* specific_types[]= {
123
struct ActivityLessThen
125
bool operator()(const QStringRef &a, const char *b)
127
return a.compare(QLatin1String(b)) < 0;
130
bool operator()(const char *a, const QStringRef &b)
132
return b.compare(QLatin1String(a)) > 0;
136
ActivityFactory::ActivityFactory()
142
ActivityFactory::~ActivityFactory()
146
QStringList ActivityFactory::features() const
148
return QStringList(NS_ACTIVITY);
151
bool ActivityFactory::canParse(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes)
153
Q_UNUSED(attributes);
154
return name == QLatin1String("activity") && uri == NS_ACTIVITY;
157
void ActivityFactory::handleStartElement(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes)
160
Q_UNUSED(attributes);
165
} else if (m_depth == 2) {
166
if(name == QLatin1String("text")) {
169
m_general = generalByName(name);
172
} else if (m_depth == 3 && m_state == AtType) {
173
m_specific = specificByName(name);
177
void ActivityFactory::handleEndElement(const QStringRef &name, const QStringRef &uri)
186
void ActivityFactory::handleCharacterData(const QStringRef &text)
188
if (m_depth == 2 && m_state == AtText)
189
m_text = text.toString();
192
void ActivityFactory::serialize(Payload *extension, QXmlStreamWriter *writer)
194
Activity *activity = se_cast<Activity*>(extension);
195
if(activity->general() == Activity::InvalidGeneral)
198
writer->writeStartElement(QLatin1String("activity"));
199
writer->writeDefaultNamespace(NS_ACTIVITY);
200
if (activity->general() != Activity::EmptyGeneral) {
201
writer->writeStartElement(enumToStr(activity->general(),general_types));
202
if (activity->specific() > Activity::InvalidSpecific)
203
writer->writeEmptyElement(enumToStr(activity->specific(),specific_types));
204
writer->writeEndElement();
205
if(!activity->text().isEmpty())
206
writeTextElement(writer,QLatin1String("text"),activity->text());
208
writer->writeEndElement();
211
void ActivityFactory::clear()
213
m_general = Activity::InvalidGeneral;
214
m_specific = Activity::InvalidSpecific;
218
Payload::Ptr ActivityFactory::createPayload()
220
Activity *activity = new Activity(m_general,m_specific,m_text);
222
return Payload::Ptr(activity);
225
template <typename T>
226
static T typeByName(const QStringRef &name, const char* types[], int n)
229
return static_cast<T>(-2); // Empty
230
ActivityLessThen activityLessThen;
231
const char **res = qBinaryFind(types, types + n, name, activityLessThen);
232
return static_cast<T>((res == types + n) ? -1 : (res - types));
235
QLatin1String ActivityFactory::generalName(Activity::General general)
237
return QLatin1String(general <= Activity::InvalidGeneral ? 0 : general_types[general]);
240
Activity::General ActivityFactory::generalByName(const QStringRef &general)
242
return typeByName<Activity::General>(general, general_types, sizeof(general_types)/sizeof(char*));
245
QLatin1String ActivityFactory::specificName(Activity::Specific specific)
247
return QLatin1String(specific <= Activity::InvalidSpecific ? 0 : specific_types[specific]);
250
Activity::Specific ActivityFactory::specificByName(const QStringRef &specific)
252
return typeByName<Activity::Specific>(specific, specific_types, sizeof(specific_types)/sizeof(char*));