qtmobility/src/contacts/qcontactmanagerengine.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
--- a/qtmobility/src/contacts/qcontactmanagerengine.cpp	Fri Apr 16 15:51:22 2010 +0300
+++ b/qtmobility/src/contacts/qcontactmanagerengine.cpp	Mon May 03 13:18:40 2010 +0300
@@ -52,9 +52,12 @@
 #include "qcontactabstractrequest_p.h"
 #include "qcontactrequests.h"
 #include "qcontactrequests_p.h"
+#include "qcontact.h"
+#include "qcontactfetchhint.h"
 
 #include "qcontact_p.h"
 #include "qcontactdetail_p.h"
+
 QTM_BEGIN_NAMESPACE
 
 /*!
@@ -62,17 +65,18 @@
   \preliminary
   \brief The QContactManagerEngine class provides the interface for all
   implementations of the contact manager backend functionality.
- 
+  \ingroup contacts-backends
+
   Instances of this class are usually provided by a
   \l QContactManagerEngineFactory, which is loaded from a plugin.
- 
+
   The default implementation of this interface provides a basic
   level of functionality for some functions so that specific engines
   can simply implement the functionality that is supported by
   the specific contacts engine that is being adapted.
- 
+
   More information on writing a contacts engine plugin is TODO.
- 
+
   \sa QContactManager, QContactManagerEngineFactory
  */
 
@@ -83,19 +87,6 @@
  */
 
 /*!
-  \fn QContactManagerEngine::deref()
-
-  Notifies the engine that it is no longer required.  If this
-  engine can not be shared between managers, it is safe for the
-  engine to delete itself in this function.
-
-  If the engine implementation can be shared, this function can use a
-  reference count and track lifetime that way.  The factory that
-  returns an instance of this engine should increment the reference
-  count in this case.
- */
-
-/*!
   \fn QContactManagerEngine::dataChanged()
 
   This signal is emitted some time after changes occur to the data managed by this
@@ -218,112 +209,64 @@
 }
 
 /*!
-  Returns a list of contact ids sorted according to the given list of \a sortOrders.
-  Depending on the backend, this operation may involve retrieving all the contacts.
-  Any error which occurs will be saved in \a error.
- */
-QList<QContactLocalId> QContactManagerEngine::contactIds(const QList<QContactSortOrder>& sortOrders, QContactManager::Error& error) const
-{
-    Q_UNUSED(sortOrders);
-    error = QContactManager::NotSupportedError;
-    return QList<QContactLocalId>();
-}
-
-/*!
   Returns a list of contact ids that match the given \a filter, sorted according to the given list of \a sortOrders.
   Depending on the backend, this filtering operation may involve retrieving all the contacts.
   Any error which occurs will be saved in \a error.
  */
-QList<QContactLocalId> QContactManagerEngine::contactIds(const QContactFilter& filter, const QList<QContactSortOrder>& sortOrders, QContactManager::Error& error) const
+QList<QContactLocalId> QContactManagerEngine::contactIds(const QContactFilter& filter, const QList<QContactSortOrder>& sortOrders, QContactManager::Error* error) const
 {
-    /* Slow way */
-    QList<QContactLocalId> ret;
-
-    /* If the filter matches all ids, then return the list of all ids */
-    if (filter.type() == QContactFilter::DefaultFilter) {
-        const QList<QContactLocalId>& allIds = contactIds(sortOrders, error);
-        if (error != QContactManager::NoError)
-            return ret;
-        return allIds;
-    }
+    Q_UNUSED(filter);
+    Q_UNUSED(sortOrders);
 
-    /* Otherwise, retrieve all contacts, test and return matching */
-    const QList<QContact>& all = contacts(sortOrders, QStringList(), error);
-
-    if (error != QContactManager::NoError)
-        return ret;
-
-    for (int j = 0; j < all.count(); j++) {
-        if (testFilter(filter, all.at(j)))
-            ret << all.at(j).localId();
-    }
-
-    return ret;
+    *error = QContactManager::NotSupportedError;
+    return QList<QContactLocalId>();
 }
 
 /*!
-  Returns the list of contacts stored in the manager sorted according to the given list of \a sortOrders.
-  If the given list of detail definition names \a definitionRestrictions is empty, each contact returned will include
-  all of the details which are stored in it, otherwise only those details which are of a definition whose name is included
-  in the \a definitionRestrictions list will be included.
-  Any error which occurs will be saved in \a error.
+  Returns the list of contacts which match the given \a filter stored in the manager sorted according to the given list of \a sortOrders.
+
+  Any operation error which occurs will be saved in \a error.
+
+  The \a fetchHint parameter describes the optimization hints that a manager may take.
+  If the \a fetchHint is the default constructed hint, all existing details, relationships and action preferences
+  in the matching contacts will be returned.  A client should not make changes to a contact which has
+  been retrieved using a fetch hint other than the default fetch hint.  Doing so will result in information
+  loss when saving the contact back to the manager (as the "new" restricted contact will
+  replace the previously saved contact in the backend).
+
+  \sa QContactFetchHint
  */
-QList<QContact> QContactManagerEngine::contacts(const QList<QContactSortOrder>& sortOrders, const QStringList& definitionRestrictions, QContactManager::Error& error) const
+QList<QContact> QContactManagerEngine::contacts(const QContactFilter& filter, const QList<QContactSortOrder>& sortOrders, const QContactFetchHint& fetchHint, QContactManager::Error* error) const
 {
+    Q_UNUSED(filter);
     Q_UNUSED(sortOrders);
-    Q_UNUSED(definitionRestrictions);
-    error = QContactManager::NotSupportedError;
+    Q_UNUSED(fetchHint);
+    *error = QContactManager::NotSupportedError;
     return QList<QContact>();
 }
 
 /*!
-  Returns a list of contacs that match the given \a filter, sorted according to the given list of \a sortOrders.
-  Depending on the backend, this filtering operation may involve retrieving all the contacts.
-  If the given list of detail definition names \a definitionRestrictions is empty, each contact returned will include
-  all of the details which are stored in it, otherwise only those details which are of a definition whose name is included
-  in the \a definitionRestrictions list will be included.
-  Any error which occurs will be saved in \a error.
- */
-QList<QContact> QContactManagerEngine::contacts(const QContactFilter& filter, const QList<QContactSortOrder>& sortOrders, const QStringList& definitionRestrictions, QContactManager::Error& error) const
-{
-    /* Slow way */
-    QList<QContact> ret;
+  Returns the contact in the database identified by \a contactId.
 
-    /* Retrieve each contact.. . . */
-    const QList<QContact>& all = contacts(sortOrders, definitionRestrictions, error);
-    if (error != QContactManager::NoError)
-        return ret;
+  If the contact does not exist, an empty, default constructed QContact will be returned,
+  and the \a error will be set to  \c QContactManager::DoesNotExistError.
 
-    if (filter.type() == QContactFilter::DefaultFilter)
-        return all;
+  Any operation error which occurs will be saved in \a error.
 
-    for (int j = 0; j < all.count(); j++) {
-        if (testFilter(filter, all.at(j))) {
-            ret << all.at(j);
-        }
-    }
-
-    return ret;
-}
+  The \a fetchHint parameter describes the optimization hints that a manager may take.
+  If the \a fetchHint is the default constructed hint, all existing details, relationships and action preferences
+  in the matching contact will be returned.  A client should not make changes to a contact which has
+  been retrieved using a fetch hint other than the default fetch hint.  Doing so will result in information
+  loss when saving the contact back to the manager (as the "new" restricted contact will
+  replace the previously saved contact in the backend).
 
-/*!
-  Returns the contact in the database identified by \a contactId.
-  If the list of detail definition names \a definitionRestrictions given is non-empty,
-  the contact returned will contain at least those details which are of a definition whose name is
-  contained in the \a definitionRestrictions list.
-  Note that the returned contact may also contain other details, but this function guarantees that
-  all details whose definition name is included in the given list of definition names \a definitionRestrictions
-  will be included in the returned contact.
-
-  The default implementation returns the entire contact.
-
-  Any errors encountered should be stored to \a error.
+  \sa QContactFetchHint
  */
-QContact QContactManagerEngine::contact(const QContactLocalId& contactId, const QStringList& definitionRestrictions, QContactManager::Error& error) const
+QContact QContactManagerEngine::contact(const QContactLocalId& contactId, const QContactFetchHint& fetchHint, QContactManager::Error* error) const
 {
     Q_UNUSED(contactId);
-    Q_UNUSED(definitionRestrictions);
-    error = QContactManager::NotSupportedError;
+    Q_UNUSED(fetchHint);
+    *error = QContactManager::NotSupportedError;
     return QContact();
 }
 
@@ -338,10 +281,11 @@
   \c QContactManager::NotSupportedError and the function will
   return false.
  */
-bool QContactManagerEngine::setSelfContactId(const QContactLocalId& contactId, QContactManager::Error& error)
+bool QContactManagerEngine::setSelfContactId(const QContactLocalId& contactId, QContactManager::Error* error)
 {
     Q_UNUSED(contactId);
-    error = QContactManager::NotSupportedError;
+
+    *error = QContactManager::NotSupportedError;
     return false;
 }
 
@@ -352,9 +296,9 @@
   the concept of a "self" contact, an invalid id will be returned
   and the \a error will be set to \c QContactManager::DoesNotExistError.
  */
