~chris.gagnon/+junk/qtpim-coverage

« back to all changes in this revision

Viewing changes to src/versit/qversitreader.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
 
 
43
#include <qversitreader.h>
 
44
#include "qversitreader_p.h"
 
45
#include "qversitutils_p.h"
 
46
 
 
47
#include <QTextCodec>
 
48
#include <QMutexLocker>
 
49
#include <QBuffer>
 
50
 
 
51
QT_BEGIN_NAMESPACE_VERSIT
 
52
 
 
53
/*!
 
54
  \class QVersitReader
 
55
  \brief The QVersitReader class provides an interface for reading versit
 
56
  documents such as vCards from a Stream.
 
57
  \ingroup versit
 
58
  \inmodule QtVersit
 
59
 
 
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
 
66
  read.
 
67
 
 
68
  \sa QVersitDocument
 
69
 */
 
70
 
 
71
/*!
 
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
 
80
 */
 
81
 
 
82
/*!
 
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
 
89
 */
 
90
 
 
91
/*!
 
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.
 
96
 */
 
97
 
 
98
/*!
 
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.
 
102
 */
 
103
 
 
104
/*! Constructs a new reader. */
 
105
QVersitReader::QVersitReader() : d(new QVersitReaderPrivate)
 
106
{
 
107
    d->init(this);
 
108
}
 
109
 
 
110
/*! Constructs a new reader that reads from \a inputDevice. */
 
111
QVersitReader::QVersitReader(QIODevice *inputDevice) : d(new QVersitReaderPrivate)
 
112
{
 
113
    d->init(this);
 
114
    d->mIoDevice = inputDevice;
 
115
}
 
116
 
 
117
/*! Constructs a new reader that reads from \a inputData. */
 
118
QVersitReader::QVersitReader(const QByteArray &inputData) : d(new QVersitReaderPrivate)
 
119
{
 
120
    d->init(this);
 
121
    d->mInputBytes.reset(new QBuffer);
 
122
    d->mInputBytes->setData(inputData);
 
123
    d->mInputBytes->open(QIODevice::ReadOnly);
 
124
    d->mIoDevice = d->mInputBytes.data();
 
125
}
 
126
 
 
127
/*!
 
128
 * Frees the memory used by the reader.
 
129
 * Waits until a pending asynchronous reading has been completed.
 
130
 */
 
131
QVersitReader::~QVersitReader()
 
132
{
 
133
    d->wait();
 
134
    delete d;
 
135
}
 
136
 
 
137
/*!
 
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
 
140
 * setData().
 
141
 *
 
142
 * The caller must ensure that \a device remains valid for the lifetime of
 
143
 * this QVersitReader object.
 
144
 */
 
145
void QVersitReader::setDevice(QIODevice* device)
 
146
{
 
147
    d->mInputBytes.reset(0);
 
148
    d->mIoDevice = device;
 
149
}
 
150
 
 
151
/*!
 
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().
 
154
 */
 
155
QIODevice* QVersitReader::device() const
 
156
{
 
157
    if (d->mInputBytes.isNull())
 
158
        return d->mIoDevice;
 
159
    else
 
160
        return 0;
 
161
}
 
162
 
 
163
/*!
 
164
 * Sets the data to read from to the byte array input source, \a inputData.
 
165
 * This overrides any device set with setDevice().
 
166
 */
 
167
void QVersitReader::setData(const QByteArray &inputData)
 
168
{
 
169
    if (d->mInputBytes.isNull()) {
 
170
        d->mInputBytes.reset(new QBuffer);
 
171
    } else if (d->mInputBytes->isOpen()) {
 
172
        d->mInputBytes->close();
 
173
    }
 
174
    d->mInputBytes->setData(inputData);
 
175
    d->mInputBytes->open(QIODevice::ReadOnly);
 
176
    d->mIoDevice = d->mInputBytes.data();
 
177
}
 
178
 
 
179
/*!
 
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
 
185
 * locale's codec.
 
186
 */
 
187
void QVersitReader::setDefaultCodec(QTextCodec *codec)
 
188
{
 
189
    d->mDefaultCodec = codec;
 
190
}
 
191
 
 
192
/*!
 
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.
 
195
 */
 
196
QTextCodec* QVersitReader::defaultCodec() const
 
197
{
 
198
    return d->mDefaultCodec;
 
199
}
 
200
 
 
201
/*!
 
202
 * Returns the state of the reader.
 
203
 */
 
204
QVersitReader::State QVersitReader::state() const
 
205
{
 
206
    return d->state();
 
207
}
 
208
 
 
209
/*!
 
210
 * Returns the error encountered by the last operation.
 
211
 */
 
212
QVersitReader::Error QVersitReader::error() const
 
213
{
 
214
    return d->error();
 
215
}
 
216
 
 
217
/*!
 
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.
 
223
 *
 
224
 * The device must be already open.  The client is responsible for
 
225
 * closing it when finished.
 
226
 */
 
227
bool QVersitReader::startReading()
 
228
{
 
229
    if (d->state() == ActiveState || d->isRunning()) {
 
230
        d->setError(QVersitReader::NotReadyError);
 
231
        return false;
 
232
    } else if (!d->mIoDevice || !d->mIoDevice->isReadable()) {
 
233
        d->setError(QVersitReader::IOError);
 
234
        return false;
 
235
    } else {
 
236
        d->setState(ActiveState);
 
237
        d->setError(NoError);
 
238
        d->setCanceling(false);
 
239
        d->start();
 
240
        return true;
 
241
    }
 
242
}
 
243
 
 
244
/*!
 
245
 * Attempts to asynchronously cancel the read request.
 
246
 */
 
247
void QVersitReader::cancel()
 
248
{
 
249
    d->setCanceling(true);
 
250
}
 
251
 
 
252
/*!
 
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
 
256
 * how long it takes.
 
257
 * If the state is FinishedState, returns true immediately.
 
258
 * Otherwise, returns false immediately.
 
259
 */
 
260
bool QVersitReader::waitForFinished(int msec)
 
261
{
 
262
    State state = d->state();
 
263
    if (state != InactiveState) {
 
264
        if (msec <= 0)
 
265
            return d->wait(ULONG_MAX);
 
266
        else
 
267
            return d->wait(msec);
 
268
    } else {
 
269
        return false;
 
270
    }
 
271
}
 
272
 
 
273
/*!
 
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.
 
276
 */
 
277
QList<QVersitDocument> QVersitReader::results() const
 
278
{
 
279
    QMutexLocker locker(&d->mMutex);
 
280
    return d->mVersitDocuments;
 
281
}
 
282
 
 
283
#include "moc_qversitreader.cpp"
 
284
QT_END_NAMESPACE_VERSIT