QVersitContactImporter ClassThe QVersitContactImporter class converts QVersitDocuments to QContacts. More... #include <QVersitContactImporter>
Public Types
Public Functions
Detailed DescriptionThe QVersitContactImporter class converts QVersitDocuments to QContacts. This class is used to convert lists of QVersitDocuments (which may be produced by a QVersitReader) to lists of QContacts (which may be saved into a QContactManager. Unless there is an error, there is a one-to-one mapping between Versit documents and QContacts. The importer can be extended by clients by associating resource and property handlers. Here is a simple example of how to use QVersitContactImporter: QVersitContactImporter importer; QVersitDocument document; QVersitProperty property; property.setName(QString::fromLatin1("N")); property.setValue("Citizen;John;Q;;"); document.addProperty(property); property.setName(QString::fromLatin1("X-UNKNOWN-PROPERTY")); property.setValue("some value"); document.addProperty(property); if (importer.importDocuments(QList<QVersitDocument>() << document)) { QList<QContact> contactList = importer.contacts(); // contactList.first() now contains the "N" property as a QContactName // propertyHandler.mUnknownProperties contains the list of unknown properties } Extension via handlersA QVersitResourceHandler is associated with the importer to supply the behaviour for saving files to persistent storage. By default, this is set to a QVersitDefaultResourceHandler, which does not save files to persistent storage. Note that photos found in vCards are not saved to disk by default. If a full-sized image needs to be loaded from a URL and persisted on disk, a custom QVersitResourceHandler should be supplied which implements this. By associating a QVersitContactImporterPropertyHandlerV2 with the importer using setPropertyHandler(), the client can pass in a handler to override the processing of properties and/or handle properties that QVersitContactImporter doesn't support. Also, handlers can be implicitly associated to an importer through the handler plugin mechanism. The importer can be constructed with a profile, which gives hints about what kind of handlers should be added to it. For example, the backup profile can be used to instruct the importer to interpret properties that have been generated by a backup-profiled QVersitContactExporter. To illustrate, a backup importer can be constructed with: QVersitContactImporter importer(QVersitContactHandlerFactory::ProfileBackup); For more details on how the backup plugin works, see Versit Plugins Importing categoriesThe importer imports the vCard CATEGORIES property by converting each category to a QContactTag. Some managers may not have support for QContactTag, but instead support categorization using the HasMember QContactRelationship along with contacts of type TypeGroup. For these backends, if the categorization information needs to be retained through group relationships, extra work needs to be done to do the conversion. Below is some example code that does this translation. /*! Adds contacts in \a newContacts into \a manager, converting categories specified with tags into group membership relationships. Note that this implementation uses the synchronous API of QContactManager for clarity. It is recommended that the asynchronous API is used in practice. Relationships are added so that if a contact, A, has a tag "b", then a HasMember relationship is created between a group contact in the manager, B with display label "b", and contact A. If there does not exist a group contact with display label "b", one is created. */ void insertWithGroups(const QList<QContact>& newContacts, QContactManager* manager) { // Cache map from group names to QContactIds QMap<QString, QContactId> groupMap; foreach (QContact contact, newContacts) { if (!manager->saveContact(&contact)) continue; // In practice, better error handling may be required foreach (const QContactTag& tag, contact.details<QContactTag>()) { QString groupName = tag.tag(); QContactId groupId; if (groupMap.contains(groupName)) { // We've already seen a group with the right name groupId = groupMap.value(groupName); } else { QContactDetailFilter groupFilter; groupFilter.setDetailDefinitionName(QContactType::DefinitionName); groupFilter.setValue(QLatin1String(QContactType::TypeGroup)); groupFilter.setMatchFlags(QContactFilter::MatchExactly); // In practice, some detail other than the display label could be used QContactDetailFilter nameFilter = QContactDisplayLabel::match(groupName); QList<QContactId> matchingGroups = manager->contactIds(groupFilter & nameFilter); if (!matchingGroups.isEmpty()) { // Found an existing group in the manager QContactId groupId; groupMap.insert(groupName, groupId); } else { // Make a new group QContact groupContact; QContactName name; name.setCustomLabel(groupName); // Beware that not all managers support custom label groupContact.saveDetail(&name); if (!manager->saveContact(&groupContact)) continue; // In practice, better error handling may be required groupId = groupContact.id(); groupMap.insert(groupName, groupId); } } // Add the relationship QContactRelationship rel; rel.setFirst(groupId); rel.setRelationshipType(QContactRelationship::HasMember); rel.setSecond(contact.id()); manager->saveRelationship(&rel); } } } See also QVersitDocument, QVersitProperty, QVersitResourceHandler, and QVersitContactImporterPropertyHandlerV2. Member Type Documentation
|
Constant | Value | Description |
---|---|---|
QVersitContactImporter::NoError | 0 | The most recent operation was successful |
QVersitContactImporter::InvalidDocumentError | 1 | One of the documents is not a vCard |
QVersitContactImporter::EmptyDocumentError | 2 | One of the documents is empty |
Constructs a new importer
Constructs a new importer for the given profile. The profile strings should be one of those defined by QVersitContactHandlerFactory, or a value otherwise agreed to by a Versit plugin.
The profile determines which plugins will be loaded to supplement the importer.
Constructs a new importer for the given profiles. The profile strings should be one of those defined by QVersitContactHandlerFactory, or a value otherwise agreed to by a Versit plugin.
The profiles determine which plugins will be loaded to supplement the importer.
Frees the memory used by the importer
Returns the contacts imported in the most recent call to importDocuments().
See also importDocuments().
Returns the map of errors encountered in the most recent call to importDocuments(). The key is the index into the input list of documents and the value is the error that occurred on that document.
See also importDocuments().
Converts documents into a corresponding list of QContacts. After calling this, the converted contacts can be retrieved by calling contacts(). Returns true on success. If any of the documents cannot be imported as contacts (eg. they aren't vCards), false is returned and errorMap() will return a list describing the errors that occurred. The successfully imported documents will still be available via contacts().
See also contacts() and errorMap().
This function is deprecated.
Gets the handler for processing QVersitProperties.
See also setPropertyHandler().
Returns the associated resource handler.
See also setResourceHandler().
Sets handler to be the handler for processing QVersitProperties, or 0 to have no handler.
Does not take ownership of the handler. The client should ensure the handler remains valid for the lifetime of the exporter. This function is used for version 2 and higher handlers.
Only one property handler can be set. If another property handler (of any version) was previously set, it will no longer be associated with the importer.
See also propertyHandler().
This function is deprecated.
Sets handler to be the handler for processing QVersitProperties, or 0 to have no handler.
Does not take ownership of the handler. The client should ensure the handler remains valid for the lifetime of the exporter. This function is used for version 1 handlers.
Only one property handler can be set. If another property handler (of any version) was previously set, it will no longer be associated with the importer.
Sets handler to be the handler to save files with, or 0 to have no handler.
Does not take ownership of the handler. The client should ensure the handler remains valid for the lifetime of the exporter.
See also resourceHandler().