~chris.gagnon/+junk/qtpim-coverage

« back to all changes in this revision

Viewing changes to src/versit/qversitdocument.cpp

  • Committer: chris.gagnon
  • Date: 2013-12-10 23:09:37 UTC
  • Revision ID: chris.gagnon@canonical.com-20131210230937-2akf1ft1edcttk87
first post

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the QtVersit module of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include <qversitdocument.h>
 
43
#include "qversitdocument_p.h"
 
44
 
 
45
#include <QTextCodec>
 
46
 
 
47
QT_BEGIN_NAMESPACE_VERSIT
 
48
 
 
49
/*!
 
50
  \class QVersitDocument
 
51
  \brief The QVersitDocument class is a container for a list of versit properties.
 
52
  \ingroup versit
 
53
  \inmodule QtVersit
 
54
 
 
55
  A vCard is represented in abstract form as a QVersitDocument that consists of a number of
 
56
  properties such as a name (N), a telephone number (TEL) and an email address (EMAIL), for
 
57
  instance.  Each of these properties is stored as an instance of a QVersitProperty in a
 
58
  QVersitDocument.
 
59
 
 
60
  In addition to the list of properties, QVersitDocument also records the type of the Versit
 
61
  document in two ways.  The VersitType enum describes the format in which the document is to be
 
62
  serialized by QVersitWriter (or the format from which it was read by QVersitReader), and should
 
63
  not be used to infer any semantics about the document data.  The componentType field is a string
 
64
  corresponding directly to the value of the BEGIN line in a document.  For example, for a vCard,
 
65
  this will always be the string "VCARD"; for an iCalendar, it could be "VCALENDAR", "VEVENT",
 
66
  "VTODO", "VJOURNAL", "VALARM" or "VTIMEZONE".
 
67
 
 
68
  As well as properties, a QVersitDocument can hold other documents.  For iCalendar, this is how
 
69
  a single VCALENDAR document can compose documents of type VEVENT, VTODO, etc.
 
70
 
 
71
  For example, for the following iCalendar:
 
72
  \code
 
73
  BEGIN:VCALENDAR
 
74
  VERSION:2.0
 
75
  BEGIN:VEVENT
 
76
  SUMMARY:Christmas
 
77
  DTSTART:20001225
 
78
  END:VEVENT
 
79
  END:VCALENDAR
 
80
  \endcode
 
81
 
 
82
  This can be represented as a QVersitDocument of with componentType VCALENDAR and versitType
 
83
  ICalendar20Type.  It contains no properties (note: the VERSION property is not stored explicitly
 
84
  as a property) and one sub-document.  The sub-document has componentType VEVENT and versitType
 
85
  ICalendar20Type, and contains two properties.
 
86
 
 
87
  QVersitDocument supports implicit sharing.
 
88
 
 
89
  \sa QVersitProperty
 
90
 */
 
91
 
 
92
/*!
 
93
  \enum QVersitDocument::VersitType
 
94
  This enum describes a Versit document serialization format and version.
 
95
  \value InvalidType No type specified or a document with an invalid type was parsed
 
96
  \value VCard21Type vCard version 2.1
 
97
  \value VCard30Type vCard version 3.0
 
98
  \value VCard40Type vCard version 4.0
 
99
  \value ICalendar20Type iCalendar version 2.0
 
100
 */
 
101
 
 
102
/*! Constructs a new empty document */
 
103
QVersitDocument::QVersitDocument() : d(new QVersitDocumentPrivate())
 
104
{
 
105
}
 
106
 
 
107
/*! Constructs a new empty document with the type set to \a type */
 
108
QVersitDocument::QVersitDocument(VersitType type) : d(new QVersitDocumentPrivate())
 
109
{
 
110
    d->mVersitType = type;
 
111
}
 
112
 
 
113
 
 
114
/*! Constructs a document that is a copy of \a other */
 
115
QVersitDocument::QVersitDocument(const QVersitDocument& other) : d(other.d)
 
116
{
 
117
}
 
118
 
 
119
/*! Frees the memory used by the document */
 
120
QVersitDocument::~QVersitDocument()
 
121
{
 
122
}
 
123
 
 
124
/*! Assigns this document to \a other */
 
125
QVersitDocument& QVersitDocument::operator=(const QVersitDocument& other)
 
126
{
 
127
    if (this != &other)
 
128
        d = other.d;
 
129
    return *this;
 
130
}
 
131
 
 
132
/*! Returns true if this is equal to \a other; false otherwise. */
 
133
bool QVersitDocument::operator==(const QVersitDocument& other) const
 
134
{
 
135
    return d->mVersitType == other.d->mVersitType &&
 
136
            d->mProperties == other.d->mProperties &&
 
137
            d->mSubDocuments == other.d->mSubDocuments &&
 
138
            d->mComponentType == other.d->mComponentType;
 
139
}
 
140
 
 
141
/*! Returns true if this is not equal to \a other; false otherwise. */
 
142
bool QVersitDocument::operator!=(const QVersitDocument& other) const
 
143
{
 
144
    return !(*this == other);
 
145
}
 
146
 
 
147
/*! Returns the hash value for \a key. */
 
148
uint qHash(const QVersitDocument &key)
 
149
{
 
150
    int hash = QT_PREPEND_NAMESPACE(qHash)(key.type());
 
151
    hash += QT_PREPEND_NAMESPACE(qHash)(key.componentType());
 
152
    hash += key.properties().length() + key.subDocuments().length();
 
153
    foreach (const QVersitProperty& property, key.properties()) {
 
154
        hash += qHash(property);
 
155
    }
 
156
    foreach (const QVersitDocument& nested, key.subDocuments()) {
 
157
        hash += qHash(nested);
 
158
    }
 
159
    return hash;
 
160
}
 
