qtmobility/src/versit/qvcard30writer.cpp
changeset 1 2b40d63a9c3d
child 4 90517678cc4f
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     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 #include "qvcard30writer_p.h"
       
    43 #include "versitutils_p.h"
       
    44 #include "qmobilityglobal.h"
       
    45 #include <QTextCodec>
       
    46 #include <QBuffer>
       
    47 
       
    48 QTM_USE_NAMESPACE
       
    49 
       
    50 /*! Constructs a writer. */
       
    51 QVCard30Writer::QVCard30Writer()
       
    52     : QVersitDocumentWriter(QByteArray("VCARD"),QByteArray("3.0"))
       
    53 {
       
    54     mPropertyNameMappings.insert(
       
    55         QLatin1String("X-NICKNAME"),QLatin1String("NICKNAME"));
       
    56     mPropertyNameMappings.insert(
       
    57         QLatin1String("X-IMPP"),QLatin1String("IMPP"));
       
    58 }
       
    59 
       
    60 /*! Destroys a writer. */
       
    61 QVCard30Writer::~QVCard30Writer()
       
    62 {
       
    63 }
       
    64 
       
    65 /*!
       
    66  * Encodes the \a property and writes it to the device.
       
    67  */
       
    68 void QVCard30Writer::encodeVersitProperty(const QVersitProperty& property)
       
    69 {
       
    70     QVersitProperty modifiedProperty(property);
       
    71     QString name = mPropertyNameMappings.value(property.name(),property.name());
       
    72     modifiedProperty.setName(name);
       
    73     encodeGroupsAndName(modifiedProperty);
       
    74 
       
    75     QVariant variant(modifiedProperty.variantValue());
       
    76     if (variant.type() == QVariant::ByteArray) {
       
    77         modifiedProperty.insertParameter(QLatin1String("ENCODING"), QLatin1String("b"));
       
    78     }
       
    79     encodeParameters(modifiedProperty.parameters());
       
    80     writeString(QLatin1String(":"));
       
    81 
       
    82     QString value;
       
    83     if (variant.canConvert<QVersitDocument>()) {
       
    84         QVersitDocument embeddedDocument = variant.value<QVersitDocument>();
       
    85         QByteArray data;
       
    86         QBuffer buffer(&data);
       
    87         buffer.open(QIODevice::WriteOnly);
       
    88         QVCard30Writer subWriter;
       
    89         subWriter.setCodec(mCodec);
       
    90         subWriter.setDevice(&buffer);
       
    91         subWriter.encodeVersitDocument(embeddedDocument);
       
    92         QString documentString(mCodec->toUnicode(data));
       
    93         VersitUtils::backSlashEscape(documentString);
       
    94         value = documentString;
       
    95     } else if (variant.type() == QVariant::String) {
       
    96         value = variant.toString();
       
    97     } else if (variant.type() == QVariant::ByteArray) {
       
    98         value = QLatin1String(variant.toByteArray().toBase64().data());
       
    99     }
       
   100     writeString(value);
       
   101     writeCrlf();
       
   102 }
       
   103 
       
   104 /*!
       
   105  * Encodes the \a parameters and writes it to the device.
       
   106  */
       
   107 void QVCard30Writer::encodeParameters(const QMultiHash<QString,QString>& parameters)
       
   108 {
       
   109     QList<QString> names = parameters.uniqueKeys();
       
   110     foreach (QString nameString, names) {
       
   111         writeString(QLatin1String(";"));
       
   112         QStringList values = parameters.values(nameString);
       
   113         VersitUtils::backSlashEscape(nameString);
       
   114         writeString(nameString);
       
   115         writeString(QLatin1String("="));
       
   116         for (int i=0; i<values.size(); i++) {
       
   117             if (i > 0)
       
   118                 writeString(QLatin1String(","));
       
   119             QString value = values.at(i);
       
   120 
       
   121             VersitUtils::backSlashEscape(value);
       
   122             writeString(value);
       
   123         }
       
   124     }
       
   125 }