9
9
** $QT_BEGIN_LICENSE:LGPL$
10
10
** Commercial Usage
11
** Licensees holding valid Qt Commercial licenses may use this file in
12
** accordance with the Qt Solutions Commercial License Agreement provided
13
** with the Software or, alternatively, in accordance with the terms
11
** Licensees holding valid Qt Commercial licenses may use this file in
12
** accordance with the Qt Commercial License Agreement provided with
13
** the Software or, alternatively, in accordance with the terms
14
14
** contained in a written agreement between you and Nokia.
16
16
** GNU Lesser General Public License Usage
33
33
** ensure the GNU General Public License version 3.0 requirements will be
34
34
** met: http://www.gnu.org/copyleft/gpl.html.
36
** Please note Third Party Software included with Qt Solutions may impose
37
** additional restrictions and it is the user's responsibility to ensure
38
** that they have met the licensing requirements of the GPL, LGPL, or Qt
39
** Solutions Commercial license and the relevant license of the Third
40
** Party Software they are using.
42
36
** If you are unsure which license is appropriate for your use, please
43
37
** contact the sales department at qt-sales@nokia.com.
44
38
** $QT_END_LICENSE$
66
60
* \a version is the version of the Versit format, as printed on the VERSION line of output.
69
QVersitDocumentWriter::QVersitDocumentWriter()
63
QVersitDocumentWriter::QVersitDocumentWriter(QVersitDocument::VersitType type)
73
68
mUtf8Encoder(QTextCodec::codecForName("UTF-8")->makeEncoder()),
98
93
// Hack so the encoder doesn't output a byte order mark for UTF-8.
99
94
if (mCodec->name() == "UTF-8")
100
95
mEncoder->fromUnicode(QString());
97
// UTF-(16|32)(LE|BE) are the only codecs where characters in the base64 range aren't encoded
98
// the same as in ASCII. For ASCII compatible codecs, we can do some optimizations.
99
mCodecIsAsciiCompatible = !(mCodec->name().startsWith("UTF-16")
100
|| mCodec->name().startsWith("UTF-32"));
112
112
* Encodes the \a document and writes it to the device. A "VERSION:" line is added iff \a
113
113
* encodeVersion is true.
115
void QVersitDocumentWriter::encodeVersitDocument(const QVersitDocument& document, bool encodeVersion)
115
bool QVersitDocumentWriter::encodeVersitDocument(const QVersitDocument& document, bool encodeVersion)
117
117
mSuccessful = true;
119
writeString(QLatin1String("BEGIN:") + document.componentType());
119
if (document.componentType().isEmpty()) {
120
// for compatibility with code for Qt Mobility 1.0, which didn't have componentType
121
writeString(QLatin1String("BEGIN:VCARD"));
123
writeString(QLatin1String("BEGIN:") + document.componentType());
121
126
if (encodeVersion) {
122
switch (document.type()) {
123
128
case QVersitDocument::VCard21Type:
124
129
writeString(QLatin1String("VERSION:2.1"));
149
154
encodeVersitDocument(document, false);
152
writeString(QLatin1String("END:") + document.componentType());
157
if (document.componentType().isEmpty()) {
158
writeString(QLatin1String("END:VCARD"));
160
writeString(QLatin1String("END:") + document.componentType());
164
// This has been set by the methods called from this function
170
Writes \a string to the device.
182
Writes \a value to the device.
184
This function tracks how many characters have been written to the line and folds (wraps) the line
185
according to RFC2425.
187
void QVersitDocumentWriter::writeBytes(const QByteArray &value)
189
int spaceRemaining = MAX_LINE_LENGTH - mCurrentLineLength;
190
int charsWritten = 0;
191
while (spaceRemaining < value.length() - charsWritten) {
192
// Write the first "spaceRemaining" characters
193
if (mDevice->write(value.constData() + charsWritten, spaceRemaining) < 0
194
|| mDevice->write("\r\n ") < 0)
196
charsWritten += spaceRemaining;
197
spaceRemaining = MAX_LINE_LENGTH - 1; // minus 1 for the space at the front.
198
mCurrentLineLength = 1;
201
if (mDevice->write(value.constData() + charsWritten) < 0)
203
mCurrentLineLength += value.length() - charsWritten;
207
Writes \a value to the device.
171
208
If \a useUtf8 is true, uses the UTF-8 codec instead of the one set in setCodec().
173
210
This function tracks how many characters have been written to the line and folds (wraps) the line
174
211
according to RFC2425.
176
void QVersitDocumentWriter::writeString(const QString &string, bool useUtf8)
213
void QVersitDocumentWriter::writeString(const QString &value, bool useUtf8)
178
QString value(string); // nonconst copy
179
215
QTextEncoder* encoder = useUtf8 ? mUtf8Encoder : mEncoder;
180
216
int spaceRemaining = MAX_LINE_LENGTH - mCurrentLineLength;
181
while (spaceRemaining < value.length()) {
217
int charsWritten = 0;
218
QString crlfSpace(QLatin1String("\r\n "));
219
while (spaceRemaining < value.length() - charsWritten) {
182
220
// Write the first "spaceRemaining" characters
183
QString line(value.left(spaceRemaining));
184
value.remove(0, spaceRemaining);
185
if (mDevice->write(encoder->fromUnicode(line + QLatin1String("\r\n "))) < 0)
221
QStringRef line(&value, charsWritten, spaceRemaining);
222
charsWritten += spaceRemaining;
223
if (mDevice->write(encoder->fromUnicode(line.constData(), line.length())) < 0
224
|| mDevice->write(encoder->fromUnicode(crlfSpace)) < 0)
186
225
mSuccessful = false;
187
226
spaceRemaining = MAX_LINE_LENGTH - 1; // minus 1 for the space at the front.
188
227
mCurrentLineLength = 1;
191
if (mDevice->write(encoder->fromUnicode(value)) < 0)
230
if (mDevice->write(encoder->fromUnicode(value.mid(charsWritten))) < 0)
192
231
mSuccessful = false;
193
mCurrentLineLength += value.length();
232
mCurrentLineLength += value.length() - charsWritten;