QtMobility Reference Documentation

QVersitContactImporter Class Reference

The QVersitContactImporter class converts QVersitDocuments to QContacts. More...

 #include <QVersitContactImporter>

Public Types

enum Error { NoError, InvalidDocumentError, EmptyDocumentError }

Public Functions

QVersitContactImporter ()
QVersitContactImporter ( const QString & profile )
QVersitContactImporter ( const QStringList & profiles )
~QVersitContactImporter ()
QList<QContact> contacts () const
QMap<int, Error> errorMap () const
bool importDocuments ( const QList<QVersitDocument> & documents )
QVersitContactImporterPropertyHandler * propertyHandler () const (deprecated)
QVersitResourceHandler * resourceHandler () const
void setPropertyHandler ( QVersitContactImporterPropertyHandlerV2 * handler )
void setPropertyHandler ( QVersitContactImporterPropertyHandler * handler ) (deprecated)
void setResourceHandler ( QVersitResourceHandler * handler )

Detailed Description

The 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::fromAscii("N"));
 property.setValue("Citizen;John;Q;;");
 document.addProperty(property);

 property.setName(QString::fromAscii("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 handlers

A 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 although photos found in vCards are not saved to disk by default, the importer does add a QContactThumbnail detail to the image. 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 categories

The 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<QContactLocalId> matchingGroups = manager->contactIds(groupFilter & nameFilter);
                 if (!matchingGroups.isEmpty()) {
                     // Found an existing group in the manager
                     QContactId groupId;
                     groupId.setManagerUri(manager->managerUri());
                     groupId.setLocalId(matchingGroups.first());
                     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

enum QVersitContactImporter::Error

This enum specifies an error that occurred during the most recent call to importDocuments()

ConstantValueDescription
QVersitContactImporter::NoError0The most recent operation was successful
QVersitContactImporter::InvalidDocumentError1One of the documents is not a vCard
QVersitContactImporter::EmptyDocumentError2One of the documents is empty

Member Function Documentation

QVersitContactImporter::QVersitContactImporter ()

Constructs a new importer

QVersitContactImporter::QVersitContactImporter ( const QString & profile )

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.

QVersitContactImporter::QVersitContactImporter ( const QStringList & profiles )

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.

QVersitContactImporter::~QVersitContactImporter ()

Frees the memory used by the importer

QList<QContact> QVersitContactImporter::contacts () const

Returns the contacts imported in the most recent call to importDocuments().

See also importDocuments().

QMap<int, Error> QVersitContactImporter::errorMap () const

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().

bool QVersitContactImporter::importDocuments ( const QList<QVersitDocument> & documents )

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().

QVersitContactImporterPropertyHandler * QVersitContactImporter::propertyHandler () const

This function is deprecated.

Gets the handler for processing QVersitProperties.

See also setPropertyHandler().

QVersitResourceHandler * QVersitContactImporter::resourceHandler () const

Returns the associated resource handler.

See also setResourceHandler().

void QVersitContactImporter::setPropertyHandler ( QVersitContactImporterPropertyHandlerV2 * handler )

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().

void QVersitContactImporter::setPropertyHandler ( QVersitContactImporterPropertyHandler * handler )

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.

void QVersitContactImporter::setResourceHandler ( QVersitResourceHandler * handler )

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().

X

Thank you for giving your feedback.

Make sure it is related to this specific page. For more general bugs and requests, please use the Qt Bug Tracker.