plugins/contacts/symbian/plugin/src/cntrelationship.cpp
changeset 5 603d3f8b6302
parent 0 876b1a06bc25
equal deleted inserted replaced
3:e4ebb16b39ea 5:603d3f8b6302
   209  */
   209  */
   210 bool CntRelationship::saveRelationships(QSet<QContactLocalId> *affectedContactIds, QList<QContactRelationship>* relationships, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
   210 bool CntRelationship::saveRelationships(QSet<QContactLocalId> *affectedContactIds, QList<QContactRelationship>* relationships, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
   211 {
   211 {
   212     QContactManager::Error singleError;    
   212     QContactManager::Error singleError;    
   213     bool returnValue(true);
   213     bool returnValue(true);
   214 
   214     bool hasManyRelationshipTypes(false);
   215     *error = QContactManager::NoError;
   215 
   216 
   216     *error = QContactManager::NoError;
   217     // loop through the relationships
   217 
   218     for (int i = 0; i < relationships->count(); i++)
   218 #ifdef SYMBIAN_BACKEND_USE_SQLITE
   219     {
   219     TInt symbianError = KErrNone;
   220         // save the relationship
   220     QString relationshipType;
   221         saveRelationship(affectedContactIds, &(relationships->operator[](i)), &singleError);
   221 
   222         if (errorMap && singleError != QContactManager::NoError) {
   222     // validate relationships
   223             errorMap->insert(i, singleError);
   223     returnValue = validateRelationships(*relationships, relationshipType, error);
   224         }
   224     hasManyRelationshipTypes = relationshipType.isEmpty();
   225         
   225 
   226         // update the total error
   226     // if all relationships are valid and of the same type, we can try to use the batch save, which is much faster
   227         if (singleError != QContactManager::NoError) {
   227     if (returnValue && !hasManyRelationshipTypes) {
   228             *error = singleError;
   228         if (!relationships->isEmpty()) {
   229             returnValue = false;
   229             CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationshipType);
   230         }
   230             TRAP(symbianError, returnValue = abstractRelationship->saveRelationshipsL(affectedContactIds, relationships, &singleError));
   231 
   231             returnValue = returnValue && singleError == QContactManager::NoError && symbianError == KErrNone;
       
   232         }
       
   233     }
       
   234 #endif
       
   235     // if validation or batch save failed, or if there are many different relationship types,
       
   236     // the relationships need to be added one by one
       
   237     if (!returnValue || hasManyRelationshipTypes) {
       
   238         for (int i = 0; i < relationships->count(); ++i) {
       
   239             // save the relationship
       
   240             saveRelationship(affectedContactIds, &(relationships->operator[](i)), &singleError);
       
   241             if (errorMap && singleError != QContactManager::NoError) {
       
   242                 errorMap->insert(i, singleError);
       
   243             }
       
   244             
       
   245             // update the total error
       
   246             if (singleError != QContactManager::NoError) {
       
   247                 *error = singleError;
       
   248                 returnValue = false;
       
   249             } 
       
   250         }
   232     }
   251     }
   233 
   252 
   234     return returnValue;
   253     return returnValue;
   235 }
   254 }
   236 
   255 
   270  * \a errorMap storage place for errors
   289  * \a errorMap storage place for errors
   271  * \return true if there were no errors removing, false otherwise
   290  * \return true if there were no errors removing, false otherwise
   272  */
   291  */
   273 bool CntRelationship::removeRelationships(QSet<QContactLocalId> *affectedContactIds, const QList<QContactRelationship>& relationships, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
   292 bool CntRelationship::removeRelationships(QSet<QContactLocalId> *affectedContactIds, const QList<QContactRelationship>& relationships, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
   274 {
   293 {
       
   294     QContactManager::Error singleError;    
   275     bool returnValue(true);
   295     bool returnValue(true);
   276     *error = QContactManager::NoError;
   296     bool hasManyRelationshipTypes(false);
   277     QContactManager::Error qtError(QContactManager::NoError);
   297 
   278 
   298     *error = QContactManager::NoError;
   279     //loop through the relationships
   299 
   280     for(int i = 0; i < relationships.count(); i++)
   300 #ifdef SYMBIAN_BACKEND_USE_SQLITE
   281     {
   301     TInt symbianError = KErrNone;
   282         //remove the relationships
   302     QString relationshipType;
   283         removeRelationship(affectedContactIds, relationships.at(i), &qtError);
   303 
   284         if (errorMap && qtError != QContactManager::NoError)
   304     // validate relationships
   285             errorMap->insert(i, qtError);
   305     returnValue = validateRelationships(relationships, relationshipType, error);
   286         
   306     hasManyRelationshipTypes = relationshipType.isEmpty();
   287         // update the total error
   307 
   288         if (qtError != QContactManager::NoError) {
   308     // if all relationships are valid and of the same type, we can try to use the batch remove, which is much faster
   289             returnValue = false;
   309     if (returnValue && !hasManyRelationshipTypes) {
   290             *error = qtError;
   310         if (!relationships.isEmpty()) {
   291         }
   311             CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationshipType);
   292     }
   312             TRAP(symbianError, returnValue = abstractRelationship->removeRelationshipsL(affectedContactIds, relationships, &singleError));
       
   313             returnValue = returnValue && singleError == QContactManager::NoError && symbianError == KErrNone;
       
   314         }
       
   315     }
       
   316 #endif
       
   317     // if validation or batch remove failed, or if there are many relationship types,
       
   318     // the relationships need to be removed one by one
       
   319     if (!returnValue || hasManyRelationshipTypes) {
       
   320         for (int i = 0; i < relationships.count(); ++i) {
       
   321             //remove the relationships
       
   322             removeRelationship(affectedContactIds, relationships.at(i), &singleError);
       
   323             if (errorMap && singleError != QContactManager::NoError) {
       
   324                 errorMap->insert(i, singleError);
       
   325             }
       
   326                 
       
   327             // update the total error
       
   328             if (singleError != QContactManager::NoError) {
       
   329                 returnValue = false;
       
   330                 *error = singleError;
       
   331             }
       
   332         }
       
   333     }
       
   334 
   293     return returnValue;
   335     return returnValue;
   294 }
   336 }
   295 
   337 
   296 bool CntRelationship::validateRelationship(const QContactRelationship &relationship, QContactManager::Error* error)
   338 bool CntRelationship::validateRelationship(const QContactRelationship &relationship, QContactManager::Error* error)
   297 {
   339 {
   348     
   390     
   349     // do additional checks in the actual implementation
   391     // do additional checks in the actual implementation
   350     CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationship.relationshipType());
   392     CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationship.relationshipType());
   351     return abstractRelationship->validateRelationship(relationship, error);
   393     return abstractRelationship->validateRelationship(relationship, error);
   352 }
   394 }
       
   395 
       
   396 #ifdef SYMBIAN_BACKEND_USE_SQLITE
       
   397 /*
       
   398  *  Validates the relationships. If all valid relationships are of the same type,
       
   399  *  the type will be passed back in relationshipType. Otherwise it will be empty.
       
   400  */
       
   401 bool CntRelationship::validateRelationships(const QList<QContactRelationship> &relationships, QString &relationshipType, QContactManager::Error *error)
       
   402 {
       
   403     QContactManager::Error validationError;
       
   404     bool hasManyRelationshipTypes = false;
       
   405     bool returnValue = true;
       
   406 
       
   407     foreach (QContactRelationship relationship, relationships) {
       
   408         if (validateRelationship(relationship, &validationError)) {
       
   409             // Update manager uri to this manager if it is empty
       
   410             if (relationship.second().managerUri().isEmpty()) {
       
   411                 QContactId second = relationship.second();
       
   412                 second.setManagerUri(m_managerUri);
       
   413                 relationship.setSecond(second);
       
   414             }
       
   415             
       
   416             // get the relationship type
       
   417             if (relationship.relationshipType() != relationshipType && !relationshipType.isEmpty()) {
       
   418                 hasManyRelationshipTypes = true;
       
   419             }
       
   420             else if (relationshipType.isEmpty()) {
       
   421                 relationshipType = relationship.relationshipType();
       
   422             }
       
   423         }
       
   424         else {
       
   425             *error = validationError;
       
   426             returnValue = false;
       
   427         }
       
   428     }
       
   429 
       
   430     if (hasManyRelationshipTypes) {
       
   431         relationshipType.clear();
       
   432     }
       
   433 
       
   434     return returnValue;
       
   435 }
       
   436 #endif