|
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 #include "qversitdocumentwriter_p.h" |
|
43 #include "versitutils_p.h" |
|
44 #include <QTextCodec> |
|
45 |
|
46 QTM_USE_NAMESPACE |
|
47 |
|
48 #define MAX_LINE_LENGTH 76 |
|
49 |
|
50 /*! |
|
51 \class QVersitDocumentWriter |
|
52 \internal |
|
53 \brief The QVersitDocumentWriter class provides an interface for writing a |
|
54 single versit document into a vCard text string. |
|
55 */ |
|
56 |
|
57 /*! Constructs a writer. |
|
58 * \a documentType is the type of Versit document, as printed on the BEGIN line of output |
|
59 * eg. "VCARD" |
|
60 * \a version is the version of the Versit format, as printed on the VERSION line of output. |
|
61 * eg. "2.1" |
|
62 */ |
|
63 QVersitDocumentWriter::QVersitDocumentWriter( |
|
64 const QByteArray& documentType, |
|
65 const QByteArray& version) |
|
66 : mDocumentType(documentType), |
|
67 mVersion(version), |
|
68 mCodec(0), |
|
69 mEncoder(0), |
|
70 mUtf8Encoder(QTextCodec::codecForName("UTF-8")->makeEncoder()), |
|
71 mSuccessful(true), |
|
72 mCurrentLineLength(0) |
|
73 { |
|
74 // Hack so the encoder doesn't output a byte order mark for UTF-8. |
|
75 mUtf8Encoder->fromUnicode(QString()); |
|
76 } |
|
77 |
|
78 QVersitDocumentWriter::~QVersitDocumentWriter() |
|
79 { |
|
80 if (mEncoder) |
|
81 delete mEncoder; |
|
82 delete mUtf8Encoder; |
|
83 } |
|
84 |
|
85 /*! |
|
86 Sets the codec to write with. |
|
87 */ |
|
88 void QVersitDocumentWriter::setCodec(QTextCodec *codec) |
|
89 { |
|
90 if (mEncoder) |
|
91 delete mEncoder; |
|
92 mCodec = codec; |
|
93 mEncoder = codec->makeEncoder(); |
|
94 |
|
95 // Hack so the encoder doesn't output a byte order mark for UTF-8. |
|
96 if (mCodec->name() == "UTF-8") |
|
97 mEncoder->fromUnicode(QString()); |
|
98 } |
|
99 |
|
100 /*! |
|
101 Sets the device to write to. |
|
102 */ |
|
103 void QVersitDocumentWriter::setDevice(QIODevice *device) |
|
104 { |
|
105 mDevice = device; |
|
106 } |
|
107 |
|
108 /*! |
|
109 * Encodes the \a document and writes it to the device |
|
110 */ |
|
111 void QVersitDocumentWriter::encodeVersitDocument(const QVersitDocument& document) |
|
112 { |
|
113 mSuccessful = true; |
|
114 QList<QVersitProperty> properties = document.properties(); |
|
115 |
|
116 writeString(QLatin1String("BEGIN:" + mDocumentType)); |
|
117 writeCrlf(); |
|
118 writeString(QLatin1String("VERSION:" + mVersion)); |
|
119 writeCrlf(); |
|
120 foreach (const QVersitProperty& property, properties) { |
|
121 encodeVersitProperty(property); |
|
122 } |
|
123 writeString(QLatin1String("END:" + mDocumentType)); |
|
124 writeCrlf(); |
|
125 } |
|
126 |
|
127 /*! |
|
128 * Encodes the groups and name in the \a property and writes it to the device |
|
129 */ |
|
130 void QVersitDocumentWriter::encodeGroupsAndName(const QVersitProperty& property) |
|
131 { |
|
132 QStringList groups = property.groups(); |
|
133 if (!groups.isEmpty()) { |
|
134 writeString(groups.join(QLatin1String("."))); |
|
135 writeString(QLatin1String(".")); |
|
136 } |
|
137 writeString(property.name()); |
|
138 } |
|
139 |
|
140 /*! |
|
141 Writes \a string to the device. |
|
142 If \a useUtf8 is true, uses the UTF-8 codec instead of the one set in setCodec(). |
|
143 |
|
144 This function tracks how many characters have been written to the line and folds (wraps) the line |
|
145 according to RFC2425. |
|
146 */ |
|
147 void QVersitDocumentWriter::writeString(const QString &string, bool useUtf8) |
|
148 { |
|
149 QString value(string); // nonconst copy |
|
150 QTextEncoder* encoder = useUtf8 ? mUtf8Encoder : mEncoder; |
|
151 int spaceRemaining = MAX_LINE_LENGTH - mCurrentLineLength; |
|
152 while (spaceRemaining < value.length()) { |
|
153 // Write the first "spaceRemaining" characters |
|
154 QString line(value.left(spaceRemaining)); |
|
155 value.remove(0, spaceRemaining); |
|
156 if (mDevice->write(encoder->fromUnicode(line + QLatin1String("\r\n "))) < 0) |
|
157 mSuccessful = false; |
|
158 spaceRemaining = MAX_LINE_LENGTH - 1; // minus 1 for the space at the front. |
|
159 mCurrentLineLength = 1; |
|
160 } |
|
161 |
|
162 if (mDevice->write(encoder->fromUnicode(value)) < 0) |
|
163 mSuccessful = false; |
|
164 mCurrentLineLength += value.length(); |
|
165 } |
|
166 |
|
167 /*! |
|
168 Writes a CRLF to the device. By using this function, rather than writeString("\\r\\n"), you will |
|
169 allow the writer to know where a line starts, for folding purposes. |
|
170 */ |
|
171 void QVersitDocumentWriter::writeCrlf() |
|
172 { |
|
173 writeString(QLatin1String("\r\n")); |
|
174 mCurrentLineLength = 0; |
|
175 } |