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

« back to all changes in this revision

Viewing changes to src/vcardfactory_p.h

  • 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
**
 
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
#ifndef VCARDFACTORY_P_H
 
27
#define VCARDFACTORY_P_H
 
28
#include "vcard_p.h"
 
29
#include "stanzaextension.h"
 
30
#include <QPair>
 
31
 
 
32
namespace Jreen {
 
33
class JREEN_AUTOTEST_EXPORT VCardFactoryPrivate;
 
34
 
 
35
class AbstractStructureParser : public XmlStreamParser
 
36
{
 
37
public:
 
38
        AbstractStructureParser(const QLatin1String &name);
 
39
        bool canParse(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes);
 
40
        void handleStartElement(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes);
 
41
        void handleEndElement(const QStringRef &name, const QStringRef &uri);
 
42
        void handleCharacterData(const QStringRef &text);
 
43
protected:
 
44
        void serialize(void *zero, void *data, QXmlStreamWriter *writer);
 
45
        void addByteArray(const QLatin1String &name, QByteArray *str);
 
46
        void addString(const QLatin1String &name, QString *str);
 
47
        void addFlag(const char **table, int size, int *value);
 
48
        template <int N>
 
49
        void addFlag(const char *(&table)[N], int *value)
 
50
        { addFlag(table, N, value); }
 
51
 
 
52
        int m_depth;
 
53
        
 
54
private:
 
55
        struct FlagInfo
 
56
        {
 
57
                const char **table;
 
58
                int tableSize;
 
59
                int *value;
 
60
        };
 
61
 
 
62
        QList<QPair<QLatin1String, QString*> > m_strings;
 
63
        QList<QPair<QLatin1String, QByteArray*> > m_byteArrays;
 
64
        QList<FlagInfo> m_flags;
 
65
        QLatin1String m_name;
 
66
        QString *m_currentString;
 
67
        QByteArray *m_currentArray;
 
68
};
 
69
 
 
70
template <typename T>
 
71
class StructureParser : public AbstractStructureParser
 
72
{
 
73
public:
 
74
        StructureParser(const QLatin1String &name) : AbstractStructureParser(name) {}
 
75
        T create() { return m_data; }
 
76
        void serialize(const T &data, QXmlStreamWriter *writer) 
 
77
        { AbstractStructureParser::serialize(&m_data, const_cast<T*>(&data), writer); }
 
78
 
 
79
protected:
 
80
        T m_data;
 
81
};
 
82
 
 
83
template <typename TPrivate, typename T>
 
84
class StructurePrivateParser : public AbstractStructureParser
 
85
{
 
86
public:
 
87
        StructurePrivateParser(const QLatin1String &name) : AbstractStructureParser(name) {}
 
88
        T create() { return T(*new TPrivate(m_data)); }
 
89
        void serialize(const T &data, QXmlStreamWriter *writer) 
 
90
        {
 
91
                TPrivate *t1 = &m_data;
 
92
                TPrivate *t2 = const_cast<TPrivate*>(TPrivate::get(&data));
 
93
                AbstractStructureParser::serialize(t1, t2, writer);
 
94
        }
 
95
 
 
96
protected:
 
97
        TPrivate m_data;
 
98
};
 
99
 
 
100
class JREEN_AUTOTEST_EXPORT VCardFactory : public PayloadFactory<VCard>
 
101
{
 
102
        Q_DECLARE_PRIVATE(VCardFactory)
 
103
public:
 
104
    VCardFactory();
 
105
    virtual ~VCardFactory();
 
106
        QStringList features() const;
 
107
        bool canParse(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes);
 
108
        void handleStartElement(const QStringRef &name, const QStringRef &uri, const QXmlStreamAttributes &attributes);
 
109
        void handleEndElement(const QStringRef &name, const QStringRef &uri);
 
110
        void handleCharacterData(const QStringRef &text);
 
111
        void serialize(Payload *extension, QXmlStreamWriter *writer);
 
112
        Payload::Ptr createPayload();   
 
113
private:
 
114
        QScopedPointer<VCardFactoryPrivate> d_ptr;
 
115
        enum State {
 
116
                AtRoot,
 
117
                AtName,
 
118
                AtPhoto,
 
119
                AtTelephone,
 
120
                AtEMail,
 
121
                AtAddress,
 
122
                AtOrg,
 
123
                AtNowhere,
 
124
                LastState
 
125
        };
 
126
};
 
127
 
 
128
} // namespace Jreen
 
129
 
 
130
#endif // VCARDFACTORY_P_H