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

« back to all changes in this revision

Viewing changes to src/activityfactory.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 © 2011 Aleksey Sidorov <gorthauer87@yandex.ru>
 
6
** Copyright © 2011 Alexey Prokhin <alexey.prokhin@yandex.ru>
 
7
**
 
8
*****************************************************************************
 
9
**
 
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.
 
15
**
 
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.
 
20
**
 
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$
 
24
**
 
25
****************************************************************************/
 
26
#include <QXmlStreamWriter>
 
27
#include <qstringlist.h>
 
28
#include "activityfactory_p.h"
 
29
#include "jstrings.h"
 
30
#include "util.h"
 
31
 
 
32
#define NS_ACTIVITY QLatin1String("http://jabber.org/protocol/activity")
 
33
 
 
34
namespace Jreen {
 
35
 
 
36
using namespace Util;
 
37
 
 
38
static const char* general_types[]= {
 
39
        "doing_chores",
 
40
        "drinking",
 
41
        "eating",
 
42
        "exercising",
 
43
        "grooming",
 
44
        "having_appointment",
 
45
        "inactive",
 
46
        "relaxing",
 
47
        "talking",
 
48
        "traveling",
 
49
        "undefined",
 
50
        "working"
 
51
};
 
52
 
 
53
static const char* specific_types[]= {
 
54
        "at_the_spa",
 
55
        "brushing_teeth",
 
56
        "buying_groceries",
 
57
        "cleaning",
 
58
        "coding",
 
59
        "commuting",
 
60
        "cooking",
 
61
        "cycling",
 
62
        "dancing",
 
63
        "day_off",
 
64
        "doing_maintenance",
 
65
        "doing_the_dishes",
 
66
        "doing_the_laundry",
 
67
        "driving",
 
68
        "fishing",
 
69
        "gaming",
 
70
        "gardening",
 
71
        "getting_a_haircut",
 
72
        "going_out",
 
73
        "hanging_out",
 
74
        "having_a_beer",
 
75
        "having_a_snack",
 
76
        "having_breakfast",
 
77
        "having_coffee",
 
78
        "having_dinner",
 
79
        "having_lunch",
 
80
        "having_tea",
 
81
        "hiding",
 
82
        "hiking",
 
83
        "in_a_car",
 
84
        "in_a_meeting",
 
85
        "in_real_life",
 
86
        "jogging",
 
87
        "on_a_bus",
 
88
        "on_a_plane",
 
89
        "on_a_train",
 
90
        "on_a_trip",
 
91
        "on_the_phone",
 
92
        "on_vacation",
 
93
        "on_video_phone",
 
94
        "other",
 
95
        "partying",
 
96
        "playing_sports",
 
97
        "praying",
 
98
        "reading",
 
99
        "rehearsing",
 
100
        "running",
 
101
        "running_an_errand",
 
102
        "scheduled_holiday",
 
103
        "shaving",
 
104
        "shopping",
 
105
        "skiing",
 
106
        "sleeping",
 
107
        "smoking",
 
108
        "socializing",
 
109
        "studying",
 
110
        "sunbathing",
 
111
        "swimming",
 
112
        "taking_a_bath",
 
113
        "taking_a_shower",
 
114
        "thinking",
 
115
        "walking",
 
116
        "walking_the_dog",
 
117
        "watching_a_movie",
 
118
        "watching_tv",
 
119
        "working_out",
 
120
        "writing"
 
121
};
 
122
 
 
123
struct ActivityLessThen
 
124
{
 
125
        bool operator()(const QStringRef &a, const char *b)
 
126
        {
 
127
                return a.compare(QLatin1String(b)) < 0;
 
128
        }
 
129
 
 
130
        bool operator()(const char *a, const QStringRef &b)
 
131
        {
 
132
                return b.compare(QLatin1String(a)) > 0;
 
133
        }
 
134
};
 
135
 
 
136
ActivityFactory::ActivityFactory()
 
137
{
 
138
        clear();
 
139
        m_depth = 0;
 
140
}
 
141
 
 
142
ActivityFactory::~ActivityFactory()
 
143
{
 
144
}
 
145
 
 
146
QStringList ActivityFactory::features() const
 
147
{
 
148
        return QStringList(NS_ACTIVITY);
 
149
}
 
150
 
 
151
bool ActivityFactory::canParse(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes)
 
152
{
 
153
        Q_UNUSED(attributes);
 
154
        return name == QLatin1String("activity") && uri == NS_ACTIVITY;
 
155
}
 
156
 
 
157
void ActivityFactory::handleStartElement(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes)
 
158
{
 
159
        Q_UNUSED(uri);
 
160
        Q_UNUSED(attributes);
 
161
        m_depth++;
 
162
        if(m_depth == 1) {
 
163
                m_state = AtNowhere;
 
164
                clear();
 
165
        } else if (m_depth == 2) {
 
166
                if(name == QLatin1String("text")) {
 
167
                        m_state = AtText;
 
168
                } else {
 
169
                        m_general = generalByName(name);
 
170
                        m_state = AtType;
 
171
                }
 
172
        } else if (m_depth == 3 && m_state == AtType) {
 
173
                m_specific = specificByName(name);
 
174
        }
 
175
}
 
176
 
 
177
void ActivityFactory::handleEndElement(const QStringRef &name, const QStringRef &uri)
 
178
{
 
179
        Q_UNUSED(name);
 
180
        Q_UNUSED(uri);
 
181
        if (m_depth == 2)
 
182
                m_state = AtNowhere;
 
183
        m_depth--;
 
184
}
 
185
 
 
186
void ActivityFactory::handleCharacterData(const QStringRef &text)
 
187
{
 
188
        if (m_depth == 2 && m_state == AtText)
 
189
                m_text = text.toString();
 
190
}
 
191
 
 
192
void ActivityFactory::serialize(Payload *extension, QXmlStreamWriter *writer)
 
193
{
 
194
        Activity *activity = se_cast<Activity*>(extension);
 
195
        if(activity->general() == Activity::InvalidGeneral)
 
196
                return;
 
197
 
 
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());
 
207
        }
 