-QContactLocalId QContactManagerEngine::selfContactId(QContactManager::Error& error) const
+QContactLocalId QContactManagerEngine::selfContactId(QContactManager::Error* error) const
 {
-    error = QContactManager::DoesNotExistError;
+    *error = QContactManager::DoesNotExistError;
     return QContactLocalId();
 }
 
@@ -365,46 +309,65 @@
   If no relationships of the given \a relationshipType in which the contact identified by the given \a participantId is involved in the given \a role exists,
   \a error is set to QContactManager::DoesNotExistError.
  */
-QList<QContactRelationship> QContactManagerEngine::relationships(const QString& relationshipType, const QContactId& participantId, QContactRelationshipFilter::Role role, QContactManager::Error& error) const
+QList<QContactRelationship> QContactManagerEngine::relationships(const QString& relationshipType, const QContactId& participantId, QContactRelationship::Role role, QContactManager::Error* error) const
 {
     Q_UNUSED(relationshipType);
     Q_UNUSED(participantId);
     Q_UNUSED(role);
-    error = QContactManager::NotSupportedError;
+
+    *error = QContactManager::NotSupportedError;
     return QList<QContactRelationship>();
 }
 
 /*!
+  Saves the given \a relationships in the database and returns true if the operation was successful.
+  For any relationship which was unable to be saved, an entry into the \a errorMap will be created,
+  with the key being the index into the input relationships list, and the value being the error which
+  occurred for that index.
+
+  The overall operation error will be saved in \a error.
+ */
+bool QContactManagerEngine::saveRelationships(QList<QContactRelationship>* relationships, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
+{
+    Q_UNUSED(relationships);
+    Q_UNUSED(errorMap);
+
+    *error = QContactManager::NotSupportedError;
+    return false;
+}
+
+/*!
   Saves the given \a relationship in the database.  If the relationship already exists in the database, this function will
   return \c false and the \a error will be set to \c QContactManager::AlreadyExistsError.
   If the relationship is saved successfully, this function will return \c true and \a error will be set
   to \c QContactManager::NoError.  Note that relationships cannot be updated directly using this function; in order
   to update a relationship, you must remove the old relationship, make the required modifications, and then save it.
 
-  The given relationship is invalid if it is circular (one of the destination contacts is also the source contact), or
-  if it references a non-existent local contact (either source or destination).  If the given \a relationship is invalid,
+  The given relationship is invalid if it is circular (the first contact is the second contact), or
+  if it references a non-existent local contact (either the first or second contact).  If the given \a relationship is invalid,
   the function will return \c false and the \a error will be set to \c QContactManager::InvalidRelationshipError.
-  If the given \a relationship could not be saved in the database (due to backend limitations)
-  the function will return \c false and \a error will be set to \c QContactManager::NotSupportedError.
 
-  If any destination contact manager URI is not set in the \a relationship, these will be
-  automatically set to the URI of this manager, before the relationship is saved.
+  The default implementation of this function converts the argument into a call to saveRelationships.
  */
-bool QContactManagerEngine::saveRelationship(QContactRelationship* relationship, QContactManager::Error& error)
+bool QContactManagerEngine::saveRelationship(QContactRelationship *relationship, QContactManager::Error *error)
 {
-    Q_UNUSED(relationship);
-    error = QContactManager::NotSupportedError;
-    return false;
-}
+    // Convert to a list op
+    if (relationship) {
+        QList<QContactRelationship> list;
+        list.append(*relationship);
+
+        QMap<int, QContactManager::Error> errors;
+        bool ret = saveRelationships(&list, &errors, error);
 
-/*!
-  Saves the given \a relationships in the database and returns a list of error codes.  Any error which occurs will be saved in \a error.
- */
-QList<QContactManager::Error> QContactManagerEngine::saveRelationships(QList<QContactRelationship>* relationships, QContactManager::Error& error)
-{
-    Q_UNUSED(relationships);
-    error = QContactManager::NotSupportedError;
-    return QList<QContactManager::Error>();
+        if (errors.count() > 0)
+            *error = errors.begin().value();
+
+        *relationship = list.value(0);
+        return ret;
+    } else {
+        *error = QContactManager::BadArgumentError;
+        return false;
+    }
 }
 
 /*!
@@ -413,23 +376,39 @@
   relationship exists in the manager, the \a error will be set to \c QContactManager::DoesNotExistError and this function
   will return false.
 
-  The priority of the relationship is ignored when determining existence of the relationship.
+  The default implementation of this function converts the argument into a call to removeRelationships
  */
-bool QContactManagerEngine::removeRelationship(const QContactRelationship& relationship, QContactManager::Error& error)
+bool QContactManagerEngine::removeRelationship(const QContactRelationship& relationship, QContactManager::Error* error)
 {
-    Q_UNUSED(relationship);
-    error = QContactManager::DoesNotExistError;
-    return false;
+    // Convert to a list op
+    QList<QContactRelationship> list;
+    list.append(relationship);
+
+    QMap<int, QContactManager::Error> errors;
+    bool ret = removeRelationships(list, &errors, error);
+
+    if (errors.count() > 0)
+        *error = errors.begin().value();
+
+    return ret;
 }
 
+
 /*!
-  Removes the given \a relationships from the database and returns a list of error codes.  Any error which occurs will be saved in \a error.
+  Removes the given \a relationships from the database and returns true if the operation was successful.
+  For any relationship which was unable to be removed, an entry into the \a errorMap will be created,
+  with the key being the index into the input relationships list, and the value being the error which
+  occurred for that index.
+
+  The overall operation error will be saved in \a error.
  */
-QList<QContactManager::Error> QContactManagerEngine::removeRelationships(const QList<QContactRelationship>& relationships, QContactManager::Error& error)
+bool QContactManagerEngine::removeRelationships(const QList<QContactRelationship>& relationships, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
 {
     Q_UNUSED(relationships);
-    error = QContactManager::DoesNotExistError;
-    return QList<QContactManager::Error>();
+    Q_UNUSED(errorMap);
+
+    *error = QContactManager::NotSupportedError;
+    return false;
 }
 
 /*!
@@ -437,10 +416,10 @@
   Any error that occurs will be stored in \a error.
   Returns the synthesized display label.
  */
-QString QContactManagerEngine::synthesizedDisplayLabel(const QContact& contact, QContactManager::Error& error) const
+QString QContactManagerEngine::synthesizedDisplayLabel(const QContact& contact, QContactManager::Error* error) const
 {
     // synthesize the display name from the name of the contact, or, failing that, the organisation of the contact.
-    error = QContactManager::NoError;
+    *error = QContactManager::NoError;
     QList<QContactDetail> allNames = contact.details(QContactName::DefinitionName);
 
     const QLatin1String space(" ");
@@ -497,22 +476,21 @@
         }
     }
 
-    error = QContactManager::UnspecifiedError;
+    *error = QContactManager::UnspecifiedError;
     return QString();
 }
 
 /*!
-  Returns a copy of the given contact \a contact with its display label set to \a displayLabel.
+  Sets the contact display label of \a contact to the supplied \a displayLabel.
+
   This function does not touch the database in any way, and is purely a convenience to allow engine implementations to set the display label.
  */