161
 
 
162
#ifndef QT_NO_DEBUG_STREAM
 
163
QDebug operator<<(QDebug dbg, const QVersitDocument& document)
 
164
{
 
165
    dbg.nospace() << "QVersitDocument(" << document.type() << ", " << document.componentType() << ')';
 
166
    foreach (const QVersitProperty& property, document.properties()) {
 
167
        dbg.space() << '\n' << property;
 
168
    }
 
169
    foreach (const QVersitDocument& nested, document.subDocuments()) {
 
170
        dbg.space() << '\n' << nested;
 
171
    }
 
172
    return dbg.maybeSpace();
 
173
}
 
174
#endif
 
175
 
 
176
/*!
 
177
 * Sets the versit document type to \a type.  This determines the format in which the document is
 
178
 * to be serialized.
 
179
 */
 
180
void QVersitDocument::setType(VersitType type)
 
181
{
 
182
    d->mVersitType = type;
 
183
}
 
184
 
 
185
/*!
 
186
 * Gets the versit document type.
 
187
 */
 
188
QVersitDocument::VersitType QVersitDocument::type() const
 
189
{
 
190
    return d->mVersitType;
 
191
}
 
192
 
 
193
/*!
 
194
 * Sets the versit component type to \a componentType (eg. VCARD, VCALENDAR, VEVENT, etc.)
 
195
 */
 
196
void QVersitDocument::setComponentType(QString componentType)
 
197
{
 
198
    d->mComponentType = componentType;
 
199
}
 
200
 
 
201
/*!
 
202
 * Gets the versit component type
 
203
 */
 
204
QString QVersitDocument::componentType() const
 
205
{
 
206
    return d->mComponentType;
 
207
}
 
208
 
 
209
/*!
 
210
 * Add \a property to the list of contained versit properties.
 
211
 * The property is appended as the last property of the list.
 
212
 */
 
213
void QVersitDocument::addProperty(const QVersitProperty& property)
 
214
{
 
215
    d->mProperties.append(property);
 
216
}
 
217
 
 
218
/*!
 
219
 * Removes the property \a property from the versit document.
 
220
 */
 
221
void QVersitDocument::removeProperty(const QVersitProperty& property)
 
222
{
 
223
    d->mProperties.removeAll(property);
 
224
}
 
225
 
 
226
/*!
 
227
 * Removes all the properties with the given \a name from the versit document.
 
228
 */
 
229
void QVersitDocument::removeProperties(const QString& name)
 
230
{
 
231
    for (int i=d->mProperties.count()-1; i >=0; i--) {
 
232
        if (d->mProperties[i].name() == name) {
 
233
            d->mProperties.removeAt(i);
 
234
        }
 
235
    }
 
236
}
 
237
 
 
238
/*!
 
239
 * Sets the list of properties to \a properties.  Logically, all of the existing properties are
 
240
 * removed and all of the supplied \a properties are added.
 
241
 */
 
242
void QVersitDocument::setProperties(const QList<QVersitProperty>& properties)
 
243
{
 
244
    d->mProperties = properties;
 
245
}
 
246
 
 
247
/*!
 
248
 * Gets the list of the contained versit properties.
 
249
 * Note that the actual properties cannot be modified using the copy.
 
250
 */
 
251
QList<QVersitProperty> QVersitDocument::properties() const
 
252
{
 
253
    return d->mProperties;
 
254
}
 
255
 
 
256
/*!
 
257
 * Adds \a subdocument to the Versit document.
 
258
 */
 
259
void QVersitDocument::addSubDocument(const QVersitDocument& subdocument)
 
260
{
 
261
    d->mSubDocuments.append(subdocument);
 
262
}
 
263
 
 
264
/*!
 
265
 * Removes the \a subdocument from the versit document.
 
266
 */
 
267
void QVersitDocument::removeSubDocument(const QVersitDocument& subdocument)
 
268
{
 
269
    d->mSubDocuments.removeAll(subdocument);
 
270
}
 
271
 
 
272
/*!
 
273
 * Sets the list of subdocuments to \a documents.
 
274
 */
 
275
void QVersitDocument::setSubDocuments(const QList<QVersitDocument>& documents)
 
276
{
 
277
    d->mSubDocuments = documents;
 
278
}
 
279
 
 
280
/*!
 
281
 * Returns the list of subdocuments contained within this Versit document.
 
282
 */
 
283
QList<QVersitDocument> QVersitDocument::subDocuments() const
 
284
{
 
285
    return d->mSubDocuments;
 
286
}
 
287
 
 
288
/*!
 
289
 * Returns true if the document is empty.
 
290
 */
 
291
bool QVersitDocument::isEmpty() const
 
292
{
 
293
    return d->mProperties.isEmpty()
 
294
        && d->mSubDocuments.isEmpty()
 
295
        && d->mVersitType == QVersitDocument::InvalidType;
 
296
}
 
297
 
 
298
/*!
 
299
 * Clears the document, removing all properties, sub-documents and metadata.
 
300
 */
 
301
void QVersitDocument::clear()
 
302
{
 
303
    d->mProperties.clear();
 
304
    d->mSubDocuments.clear();
 
305
    d->mVersitType = QVersitDocument::InvalidType;
 
306
    d->mComponentType.clear();
 
307
}
 
308
 
 
309
QT_END_NAMESPACE_VERSIT