diff options
| author | David Laing <david.laing@nokia.com> | 2010-07-04 14:47:22 +1000 |
|---|---|---|
| committer | David Laing <david.laing@nokia.com> | 2010-07-04 14:47:22 +1000 |
| commit | e38ebf00c0b539a7756b21946693e201269b976a (patch) | |
| tree | 8f90c28f7e57095b9bd8749d17a6a36e9a297dcd /doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp | |
| parent | 448d4a4301b7abeb9065aca13b62c70422239b94 (diff) | |
Revert "Merge branch 'master' of ../../mainline/qtmobility"
This reverts commit 448d4a4301b7abeb9065aca13b62c70422239b94, reversing
changes made to 815d646dc485b6d15fbddc2814201a5b50be4a85.
Diffstat (limited to 'doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp')
| -rw-r--r-- | doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp | 112 |
1 files changed, 102 insertions, 10 deletions
diff --git a/doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp b/doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp index c84f32700e..257251512d 100644 --- a/doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp +++ b/doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp @@ -60,6 +60,104 @@ void completeExample(); void exportExample(); 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) + // 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; + } +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; + } + /* 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] + //! [Resource handler] class MyResourceHandler : public QVersitDefaultResourceHandler { public: @@ -143,9 +241,8 @@ void exportExample() //! [Export example] QVersitContactExporter contactExporter; - QVersitContactExporterDetailHandlerV2* backupHandler = - QVersitContactExporterDetailHandlerV2::createBackupHandler(); - contactExporter.setDetailHandler(backupHandler); + MyDetailHandler detailHandler; + contactExporter.setDetailHandler(&detailHandler); QContact contact; // Create a name @@ -158,8 +255,6 @@ void exportExample() QList<QVersitDocument> versitDocuments = contactExporter.documents(); // detailHandler.mUnknownDetails now contains the list of unknown details - - delete backupHandler; //! [Export example] } @@ -168,9 +263,8 @@ void importExample() //! [Import example] QVersitContactImporter importer; - QVersitContactImporterPropertyHandlerV2* backupHandler = - QVersitContactImporterPropertyHandlerV2::createBackupHandler(); - importer.setPropertyHandler(backupHandler); + MyPropertyHandler propertyHandler; + importer.setPropertyHandler(&propertyHandler); QVersitDocument document; @@ -188,8 +282,6 @@ void importExample() // contactList.first() now contains the "N" property as a QContactName // propertyHandler.mUnknownProperties contains the list of unknown properties } - - delete backupHandler; //! [Import example] } |