-QContact QContactManagerEngine::setContactDisplayLabel(const QString& displayLabel, const QContact& contact) const
+void QContactManagerEngine::setContactDisplayLabel(QContact* contact, const QString& displayLabel)
 {
-    QContact retn = contact;
     QContactDisplayLabel dl;
     dl.setValue(QContactDisplayLabel::FieldLabel, displayLabel);
     setDetailAccessConstraints(&dl, QContactDetail::Irremovable);
-    retn.d->m_details.replace(0, dl);
-    return retn;
+    contact->d->m_details.replace(0, dl);
 }
 
 /*!
@@ -522,10 +500,168 @@
 {
     Q_UNUSED(feature);
     Q_UNUSED(contactType);
+
     return false;
 }
 
 /*!
+  Given an input \a filter, returns the canonical version of the filter.
+
+  Some of the following transformations may be applied:
+  \list
+   \o Any QContactActionFilters are transformed into the corresponding
+     QContactFilters returned by matching actions
+   \o Any QContactInvalidFilters contained in a union filter will be removed
+   \o Any default QContactFilters contained in an intersection filter will be removed
+   \o Any QContactIntersectionFilters with a QContactInvalidFilter contained will be
+     replaced with a QContactInvalidFilter
+   \o Any QContactUnionFilters with a default QContactFilter contained will be replaced
+     with a default QContactFilter
+   \o An empty QContactIntersectionFilter will be replaced with a QContactDefaultFilter
+   \o An empty QContactUnionFilter will be replaced with a QContactInvalidFilter
+   \o An empty QContactLocalIdFilter will be replaced with a QContactInvalidFilter
+   \o An intersection or union filter with a single entry will be replaced by that entry
+   \o A QContactDetailFilter or QContactDetailRangeFilter with no definition name will be replaced with a QContactInvalidFilter
+   \o A QContactDetailRangeFilter with no range specified will be converted to a QContactDetailFilter
+  \endlist
+*/
+QContactFilter QContactManagerEngine::canonicalizedFilter(const QContactFilter &filter)
+{
+    switch(filter.type()) {
+        case QContactFilter::ActionFilter:
+        {
+            // Find any matching actions, and do a union filter on their filter objects
+            QContactActionFilter af(filter);
+            QList<QContactActionDescriptor> descriptors = QContactAction::actionDescriptors(af.actionName(), af.vendorName(), af.implementationVersion());
+
+            QList<QContactFilter> filters;
+            // There's a small wrinkle if there's a value specified in the action filter
+            // we have to adjust any contained QContactDetailFilters to have that value
+            // or test if a QContactDetailRangeFilter contains this value already
+            for (int j = 0; j < descriptors.count(); j++) {
+                QContactAction* action = QContactAction::action(descriptors.at(j));
+
+                // Action filters are not allowed to return action filters, at all
+                // it's too annoying to check for recursion
+                QContactFilter d = action->contactFilter(af.value());
+                delete action; // clean up.
+                if (!validateActionFilter(d))
+                    continue;
+
+                filters.append(d);
+            }
+
+            if (filters.count() == 0)
+                return QContactInvalidFilter();
+            if (filters.count() == 1)
+                return filters.first();
+
+            QContactUnionFilter f;
+            f.setFilters(filters);
+            return canonicalizedFilter(f);
+        }
+        break;
+
+        case QContactFilter::IntersectionFilter:
+        {
+            QContactIntersectionFilter f(filter);
+            QList<QContactFilter> filters = f.filters();
+            QList<QContactFilter>::iterator it = filters.begin();
+
+            // XXX in theory we can remove duplicates in a set filter
+            while (it != filters.end()) {
+                QContactFilter canon = canonicalizedFilter(*it);
+                if (canon.type() == QContactFilter::DefaultFilter) {
+                    it = filters.erase(it);
+                } else if (canon.type() == QContactFilter::InvalidFilter) {
+                    return QContactInvalidFilter();
+                } else {
+                    *it = canon;
+                    ++it;
+                }
+            }
+
+            if (filters.count() == 0)
+                return QContactFilter();
+            if (filters.count() == 1)
+                return filters.first();
+
+            f.setFilters(filters);
+            return f;
+        }
+        break;
+
+        case QContactFilter::UnionFilter:
+        {
+            QContactUnionFilter f(filter);
+            QList<QContactFilter> filters = f.filters();
+            QList<QContactFilter>::iterator it = filters.begin();
+
+            // XXX in theory we can remove duplicates in a set filter
+            while (it != filters.end()) {
+                QContactFilter canon = canonicalizedFilter(*it);
+                if (canon.type() == QContactFilter::InvalidFilter) {
+                    it = filters.erase(it);
+                } else if (canon.type() == QContactFilter::DefaultFilter) {
+                    return QContactFilter();
+                } else {
+                    *it = canon;
+                    ++it;
+                }
+            }
+
+            if (filters.count() == 0)
+                return QContactInvalidFilter();
+            if (filters.count() == 1)
+                return filters.first();
+
+            f.setFilters(filters);
+            return f;
+        }
+        break;
+
+        case QContactFilter::LocalIdFilter:
+        {
+            QContactLocalIdFilter f(filter);
+            if (f.ids().count() == 0)
+                return QContactInvalidFilter();
+        }
+        break; // fall through to return at end
+
+        case QContactFilter::ContactDetailRangeFilter:
+        {
+            QContactDetailRangeFilter f(filter);
+            if (f.detailDefinitionName().isEmpty())
+                return QContactInvalidFilter();
+            if (f.minValue() == f.maxValue()
+                && f.rangeFlags() == (QContactDetailRangeFilter::ExcludeLower | QContactDetailRangeFilter::ExcludeUpper))
+                return QContactInvalidFilter();
+            if ((f.minValue().isNull() && f.maxValue().isNull()) || (f.minValue() == f.maxValue())) {
+                QContactDetailFilter df;
+                df.setDetailDefinitionName(f.detailDefinitionName(), f.detailFieldName());
+                df.setMatchFlags(f.matchFlags());
+                df.setValue(f.minValue());
+                return df;
+            }
+        }
+        break; // fall through to return at end
+
+        case QContactFilter::ContactDetailFilter:
+        {
+            QContactDetailFilter f(filter);
+            if (f.detailDefinitionName().isEmpty())
+                return QContactInvalidFilter();
+        }
+        break; // fall through to return at end
+
+        default:
+            break; // fall through to return at end
+    }
+    return filter;
+}
+
+
+/*!
   Returns a whether the supplied \a filter can be implemented
   natively by this engine.  If not, the base class implementation
   will emulate the functionality.
@@ -533,6 +669,7 @@
 bool QContactManagerEngine::isFilterSupported(const QContactFilter& filter) const
 {
     Q_UNUSED(filter);
+
     return false;
 }
 
@@ -545,12 +682,20 @@
 }
 
 /*!
-  Returns the list of relationship types supported by this engine for contacts whose type is the given \a contactType.
+  Returns true if the manager supports the relationship type specified in \a relationshipType for
+  contacts whose type is the given \a contactType.
+
+  Note that some managers may support the relationship type for a contact in a limited manner
+  (for example, only as the first contact in the relationship, or only as the second contact
+  in the relationship).  In this case, it will still return true.  It will only return false
+  if the relationship is entirely unsupported for the given type of contact.
  */
-QStringList QContactManagerEngine::supportedRelationshipTypes(const QString& contactType) const
+bool QContactManagerEngine::isRelationshipTypeSupported(const QString& relationshipType, const QString& contactType) const
 {
+    Q_UNUSED(relationshipType);
     Q_UNUSED(contactType);
-    return QStringList();
+
+    return false;
 }
 
 /*!
@@ -562,7 +707,7 @@
 QStringList QContactManagerEngine::supportedContactTypes() const
 {
     QContactManager::Error error;
-    QList<QVariant> allowableVals = detailDefinition(QContactType::DefinitionName, QContactType::TypeContact, error).fields().value(QContactType::FieldType).allowableValues();
+    QList<QVariant> allowableVals = detailDefinition(QContactType::DefinitionName, QContactType::TypeContact, &error).fields().value(QContactType::FieldType).allowableValues();
     QStringList retn;
     for (int i = 0; i < allowableVals.size(); i++)
         retn += allowableVals.at(i).toString();
@@ -570,13 +715,10 @@
 }
 
 /*!
+  \fn int QContactManagerEngine::managerVersion() const
+
   Returns the engine backend implementation version number
  */
-int QContactManagerEngine::managerVersion() const
-{
-    return 0;
-}
-  
 
 /*! Returns the base schema definitions */
 QMap<QString, QMap<QString, QContactDetailDefinition> > QContactManagerEngine::schemaDefinitions()
@@ -677,9 +819,10 @@
     f.setDataType(QVariant::String);
     f.setAllowableValues(QVariantList());
     fields.insert(QContactOrganization::FieldName, f);
-    fields.insert(QContactOrganization::FieldLogo, f);
     fields.insert(QContactOrganization::FieldLocation, f);
     fields.insert(QContactOrganization::FieldTitle, f);
+    f.setDataType(QVariant::Url);
+    fields.insert(QContactOrganization::FieldLogoUrl, f);
     f.setDataType(QVariant::StringList);
     fields.insert(QContactOrganization::FieldDepartment, f);
     f.setAllowableValues(contexts);
@@ -700,7 +843,7 @@
     subTypes << QString(QLatin1String(QContactPhoneNumber::SubTypeBulletinBoardSystem));
     subTypes << QString(QLatin1String(QContactPhoneNumber::SubTypeCar));
     subTypes << QString(QLatin1String(QContactPhoneNumber::SubTypeDtmfMenu));
-    subTypes << QString(QLatin1String(QContactPhoneNumber::SubTypeFacsimile));
+    subTypes << QString(QLatin1String(QContactPhoneNumber::SubTypeFax));
     subTypes << QString(QLatin1String(QContactPhoneNumber::SubTypeLandline));
     subTypes << QString(QLatin1String(QContactPhoneNumber::SubTypeMessagingCapable));
     subTypes << QString(QLatin1String(QContactPhoneNumber::SubTypeMobile));
@@ -827,18 +970,6 @@
     f.setDataType(QVariant::String);
     fields.insert(QContactOnlineAccount::FieldAccountUri, f);
     fields.insert(QContactOnlineAccount::FieldServiceProvider, f);
-    fields.insert(QContactOnlineAccount::FieldNickname, f);
-    fields.insert(QContactOnlineAccount::FieldStatusMessage, f);
-    QVariantList presenceValues;
-    presenceValues << QString(QLatin1String(QContactOnlineAccount::PresenceAvailable));
-    presenceValues << QString(QLatin1String(QContactOnlineAccount::PresenceHidden));
-    presenceValues << QString(QLatin1String(QContactOnlineAccount::PresenceBusy));
-    presenceValues << QString(QLatin1String(QContactOnlineAccount::PresenceAway));
-    presenceValues << QString(QLatin1String(QContactOnlineAccount::PresenceExtendedAway));
-    presenceValues << QString(QLatin1String(QContactOnlineAccount::PresenceUnknown));
-    presenceValues << QString(QLatin1String(QContactOnlineAccount::PresenceOffline));
-    f.setAllowableValues(presenceValues);
-    fields.insert(QContactOnlineAccount::FieldPresence, f);
     f.setDataType(QVariant::StringList);
     f.setAllowableValues(contexts);
     fields.insert(QContactDetail::FieldContext, f);
@@ -848,24 +979,30 @@
     d.setUnique(false);
     retn.insert(d.name(), d);
 
-    // avatar
-    d.setName(QContactAvatar::DefinitionName);
+    // presence
+    d.setName(QContactPresence::DefinitionName);
     fields.clear();
-    f.setDataType(QVariant::String);
-    f.setAllowableValues(QVariantList());
-    fields.insert(QContactAvatar::FieldAvatar, f);
-
-    f.setDataType(QVariant::Pixmap);
     f.setAllowableValues(QVariantList());
