diff options
| author | David Laing <david.laing@nokia.com> | 2010-07-05 10:38:49 +1000 |
|---|---|---|
| committer | David Laing <david.laing@nokia.com> | 2010-07-05 10:38:49 +1000 |
| commit | acd3f83c6529b49e26d3dcd8a00205ede87b4495 (patch) | |
| tree | 37351fcebfd8221d77e4eb97b51f94a96f1453e0 /doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp | |
| parent | 08805dd34340dcec307b3b56bbbb78f8ba96ea6e (diff) | |
| parent | a0538e518b3783bb9365b616e71b59e4c59ee2c1 (diff) | |
Merge branch '1.0' into maemo
Diffstat (limited to 'doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp')
| -rw-r--r-- | doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp | 98 |
1 files changed, 79 insertions, 19 deletions
diff --git a/doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp b/doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp index e857181734..257251512d 100644 --- a/doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp +++ b/doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). ** All rights reserved. ** Contact: Nokia Corporation (qt-info@nokia.com) ** @@ -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] |
