src/versit/qvcard21writer.cpp
changeset 0 876b1a06bc25
child 5 603d3f8b6302
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     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 "qvcard21writer_p.h"
       
    43 #include "versitutils_p.h"
       
    44 #include "qmobilityglobal.h"
       
    45 
       
    46 #include <QTextCodec>
       
    47 
       
    48 QTM_USE_NAMESPACE
       
    49 
       
    50 /*! Constructs a writer. */
       
    51 QVCard21Writer::QVCard21Writer()
       
    52     : QVersitDocumentWriter(QByteArray("VCARD"),QByteArray("2.1"))
       
    53 {
       
    54 }
       
    55 
       
    56 /*! Destroys a writer. */
       
    57 QVCard21Writer::~QVCard21Writer()
       
    58 {
       
    59 }
       
    60 
       
    61 /*!
       
    62  * Encodes the \a property and writes it to the device.
       
    63  */
       
    64 void QVCard21Writer::encodeVersitProperty(const QVersitProperty& property)
       
    65 {
       
    66     encodeGroupsAndName(property);
       
    67     QMultiHash<QString,QString> parameters = property.parameters();
       
    68     QVariant variant(property.variantValue());
       
    69 
       
    70     QString renderedValue;
       
    71     bool useUtf8 = false;
       
    72 
       
    73     /* Structured values need to have their components backslash-escaped (in vCard 2.1, semicolons
       
    74        must be escaped for compound values and commas must be escaped for list values). */
       
    75     if (variant.type() == QVariant::StringList) {
       
    76         QStringList values = property.variantValue().toStringList();
       
    77         QString separator;
       
    78         if (property.valueType() == QVersitProperty::CompoundType) {
       
    79             separator = QLatin1String(";");
       
    80         } else {
       
    81             if (property.valueType() != QVersitProperty::ListType) {
       
    82                 qWarning("Variant value is a QStringList but the property's value type is neither "
       
    83                          "CompoundType or ListType");
       
    84             }
       
    85             // Assume it's a ListType
       
    86             separator = QLatin1String(",");
       
    87         }
       
    88         QString replacement = QLatin1Char('\\') + separator;
       
    89         QRegExp separatorRegex = QRegExp(separator);
       
    90         bool first = true;
       
    91         foreach (QString value, values) {
       
    92             if (!(value.isEmpty() && property.valueType() == QVersitProperty::ListType)) {
       
    93                 useUtf8 |= encodeVersitValue(parameters, value);
       
    94                 if (!first) {
       
    95                     renderedValue += separator;
       
    96                 }
       
    97                 renderedValue += value.replace(separatorRegex, replacement);
       
    98                 first = false;
       
    99             }
       
   100         }
       
   101     } else if (variant.type() == QVariant::String) {
       
   102         renderedValue = variant.toString();
       
   103         useUtf8 = encodeVersitValue(parameters, renderedValue);
       
   104     } else if (variant.type() == QVariant::ByteArray) {
       
   105         parameters.insert(QLatin1String("ENCODING"), QLatin1String("BASE64"));
       
   106         renderedValue = QLatin1String(variant.toByteArray().toBase64().data());
       
   107     }
       
   108 
       
   109     // Encode parameters
       
   110     encodeParameters(parameters);
       
   111 
       
   112     // Encode value
       
   113     writeString(QLatin1String(":"));
       
   114     if (variant.canConvert<QVersitDocument>()) {
       
   115         writeCrlf();
       
   116         QVersitDocument embeddedDocument = variant.value<QVersitDocument>();
       
   117         encodeVersitDocument(embeddedDocument);
       
   118     } else if (variant.type() == QVariant::String || variant.type() == QVariant::StringList) {
       
   119         writeString(renderedValue, useUtf8);
       
   120     } else if (variant.type() == QVariant::ByteArray) {
       
   121         // One extra folding before the value and
       
   122         // one extra line break after the value are needed in vCard 2.1
       
   123         writeCrlf();
       
   124         writeString(QLatin1String(" "));
       
   125         writeString(renderedValue, useUtf8);
       
   126         writeCrlf();
       
   127     }
       
   128     writeCrlf();
       
   129 }
       
   130 
       
   131 /*! Performs Quoted-Printable encoding and charset encoding on \a value as per vCard 2.1 spec.
       
   132     Returns true if the value will need to be encoded with UTF-8, false if mCodec is sufficient. */
       
   133 bool QVCard21Writer::encodeVersitValue(QMultiHash<QString,QString>& parameters, QString& value)
       
   134 {
       
   135     // Quoted-Printable encode the value and add Quoted-Printable parameter, if necessary
       
   136     if (quotedPrintableEncode(value))
       
   137         parameters.insert(QLatin1String("ENCODING"), QLatin1String("QUOTED-PRINTABLE"));
       
   138 
       
   139     // Add the CHARSET parameter, if necessary and encode in UTF-8 later
       
   140     if (!mCodec->canEncode(value)) {
       
   141         parameters.insert(QLatin1String("CHARSET"), QLatin1String("UTF-8"));
       
   142         return true;
       
   143     }
       
   144     return false;
       
   145 }
       
   146 
       
   147 /*!
       
   148  * Encodes the \a parameters and writes it to the device.
       
   149  */
       
   150 void QVCard21Writer::encodeParameters(const QMultiHash<QString,QString>& parameters)
       
   151 {
       
   152     QList<QString> names = parameters.uniqueKeys();
       
   153     foreach (const QString& name, names) {
       
   154         QStringList values = parameters.values(name);
       
   155         foreach (const QString& value, values) {
       
   156             writeString(QLatin1String(";"));
       
   157             QString typeParameterName(QLatin1String("TYPE"));
       
   158             if (name.length() > 0 && name != typeParameterName) {
       
   159                 writeString(name);
       
   160                 writeString(QLatin1String("="));
       
   161             }
       
   162             writeString(value);
       
   163         }
       
   164     }
       
   165 }
       
   166 
       
   167 
       
   168 
       
   169 /*!
       
   170  * Encodes special characters in \a text
       
   171  * using Quoted-Printable encoding (RFC 1521).
       
   172  * Returns true if at least one character was encoded.
       
   173  */
       
   174 bool QVCard21Writer::quotedPrintableEncode(QString& text) const
       
   175 {
       
   176     bool encoded = false;
       
   177     for (int i=0; i<text.length(); i++) {
       
   178         QChar current = text.at(i);
       
   179         if (shouldBeQuotedPrintableEncoded(current)) {
       
   180             QString encodedStr(QString::fromAscii("=%1").
       
   181                                arg(current.unicode(), 2, 16, QLatin1Char('0')).toUpper());
       
   182             text.replace(i, 1, encodedStr);
       
   183             i += 2;
       
   184             encoded = true;
       
   185         }
       
   186     }
       
   187     return encoded;
       
   188 }
       
   189 
       
   190 
       
   191 /*!
       
   192  * Checks whether the \a chr should be Quoted-Printable encoded (RFC 1521).
       
   193  */
       
   194 bool QVCard21Writer::shouldBeQuotedPrintableEncoded(QChar chr) const
       
   195 {
       
   196     int c = chr.unicode();
       
   197     return (c < 32 ||
       
   198             c == '!' || c == '"' || c == '#' || c == '$' ||
       
   199             c == '=' || c == '@' || c == '[' || c == '\\' ||
       
   200             c == ']' || c == '^' || c == '`' ||
       
   201             (c > 122 && c < 256));
       
   202 }