-    fields.insert(QContactAvatar::FieldAvatarPixmap, f);
-
-    f.setDataType(QVariant::String); // only allowed to be a single subtype
-    subTypes.clear();
-    subTypes << QString(QLatin1String(QContactAvatar::SubTypeImage));
-    subTypes << QString(QLatin1String(QContactAvatar::SubTypeTexturedMesh));
-    subTypes << QString(QLatin1String(QContactAvatar::SubTypeVideo));
-    f.setAllowableValues(subTypes);
-    fields.insert(QContactAvatar::FieldSubType, f);
+    f.setDataType(QVariant::DateTime);
+    fields.insert(QContactPresence::FieldTimestamp, f);
+    f.setDataType(QVariant::String);
+    fields.insert(QContactPresence::FieldNickname, f);
+    fields.insert(QContactPresence::FieldCustomMessage, f);
+    fields.insert(QContactPresence::FieldPresenceStateText, f);
+    QVariantList presenceValues;
+    presenceValues << QContactPresence::PresenceAvailable;
+    presenceValues << QContactPresence::PresenceAway;
+    presenceValues << QContactPresence::PresenceBusy;
+    presenceValues << QContactPresence::PresenceExtendedAway;
+    presenceValues << QContactPresence::PresenceHidden;
+    presenceValues << QContactPresence::PresenceOffline;
+    presenceValues << QContactPresence::PresenceUnknown;
+    f.setAllowableValues(presenceValues);
+    f.setDataType(QVariant::Int);
+    fields.insert(QContactPresence::FieldPresenceState, f);
+    f.setAllowableValues(QVariantList());
+    f.setDataType(QVariant::Url);
+    fields.insert(QContactPresence::FieldPresenceStateImageUrl, f);
     f.setDataType(QVariant::StringList);
     f.setAllowableValues(contexts);
     fields.insert(QContactDetail::FieldContext, f);
@@ -873,6 +1010,68 @@
     d.setUnique(false);
     retn.insert(d.name(), d);
 
+    // global presence
+    d.setName(QContactGlobalPresence::DefinitionName);
+    fields.clear();
+    f.setAllowableValues(QVariantList());
+    f.setDataType(QVariant::DateTime);
+    fields.insert(QContactGlobalPresence::FieldTimestamp, f);
+    f.setDataType(QVariant::String);
+    fields.insert(QContactGlobalPresence::FieldNickname, f);
+    fields.insert(QContactGlobalPresence::FieldCustomMessage, f);
+    fields.insert(QContactGlobalPresence::FieldPresenceStateText, f);
+    f.setAllowableValues(presenceValues);
+    f.setDataType(QVariant::Int);
+    fields.insert(QContactGlobalPresence::FieldPresenceState, f);
+    f.setAllowableValues(QVariantList());
+    f.setDataType(QVariant::Url);
+    fields.insert(QContactGlobalPresence::FieldPresenceStateImageUrl, f);
+    f.setDataType(QVariant::StringList);
+    f.setAllowableValues(contexts);
+    fields.insert(QContactDetail::FieldContext, f);
+    d.setFields(fields);
+    d.setUnique(true); // unique and read only!
+    retn.insert(d.name(), d);
+
+    // avatar
+    d.setName(QContactAvatar::DefinitionName);
+    fields.clear();
+    f.setDataType(QVariant::Url);
+    f.setAllowableValues(QVariantList());
+    fields.insert(QContactAvatar::FieldImageUrl, f);
+    fields.insert(QContactAvatar::FieldVideoUrl, f);
+    f.setDataType(QVariant::StringList);
+    f.setAllowableValues(contexts);
+    fields.insert(QContactDetail::FieldContext, f);
+    d.setFields(fields);
+    d.setUnique(false);
+    retn.insert(d.name(), d);
+
+    // ringtone
+    d.setName(QContactRingtone::DefinitionName);
+    fields.clear();
+    f.setDataType(QVariant::Url);
+    f.setAllowableValues(QVariantList());
+    fields.insert(QContactRingtone::FieldAudioRingtoneUrl, f);
+    fields.insert(QContactRingtone::FieldVideoRingtoneUrl, f);
+    fields.insert(QContactRingtone::FieldVibrationRingtoneUrl, f);
+    f.setDataType(QVariant::StringList);
+    f.setAllowableValues(contexts);
+    fields.insert(QContactDetail::FieldContext, f);
+    d.setFields(fields);
+    d.setUnique(false);
+    retn.insert(d.name(), d);
+
+    // thumbnail
+    d.setName(QContactThumbnail::DefinitionName);
+    fields.clear();
+    f.setDataType(QVariant::Image);
+    f.setAllowableValues(QVariantList());
+    fields.insert(QContactThumbnail::FieldThumbnail, f);
+    d.setFields(fields);
+    d.setUnique(true); // only one thumbnail, no context.
+    retn.insert(d.name(), d);
+
     // GeoLocation
     d.setName(QContactGeoLocation::DefinitionName);
     fields.clear();
@@ -940,6 +1139,19 @@
     d.setUnique(false);
     retn.insert(d.name(), d);
 
+    // tag
+    d.setName(QContactTag::DefinitionName);
+    fields.clear();
+    f.setDataType(QVariant::String);
+    f.setAllowableValues(QVariantList());
+    fields.insert(QContactTag::FieldTag, f);
+    f.setDataType(QVariant::StringList);
+    f.setAllowableValues(contexts);
+    fields.insert(QContactDetail::FieldContext, f);
+    d.setFields(fields);
+    d.setUnique(false);
+    retn.insert(d.name(), d);
+
     // in the default schema, we have two contact types: TypeContact, TypeGroup.
     // the entire default schema is valid for both types.
     QMap<QString, QMap<QString, QContactDetailDefinition> > retnSchema;
@@ -949,57 +1161,6 @@
     return retnSchema;
 }
 
-
-/*!
-  Adds the given \a contact to the database if \a contact has a
-  default-constructed id, or an id with the manager URI set to the URI of
-  this manager and a local id of zero.
-
-  If the manager URI of the id of the \a contact is neither empty nor equal to the URI of
-  this manager, or local id of the \a contact is non-zero but does not exist in the
-  manager, the operation will fail and \a error will be set to
-  \c QContactManager::DoesNotExistError.
-
-  Alternatively, the function will update the existing contact in the database if \a contact
-  has a non-zero id and currently exists in the database.
-
-  If the \a contact contains one or more details whose definitions have
-  not yet been saved with the manager, the operation will fail and \a error will be
-  set to \c QContactManager::UnsupportedError.
-
-  If the \a contact has had its relationships reordered, the manager
-  will check to make sure that every relationship that the contact is currently
-  involved in is included in the reordered list, and that no relationships which
-  either do not involve the contact, or have not been saved in the manager are
-  included in the list.  If these conditions are not met, the function will
-  return \c false and \a error will be set to \c QContactManager::InvalidRelationshipError.
-
-  The engine must automatically synthesize the display label of the contact when it is saved,
-  by either using the built in \l synthesizedDisplayLabel() function or overriding it, and
-  then calling \l setContactDisplayLabel().
-
-  Returns false on failure, or true on
-  success.  On successful save of a contact with an id of zero, its
-  id will be set to a new, valid id with the manager URI set to the URI of
-  this manager, and the local id set to a new, valid local id.
-
-  This function is called by the contacts framework in both the
-  single contact save and batch contact save, if the saveContacts
-  function is not overridden.
-
-  The backend must emit the appropriate signals to inform clients of changes
-  to the database resulting from this operation.
-
-  Any errors encountered during this operation should be stored to
-  \a error.
- */
-bool QContactManagerEngine::saveContact(QContact* contact, QContactManager::Error& error)
-{
-    Q_UNUSED(contact);
-    error = QContactManager::NotSupportedError;
-    return false;
-}
-
 /*!
   Checks that the given contact \a contact does not have details which
   don't conform to a valid definition, violate uniqueness constraints,
@@ -1016,7 +1177,7 @@
   Any errors encountered during this operation should be stored to
   \a error.
  */
-bool QContactManagerEngine::validateContact(const QContact& contact, QContactManager::Error& error) const
+bool QContactManagerEngine::validateContact(const QContact& contact, QContactManager::Error* error) const
 {
     QList<QString> uniqueDefinitionIds;
 
@@ -1026,15 +1187,15 @@
         QVariantMap values = d.variantValues();
         QContactDetailDefinition def = detailDefinition(d.definitionName(), contact.type(), error);
         // check that the definition is supported
-        if (error != QContactManager::NoError) {
-            error = QContactManager::InvalidDetailError;
+        if (*error != QContactManager::NoError) {
+            *error = QContactManager::InvalidDetailError;
             return false; // this definition is not supported.
         }
 
         // check uniqueness
         if (def.isUnique()) {
             if (uniqueDefinitionIds.contains(def.name())) {
-                error = QContactManager::AlreadyExistsError;
+                *error = QContactManager::AlreadyExistsError;
                 return false; // can't have two of a unique detail.
             }
             uniqueDefinitionIds.append(def.name());
@@ -1045,14 +1206,14 @@
             const QString& key = keys.at(i);
             // check that no values exist for nonexistent fields.
             if (!def.fields().contains(key)) {
-                error = QContactManager::InvalidDetailError;
+                *error = QContactManager::InvalidDetailError;
                 return false; // value for nonexistent field.
             }
 
             QContactDetailFieldDefinition field = def.fields().value(key);
             // check that the type of each value corresponds to the allowable field type
             if (static_cast<int>(field.dataType()) != values.value(key).userType()) {
-                error = QContactManager::InvalidDetailError;
+                *error = QContactManager::InvalidDetailError;
                 return false; // type doesn't match.
             }
 
@@ -1064,13 +1225,13 @@
                     QList<QVariant> innerValues = values.value(key).toList();
                     for (int i = 0; i < innerValues.size(); i++) {
                         if (!field.allowableValues().contains(innerValues.at(i))) {
-                            error = QContactManager::InvalidDetailError;
+                            *error = QContactManager::InvalidDetailError;
                             return false; // value not allowed.
                         }
                     }
                 } else if (!field.allowableValues().contains(values.value(key))) {
                     // the datatype is not a list; the value wasn't allowed.
-                    error = QContactManager::InvalidDetailError;
+                    *error = QContactManager::InvalidDetailError;
                     return false; // value not allowed.
                 }
             }
@@ -1080,7 +1241,6 @@
     return true;
 }
 
