1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtVersit module of the Qt Toolkit.
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.
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.
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.
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.
40
****************************************************************************/
42
#include <qversitdocument.h>
43
#include "qversitdocument_p.h"
47
QT_BEGIN_NAMESPACE_VERSIT
50
\class QVersitDocument
51
\brief The QVersitDocument class is a container for a list of versit properties.
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
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".
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.
71
For example, for the following iCalendar:
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.
87
QVersitDocument supports implicit sharing.
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
102
/*! Constructs a new empty document */
103
QVersitDocument::QVersitDocument() : d(new QVersitDocumentPrivate())
107
/*! Constructs a new empty document with the type set to \a type */
108
QVersitDocument::QVersitDocument(VersitType type) : d(new QVersitDocumentPrivate())
110
d->mVersitType = type;
114
/*! Constructs a document that is a copy of \a other */
115
QVersitDocument::QVersitDocument(const QVersitDocument& other) : d(other.d)
119
/*! Frees the memory used by the document */
120
QVersitDocument::~QVersitDocument()
124
/*! Assigns this document to \a other */
125
QVersitDocument& QVersitDocument::operator=(const QVersitDocument& other)
132
/*! Returns true if this is equal to \a other; false otherwise. */
133
bool QVersitDocument::operator==(const QVersitDocument& other) const
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;
141
/*! Returns true if this is not equal to \a other; false otherwise. */
142
bool QVersitDocument::operator!=(const QVersitDocument& other) const
144
return !(*this == other);
147
/*! Returns the hash value for \a key. */
148
uint qHash(const QVersitDocument &key)
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);
156
foreach (const QVersitDocument& nested, key.subDocuments()) {
157
hash += qHash(nested);
162
#ifndef QT_NO_DEBUG_STREAM
163
QDebug operator<<(QDebug dbg, const QVersitDocument& document)
165
dbg.nospace() << "QVersitDocument(" << document.type() << ", " << document.componentType() << ')';
166
foreach (const QVersitProperty& property, document.properties()) {
167
dbg.space() << '\n' << property;
169
foreach (const QVersitDocument& nested, document.subDocuments()) {
170
dbg.space() << '\n' << nested;
172
return dbg.maybeSpace();
177
* Sets the versit document type to \a type. This determines the format in which the document is
180
void QVersitDocument::setType(VersitType type)
182
d->mVersitType = type;
186
* Gets the versit document type.
188
QVersitDocument::VersitType QVersitDocument::type() const
190
return d->mVersitType;
194
* Sets the versit component type to \a componentType (eg. VCARD, VCALENDAR, VEVENT, etc.)
196
void QVersitDocument::setComponentType(QString componentType)
198
d->mComponentType = componentType;
202
* Gets the versit component type
204
QString QVersitDocument::componentType() const
206
return d->mComponentType;
210
* Add \a property to the list of contained versit properties.
211
* The property is appended as the last property of the list.
213
void QVersitDocument::addProperty(const QVersitProperty& property)
215
d->mProperties.append(property);
219
* Removes the property \a property from the versit document.
221
void QVersitDocument::removeProperty(const QVersitProperty& property)
223
d->mProperties.removeAll(property);
227
* Removes all the properties with the given \a name from the versit document.
229
void QVersitDocument::removeProperties(const QString& name)
231
for (int i=d->mProperties.count()-1; i >=0; i--) {
232
if (d->mProperties[i].name() == name) {
233
d->mProperties.removeAt(i);
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.
242
void QVersitDocument::setProperties(const QList<QVersitProperty>& properties)
244
d->mProperties = properties;
248
* Gets the list of the contained versit properties.
249
* Note that the actual properties cannot be modified using the copy.
251
QList<QVersitProperty> QVersitDocument::properties() const
253
return d->mProperties;
257
* Adds \a subdocument to the Versit document.
259
void QVersitDocument::addSubDocument(const QVersitDocument& subdocument)
261
d->mSubDocuments.append(subdocument);
265
* Removes the \a subdocument from the versit document.
267
void QVersitDocument::removeSubDocument(const QVersitDocument& subdocument)
269
d->mSubDocuments.removeAll(subdocument);
273
* Sets the list of subdocuments to \a documents.
275
void QVersitDocument::setSubDocuments(const QList<QVersitDocument>& documents)
277
d->mSubDocuments = documents;
281
* Returns the list of subdocuments contained within this Versit document.
283
QList<QVersitDocument> QVersitDocument::subDocuments() const
285
return d->mSubDocuments;
289
* Returns true if the document is empty.
291
bool QVersitDocument::isEmpty() const
293
return d->mProperties.isEmpty()
294
&& d->mSubDocuments.isEmpty()
295
&& d->mVersitType == QVersitDocument::InvalidType;
299
* Clears the document, removing all properties, sub-documents and metadata.
301
void QVersitDocument::clear()
303
d->mProperties.clear();
304
d->mSubDocuments.clear();
305
d->mVersitType = QVersitDocument::InvalidType;
306
d->mComponentType.clear();
309
QT_END_NAMESPACE_VERSIT