diff options
| author | Kevin Wu Won <kevin.wu-won@nokia.com> | 2010-05-04 14:16:19 +1000 |
|---|---|---|
| committer | Kevin Wu Won <kevin.wu-won@nokia.com> | 2010-05-04 14:32:21 +1000 |
| commit | 2acf3360c3cf183630311f492f815acacebefccb (patch) | |
| tree | 939f54cc6689cac88cd915d72de3941672bcfcfb /doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp | |
| parent | 4a0681a480b8f9bc9fd8c5cfbcc49c64516297f6 (diff) | |
Add doc snippets to demonstrate encoding/decoding unsupported details in vCards
eg. a detail with definition name "Detail" and fields "Field1"="Value1" and
"Field2"="Value2" will be exported to the vCard properties:
G0.DETAIL-FIELD1:Value1
G0.DETAIL-FIELD2:Value2
and different details will have different vCard groups (eg. G1, G2, etc.)
Relates to MOBILITY-930
Diffstat (limited to 'doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp')
| -rw-r--r-- | doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp | 96 |
1 files changed, 78 insertions, 18 deletions
diff --git a/doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp b/doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp index e857181734..6ce4bb425f 100644 --- a/doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp +++ b/doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp @@ -63,38 +63,98 @@ void importExample(); //! [Detail handler] class MyDetailHandler : public QVersitContactExporterDetailHandler { public: + MyDetailHandler() : detailNumber(0) {} bool preProcessDetail(const QContact& contact, const QContactDetail& detail, QVersitDocument* document) { Q_UNUSED(contact) Q_UNUSED(detail) Q_UNUSED(document) return false; } + /* eg. a detail with definition name "Detail1" and fields "Field1"="Value1" and + * "Field2"="Value2" will be exported to the vCard properties: + * G0.DETAIL1-FIELD1:Value1 + * G0.DETAIL1-FIELD2:Value2 + * And the next detail (say, "Detail2" with a field "Field3"="Value3" will generate: + * G1.DETAIL2-FIELD3:Value3 + * ie. Different details will have different vCard groups. + */ bool postProcessDetail(const QContact& contact, const QContactDetail& detail, bool alreadyProcessed, QVersitDocument* document) { - Q_UNUSED(contact) Q_UNUSED(document) - if (!alreadyProcessed) - mUnknownDetails.append(detail); - return false; + Q_UNUSED(contact) + // beware: if the base implementation exports some but not all fields, alreadyProcessed + // will be true and the unprocessed fields won't be exported + if (alreadyProcessed) + return false; + if (detail.definitionName() == QContactType::DefinitionName) + return false; // special case of an unhandled detail that we don't export + QVersitProperty property; + QVariantMap fields = detail.variantValues(); + // fields from the same detail have the same group so the importer can collate them + QString detailGroup = QLatin1String("G") + QString::number(detailNumber++); + for (QVariantMap::const_iterator it = fields.constBegin(); + it != fields.constEnd(); + it++) { + property.setGroups(QStringList(detailGroup)); + // beware: detail.definitionName and the field name will be made uppercase on export + property.setName(QLatin1String("X-QCONTACTDETAIL-") + + detail.definitionName() + + QLatin1String("-") + + it.key()); + // beware: this might not handle nonstring values properly: + property.setValue(it.value()); + document->addProperty(property); + } + return true; } - QList<QContactDetail> mUnknownDetails; +private: + int detailNumber; }; //! [Detail handler] //! [Property handler] class MyPropertyHandler : public QVersitContactImporterPropertyHandler { public: - bool preProcessProperty(const QVersitDocument& document, const QVersitProperty& property, - int contactIndex, QContact* contact) { - Q_UNUSED(document) Q_UNUSED(property) Q_UNUSED(contactIndex) Q_UNUSED(contact) - return false; - } - bool postProcessProperty(const QVersitDocument& document, const QVersitProperty& property, - bool alreadyProcessed, int contactIndex, QContact* contact) { - Q_UNUSED(document) Q_UNUSED(contactIndex) Q_UNUSED(contact) - if (!alreadyProcessed) - mUnknownProperties.append(property); - return false; - } - QList<QVersitProperty> mUnknownProperties; + bool preProcessProperty(const QVersitDocument& document, const QVersitProperty& property, + int contactIndex, QContact* contact) { + Q_UNUSED(document) Q_UNUSED(property) Q_UNUSED(contactIndex) Q_UNUSED(contact) + return false; + } + /* eg. if the document has the properties: + * G0.DETAIL-FIELD1:Value1 + * G0.DETAIL-FIELD2:Value2 + * G1.DETAIL-FIELD1:Value3 + * This will generate two details - the first with fields "FIELD1"="Value1" and + * "FIELD2"="Value2" and the second with "FIELD1"="Value3" + * ie. the vCard groups determine which properties form a single detail. + */ + bool postProcessProperty(const QVersitDocument& document, const QVersitProperty& property, + bool alreadyProcessed, int contactIndex, QContact* contact) { + Q_UNUSED(document) Q_UNUSED(contactIndex) + const QString prefix = QLatin1String("X-QCONTACTDETAIL-"); + if (alreadyProcessed) + return false; + if (!property.name().startsWith(prefix)) + return false; + QString detailAndField = property.name().mid(prefix.size()); + QStringList detailAndFieldParts = detailAndField.split(QLatin1Char('-'), + QString::SkipEmptyParts); + if (detailAndFieldParts.size() != 2) + return false; + QString definitionName = detailAndFieldParts.at(0); + QString fieldName = detailAndFieldParts.at(1); + if (property.groups().size() != 1) + return false; + QString group = property.groups().first(); + // find a detail generated from the a property with the same group + QContactDetail detail = handledDetails.value(group); + // make sure the the existing detail has the same definition name + if (detail.definitionName() != definitionName) + detail = QContactDetail(definitionName); + detail.setValue(fieldName, property.value()); + contact->saveDetail(&detail); + handledDetails.insert(group, detail); + return false; + } + QMap<QString, QContactDetail> handledDetails; // map from group name to detail }; //! [Property handler] |