-
 /*!
   Checks that the given detail definition \a definition seems valid,
   with a correct id, defined fields, and any specified value types
@@ -1093,15 +1253,15 @@
   Any errors encountered during this operation should be stored to
   \a error.
  */
-bool QContactManagerEngine::validateDefinition(const QContactDetailDefinition& definition, QContactManager::Error& error) const
+bool QContactManagerEngine::validateDefinition(const QContactDetailDefinition& definition, QContactManager::Error* error) const
 {
     if (definition.name().isEmpty()) {
-        error = QContactManager::BadArgumentError;
+        *error = QContactManager::BadArgumentError;
         return false;
     }
 
     if (definition.fields().count() == 0) {
-        error = QContactManager::BadArgumentError;
+        *error = QContactManager::BadArgumentError;
         return false;
     }
 
@@ -1111,56 +1271,37 @@
     while(it.hasNext()) {
         it.next();
         if (it.key().isEmpty()) {
-            error = QContactManager::BadArgumentError;
+            *error = QContactManager::BadArgumentError;
             return false;
         }
 
         if (!types.contains(it.value().dataType())) {
-            error = QContactManager::BadArgumentError;
+            *error = QContactManager::BadArgumentError;
             return false;
         }
 
         // Check that each allowed value is the same type
         for (int i=0; i < it.value().allowableValues().count(); i++) {
             if (it.value().allowableValues().at(i).type() != it.value().dataType()) {
-                error = QContactManager::BadArgumentError;
+                *error = QContactManager::BadArgumentError;
                 return false;
             }
         }
     }
-    error = QContactManager::NoError;
+    *error = QContactManager::NoError;
     return true;
 }
 
 /*!
-  Remove the contact identified by \a contactId from the database,
-  and removes the contact from any relationships in which it was involved.
-  Returns true if the contact was removed successfully, otherwise
-  returns false.
-
-  The backend must emit the appropriate signals to inform clients of changes
-  to the database resulting from this operation.
-
-  Any errors encountered during this operation should be stored to
-  \a error.
- */
-bool QContactManagerEngine::removeContact(const QContactLocalId& contactId, QContactManager::Error& error)
-{
-    Q_UNUSED(contactId);
-    error = QContactManager::NotSupportedError;
-    return false;
-}
-
-/*!
   Returns the registered detail definitions which are valid for contacts whose type is of the given \a contactType in this engine.
 
   Any errors encountered during this operation should be stored to
   \a error.
  */
-QMap<QString, QContactDetailDefinition> QContactManagerEngine::detailDefinitions(const QString& contactType, QContactManager::Error& error) const
+QMap<QString, QContactDetailDefinition> QContactManagerEngine::detailDefinitions(const QString& contactType, QContactManager::Error* error) const
 {
     Q_UNUSED(contactType);
-    error = QContactManager::NotSupportedError;
+    *error = QContactManager::NotSupportedError;
     return QMap<QString, QContactDetailDefinition>();
 }
 
@@ -1172,16 +1313,14 @@
   Any errors encountered during this operation should be stored to
   \a error.
  */
-QContactDetailDefinition QContactManagerEngine::detailDefinition(const QString& definitionName, const QString& contactType, QContactManager::Error& error) const
+QContactDetailDefinition QContactManagerEngine::detailDefinition(const QString& definitionName, const QString& contactType, QContactManager::Error* error) const
 {
-    Q_UNUSED(definitionName);
-
     QMap<QString, QContactDetailDefinition> definitions = detailDefinitions(contactType, error);
     if (definitions.contains(definitionName))  {
-        error = QContactManager::NoError;
+        *error = QContactManager::NoError;
         return definitions.value(definitionName);
     } else {
-        error = QContactManager::DoesNotExistError;
+        *error = QContactManager::DoesNotExistError;
         return QContactDetailDefinition();
     }
 }
@@ -1197,11 +1336,12 @@
   Any errors encountered during this operation should be stored to
   \a error.
  */
-bool QContactManagerEngine::saveDetailDefinition(const QContactDetailDefinition& def, const QString& contactType, QContactManager::Error& error)
+bool QContactManagerEngine::saveDetailDefinition(const QContactDetailDefinition& def, const QString& contactType, QContactManager::Error* error)
 {
     Q_UNUSED(def);
     Q_UNUSED(contactType);
-    error = QContactManager::NotSupportedError;
+
+    *error = QContactManager::NotSupportedError;
     return false;
 }
 
@@ -1216,11 +1356,12 @@
   Any errors encountered during this operation should be stored to
   \a error.
  */
-bool QContactManagerEngine::removeDetailDefinition(const QString& definitionName, const QString& contactType, QContactManager::Error& error)
+bool QContactManagerEngine::removeDetailDefinition(const QString& definitionName, const QString& contactType, QContactManager::Error* error)
 {
     Q_UNUSED(definitionName);
     Q_UNUSED(contactType);
-    error = QContactManager::NotSupportedError;
+
+    *error = QContactManager::NotSupportedError;
     return false;
 }
 
@@ -1234,13 +1375,76 @@
   Application code should not call this function, since validation of the
   detail will happen in the engine in any case.
  */
-void QContactManagerEngine::setDetailAccessConstraints(QContactDetail *detail, QContactDetail::AccessConstraints constraints) const
+void QContactManagerEngine::setDetailAccessConstraints(QContactDetail *detail, QContactDetail::AccessConstraints constraints)
 {
     if (detail) {
         QContactDetailPrivate::setAccessConstraints(detail, constraints);
     }
 }
 
+
+/*!
+  Adds the given \a contact to the database if \a contact has a
+  default-constructed id, or an id with the manager URI set to the URI of
+  this manager and a local id of zero, otherwise updates the contact in
+  the database which has the same id to be the given \a contact.
+  If the id is non-zero but does not identify any contact stored in the
+  manager, the function will return false and \a error will be set to
+  \c QContactManager::DoesNotExistError.
+
+  Returns true if the save operation completed successfully, otherwise
+  returns false.  Any error which occurs will be saved in \a error.
+
+  The default implementation will convert this into a call to saveContacts.
+
+  \sa managerUri()
+ */
+bool QContactManagerEngine::saveContact(QContact* contact, QContactManager::Error* error)
+{
+    // Convert to a list op
+    if (contact) {
+        QList<QContact> list;
+        list.append(*contact);
+
+        QMap<int, QContactManager::Error> errors;
+        bool ret = saveContacts(&list, &errors, error);
+
+        if (errors.count() > 0)
+            *error = errors.begin().value();
+
+        *contact = list.value(0);
+        return ret;
+    } else {
+        *error = QContactManager::BadArgumentError;
+        return false;
+    }
+}
+
+/*!
+  Remove the contact identified by \a contactId from the database,
+  and also removes any relationships in which the contact was involved.
+  Returns true if the contact was removed successfully, otherwise
+  returns false.
+
+  Any error which occurs will be saved in \a error.
+
+  The default implementation will convert this into a call to removeContacts.
+ */
+bool QContactManagerEngine::removeContact(const QContactLocalId& contactId, QContactManager::Error* error)
+{
+    // Convert to a list op
+    QList<QContactLocalId> list;
+    list.append(contactId);
+
+    QMap<int, QContactManager::Error> errors;
+    bool ret = removeContacts(list, &errors, error);
+
+    if (errors.count() > 0)
+        *error = errors.begin().value();
+
+    return ret;
+}
+
 /*!
   Adds the list of contacts given by \a contacts list to the database.
   Returns true if the contacts were saved successfully, otherwise false.
@@ -1260,32 +1464,12 @@
 
   \sa QContactManager::saveContact()
  */
