|
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 "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 renderedValue; |
|
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 backSlashEscape(documentString); |
|
94 renderedValue = documentString; |
|
95 } else if (variant.type() == QVariant::String) { |
|
96 renderedValue = variant.toString(); |
|
97 backSlashEscape(renderedValue); |
|
98 } else if (variant.type() == QVariant::StringList) { |
|
99 // We need to backslash escape and concatenate the values in the list |
|
100 QStringList values = property.variantValue().toStringList(); |
|
101 QString separator; |
|
102 if (property.valueType() == QVersitProperty::CompoundType) { |
|
103 separator = QLatin1String(";"); |
|
104 } else { |
|
105 if (property.valueType() != QVersitProperty::ListType) { |
|
106 qWarning("Variant value is a QStringList but the property's value type is neither " |
|
107 "CompoundType or ListType"); |
|
108 } |
|
109 // Assume it's a ListType |
|
110 separator = QLatin1String(","); |
|
111 } |
|
112 bool first = true; |
|
113 foreach (QString value, values) { |
|
114 if (!(value.isEmpty() && property.valueType() == QVersitProperty::ListType)) { |
|
115 if (!first) { |
|
116 renderedValue += separator; |
|
117 } |
|
118 backSlashEscape(value); |
|
119 renderedValue += value; |
|
120 first = false; |
|
121 } |
|
122 } |
|
123 } else if (variant.type() == QVariant::ByteArray) { |
|
124 renderedValue = QLatin1String(variant.toByteArray().toBase64().data()); |
|
125 } |
|
126 writeString(renderedValue); |
|
127 writeCrlf(); |
|
128 } |
|
129 |
|
130 /*! |
|
131 * Encodes the \a parameters and writes it to the device. |
|
132 */ |
|
133 void QVCard30Writer::encodeParameters(const QMultiHash<QString,QString>& parameters) |
|
134 { |
|
135 QList<QString> names = parameters.uniqueKeys(); |
|
136 foreach (QString nameString, names) { |
|
137 writeString(QLatin1String(";")); |
|
138 QStringList values = parameters.values(nameString); |
|
139 backSlashEscape(nameString); |
|
140 writeString(nameString); |
|
141 writeString(QLatin1String("=")); |
|
142 for (int i=0; i<values.size(); i++) { |
|
143 if (i > 0) |
|
144 writeString(QLatin1String(",")); |
|
145 QString value = values.at(i); |
|
146 |
|
147 backSlashEscape(value); |
|
148 writeString(value); |
|
149 } |
|
150 } |
|
151 } |
|
152 |
|
153 |
|
154 /*! |
|
155 * Performs backslash escaping for line breaks (CRLFs), semicolons, backslashes and commas according |
|
156 * to RFC 2426. This is called on parameter names and values and property values. |
|
157 * Colons ARE NOT escaped because the examples in RFC2426 suggest that they shouldn't be. |
|
158 */ |
|
159 void QVCard30Writer::backSlashEscape(QString& text) |
|
160 { |
|
161 /* replaces ; with \; |
|
162 , with \, |
|
163 \ with \\ |
|
164 */ |
|
165 text.replace(QRegExp(QLatin1String("([;,\\\\])")), QLatin1String("\\\\1")); |
|
166 // replaces any CRLFs with \n |
|
167 text.replace(QRegExp(QLatin1String("\r\n|\r|\n")), QLatin1String("\\n")); |
|
168 } |