208
        writer->writeEndElement();
 
209
}
 
210
 
 
211
void ActivityFactory::clear()
 
212
{
 
213
        m_general = Activity::InvalidGeneral;
 
214
        m_specific = Activity::InvalidSpecific;
 
215
        m_text.clear();
 
216
}
 
217
 
 
218
Payload::Ptr ActivityFactory::createPayload()
 
219
{
 
220
        Activity *activity = new Activity(m_general,m_specific,m_text);
 
221
        clear();
 
222
        return Payload::Ptr(activity);
 
223
}
 
224
 
 
225
template <typename T>
 
226
static T typeByName(const QStringRef &name, const char* types[], int n)
 
227
{
 
228
        if (name.isEmpty())
 
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));
 
233
}
 
234
 
 
235
QLatin1String ActivityFactory::generalName(Activity::General general)
 
236
{
 
237
        return QLatin1String(general <= Activity::InvalidGeneral ? 0 : general_types[general]);
 
238
}
 
239
 
 
240
Activity::General ActivityFactory::generalByName(const QStringRef &general)
 
241
{
 
242
        return typeByName<Activity::General>(general, general_types, sizeof(general_types)/sizeof(char*));
 
243
}
 
244
 
 
245
QLatin1String ActivityFactory::specificName(Activity::Specific specific)
 
246
{
 
247
        return QLatin1String(specific <= Activity::InvalidSpecific ? 0 : specific_types[specific]);
 
248
}
 
249
 
 
250
Activity::Specific ActivityFactory::specificByName(const QStringRef &specific)
 
251
{
 
252
        return typeByName<Activity::Specific>(specific, specific_types, sizeof(specific_types)/sizeof(char*));
 
253
}
 
254
 
 
255
} // namespace Jreen