-bool QContactManagerEngine::saveContacts(QList<QContact>* contacts, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error& error)
+bool QContactManagerEngine::saveContacts(QList<QContact>* contacts, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
 {
-    if(errorMap) {
-        errorMap->clear();
-    }
-
-    if (!contacts) {
-        error = QContactManager::BadArgumentError;
-        return false;
-    }
-
-    QContactManager::Error functionError = QContactManager::NoError;
-    for (int i = 0; i < contacts->count(); i++) {
-        QContact current = contacts->at(i);
-        if (!saveContact(&current, error)) {
-            functionError = error;
-            if (errorMap) {
-                errorMap->insert(i, functionError);
-            }
-        } else {
-            (*contacts)[i] = current;
-        }
-    }
-
-    error = functionError;
-    return (functionError == QContactManager::NoError);
+    Q_UNUSED(contacts);
+    Q_UNUSED(errorMap);
+    *error = QContactManager::NotSupportedError;
+    return false;
 }
 
 /*!
@@ -1293,6 +1477,9 @@
   \a contactIds.  Returns true if all contacts were removed successfully,
   otherwise false.
 
+  Any contact that was removed successfully will have the relationships
+  in which it was involved removed also.
+
   The manager might populate \a errorMap (the map of indices of the \a contactIds list to
   the error which occurred when saving the contact at that index) for every
   index for which the contact could not be removed, if it is able.
@@ -1300,44 +1487,108 @@
   only return \c QContactManager::NoError if all contacts were removed
   successfully.
 
-  For each contact that was removed succesfully, the corresponding
-  id in the \a contactIds list will be retained but set to zero.  The id of contacts
-  that were not successfully removed will be left alone.
-
-  Any contact that was removed successfully will have the relationships
-  in which it was involved removed also.
+  If the list contains ids which do not identify a valid contact in the manager, the function will
+  remove any contacts which are identified by ids in the \a contactIds list, insert
+  \c QContactManager::DoesNotExist entries into the \a errorMap for the indices of invalid ids
+  in the \a contactIds list, return false, and set the overall operation error to
+  \c QContactManager::DoesNotExistError.
 
   Any errors encountered during this operation should be stored to
   \a error.
 
   \sa QContactManager::removeContact()
  */
-bool QContactManagerEngine::removeContacts(QList<QContactLocalId>* contactIds, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error& error)
+bool QContactManagerEngine::removeContacts(const QList<QContactLocalId>& contactIds, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
+{
+    Q_UNUSED(contactIds);
+    Q_UNUSED(errorMap);
+    *error = QContactManager::NotSupportedError;
+    return false;
+}
+
+/*!
+  Returns a pruned or modified version of the \a original contact which is valid and can be saved in the manager.
+  The returned contact might have entire details removed or arbitrarily changed.  The cache of relationships
+  in the contact are ignored entirely when considering compatibility with the backend, as they are
+  saved and validated separately.  Any error which occurs will be saved to \a error.
+ */
+QContact QContactManagerEngine::compatibleContact(const QContact& original, QContactManager::Error* error) const
 {
-    if(errorMap) {
-        errorMap->clear();
-    }
+    QContact conforming;
+    QContactManager::Error tempError;
+    QList<QString> uniqueDefinitionIds;
+    QList<QContactDetail> allDetails = original.details();
+    QMap<QString, QContactDetailDefinition> defs = detailDefinitions(original.type(), &tempError);
+    for (int j = 0; j < allDetails.size(); j++) {
+        // check that the detail conforms to the definition in this manager.
+        // if so, then add it to the conforming contact to be returned.  if not, prune it.
+        const QContactDetail& d = allDetails.at(j);
 
-    if (!contactIds) {
-        error = QContactManager::BadArgumentError;
-        return false;
-    }
+        QVariantMap values = d.variantValues();
+        QContactDetailDefinition def = detailDefinition(d.definitionName(), original.type(), &tempError);
+        // check that the definition is supported
+        if (*error != QContactManager::NoError) {
+            continue; // this definition is not supported.
+        }
+
+        // check uniqueness
+        if (def.isUnique()) {
+            if (uniqueDefinitionIds.contains(def.name())) {
+                continue; // can't have two of a unique detail.
+            }
+            uniqueDefinitionIds.append(def.name());
+        }
 
-    QContactManager::Error functionError = QContactManager::NoError;
-    for (int i = 0; i < contactIds->count(); i++) {
-        QContactLocalId current = contactIds->at(i);
-        if (!removeContact(current, error)) {
-            functionError = error;
-            if (errorMap) {
-                errorMap->insert(i, functionError);
+        bool addToConforming = true;
+        QList<QString> keys = values.keys();
+        for (int i=0; i < keys.count(); i++) {
+            const QString& key = keys.at(i);
+            // check that no values exist for nonexistent fields.
+            if (!def.fields().contains(key)) {
+                addToConforming = false;
+                break; // value for nonexistent field.
+            }
+
+            QContactDetailFieldDefinition field = def.fields().value(key);
+            // check that the type of each value corresponds to the allowable field type
+            if (static_cast<int>(field.dataType()) != values.value(key).userType()) {
+                addToConforming = false;
+                break; // type doesn't match.
             }
-        } else {
-            (*contactIds)[i] = 0;
+
+            // check that the value is allowable
+            // if the allowable values is an empty list, any are allowed.
+            if (!field.allowableValues().isEmpty()) {
+                // if the field datatype is a list, check that it contains only allowable values
+                if (field.dataType() == QVariant::List || field.dataType() == QVariant::StringList) {
+                    QList<QVariant> innerValues = values.value(key).toList();
+                    for (int i = 0; i < innerValues.size(); i++) {
+                        if (!field.allowableValues().contains(innerValues.at(i))) {
+                            addToConforming = false;
+                            break; // value not allowed.
+                        }
+                    }
+                } else if (!field.allowableValues().contains(values.value(key))) {
+                    // the datatype is not a list; the value wasn't allowed.
+                    addToConforming = false;
+                    break; // value not allowed.
+                }
+            }
+        }
+
+        // if it conforms to this manager's schema, save it in the conforming contact
+        // else, ignore it (prune it out of the conforming contact).
+        if (addToConforming) {
+            QContactDetail saveCopy = d;
+            conforming.saveDetail(&saveCopy);
         }
     }
 
-    error = functionError;
-    return (functionError == QContactManager::NoError);
+    if (!conforming.isEmpty())
+        *error = QContactManager::NoError;
+    else
+        *error = QContactManager::DoesNotExistError;
+    return conforming;
 }
 
 /*!
@@ -1461,7 +1712,92 @@
                 Qt::CaseSensitivity cs = (cdf.matchFlags() & QContactFilter::MatchCaseSensitive) ? Qt::CaseSensitive : Qt::CaseInsensitive;
 
                 /* See what flags are requested, since we're looking at a value */
-                if (cdf.matchFlags() & (QContactFilter::MatchEndsWith | QContactFilter::MatchStartsWith | QContactFilter::MatchContains | QContactFilter::MatchFixedString)) {
+                if (cdf.matchFlags() & QContactFilter::MatchPhoneNumber) {
+                    /* Doing phone number filtering.  We hand roll an implementation here, backends will obviously want to override this. */
+                    QString input = cdf.value().toString();
+
+                    /* preprocess the input - ignore any non-digits (doesn't perform ITU-T collation */
+                    QString preprocessedInput;
+                    for (int i = 0; i < input.size(); i++) {
+                        QChar current = input.at(i).toLower();
+                        if (current.isDigit()) preprocessedInput.append(current);
+                        // note: we ignore characters like '+', 'p', 'w', '*' and '#' which may be important.
+                    }
+
+                    /* Look at every detail in the set of details and compare */
+                    for (int j = 0; j < details.count(); j++) {
+                        const QContactDetail& detail = details.at(j);
+                        const QString& valueString = detail.value(cdf.detailFieldName());
+                        QString preprocessedValueString;
+                        for (int i = 0; i < valueString.size(); i++) {
+                            QChar current = valueString.at(i).toLower();
+                            if (current.isDigit()) preprocessedValueString.append(current);
+                            // note: we ignore characters like '+', 'p', 'w', '*' and '#' which may be important.
+                        }
+
+                        // if the matchflags input don't require a particular criteria to pass, we assume that it has passed.
+                        // the "default" match strategy is an "endsWith" strategy.
+                        bool me = (cdf.matchFlags() & 7) == QContactFilter::MatchExactly;
+                        bool mc = (cdf.matchFlags() & 7) == QContactFilter::MatchContains;
+                        bool msw = (cdf.matchFlags() & 7) == QContactFilter::MatchStartsWith;
+                        bool mew = (cdf.matchFlags() & 7) == QContactFilter::MatchEndsWith;
+
+                        bool mer = (me ? preprocessedValueString == preprocessedInput : true);
+                        bool mcr = (mc ? preprocessedValueString.contains(preprocessedInput) : true);
+                        bool mswr = (msw ? preprocessedValueString.startsWith(preprocessedInput) : true);
+                        bool mewr = (mew ? preprocessedValueString.endsWith(preprocessedInput) : true);
+                        if (mewr && mswr && mcr && mer) {
+                            return true; // this detail meets all of the criteria which were required, and hence must match.
+                        }
+                    }
+                } else if (cdf.matchFlags() & QContactFilter::MatchKeypadCollation) {
+                    // XXX TODO: not sure about the filtering semantics for MatchKeypadCollation.
+                    QString input = cdf.value().toString();
+
+                    /* Look at every detail in the set of details and compare */
+                    for (int j = 0; j < details.count(); j++) {
+                        const QContactDetail& detail = details.at(j);
+                        const QString& valueString = detail.value(cdf.detailFieldName()).toLower();
+
+                        // preprocess the valueString
+                        QString preprocessedValue;
+                        for (int i = 0; i < valueString.size(); i++) {
+                            // we use ITU-T keypad collation by default.
+                            QChar currentValueChar = valueString.at(i);
+                            if (currentValueChar == QLatin1Char('a') || currentValueChar == QLatin1Char('b') || currentValueChar == QLatin1Char('c'))
+                                preprocessedValue.append(QLatin1Char('2'));
+                            else if (currentValueChar == QLatin1Char('d') || currentValueChar == QLatin1Char('e') || currentValueChar == QLatin1Char('f'))
+                                preprocessedValue.append(QLatin1Char('3'));
+                            else if (currentValueChar == QLatin1Char('g') || currentValueChar == QLatin1Char('h') || currentValueChar == QLatin1Char('i'))
+                                preprocessedValue.append(QLatin1Char('4'));
+                            else if (currentValueChar == QLatin1Char('j') || currentValueChar == QLatin1Char('k') || currentValueChar == QLatin1Char('l'))
+                                preprocessedValue.append(QLatin1Char('5'));
+                            else if (currentValueChar == QLatin1Char('m') || currentValueChar == QLatin1Char('n') || currentValueChar == QLatin1Char('o'))
+                                preprocessedValue.append(QLatin1Char('6'));
+                            else if (currentValueChar == QLatin1Char('p') || currentValueChar == QLatin1Char('q') || currentValueChar == QLatin1Char('r') || currentValueChar == QLatin1Char('s'))
+                                preprocessedValue.append(QLatin1Char('7'));
+                            else if (currentValueChar == QLatin1Char('t') || currentValueChar == QLatin1Char('u') || currentValueChar == QLatin1Char('v'))
+                                preprocessedValue.append(QLatin1Char('8'));
+                            else if (currentValueChar == QLatin1Char('w') || currentValueChar == QLatin1Char('x') || currentValueChar == QLatin1Char('y') || currentValueChar == QLatin1Char('z'))
+                                preprocessedValue.append(QLatin1Char('9'));
+                            else
+                                preprocessedValue.append(currentValueChar);
+                        }
+
+                        bool me = (cdf.matchFlags() & 7) == QContactFilter::MatchExactly;
+                        bool mc = (cdf.matchFlags() & 7) == QContactFilter::MatchContains;
+                        bool msw = (cdf.matchFlags() & 7) == QContactFilter::MatchStartsWith;
+                        bool mew = (cdf.matchFlags() & 7) == QContactFilter::MatchEndsWith;
+
+                        bool mer = (me ? preprocessedValue == input : true);
+                        bool mcr = (mc ? preprocessedValue.contains(input) : true);
+                        bool mswr = (msw ? preprocessedValue.startsWith(input) : true);
+                        bool mewr = (mew ? preprocessedValue.endsWith(input) : true);
+                        if (mewr && mswr && mcr && mer) {
+                            return true; // this detail meets all of the criteria which were required, and hence must match.
+                        }
+                    }
+                } else if (cdf.matchFlags() & (QContactFilter::MatchEndsWith | QContactFilter::MatchStartsWith | QContactFilter::MatchContains | QContactFilter::MatchFixedString)) {
                     /* We're strictly doing string comparisons here */
                     bool matchStarts = (cdf.matchFlags() & 7) == QContactFilter::MatchStartsWith;
                     bool matchEnds = (cdf.matchFlags() & 7) == QContactFilter::MatchEndsWith;
@@ -1610,17 +1946,17 @@
                 // now check to see if we have a match.
                 foreach (const QContactRelationship& rel, allRelationships) {
                     // perform the matching.
-                    if (rf.relatedContactRole() == QContactRelationshipFilter::Second) { // this is the role of the related contact; ie, to match, contact.id() must be the first in the relationship.
+                    if (rf.relatedContactRole() == QContactRelationship::Second) { // this is the role of the related contact; ie, to match, contact.id() must be the first in the relationship.
                         if ((rf.relationshipType().isEmpty() || rel.relationshipType() == rf.relationshipType())
                                 && CONTACT_IDS_MATCH(rel.first(), contact.id()) && CONTACT_IDS_MATCH(relatedContactId, rel.second())) {
                             return true;
                         }
-                    } else if (rf.relatedContactRole() == QContactRelationshipFilter::First) { // this is the role of the related contact; ie, to match, contact.id() must be the second in the relationship.
+                    } else if (rf.relatedContactRole() == QContactRelationship::First) { // this is the role of the related contact; ie, to match, contact.id() must be the second in the relationship.
                         if ((rf.relationshipType().isEmpty() || rel.relationshipType() == rf.relationshipType())
                                 && CONTACT_IDS_MATCH(rel.second(), contact.id()) && CONTACT_IDS_MATCH(relatedContactId, rel.first())) {
                             return true;
                         }
-                    } else { // QContactRelationshipFilter::Either
+                    } else { // QContactRelationship::Either
                         if ((rf.relationshipType().isEmpty() || rel.relationshipType() == rf.relationshipType())
                                 && ((CONTACT_IDS_MATCH(relatedContactId, rel.first()) && !CONTACT_IDS_MATCH(contactUri, relatedContactId)) || (CONTACT_IDS_MATCH(relatedContactId, rel.second()) && !CONTACT_IDS_MATCH(contactUri, relatedContactId)))) {
                             return true;
@@ -1799,12 +2135,14 @@
  */
 void QContactManagerEngine::addSorted(QList<QContact>* sorted, const QContact& toAdd, const QList<QContactSortOrder>& sortOrders)
 {
-    for (int i = 0; i < sorted->size(); i++) {
-        // check to see if the new contact should be inserted here
-        int comparison = compareContact(sorted->at(i), toAdd, sortOrders);
-        if (comparison > 0) {
-            sorted->insert(i, toAdd);
-            return;
+    if (sortOrders.count() > 0) {
+        for (int i = 0; i < sorted->size(); i++) {
+            // check to see if the new contact should be inserted here
+            int comparison = compareContact(sorted->at(i), toAdd, sortOrders);
+            if (comparison > 0) {
+                sorted->insert(i, toAdd);
+                return;
+            }
         }
     }
 
@@ -1822,11 +2160,11 @@
             QContactManagerEngine::addSorted(&sortedContacts, c, sortOrders);
         }
 
-        foreach(const QContact c, sortedContacts) {
+        foreach(const QContact& c, sortedContacts) {
             sortedIds.append(c.localId());
         }
     } else {
-        foreach(const QContact c, cs) {
+        foreach(const QContact& c, cs) {
             sortedIds.append(c.localId());
         }
     }
@@ -1885,136 +2223,219 @@
 
 /*!
   Updates the given asynchronous request \a req by setting the new \a state
-  of the request.  It then causes the stateChanged() signal to be emitted by the request.
+  of the request.  If the new state is different, the stateChanged() signal
+  will be emitted by the request.
  */
 void QContactManagerEngine::updateRequestState(QContactAbstractRequest* req, QContactAbstractRequest::State state)
 {
-    req->d_ptr->m_state = state;
-    emit req->stateChanged(state);
+    if (req->d_ptr->m_state != state) {
+        req->d_ptr->m_state = state;
+        emit req->stateChanged(state);
+    }
 }
 
 /*!
   Updates the given QContactLocalIdFetchRequest \a req with the latest results \a result, and operation error \a error.
+  In addition, the state of the request will be changed to \a newState.
+
   It then causes the request to emit its resultsAvailable() signal to notify clients of the request progress.
+
+  If the new request state is different from the previous state, the stateChanged() signal will also be emitted from the request.
  */
-void QContactManagerEngine::updateContactLocalIdFetchRequest(QContactLocalIdFetchRequest* req, const QList<QContactLocalId>& result, QContactManager::Error error)
+void QContactManagerEngine::updateContactLocalIdFetchRequest(QContactLocalIdFetchRequest* req, const QList<QContactLocalId>& result, QContactManager::Error error, QContactAbstractRequest::State newState)
 {
     QContactLocalIdFetchRequestPrivate* rd = static_cast<QContactLocalIdFetchRequestPrivate*>(req->d_ptr);
     req->d_ptr->m_error = error;
     rd->m_ids = result;
+    bool emitState = rd->m_state != newState;
+    rd->m_state = newState;
     emit req->resultsAvailable();
+    if (emitState)
+        emit req->stateChanged(newState);
 }
 
 /*!
   Updates the given QContactFetchRequest \a req with the latest results \a result, and operation error \a error.
+  In addition, the state of the request will be changed to \a newState.
+
   It then causes the request to emit its resultsAvailable() signal to notify clients of the request progress.
+
+  If the new request state is different from the previous state, the stateChanged() signal will also be emitted from the request.
  */
-void QContactManagerEngine::updateContactFetchRequest(QContactFetchRequest* req, const QList<QContact>& result, QContactManager::Error error)
+void QContactManagerEngine::updateContactFetchRequest(QContactFetchRequest* req, const QList<QContact>& result, QContactManager::Error error, QContactAbstractRequest::State newState)
 {
     QContactFetchRequestPrivate* rd = static_cast<QContactFetchRequestPrivate*>(req->d_ptr);
     req->d_ptr->m_error = error;
     rd->m_contacts = result;
+    bool emitState = rd->m_state != newState;
+    rd->m_state = newState;
     emit req->resultsAvailable();
+    if (emitState)
+        emit req->stateChanged(newState);
 }
 
 /*!
   Updates the given QContactRemoveRequest \a req with the operation error \a error, and map of input index to individual error \a errorMap.
+  In addition, the state of the request will be changed to \a newState.
+
   It then causes the request to emit its resultsAvailable() signal to notify clients of the request progress.
+
+  If the new request state is different from the previous state, the stateChanged() signal will also be emitted from the request.
  */
-void QContactManagerEngine::updateContactRemoveRequest(QContactRemoveRequest* req, QContactManager::Error error, const QMap<int, QContactManager::Error>& errorMap)
+void QContactManagerEngine::updateContactRemoveRequest(QContactRemoveRequest* req, QContactManager::Error error, const QMap<int, QContactManager::Error>& errorMap, QContactAbstractRequest::State newState)
 {
     QContactRemoveRequestPrivate* rd = static_cast<QContactRemoveRequestPrivate*>(req->d_ptr);
     req->d_ptr->m_error = error;
     rd->m_errors = errorMap;
+    bool emitState = rd->m_state != newState;
+    rd->m_state = newState;
     emit req->resultsAvailable();
+    if (emitState)
+        emit req->stateChanged(newState);
 }
 
 /*!
   Updates the given QContactSaveRequest \a req with the latest results \a result, operation error \a error, and map of input index to individual error \a errorMap.
+  In addition, the state of the request will be changed to \a newState.
+
   It then causes the request to emit its resultsAvailable() signal to notify clients of the request progress.
+
+  If the new request state is different from the previous state, the stateChanged() signal will also be emitted from the request.
  */
-void QContactManagerEngine::updateContactSaveRequest(QContactSaveRequest* req, const QList<QContact>& result, QContactManager::Error error, const QMap<int, QContactManager::Error>& errorMap)
+void QContactManagerEngine::updateContactSaveRequest(QContactSaveRequest* req, const QList<QContact>& result, QContactManager::Error error, const QMap<int, QContactManager::Error>& errorMap, QContactAbstractRequest::State newState)
 {
     QContactSaveRequestPrivate* rd = static_cast<QContactSaveRequestPrivate*>(req->d_ptr);
     req->d_ptr->m_error = error;
     rd->m_errors = errorMap;
     rd->m_contacts = result;
+    bool emitState = rd->m_state != newState;
+    rd->m_state = newState;
     emit req->resultsAvailable();
+    if (emitState)
+        emit req->stateChanged(newState);
 }
 
 /*!
   Updates the given QContactDetailDefinitionSaveRequest \a req with the latest results \a result, operation error \a error, and map of input index to individual error \a errorMap.
+  In addition, the state of the request will be changed to \a newState.
+
   It then causes the request to emit its resultsAvailable() signal to notify clients of the request progress.
+
+  If the new request state is different from the previous state, the stateChanged() signal will also be emitted from the request.
  */
-void QContactManagerEngine::updateDefinitionSaveRequest(QContactDetailDefinitionSaveRequest* req, const QList<QContactDetailDefinition>& result, QContactManager::Error error, const QMap<int, QContactManager::Error>& errorMap)
+void QContactManagerEngine::updateDefinitionSaveRequest(QContactDetailDefinitionSaveRequest* req, const QList<QContactDetailDefinition>& result, QContactManager::Error error, const QMap<int, QContactManager::Error>& errorMap, QContactAbstractRequest::State newState)
 {
     QContactDetailDefinitionSaveRequestPrivate* rd = static_cast<QContactDetailDefinitionSaveRequestPrivate*>(req->d_ptr);
     req->d_ptr->m_error = error;
     rd->m_errors = errorMap;
     rd->m_definitions = result;
+    bool emitState = rd->m_state != newState;
+    rd->m_state = newState;
     emit req->resultsAvailable();
+    if (emitState)
+        emit req->stateChanged(newState);
 }
 
 /*!
   Updates the given QContactDetailDefinitionRemoveRequest \a req with the operation error \a error, and map of input index to individual error \a errorMap.
+  In addition, the state of the request will be changed to \a newState.
+
   It then causes the request to emit its resultsAvailable() signal to notify clients of the request progress.
+
+  If the new request state is different from the previous state, the stateChanged() signal will also be emitted from the request.
  */
-void QContactManagerEngine::updateDefinitionRemoveRequest(QContactDetailDefinitionRemoveRequest* req, QContactManager::Error error, const QMap<int, QContactManager::Error>& errorMap)
+void QContactManagerEngine::updateDefinitionRemoveRequest(QContactDetailDefinitionRemoveRequest* req, QContactManager::Error error, const QMap<int, QContactManager::Error>& errorMap, QContactAbstractRequest::State newState)
 {
     QContactDetailDefinitionRemoveRequestPrivate* rd = static_cast<QContactDetailDefinitionRemoveRequestPrivate*>(req->d_ptr);
     req->d_ptr->m_error = error;
     rd->m_errors = errorMap;
+    bool emitState = rd->m_state != newState;
+    rd->m_state = newState;
     emit req->resultsAvailable();
+    if (emitState)
+        emit req->stateChanged(newState);
 }
 
 /*!
   Updates the given QContactDetailDefinitionFetchRequest \a req with the latest results \a result, operation error \a error, and map of input index to individual error \a errorMap.
+  In addition, the state of the request will be changed to \a newState.
+
   It then causes the request to emit its resultsAvailable() signal to notify clients of the request progress.
+
+  If the new request state is different from the previous state, the stateChanged() signal will also be emitted from the request.
  */
-void QContactManagerEngine::updateDefinitionFetchRequest(QContactDetailDefinitionFetchRequest* req, const QMap<QString, QContactDetailDefinition>& result, QContactManager::Error error, const QMap<int, QContactManager::Error>& errorMap)
+void QContactManagerEngine::updateDefinitionFetchRequest(QContactDetailDefinitionFetchRequest* req, const QMap<QString, QContactDetailDefinition>& result, QContactManager::Error error, const QMap<int, QContactManager::Error>& errorMap, QContactAbstractRequest::State newState)
 {
     QContactDetailDefinitionFetchRequestPrivate* rd = static_cast<QContactDetailDefinitionFetchRequestPrivate*>(req->d_ptr);
     req->d_ptr->m_error = error;
     rd->m_errors = errorMap;
     rd->m_definitions = result;
+    bool emitState = rd->m_state != newState;
+    rd->m_state = newState;
     emit req->resultsAvailable();
+    if (emitState)
+        emit req->stateChanged(newState);
 }
 
 /*!
   Updates the given QContactRelationshipSaveRequest \a req with the latest results \a result, operation error \a error, and map of input index to individual error \a errorMap.
+  In addition, the state of the request will be changed to \a newState.
+
   It then causes the request to emit its resultsAvailable() signal to notify clients of the request progress.
+
+  If the new request state is different from the previous state, the stateChanged() signal will also be emitted from the request.
  */
-void QContactManagerEngine::updateRelationshipSaveRequest(QContactRelationshipSaveRequest* req, const QList<QContactRelationship>& result, QContactManager::Error error, const QMap<int, QContactManager::Error>& errorMap)
+void QContactManagerEngine::updateRelationshipSaveRequest(QContactRelationshipSaveRequest* req, const QList<QContactRelationship>& result, QContactManager::Error error, const QMap<int, QContactManager::Error>& errorMap, QContactAbstractRequest::State newState)
 {
     QContactRelationshipSaveRequestPrivate* rd = static_cast<QContactRelationshipSaveRequestPrivate*>(req->d_ptr);
     req->d_ptr->m_error = error;
     rd->m_errors = errorMap;
     rd->m_relationships = result;
+    bool emitState = rd->m_state != newState;
+    rd->m_state = newState;
     emit req->resultsAvailable();
+    if (emitState)
+        emit req->stateChanged(newState);
 }
 
 /*!
   Updates the given QContactRelationshipRemoveRequest \a req with the operation error \a error, and map of input index to individual error \a errorMap.
+  In addition, the state of the request will be changed to \a newState.
+
   It then causes the request to emit its resultsAvailable() signal to notify clients of the request progress.
+
+  If the new request state is different from the previous state, the stateChanged() signal will also be emitted from the request.
  */
-void QContactManagerEngine::updateRelationshipRemoveRequest(QContactRelationshipRemoveRequest* req, QContactManager::Error error, const QMap<int, QContactManager::Error>& errorMap)
+void QContactManagerEngine::updateRelationshipRemoveRequest(QContactRelationshipRemoveRequest* req, QContactManager::Error error, const QMap<int, QContactManager::Error>& errorMap, QContactAbstractRequest::State newState)
 {
     QContactRelationshipRemoveRequestPrivate* rd = static_cast<QContactRelationshipRemoveRequestPrivate*>(req->d_ptr);
     req->d_ptr->m_error = error;
     rd->m_errors = errorMap;
+    bool emitState = rd->m_state != newState;
+    rd->m_state = newState;
     emit req->resultsAvailable();
+    if (emitState)
+        emit req->stateChanged(newState);
 }
 
 /*!
   Updates the given QContactRelationshipFetchRequest \a req with the latest results \a result, and operation error \a error.
+  In addition, the state of the request will be changed to \a newState.
+
   It then causes the request to emit its resultsAvailable() signal to notify clients of the request progress.
+
+  If the new request state is different from the previous state, the stateChanged() signal will also be emitted from the request.
  */
-void QContactManagerEngine::updateRelationshipFetchRequest(QContactRelationshipFetchRequest* req, const QList<QContactRelationship>& result, QContactManager::Error error)
+void QContactManagerEngine::updateRelationshipFetchRequest(QContactRelationshipFetchRequest* req, const QList<QContactRelationship>& result, QContactManager::Error error, QContactAbstractRequest::State newState)
 {
     QContactRelationshipFetchRequestPrivate* rd = static_cast<QContactRelationshipFetchRequestPrivate*>(req->d_ptr);
     req->d_ptr->m_error = error;
     rd->m_relationships = result;
+    bool emitState = rd->m_state != newState;
+    rd->m_state = newState;
     emit req->resultsAvailable();
+    if (emitState)
+        emit req->stateChanged(newState);
 }
 
 #include "moc_qcontactmanagerengine.cpp"