qtmobility/plugins/contacts/symbian/src/cntrelationship.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 11 06b8e2af4411
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    69  */
    69  */
    70 CntRelationship::~CntRelationship()
    70 CntRelationship::~CntRelationship()
    71 {
    71 {
    72     QMap<QString, CntAbstractRelationship *>::iterator itr;
    72     QMap<QString, CntAbstractRelationship *>::iterator itr;
    73 
    73 
       
    74     /* XXX maybe use qDeleteAll? */
    74     for (itr = m_relationshipMap.begin(); itr != m_relationshipMap.end(); ++itr)
    75     for (itr = m_relationshipMap.begin(); itr != m_relationshipMap.end(); ++itr)
    75     {
    76     {
    76         CntAbstractRelationship* value = itr.value();
    77         CntAbstractRelationship* value = itr.value();
    77         delete value;
    78         delete value;
    78         value = 0;
    79         value = 0;
    79     }
    80     }
    80 }
    81 }
    81 
    82 
    82 /*!
    83 /*!
    83  * \return The supported relationship types.
    84  * \return whether relationships of type \a relationshipType is supported by contacts of \a contactType
    84  */
    85  */
    85 QStringList CntRelationship::supportedRelationshipTypes(const QString &contactType) const
    86 bool CntRelationship::isRelationshipTypeSupported(const QString &relationshipType, const QString &contactType) const
    86 {
    87 {
    87     Q_UNUSED(contactType);
    88     Q_UNUSED(contactType);
    88     
    89     return m_relationshipMap.contains(relationshipType);
       
    90 
       
    91     /* XXX Old code:
    89     QStringList supportedTypes;
    92     QStringList supportedTypes;
    90 
    93 
    91     foreach(QString type, m_relationshipMap.keys()) {
    94     foreach(const QString& type, m_relationshipMap.keys()) {
    92         supportedTypes.append(type);
    95         supportedTypes.append(type);
    93     }
    96     }
    94     return supportedTypes;
    97     return supportedTypes;
       
    98     */
    95 }
    99 }
    96 
   100 
    97 /* !
   101 /* !
    98  * Retrive the contacts relationships
   102  * Retrive the contacts relationships
    99  *
   103  *
   100  * \a relationshipType The type of the relationship
   104  * \a relationshipType The type of the relationship
   101  * \a participantId The contact id
   105  * \a participantId The contact id
   102  * \a role The contact role
   106  * \a role The contact role
   103  * \a error Error returned
   107  * \a error Error returned
   104  */
   108  */
   105 QList<QContactRelationship> CntRelationship::relationships(const QString& relationshipType, const QContactId& participantId, QContactRelationshipFilter::Role role, QContactManager::Error& error) const
   109 QList<QContactRelationship> CntRelationship::relationships(const QString& relationshipType, const QContactId& participantId, QContactRelationship::Role role, QContactManager::Error* error) const
   106 {
   110 {
   107     QList<QContactRelationship> returnValue;
   111     QList<QContactRelationship> returnValue;
   108     error = QContactManager::NoError;
   112     *error = QContactManager::NoError;
   109 
   113 
   110     // if relationshipType is empty, relationships of any type are returned.
   114     // if relationshipType is empty, relationships of any type are returned.
   111     if (relationshipType.isEmpty())
   115     if (relationshipType.isEmpty())
   112     {
   116     {
   113         foreach (QString type, m_relationshipMap.keys())
   117         foreach (const QString& type, m_relationshipMap.keys())
   114         {
   118         {
   115             // get the relationship
   119             // get the relationship
   116             CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(type);
   120             CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(type);
   117 
   121 
   118             // retrieve the relationships
   122             // retrieve the relationships
   119             TRAPD(symbianError, QT_TRYCATCH_LEAVING(returnValue.append(abstractRelationship->relationshipsL(participantId, role, error))));
   123             TRAPD(symbianError, returnValue.append(abstractRelationship->relationshipsL(participantId, role, error)));
   120 
   124 
   121             // if error translate it into a qt error
   125             // if error translate it into a qt error
   122             if (symbianError != KErrNone){
   126             if (symbianError != KErrNone){
   123                 CntSymbianTransformError::transformError(symbianError, error);
   127                 CntSymbianTransformError::transformError(symbianError, error);
   124             }
   128             }
   125             
   129             
   126             // return empty list if there was an error
   130             // return empty list if there was an error
   127             if (error != QContactManager::NoError && error != QContactManager::DoesNotExistError) {
   131             if (*error != QContactManager::NoError && *error != QContactManager::DoesNotExistError) {
   128                 return QList<QContactRelationship>();
   132                 return QList<QContactRelationship>();
   129             }
   133             }
   130         }
   134         }
   131         // if relationships found, update error
   135         // if relationships found, update error
   132         if (!returnValue.isEmpty() && error == QContactManager::DoesNotExistError) {
   136         if (!returnValue.isEmpty() && *error == QContactManager::DoesNotExistError) {
   133             // this can be the case if nothing is found for last relationship type
   137             // this can be the case if nothing is found for last relationship type
   134             error = QContactManager::NoError;
   138             *error = QContactManager::NoError;
   135         }
   139         }
   136     }
   140     }
   137     //check if we support the relationship
   141     //check if we support the relationship
   138     else if (m_relationshipMap.contains(relationshipType))
   142     else if (m_relationshipMap.contains(relationshipType))
   139     {
   143     {
   140         //get the relationship
   144         //get the relationship
   141         CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationshipType);
   145         CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationshipType);
   142 
   146 
   143         //retrieve the relationships
   147         //retrieve the relationships
   144         TRAPD(symbianError, QT_TRYCATCH_LEAVING(returnValue = abstractRelationship->relationshipsL(participantId, role, error)));
   148         TRAPD(symbianError, returnValue = abstractRelationship->relationshipsL(participantId, role, error));
   145 
   149 
   146         //if error translate it into a qt error
   150         //if error translate it into a qt error
   147         if (symbianError != KErrNone){
   151         if (symbianError != KErrNone){
   148             CntSymbianTransformError::transformError(symbianError, error);
   152             CntSymbianTransformError::transformError(symbianError, error);
   149         }
   153         }
   150     }
   154     }
   151     else{
   155     else{
   152         error = QContactManager::NotSupportedError;
   156         *error = QContactManager::NotSupportedError;
   153     }
   157     }
   154     
   158     
   155     // No relationships found?
   159     // No relationships found?
   156     if (error == QContactManager::NoError && returnValue.count() == 0 ) {
   160     if (*error == QContactManager::NoError && returnValue.count() == 0 ) {
   157         error = QContactManager::DoesNotExistError;
   161         *error = QContactManager::DoesNotExistError;
   158     }
   162     }
   159 
   163 
   160     return returnValue;
   164     return returnValue;
   161 }
   165 }
   162 
   166 
   165  *
   169  *
   166  * \a affectedContactIds will include the affected contact ids
   170  * \a affectedContactIds will include the affected contact ids
   167  * \a relationship to be saved
   171  * \a relationship to be saved
   168  * \a error Error returned
   172  * \a error Error returned
   169  */
   173  */
   170 bool CntRelationship::saveRelationship(QSet<QContactLocalId> *affectedContactIds, QContactRelationship* relationship, QContactManager::Error& error)
   174 bool CntRelationship::saveRelationship(QSet<QContactLocalId> *affectedContactIds, QContactRelationship* relationship, QContactManager::Error* error)
   171 {
   175 {
   172     bool returnValue(false);
   176     bool returnValue(false);
   173     error = QContactManager::NoError;
   177     *error = QContactManager::NoError;
   174     if (validateRelationship(*relationship, error))
   178     if (validateRelationship(*relationship, error))
   175     {
   179     {
   176         // Update manager uri to this manager if it is empty
   180         // Update manager uri to this manager if it is empty
   177         if (relationship->second().managerUri().isEmpty()) {
   181         if (relationship->second().managerUri().isEmpty()) {
   178             QContactId second = relationship->second();
   182             QContactId second = relationship->second();
   182         
   186         
   183         //get the relationship
   187         //get the relationship
   184         CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationship->relationshipType());
   188         CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationship->relationshipType());
   185     
   189     
   186         //save the relationship
   190         //save the relationship
   187         TRAPD(symbianError, QT_TRYCATCH_LEAVING(returnValue = abstractRelationship->saveRelationshipL(affectedContactIds, relationship, error)));
   191         TRAPD(symbianError, returnValue = abstractRelationship->saveRelationshipL(affectedContactIds, relationship, error));
   188     
   192     
   189         //if symbian error translate it into a qt error
   193         //if symbian error translate it into a qt error
   190         if (symbianError != KErrNone){
   194         if (symbianError != KErrNone){
   191             returnValue = false;
   195             returnValue = false;
   192             CntSymbianTransformError::transformError(symbianError, error);
   196             CntSymbianTransformError::transformError(symbianError, error);
   198 /* !
   202 /* !
   199  * Save many relationships
   203  * Save many relationships
   200  *
   204  *
   201  * \a affectedContactIds will include the affected contact ids
   205  * \a affectedContactIds will include the affected contact ids
   202  * \a relationships to be saved
   206  * \a relationships to be saved
   203  * \return a list of errors
   207  * \a errorMap storage place for errors
   204  */
   208  * \return true if there were no errors saving
   205 QList<QContactManager::Error> CntRelationship::saveRelationships(QSet<QContactLocalId> *affectedContactIds, QList<QContactRelationship>* relationships, QContactManager::Error& error)
   209  */
   206 {
   210 bool CntRelationship::saveRelationships(QSet<QContactLocalId> *affectedContactIds, QList<QContactRelationship>* relationships, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
   207     QList<QContactManager::Error> returnValue;
   211 {
   208     error = QContactManager::NoError;
       
   209     QContactManager::Error singleError;    
   212     QContactManager::Error singleError;    
       
   213     bool returnValue(true);
       
   214 
       
   215     *error = QContactManager::NoError;
   210 
   216 
   211     // loop through the relationships
   217     // loop through the relationships
   212     for (int i = 0; i < relationships->count(); i++)
   218     for (int i = 0; i < relationships->count(); i++)
   213     {
   219     {
   214         // save the relationship
   220         // save the relationship
   215         saveRelationship(affectedContactIds, &(relationships->operator[](i)), singleError);
   221         saveRelationship(affectedContactIds, &(relationships->operator[](i)), &singleError);
   216         returnValue.append(singleError);
   222         if (errorMap && singleError != QContactManager::NoError) {
       
   223             errorMap->insert(i, singleError);
       
   224         }
   217         
   225         
   218         // update the total error
   226         // update the total error
   219         if (singleError != QContactManager::NoError)
   227         if (singleError != QContactManager::NoError) {
   220             error = singleError;
   228             *error = singleError;
       
   229             returnValue = false;
       
   230         }
       
   231 
   221     }
   232     }
   222 
   233 
   223     return returnValue;
   234     return returnValue;
   224 }
   235 }
   225 
   236 
   229  * \a affectedContactIds will include the affected contact ids
   240  * \a affectedContactIds will include the affected contact ids
   230  * \a relationship to be removed
   241  * \a relationship to be removed
   231  * \a error Error returned
   242  * \a error Error returned
   232  * \return true if no error otherwise false
   243  * \return true if no error otherwise false
   233  */
   244  */
   234 bool CntRelationship::removeRelationship(QSet<QContactLocalId> *affectedContactIds, const QContactRelationship &relationship, QContactManager::Error& error)
   245 bool CntRelationship::removeRelationship(QSet<QContactLocalId> *affectedContactIds, const QContactRelationship &relationship, QContactManager::Error* error)
   235 {
   246 {
   236     bool returnValue(false);
   247     bool returnValue(false);
   237     error = QContactManager::NoError;
   248     *error = QContactManager::NoError;
   238     if (validateRelationship(relationship, error))
   249     if (validateRelationship(relationship, error))
   239     {
   250     {
   240         //get the relationship
   251         //get the relationship
   241         CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationship.relationshipType());
   252         CntAbstractRelationship *abstractRelationship = m_relationshipMap.value(relationship.relationshipType());
   242 
   253 
   243         TRAPD(symbianError, QT_TRYCATCH_LEAVING(returnValue = abstractRelationship->removeRelationshipL(affectedContactIds, relationship, error)));
   254         TRAPD(symbianError, returnValue = abstractRelationship->removeRelationshipL(affectedContactIds, relationship, error));
   244 
   255 
   245         //if symbian error translate it into a qt error
   256         //if symbian error translate it into a qt error
   246         if (symbianError != KErrNone){
   257         if (symbianError != KErrNone){
   247             returnValue = false;
   258             returnValue = false;
   248             CntSymbianTransformError::transformError(symbianError, error);
   259             CntSymbianTransformError::transformError(symbianError, error);
   254 /* !
   265 /* !
   255  * Remove many relationships
   266  * Remove many relationships
   256  *
   267  *
   257  * \a affectedContactIds will include the affected contact ids
   268  * \a affectedContactIds will include the affected contact ids
   258  * \a relationships to be removed
   269  * \a relationships to be removed
   259  * \return a list of errors
   270  * \a errorMap storage place for errors
   260  */
   271  * \return true if there were no errors removing, false otherwise
   261 QList<QContactManager::Error> CntRelationship::removeRelationships(QSet<QContactLocalId> *affectedContactIds, const QList<QContactRelationship>& relationships, QContactManager::Error& error)
   272  */
   262 {
   273 bool CntRelationship::removeRelationships(QSet<QContactLocalId> *affectedContactIds, const QList<QContactRelationship>& relationships, QMap<int, QContactManager::Error>* errorMap, QContactManager::Error* error)
   263     QList<QContactManager::Error> returnValue;
   274 {
   264     error = QContactManager::NoError;
   275     bool returnValue(true);
       
   276     *error = QContactManager::NoError;
   265     QContactManager::Error qtError(QContactManager::NoError);
   277     QContactManager::Error qtError(QContactManager::NoError);
   266 
   278 
   267     //loop through the relationships
   279     //loop through the relationships
   268     for(int i = 0; i < relationships.count(); i++)
   280     for(int i = 0; i < relationships.count(); i++)
   269     {
   281     {
   270         //remove the relationships
   282         //remove the relationships
   271         removeRelationship(affectedContactIds, relationships.at(i), qtError);
   283         removeRelationship(affectedContactIds, relationships.at(i), &qtError);
   272         returnValue.append(qtError);
   284         if (errorMap && qtError != QContactManager::NoError)
       
   285             errorMap->insert(i, qtError);
   273         
   286         
   274         // update the total error
   287         // update the total error
   275         if (qtError != QContactManager::NoError)
   288         if (qtError != QContactManager::NoError) {
   276             error = qtError;
   289             returnValue = false;
   277     }
   290             *error = qtError;
   278     return returnValue;
   291         }
   279 }
   292     }
   280 
   293     return returnValue;
   281 bool CntRelationship::validateRelationship(const QContactRelationship &relationship, QContactManager::Error& error)
   294 }
   282 {
   295 
   283     error = QContactManager::NoError;
   296 bool CntRelationship::validateRelationship(const QContactRelationship &relationship, QContactManager::Error* error)
       
   297 {
       
   298     *error = QContactManager::NoError;
   284     
   299     
   285     // check if supported in this manager
   300     // check if supported in this manager
   286     if (!m_relationshipMap.contains(relationship.relationshipType())) {
   301     if (!m_relationshipMap.contains(relationship.relationshipType())) {
   287         error = QContactManager::NotSupportedError;
   302         *error = QContactManager::NotSupportedError;
   288         return false;
   303         return false;
   289     }
   304     }
   290     
   305     
   291     QContactId first = relationship.first();
   306     QContactId first = relationship.first();
   292     QContactId second = relationship.second();    
   307     QContactId second = relationship.second();    
   293     
   308     
   294     // zero id contacts not accepted
   309     // zero id contacts not accepted
   295     if (!(first.localId() && second.localId())) {
   310     if (!(first.localId() && second.localId())) {
   296         error = QContactManager::InvalidRelationshipError;
   311         *error = QContactManager::InvalidRelationshipError;
   297         return false;
   312         return false;
   298     }
   313     }
   299     
   314     
   300     // "first" must be a contact in this manager
   315     // "first" must be a contact in this manager
   301     if (!first.managerUri().isEmpty() && first.managerUri() != m_managerUri) {
   316     if (!first.managerUri().isEmpty() && first.managerUri() != m_managerUri) {
   302         error = QContactManager::InvalidRelationshipError;
   317         *error = QContactManager::InvalidRelationshipError;
   303         return false;
   318         return false;
   304     }
   319     }
   305 
   320 
   306     // "first" must be found in the database
   321     // "first" must be found in the database
   307     CContactItem* contact = 0;
   322     CContactItem* contact = 0;
   308     TRAP_IGNORE(contact = m_contactDatabase->ReadContactL(first.localId()));
   323     TRAP_IGNORE(contact = m_contactDatabase->ReadContactL(first.localId()));
   309     if (!contact) {
   324     if (!contact) {
   310         error = QContactManager::InvalidRelationshipError;
   325         *error = QContactManager::InvalidRelationshipError;
   311         return false;
   326         return false;
   312     }
   327     }
   313     delete contact;
   328     delete contact;
   314     
   329     
   315     // check if "second" is in this manager 
   330     // check if "second" is in this manager 
   316     if (second.managerUri().isEmpty() || second.managerUri() == m_managerUri)
   331     if (second.managerUri().isEmpty() || second.managerUri() == m_managerUri)
   317     {
   332     {
   318         // circular relationships not allowed
   333         // circular relationships not allowed
   319         if (first.localId() == second.localId()) {
   334         if (first.localId() == second.localId()) {
   320             error = QContactManager::InvalidRelationshipError;
   335             *error = QContactManager::InvalidRelationshipError;
   321             return false;
   336             return false;
   322         }
   337         }
   323     
   338     
   324         // "second" must be found in the database
   339         // "second" must be found in the database
   325         contact = 0;
   340         contact = 0;
   326         TRAP_IGNORE(contact = m_contactDatabase->ReadContactL(second.localId()));
   341         TRAP_IGNORE(contact = m_contactDatabase->ReadContactL(second.localId()));
   327         if (!contact) {
   342         if (!contact) {
   328             error = QContactManager::InvalidRelationshipError;
   343             *error = QContactManager::InvalidRelationshipError;
   329             return false;
   344             return false;
   330         }
   345         }
   331         delete contact;        
   346         delete contact;        
   332     }
   347     }
   333     
   348