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
****************************************************************************/
43
#include <qversitreader.h>
44
#include "qversitreader_p.h"
45
#include "qversitutils_p.h"
48
#include <QMutexLocker>
51
QT_BEGIN_NAMESPACE_VERSIT
55
\brief The QVersitReader class provides an interface for reading versit
56
documents such as vCards from a Stream.
60
QVersitReader concatenation of Versit documents such as vCards
61
from a text stream and returns a list of QVersitDocument instances.
62
QVersitReader supports reading from an abstract I/O device
63
which can be, for example, a file or a memory buffer.
64
The reading can be done asynchronously, and the
65
waitForFinished() function can be used to make a blocking
72
* \enum QVersitReader::Error
73
* This enum specifies an error that occurred during the most recent operation:
74
* \value NoError The most recent operation was successful
75
* \value UnspecifiedError The most recent operation failed for an undocumented reason
76
* \value IOError The most recent operation failed because of a problem with the device
77
* \value OutOfMemoryError The most recent operation failed due to running out of memory
78
* \value NotReadyError The most recent operation failed because there is an operation in progress
79
* \value ParseError The most recent operation failed because the input was malformed
83
* \enum QVersitReader::State
84
* Enumerates the various states that a reader may be in at any given time
85
* \value InactiveState Read operation not yet started
86
* \value ActiveState Read operation started, not yet finished
87
* \value CanceledState Read operation is finished due to cancellation
88
* \value FinishedState Read operation successfully completed
92
* \fn QVersitReader::stateChanged(QVersitReader::State state)
93
* The signal is emitted by the reader when its state has changed (eg. when it has finished
94
* reading from the device).
95
* \a state is the new state of the reader.
99
* \fn QVersitReader::resultsAvailable()
100
* The signal is emitted by the reader as it reads from the device when it has made more Versit
101
* documents available.
104
/*! Constructs a new reader. */
105
QVersitReader::QVersitReader() : d(new QVersitReaderPrivate)
110
/*! Constructs a new reader that reads from \a inputDevice. */
111
QVersitReader::QVersitReader(QIODevice *inputDevice) : d(new QVersitReaderPrivate)
114
d->mIoDevice = inputDevice;
117
/*! Constructs a new reader that reads from \a inputData. */
118
QVersitReader::QVersitReader(const QByteArray &inputData) : d(new QVersitReaderPrivate)
121
d->mInputBytes.reset(new QBuffer);
122
d->mInputBytes->setData(inputData);
123
d->mInputBytes->open(QIODevice::ReadOnly);
124
d->mIoDevice = d->mInputBytes.data();
128
* Frees the memory used by the reader.
129
* Waits until a pending asynchronous reading has been completed.
131
QVersitReader::~QVersitReader()
138
* Sets the device used for reading the input to be the given \a device.
139
* Does not take ownership of the device. This overrides any byte array input source set with
142
* The caller must ensure that \a device remains valid for the lifetime of
143
* this QVersitReader object.
145
void QVersitReader::setDevice(QIODevice* device)
147
d->mInputBytes.reset(0);
148
d->mIoDevice = device;
152
* Returns the device used for reading input, or 0 if no device has been set (or if the input source
153
* was set with setData().
155
QIODevice* QVersitReader::device() const
157
if (d->mInputBytes.isNull())
164
* Sets the data to read from to the byte array input source, \a inputData.
165
* This overrides any device set with setDevice().
167
void QVersitReader::setData(const QByteArray &inputData)
169
if (d->mInputBytes.isNull()) {
170
d->mInputBytes.reset(new QBuffer);
171
} else if (d->mInputBytes->isOpen()) {
172
d->mInputBytes->close();
174
d->mInputBytes->setData(inputData);
175
d->mInputBytes->open(QIODevice::ReadOnly);
176
d->mIoDevice = d->mInputBytes.data();
180
* Sets \a codec as the codec for the reader to use when parsing the input stream to.
181
* This codec is not used for values where the CHARSET Versit parameter occurs.
182
* If the codec is null, this denotes that the reader will try to detect the codec
183
* from the input. The codec autodetection algorithm can detect UTF-8, UTF-16 or
184
* UTF-32. If the input is in some 8-bit codec, it will fall back to using the system
187
void QVersitReader::setDefaultCodec(QTextCodec *codec)
189
d->mDefaultCodec = codec;
193
* Returns the codec the reader uses when parsing the input stream. If the codec is
194
* null, this denotes that the reader will try to detect the codec from the input.
196
QTextCodec* QVersitReader::defaultCodec() const
198
return d->mDefaultCodec;
202
* Returns the state of the reader.
204
QVersitReader::State QVersitReader::state() const
210
* Returns the error encountered by the last operation.
212
QVersitReader::Error QVersitReader::error() const
218
* Starts reading the input asynchronously.
219
* Returns false if the input device has not been set or opened or
220
* if there is another asynchronous read operation already pending.
221
* Signal \l stateChanged() is emitted with parameter FinishedState
222
* when the reading has finished.
224
* The device must be already open. The client is responsible for
225
* closing it when finished.
227
bool QVersitReader::startReading()
229
if (d->state() == ActiveState || d->isRunning()) {
230
d->setError(QVersitReader::NotReadyError);
232
} else if (!d->mIoDevice || !d->mIoDevice->isReadable()) {
233
d->setError(QVersitReader::IOError);
236
d->setState(ActiveState);
237
d->setError(NoError);
238
d->setCanceling(false);
245
* Attempts to asynchronously cancel the read request.
247
void QVersitReader::cancel()
249
d->setCanceling(true);
253
* If the state is ActiveState, blocks until the reader has finished reading or \a msec milliseconds
254
* has elapsed, returning true if it successfully finishes or is cancelled by the user.
255
* If \a msec is negative or zero, the function blocks until the writer has finished, regardless of
257
* If the state is FinishedState, returns true immediately.
258
* Otherwise, returns false immediately.
260
bool QVersitReader::waitForFinished(int msec)
262
State state = d->state();
263
if (state != InactiveState) {
265
return d->wait(ULONG_MAX);
267
return d->wait(msec);
274
* Returns the reading result. Even if there was an error or reading has not completed, a partial
275
* list of results may be returned.
277
QList<QVersitDocument> QVersitReader::results() const
279
QMutexLocker locker(&d->mMutex);
280
return d->mVersitDocuments;
283
#include "moc_qversitreader.cpp"
284
QT_END_NAMESPACE_VERSIT