|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 |
|
43 #include "qversitreader.h" |
|
44 #include "qversitreader_p.h" |
|
45 #include "versitutils_p.h" |
|
46 #include "qmobilityglobal.h" |
|
47 |
|
48 #include <QTextCodec> |
|
49 #include <QMutexLocker> |
|
50 #include <QBuffer> |
|
51 |
|
52 QTM_USE_NAMESPACE |
|
53 |
|
54 /*! |
|
55 \class QVersitReader |
|
56 \brief The QVersitReader class reads Versit documents such as vCards from a device. |
|
57 \ingroup versit |
|
58 |
|
59 QVersitReader concatenation of Versit documents such as vCards |
|
60 from a text stream and returns a list of QVersitDocument instances. |
|
61 QVersitReader supports reading from an abstract I/O device |
|
62 which can be, for example, a file or a memory buffer. |
|
63 The reading can be done asynchronously, and the |
|
64 waitForFinished() function can be used to make a blocking |
|
65 read. |
|
66 |
|
67 \sa QVersitDocument |
|
68 */ |
|
69 |
|
70 /*! |
|
71 * \enum QVersitReader::Error |
|
72 * This enum specifies an error that occurred during the most recent operation: |
|
73 * \value NoError The most recent operation was successful |
|
74 * \value UnspecifiedError The most recent operation failed for an undocumented reason |
|
75 * \value IOError The most recent operation failed because of a problem with the device |
|
76 * \value OutOfMemoryError The most recent operation failed due to running out of memory |
|
77 * \value NotReadyError The most recent operation failed because there is an operation in progress |
|
78 * \value ParseError The most recent operation failed because the input was malformed |
|
79 */ |
|
80 |
|
81 /*! |
|
82 * \enum QVersitReader::State |
|
83 * Enumerates the various states that a reader may be in at any given time |
|
84 * \value InactiveState Read operation not yet started |
|
85 * \value ActiveState Read operation started, not yet finished |
|
86 * \value CanceledState Read operation is finished due to cancellation |
|
87 * \value FinishedState Read operation successfully completed |
|
88 */ |
|
89 |
|
90 /*! |
|
91 * \fn QVersitReader::stateChanged(QVersitReader::State state) |
|
92 * The signal is emitted by the reader when its state has changed (eg. when it has finished |
|
93 * reading from the device). |
|
94 * \a state is the new state of the reader. |
|
95 */ |
|
96 |
|
97 /*! |
|
98 * \fn QVersitReader::resultsAvailable() |
|
99 * The signal is emitted by the reader as it reads from the device when it has made more Versit |
|
100 * documents available. |
|
101 */ |
|
102 |
|
103 /*! Constructs a new reader. */ |
|
104 QVersitReader::QVersitReader() : d(new QVersitReaderPrivate) |
|
105 { |
|
106 d->init(this); |
|
107 } |
|
108 |
|
109 /*! Constructs a new reader that reads from \a inputDevice. */ |
|
110 QVersitReader::QVersitReader(QIODevice *inputDevice) : d(new QVersitReaderPrivate) |
|
111 { |
|
112 d->init(this); |
|
113 d->mIoDevice = inputDevice; |
|
114 } |
|
115 |
|
116 /*! Constructs a new reader that reads from \a inputData. */ |
|
117 QVersitReader::QVersitReader(const QByteArray &inputData) : d(new QVersitReaderPrivate) |
|
118 { |
|
119 d->init(this); |
|
120 d->mInputBytes.reset(new QBuffer); |
|
121 d->mInputBytes->setData(inputData); |
|
122 d->mInputBytes->open(QIODevice::ReadOnly); |
|
123 d->mIoDevice = d->mInputBytes.data(); |
|
124 } |
|
125 |
|
126 /*! |
|
127 * Frees the memory used by the reader. |
|
128 * Waits until a pending asynchronous reading has been completed. |
|
129 */ |
|
130 QVersitReader::~QVersitReader() |
|
131 { |
|
132 d->wait(); |
|
133 delete d; |
|
134 } |
|
135 |
|
136 /*! |
|
137 * Sets the device used for reading the input to be the given \a device. |
|
138 * Does not take ownership of the device. This overrides any byte array input source set with |
|
139 * setData(). |
|
140 */ |
|
141 void QVersitReader::setDevice(QIODevice* device) |
|
142 { |
|
143 d->mInputBytes.reset(0); |
|
144 d->mIoDevice = device; |
|
145 } |
|
146 |
|
147 /*! |
|
148 * Returns the device used for reading input, or 0 if no device has been set (or if the input source |
|
149 * was set with setData(). |
|
150 */ |
|
151 QIODevice* QVersitReader::device() const |
|
152 { |
|
153 if (d->mInputBytes.isNull()) |
|
154 return d->mIoDevice; |
|
155 else |
|
156 return 0; |
|
157 } |
|
158 |
|
159 /*! |
|
160 * Sets the data to read from to the byte array input source, \a inputData. |
|
161 * This overrides any device set with setDevice(). |
|
162 */ |
|
163 void QVersitReader::setData(const QByteArray &inputData) |
|
164 { |
|
165 if (d->mInputBytes.isNull()) |
|
166 d->mInputBytes.reset(new QBuffer); |
|
167 d->mInputBytes->setData(inputData); |
|
168 d->mIoDevice = d->mInputBytes.data(); |
|
169 } |
|
170 |
|
171 /*! |
|
172 * Sets \a codec as the codec for the reader to use when parsing the input stream to. |
|
173 * This codec is not used for values where the CHARSET Versit parameter occurs. |
|
174 */ |
|
175 void QVersitReader::setDefaultCodec(QTextCodec *codec) |
|
176 { |
|
177 if (codec != NULL) { |
|
178 d->mDefaultCodec = codec; |
|
179 } else { |
|
180 d->mDefaultCodec = QTextCodec::codecForName("UTF-8"); |
|
181 } |
|
182 } |
|
183 |
|
184 /*! |
|
185 * Returns the codec the reader uses when parsing the input stream. |
|
186 */ |
|
187 QTextCodec* QVersitReader::defaultCodec() const |
|
188 { |
|
189 return d->mDefaultCodec; |
|
190 } |
|
191 |
|
192 /*! |
|
193 * Returns the state of the reader. |
|
194 */ |
|
195 QVersitReader::State QVersitReader::state() const |
|
196 { |
|
197 return d->state(); |
|
198 } |
|
199 |
|
200 /*! |
|
201 * Returns the error encountered by the last operation. |
|
202 */ |
|
203 QVersitReader::Error QVersitReader::error() const |
|
204 { |
|
205 return d->error(); |
|
206 } |
|
207 |
|
208 /*! |
|
209 * Starts reading the input asynchronously. |
|
210 * Returns false if the input device has not been set or opened or |
|
211 * if there is another asynchronous read operation already pending. |
|
212 * Signal \l stateChanged() is emitted with parameter FinishedState |
|
213 * when the reading has finished. |
|
214 */ |
|
215 bool QVersitReader::startReading() |
|
216 { if (d->state() == ActiveState || d->isRunning()) { |
|
217 d->setError(QVersitReader::NotReadyError); |
|
218 return false; |
|
219 } else if (!d->mIoDevice || !d->mIoDevice->isReadable()) { |
|
220 d->setError(QVersitReader::IOError); |
|
221 return false; |
|
222 } else { |
|
223 d->setState(ActiveState); |
|
224 d->setError(NoError); |
|
225 d->setCanceling(false); |
|
226 d->start(); |
|
227 return true; |
|
228 } |
|
229 } |
|
230 |
|
231 /*! |
|
232 * Attempts to asynchronously cancel the read request. |
|
233 */ |
|
234 void QVersitReader::cancel() |
|
235 { |
|
236 d->setCanceling(true); |
|
237 } |
|
238 |
|
239 /*! |
|
240 * If the state is ActiveState, blocks until the reader has finished reading or \a msec milliseconds |
|
241 * has elapsed, returning true if it successfully finishes or is cancelled by the user. |
|
242 * If the state is FinishedState, returns true immediately. |
|
243 * Otherwise, returns false immediately. |
|
244 */ |
|
245 bool QVersitReader::waitForFinished(int msec) |
|
246 { |
|
247 State state = d->state(); |
|
248 if (state == ActiveState) { |
|
249 return d->wait(msec); |
|
250 } else if (state == FinishedState) { |
|
251 return true; |
|
252 } else { |
|
253 return false; |
|
254 } |
|
255 } |
|
256 |
|
257 /*! |
|
258 * Returns the reading result. Even if there was an error or reading has not completed, a partial |
|
259 * list of results may be returned. |
|
260 */ |
|
261 QList<QVersitDocument> QVersitReader::results() const |
|
262 { |
|
263 QMutexLocker locker(&d->mMutex); |
|
264 return d->mVersitDocuments; |
|
265 } |
|
266 |
|
267 #include "moc_qversitreader.cpp" |