|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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 #ifndef QVERSITREADER_P_H |
|
44 #define QVERSITREADER_P_H |
|
45 |
|
46 // |
|
47 // W A R N I N G |
|
48 // ------------- |
|
49 // |
|
50 // This file is not part of the Qt API. It exists purely as an |
|
51 // implementation detail. This header file may change from version to |
|
52 // version without notice, or even be removed. |
|
53 // |
|
54 // We mean it. |
|
55 // |
|
56 |
|
57 #include "qmobilityglobal.h" |
|
58 #include "qversitreader.h" |
|
59 #include "qversitdocument.h" |
|
60 #include "qversitproperty.h" |
|
61 #include "versitutils_p.h" |
|
62 |
|
63 #include <QObject> |
|
64 #include <QThread> |
|
65 #include <QByteArray> |
|
66 #include <QIODevice> |
|
67 #include <QList> |
|
68 #include <QPointer> |
|
69 #include <QByteArray> |
|
70 #include <QMutex> |
|
71 #include <QWaitCondition> |
|
72 |
|
73 QTM_BEGIN_NAMESPACE |
|
74 |
|
75 // The maximum number of bytes allowed to stay in memory after being read. The smaller this is, |
|
76 // the more time spent moving bytes around. The larger it is, the more memory is wasted. |
|
77 static const int MAX_OLD_BYTES_TO_KEEP = 8192; |
|
78 |
|
79 class Q_AUTOTEST_EXPORT VersitCursor |
|
80 { |
|
81 public: |
|
82 VersitCursor() : position(-1), selection(-1) {} |
|
83 explicit VersitCursor(const QByteArray& d) :data(d), position(0), selection(0) {} |
|
84 QByteArray data; |
|
85 int position; |
|
86 int selection; |
|
87 |
|
88 void setData(const QByteArray& d) {data = d; position = selection = 0;} |
|
89 void setPosition(int pos) {position = pos; selection = qMax(pos, selection);} |
|
90 void setSelection(int pos) {selection = qMax(pos, position);} |
|
91 void dropOldData() |
|
92 { |
|
93 if (position > MAX_OLD_BYTES_TO_KEEP && selection >= position) { |
|
94 data.remove(0, position); |
|
95 selection -= position; |
|
96 position = 0; |
|
97 } |
|
98 } |
|
99 }; |
|
100 |
|
101 class Q_AUTOTEST_EXPORT LineReader |
|
102 { |
|
103 public: |
|
104 LineReader(QIODevice* device, QTextCodec* codec, int chunkSize = 1000); |
|
105 VersitCursor readLine(); |
|
106 int odometer(); |
|
107 bool atEnd(); |
|
108 QTextCodec* codec(); |
|
109 |
|
110 private: |
|
111 bool tryReadLine(VersitCursor& cursor, bool atEnd); |
|
112 |
|
113 QIODevice* mDevice; |
|
114 QTextCodec* mCodec; |
|
115 int mChunkSize; // How many bytes to read in one go. |
|
116 QList<QByteArrayMatcher> mCrlfList; |
|
117 VersitCursor mBuffer; |
|
118 int mOdometer; |
|
119 int mSearchFrom; |
|
120 }; |
|
121 |
|
122 class Q_AUTOTEST_EXPORT QVersitReaderPrivate : public QThread |
|
123 { |
|
124 Q_OBJECT |
|
125 |
|
126 public: // Constructors and destructor |
|
127 QVersitReaderPrivate(); |
|
128 ~QVersitReaderPrivate(); |
|
129 |
|
130 signals: |
|
131 void stateChanged(QVersitReader::State state); |
|
132 void resultsAvailable(QList<QVersitDocument>& results); |
|
133 |
|
134 protected: // From QThread |
|
135 void run(); |
|
136 |
|
137 public: // New functions |
|
138 void read(); |
|
139 |
|
140 // mutexed getters and setters. |
|
141 void setState(QVersitReader::State); |
|
142 QVersitReader::State state() const; |
|
143 void setError(QVersitReader::Error); |
|
144 QVersitReader::Error error() const; |
|
145 void setCanceling(bool cancelling); |
|
146 bool isCanceling(); |
|
147 |
|
148 bool parseVersitDocument(LineReader& device, |
|
149 QVersitDocument& document, |
|
150 bool foundBegin = false); |
|
151 |
|
152 QVersitProperty parseNextVersitProperty( |
|
153 QVersitDocument::VersitType versitType, |
|
154 LineReader& lineReader); |
|
155 |
|
156 void parseVCard21Property( |
|
157 VersitCursor& text, |
|
158 QVersitProperty& property, |
|
159 LineReader& lineReader); |
|
160 |
|
161 void parseVCard30Property( |
|
162 VersitCursor& text, |
|
163 QVersitProperty& property, |
|
164 LineReader& lineReader); |
|
165 |
|
166 bool setVersionFromProperty( |
|
167 QVersitDocument& document, |
|
168 const QVersitProperty& property) const; |
|
169 |
|
170 void unencode( |
|
171 QVariant& value, |
|
172 VersitCursor& cursor, |
|
173 QVersitProperty& property, |
|
174 QTextCodec* codec, |
|
175 LineReader& lineReader) const; |
|
176 |
|
177 QString decodeCharset( |
|
178 const QByteArray& value, |
|
179 QVersitProperty& property, |
|
180 QTextCodec* defaultCodec, |
|
181 QTextCodec** codec) const; |
|
182 |
|
183 void decodeQuotedPrintable(QString& text) const; |
|
184 |
|
185 /* These functions operate on a cursor describing a single line */ |
|
186 QPair<QStringList,QString> extractPropertyGroupsAndName(VersitCursor& line, QTextCodec* codec) |
|
187 const; |
|
188 QByteArray extractPropertyValue(VersitCursor& line) const; |
|
189 QMultiHash<QString,QString> extractVCard21PropertyParams(VersitCursor& line, QTextCodec* codec) |
|
190 const; |
|
191 QMultiHash<QString,QString> extractVCard30PropertyParams(VersitCursor& line, QTextCodec* codec) |
|
192 const; |
|
193 |
|
194 // "Private" functions |
|
195 QList<QByteArray> extractParams(VersitCursor& line, QTextCodec *codec) const; |
|
196 QList<QByteArray> extractParts(const QByteArray& text, const QByteArray& separator, |
|
197 QTextCodec *codec) const; |
|
198 QByteArray extractPart(const QByteArray& text, int startPosition, int length=-1) const; |
|
199 QString paramName(const QByteArray& parameter, QTextCodec* codec) const; |
|
200 QString paramValue(const QByteArray& parameter, QTextCodec* codec) const; |
|
201 static bool containsAt(const QByteArray& text, const QByteArray& ba, int index); |
|
202 |
|
203 public: // Data |
|
204 QPointer<QIODevice> mIoDevice; |
|
205 QList<QVersitDocument> mVersitDocuments; |
|
206 int mDocumentNestingLevel; // Depth in parsing nested Versit documents |
|
207 QTextCodec* mDefaultCodec; |
|
208 QVersitReader::State mState; |
|
209 QVersitReader::Error mError; |
|
210 bool mIsCanceling; |
|
211 mutable QMutex mMutex; |
|
212 }; |
|
213 |
|
214 QTM_END_NAMESPACE |
|
215 |
|
216 #endif // QVERSITREADER_P_H |