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 <qversitwriter.h>
43
#include "qversitwriter_p.h"
44
#include "qversitutils_p.h"
46
#include <QStringList>
50
QT_BEGIN_NAMESPACE_VERSIT
54
\brief The QVersitWriter class writes Versit documents such as vCards to a device.
58
QVersitWriter converts a QVersitDocument into its textual representation.
59
QVersitWriter supports writing to an abstract I/O device
60
which can be for example a file or a memory buffer.
61
The writing can be done asynchronously and the waitForFinished()
62
function can be used to implement a blocking write.
64
The serialization of the document is done in accordance with the type of the QVersitDocument
65
being written. The value of each QVersitProperty is encoded according to the type of object:
67
\li \l{QString}{QStrings} are serialized verbatim, unless the default codec of the writer cannot
68
encode the string: in this case, UTF-8 is used to encode it (and the CHARSET parameter added to
69
the property, as per the vCard 2.1 specification). If the document type is vCard 2.1,
70
quoted-printable encoding may also be performed.
71
\li \l{QByteArray}{QByteArrays} are assumed to be binary data and are serialized as base-64 encoded
73
\li \l{QVersitDocument}{QVersitDocuments} are serialized as a nested document (eg. as per the
74
AGENT property in vCard).
77
\sa QVersitDocument, QVersitProperty
82
* \enum QVersitWriter::Error
83
* This enum specifies an error that occurred during the most recent operation:
84
* \value NoError The most recent operation was successful
85
* \value UnspecifiedError The most recent operation failed for an undocumented reason
86
* \value IOError The most recent operation failed because of a problem with the device
87
* \value OutOfMemoryError The most recent operation failed due to running out of memory
88
* \value NotReadyError The most recent operation failed because there is an operation in progress
92
* \enum QVersitWriter::State
93
* Enumerates the various states that a reader may be in at any given time
94
* \value InactiveState Write operation not yet started
95
* \value ActiveState Write operation started, not yet finished
96
* \value CanceledState Write operation is finished due to cancelation
97
* \value FinishedState Write operation successfully completed
101
* \fn QVersitWriter::stateChanged(QVersitWriter::State state)
102
* The signal is emitted by the writer when its state has changed (eg. when it has finished
103
* writing to the device).
104
* \a state is the new state of the writer.
107
/*! Constructs a new writer. */
108
QVersitWriter::QVersitWriter() : d(new QVersitWriterPrivate)
113
/*! Constructs a new writer that writes to \a outputDevice. */
114
QVersitWriter::QVersitWriter(QIODevice *outputDevice) : d(new QVersitWriterPrivate)
117
d->mIoDevice = outputDevice;
120
/*! Constructs a new writer that appends to \a outputBytes. */
121
QVersitWriter::QVersitWriter(QByteArray *outputBytes) : d(new QVersitWriterPrivate)
124
d->mOutputBytes.reset(new QBuffer);
125
d->mOutputBytes->setBuffer(outputBytes);
126
d->mOutputBytes->open(QIODevice::WriteOnly);
127
d->mIoDevice = d->mOutputBytes.data();
131
* Frees the memory used by the writer.
132
* Waits until a pending asynchronous writing has been completed.
134
QVersitWriter::~QVersitWriter()
141
* Sets the device used for writing to \a device.
142
* Does not take ownership of the device.
144
void QVersitWriter::setDevice(QIODevice* device)
146
d->mOutputBytes.reset(0);
147
d->mIoDevice = device;
151
* Returns the device used for writing, or 0 if no device has been set.
153
QIODevice* QVersitWriter::device() const
155
if (d->mOutputBytes.isNull())
162
* Sets the default codec for the writer to use for writing the entire output.
164
* If \a codec is NULL, the writer uses the codec according to the specification prescribed default.
165
* (for vCard 2.1, ASCII; for vCard 3.0, UTF-8).
167
void QVersitWriter::setDefaultCodec(QTextCodec *codec)
169
d->mDefaultCodec = codec;
173
* Returns the document's codec.
175
QTextCodec* QVersitWriter::defaultCodec() const
177
return d->mDefaultCodec;
181
* Returns the state of the writer.
183
QVersitWriter::State QVersitWriter::state() const
189
* Returns the error encountered by the last operation.
191
QVersitWriter::Error QVersitWriter::error() const
197
* Starts writing \a input to device() asynchronously. The serialization format is determined based
198
* on the contents of the input documents.
200
* Returns false if the output device has not been set or opened or if there is another asynchronous
201
* write operation already pending. Signal \l stateChanged() is emitted with parameter
202
* FinishedState when the writing has finished.
204
* The device must be already open. The client is responsible for closing it when finished.
206
bool QVersitWriter::startWriting(const QList<QVersitDocument>& input)
208
return startWriting(input, QVersitDocument::InvalidType);
212
* Starts writing \a input to device() asynchronously using the serialization format specified by \a
213
* type. If \a type is QVersitDocument::InvalidType, the format will be determined based on the
214
* contents of the input documents.
216
* Returns false if the output device has not been set or opened or if there is another asynchronous
217
* write operation already pending. Signal \l stateChanged() is emitted with parameter
218
* FinishedState when the writing has finished.
220
* The device must be already open. The client is responsible for closing it when finished.
222
bool QVersitWriter::startWriting(const QList<QVersitDocument>& input, QVersitDocument::VersitType type)
225
if (d->state() == ActiveState || d->isRunning()) {
226
d->setError(QVersitWriter::NotReadyError);
228
} else if (!d->mIoDevice || !d->mIoDevice->isWritable()) {
229
d->setError(QVersitWriter::IOError);
232
d->setState(ActiveState);
233
d->setError(NoError);
234
d->setDocumentType(type);
241
* Starts writing \a input to device() asynchronously. The serialization format is determined based
242
* on the contents of the input documents.
244
* Returns false if the output device has not been set or opened or if there is another asynchronous
245
* write operation already pending. Signal \l stateChanged() is emitted with parameter
246
* FinishedState when the writing has finished.
248
* The device must be already open. The client is responsible for closing it when finished.
250
bool QVersitWriter::startWriting(const QVersitDocument& input)
252
return startWriting(QList<QVersitDocument>() << input, QVersitDocument::InvalidType);
256
* Starts writing \a input to device() asynchronously using the serialization format specified by \a
257
* type. If \a type is QVersitDocument::InvalidType, the format will be determined based on the
258
* contents of the input documents.
260
* Returns false if the output device has not been set or opened or if there is another asynchronous
261
* write operation already pending. Signal \l stateChanged() is emitted with parameter
262
* FinishedState when the writing has finished.
264
* The device must be already open. The client is responsible for closing it when finished.
266
bool QVersitWriter::startWriting(const QVersitDocument& input, QVersitDocument::VersitType type)
268
return startWriting(QList<QVersitDocument>() << input, type);
272
* Attempts to asynchronously cancel the write request.
274
void QVersitWriter::cancel()
276
d->setCanceling(true);
280
* If the state is ActiveState, blocks until the writer has finished writing or \a msec milliseconds
281
* has elapsed, returning true if it successfully finishes or is cancelled by the user.
282
* If \a msec is negative or zero, the function blocks until the writer has finished, regardless of
284
* If the state is FinishedState, returns true immediately.
285
* Otherwise, returns false immediately.
287
bool QVersitWriter::waitForFinished(int msec)
289
State state = d->state();
290
if (state != InactiveState) {
292
return d->wait(ULONG_MAX);
294
return d->wait(msec);
300
#include "moc_qversitwriter.cpp"
301
QT_END_NAMESPACE_VERSIT