~ubuntu-branches/ubuntu/quantal/qtmobility/quantal

« back to all changes in this revision

Viewing changes to src/versit/qversitdocumentwriter_p.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-11-16 16:18:07 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20101116161807-k2dzt2nyse975r3l
Tags: 1.1.0-0ubuntu1
* New upstream release
* Syncronise with Debian, no remaining changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
**
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.
15
15
**
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.
35
35
**
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.
41
 
**
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.
67
61
 * eg. "2.1"
68
62
 */
69
 
QVersitDocumentWriter::QVersitDocumentWriter()
70
 
    : mDevice(0),
 
63
QVersitDocumentWriter::QVersitDocumentWriter(QVersitDocument::VersitType type)
 
64
    : mType(type),
 
65
    mDevice(0),
71
66
    mCodec(0),
72
67
    mEncoder(0),
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());
 
96
 
 
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"));
101
101
}
102
102
 
103
103
/*!
112
112
 * Encodes the \a document and writes it to the device.  A "VERSION:" line is added iff \a
113
113
 * encodeVersion is true.
114
114
 */
115
 
void QVersitDocumentWriter::encodeVersitDocument(const QVersitDocument& document, bool encodeVersion)
 
115
bool QVersitDocumentWriter::encodeVersitDocument(const QVersitDocument& document, bool encodeVersion)
116
116
{
117
117
    mSuccessful = true;
118
118
 
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"));
 
122
    } else {
 
123
        writeString(QLatin1String("BEGIN:") + document.componentType());
 
124
    }
120
125
    writeCrlf();
121
126
    if (encodeVersion) {
122
 
        switch (document.type()) {
 
127
        switch (mType) {
123
128
        case QVersitDocument::VCard21Type:
124
129
            writeString(QLatin1String("VERSION:2.1"));
125
130
            writeCrlf();
149
154
        encodeVersitDocument(document, false);
150
155
    }
151
156
 
152
 
    writeString(QLatin1String("END:") + document.componentType());
 
157
    if (document.componentType().isEmpty()) {
 
158
        writeString(QLatin1String("END:VCARD"));
 
159
    } else {
 
160
        writeString(QLatin1String("END:") + document.componentType());
 
161
    }
153
162
    writeCrlf();
 
163
 
 
164
    // This has been set by the methods called from this function
 
165
    return mSuccessful;
154
166
}
155
167
 
156
168
/*!
167
179
}
168
180
 
169
181
/*!
170
 
  Writes \a string to the device.
 
182
  Writes \a value to the device.
 
183
 
 
184
  This function tracks how many characters have been written to the line and folds (wraps) the line
 
185
  according to RFC2425.
 
186
  */
 
187
void QVersitDocumentWriter::writeBytes(const QByteArray &value)
 
188
{
 
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)
 
195
            mSuccessful = false;
 
196
        charsWritten += spaceRemaining;
 
197
        spaceRemaining = MAX_LINE_LENGTH - 1; // minus 1 for the space at the front.
 
198
        mCurrentLineLength = 1;
 
199
    }
 
200
 
 
201
    if (mDevice->write(value.constData() + charsWritten) < 0)
 
202
        mSuccessful = false;
 
203
    mCurrentLineLength += value.length() - charsWritten;
 
204
}
 
205
 
 
206
/*!
 
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().
172
209
 
173
210
  This function tracks how many characters have been written to the line and folds (wraps) the line
174
211
  according to RFC2425.
175
212
  */
176
 
void QVersitDocumentWriter::writeString(const QString &string, bool useUtf8)
 
213
void QVersitDocumentWriter::writeString(const QString &value, bool useUtf8)
177
214
{
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;
189
228
    }
190
229
 
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;
194
233
}
195
234
 
196
235
/*!