phonebookengines/VirtualPhonebook/VPbkVCardEng/src/CVPbkGroupCardHandler.cpp
branchRCL_3
changeset 17 2666d9724c76
parent 0 e686773b3f54
equal deleted inserted replaced
15:34879f5cfc63 17:2666d9724c76
    65 // ----------------------------------------------------------------------------
    65 // ----------------------------------------------------------------------------
    66 // CVPbkGroupCardHandler::~CVPbkGroupCardHandler
    66 // CVPbkGroupCardHandler::~CVPbkGroupCardHandler
    67 // ----------------------------------------------------------------------------
    67 // ----------------------------------------------------------------------------
    68 CVPbkGroupCardHandler::~CVPbkGroupCardHandler()
    68 CVPbkGroupCardHandler::~CVPbkGroupCardHandler()
    69     {
    69     {
       
    70     // The code that started iWait must know if this instance has been deleted.
       
    71     if ( iDestroyed )
       
    72         {
       
    73         *iDestroyed = ETrue;
       
    74         }
       
    75     
       
    76     if ( iSelfPtr )
       
    77         {
       
    78         *iSelfPtr = NULL;
       
    79         }
       
    80     
       
    81     delete iRetrieveOp;
       
    82     
    70     if ( iWait )
    83     if ( iWait )
    71         {
    84         {
    72         if( iWait->IsStarted() )
    85         if( iWait->IsStarted() )
    73             iWait->AsyncStop();
    86             iWait->AsyncStop();
    74         delete iWait;
    87         delete iWait;
   120         }
   133         }
   121 
   134 
   122     CDesCArrayFlat* desArray=new (ELeave)CDesCArrayFlat(totalGroupsInContact);
   135     CDesCArrayFlat* desArray=new (ELeave)CDesCArrayFlat(totalGroupsInContact);
   123     CleanupStack::PushL(desArray);
   136     CleanupStack::PushL(desArray);
   124 
   137 
   125     //Append all the Group Names of contact to X-CATEGORIES field value. 
   138     //Append all the Group Names of contact to X-CATEGORIES field value.
   126     for ( TInt m = 0 ; m < totalGroupsInContact ; m++ )
   139     TBool thisDestroyed = EFalse;
       
   140     for ( TInt m = 0 ; m < totalGroupsInContact && !thisDestroyed ; m++ )
   127         {                  
   141         {                  
   128         const  MVPbkContactLink& groupLink = (*groupIdArray)[m];                                             
   142         const  MVPbkContactLink& groupLink = (*groupIdArray)[m];
   129         iData.GetContactManager().RetrieveContactL( groupLink, *this );
   143         thisDestroyed = RetrieveContactL( groupLink, *this );
   130         //wait for ContactRetrieve operation to complete, to get storecontact for the current group link
   144         if (!thisDestroyed && iTargetStore)
   131         if( ! (iWait->IsStarted()) )
       
   132             {
       
   133             iWait->Start();
       
   134             }
       
   135 
       
   136         if (iTargetStore)
       
   137             {
   145             {
   138             MVPbkContactGroup* contactGrp = iTargetStore->Group();
   146             MVPbkContactGroup* contactGrp = iTargetStore->Group();
   139             if(contactGrp)
   147             if(contactGrp)
   140                 {
   148                 {
   141                 CParserPropertyValueHBufC* tempValue = CParserPropertyValueHBufC::NewL( contactGrp->GroupLabel() );
   149                 CParserPropertyValueHBufC* tempValue = CParserPropertyValueHBufC::NewL( contactGrp->GroupLabel() );
   175 
   183 
   176     TInt groupIdsCount = 0;
   184     TInt groupIdsCount = 0;
   177     groupIdsCount = groupIds->Count();    
   185     groupIdsCount = groupIds->Count();    
   178     const MVPbkContactLink* contactLink = iStore->CreateLinkLC();
   186     const MVPbkContactLink* contactLink = iStore->CreateLinkLC();
   179 
   187 
   180     //Remove the contact from all the Groups it belongs. 
   188     //Remove the contact from all the Groups it belongs.
   181     for(TInt i = 0;i < groupIdsCount;i++)
   189     TBool thisDestroyed = EFalse;
       
   190     for(TInt i = 0;i < groupIdsCount && !thisDestroyed;i++)
   182         {
   191         {
   183         const MVPbkContactLink& groupLink = (*groupIds)[i];
   192         const MVPbkContactLink& groupLink = (*groupIds)[i];
   184         iData.GetContactManager().RetrieveContactL( groupLink, *this );
   193         thisDestroyed = RetrieveContactL( groupLink, *this );
   185         //wait for Retrieve operation to complete,to get storecontact for the current group link
   194         if (!thisDestroyed && iTargetStore)
   186         if( ! (iWait->IsStarted()) )
       
   187             {
       
   188             iWait->Start();
       
   189             }
       
   190 
       
   191         if (iTargetStore)
       
   192             { 
   195             { 
   193             MVPbkContactGroup* contactGrp = iTargetStore->Group();
   196             MVPbkContactGroup* contactGrp = iTargetStore->Group();
   194             if(contactGrp)
   197             if(contactGrp)
   195                 {   
   198                 {   
   196                 iTargetStore->LockL(*this); //Lock the contact before editing the contact.
   199                 iTargetStore->LockL(*this); //Lock the contact before editing the contact.
   233         {                    
   236         {                    
   234         groupItem->SetGroupLabelL(aGroupLabel); // set the group name.
   237         groupItem->SetGroupLabelL(aGroupLabel); // set the group name.
   235         
   238         
   236         //Retrieve group store
   239         //Retrieve group store
   237         const MVPbkContactLink* grpContactLink = groupItem->CreateLinkLC();
   240         const MVPbkContactLink* grpContactLink = groupItem->CreateLinkLC();
   238         iData.GetContactManager().RetrieveContactL( *grpContactLink, *this );
   241         TBool thisDestroyed = RetrieveContactL( *grpContactLink, *this );
   239         //wait for Retrieve operation to complete, to get storecontact for the current group link
       
   240         if( ! (iWait->IsStarted()) )
       
   241             {
       
   242             iWait->Start();
       
   243             }
       
   244         CleanupStack::PopAndDestroy();// to clean grpContactLink
   242         CleanupStack::PopAndDestroy();// to clean grpContactLink
   245 
   243 
   246         if (iTargetStore)
   244         if (!thisDestroyed && iTargetStore)
   247             {
   245             {
   248             const MVPbkContactLink* contactLink = iStore->CreateLinkLC();
   246             const MVPbkContactLink* contactLink = iStore->CreateLinkLC();
   249             groupItem->AddContactL(*contactLink);
   247             groupItem->AddContactL(*contactLink);
   250             CleanupStack::PopAndDestroy(); //For aContactLink
   248             CleanupStack::PopAndDestroy(); //For aContactLink
   251             groupItem->CommitL(*this); 
   249             groupItem->CommitL(*this); 
   261 
   259 
   262             if(iContactsGroupsMap == NULL)
   260             if(iContactsGroupsMap == NULL)
   263                 {
   261                 {
   264                 iContactsGroupsMap = new(ELeave) RPtrHashMap<TDesC16, MVPbkStoreContact>();
   262                 iContactsGroupsMap = new(ELeave) RPtrHashMap<TDesC16, MVPbkStoreContact>();
   265                 }
   263                 }
   266             iContactsGroupsMap->Insert(grpName, iTargetStore);
   264             // iContactsGroupsMap takes the ownership of iTargetStore
       
   265             TInt res = iContactsGroupsMap->Insert(grpName, iTargetStore);
       
   266             if ( res != KErrNone )
       
   267                 {
       
   268                 delete iTargetStore;
       
   269                 }
       
   270             iTargetStore = NULL;
   267             CleanupStack::Pop(); // To pop up grpName
   271             CleanupStack::Pop(); // To pop up grpName
   268             }
   272             }
   269         }   
   273         }   
   270 
   274 
   271     CleanupStack::PopAndDestroy(); // To pop up and destroy groupItem  
   275     CleanupStack::PopAndDestroy(); // To pop up and destroy groupItem  
   272     }
   276     }
   273 
   277 
   274 // ----------------------------------------------------------------------------
   278 // ----------------------------------------------------------------------------
   275 // CVPbkGroupCardHandler::DecodeContactGroupInVCardL
   279 // CVPbkGroupCardHandler::DecodeContactGroupInVCardL
   276 // ----------------------------------------------------------------------------
   280 // ----------------------------------------------------------------------------
   277 void CVPbkGroupCardHandler::DecodeContactGroupInVCardL(TPtr16 aValue)
   281 void CVPbkGroupCardHandler::DecodeContactGroupInVCardL(TPtr16 aValue,
       
   282         CVPbkGroupCardHandler** aSelfPointer )
   278     {        
   283     {        
   279     if(iStore == NULL)
   284     if(iStore == NULL)
   280         return; 
   285         return;
       
   286     iSelfPtr = aSelfPointer;
   281     const MVPbkContactLink* contactLink = iStore->CreateLinkLC();
   287     const MVPbkContactLink* contactLink = iStore->CreateLinkLC();
   282 
   288 
   283     TInt newLinePos = aValue.Find(KVersitTokencrlf());
   289     TInt newLinePos = aValue.Find(KVersitTokencrlf());
   284 
   290 
   285     if (newLinePos != KErrNotFound)
   291     if (newLinePos != KErrNotFound)
   288     TInt semiColonPos = 0;
   294     TInt semiColonPos = 0;
   289     TBool endGrpValue = EFalse;    
   295     TBool endGrpValue = EFalse;    
   290 
   296 
   291     DeleteContactFromGroupsL(); //Delete the contact from the Groups it belongs
   297     DeleteContactFromGroupsL(); //Delete the contact from the Groups it belongs
   292 
   298 
   293     while(!endGrpValue)
   299     TBool destroyed = EFalse;
       
   300     while(!endGrpValue && !destroyed)
   294         {        
   301         {        
   295         semiColonPos = aValue.Find( KSemiColon() );
   302         semiColonPos = aValue.Find( KSemiColon() );
   296         if(semiColonPos == KErrNotFound)
   303         if(semiColonPos == KErrNotFound)
   297             {
   304             {
   298             semiColonPos = aValue.Length();
   305             semiColonPos = aValue.Length();
   320             // if group already present add the contact to existing group otherwise 
   327             // if group already present add the contact to existing group otherwise 
   321             // add the contact to new group 
   328             // add the contact to new group 
   322             if(groupContactItemId != NULL)
   329             if(groupContactItemId != NULL)
   323                 { 
   330                 { 
   324                 storeContact->LockL(*this);
   331                 storeContact->LockL(*this);
       
   332                 iDestroyed = &destroyed;
   325                 //wait for Lock operation to complete
   333                 //wait for Lock operation to complete
   326                 if( ! (iWait->IsStarted()) )
   334                 if( ! (iWait->IsStarted()) )
   327                     {
   335                     {
   328                     iWait->Start();
   336                     iWait->Start();
   329                     }
   337                     }
   330                 groupContactItemId->AddContactL(*contactLink);
   338                 if ( !destroyed )
   331                 storeContact->CommitL(*this);  // save Group modifications.
       
   332                 if( ! (iWait->IsStarted()) )
       
   333                     {
   339                     {
   334                     iWait->Start();
   340                     iDestroyed = NULL;
       
   341                     groupContactItemId->AddContactL(*contactLink);
       
   342                     storeContact->CommitL(*this);  // save Group modifications.
       
   343                     iDestroyed = &destroyed;
       
   344                     if( ! (iWait->IsStarted()) )
       
   345                         {
       
   346                         iWait->Start();
       
   347                         }
       
   348                     if ( !destroyed )
       
   349                         {
       
   350                         iDestroyed = NULL;
       
   351                         }
   335                     }
   352                     }
   336                 }
   353                 }
   337             else
   354             else
   338                 {                                
   355                 {                                
   339                 CreateNewGroupFromContactL(desGrpValueHash);
   356                 CreateNewGroupFromContactL(desGrpValueHash);
   347             aValue = aValue.Right( aValue.Length() - semiColonPos - KSemiColon().Length());
   364             aValue = aValue.Right( aValue.Length() - semiColonPos - KSemiColon().Length());
   348         } //End Of While loop
   365         } //End Of While loop
   349 
   366 
   350     CleanupStack::PopAndDestroy(); // For contactLink 
   367     CleanupStack::PopAndDestroy(); // For contactLink 
   351 
   368 
   352     if(iStore)
   369     if(!destroyed)
   353         {
   370         {
       
   371         iSelfPtr = NULL;
   354         delete iStore;
   372         delete iStore;
   355         iStore = NULL;
   373         iStore = NULL;
   356         }
   374         }
   357 
   375 
   358     }
   376     }
   359 // ----------------------------------------------------------------------------
   377 // ----------------------------------------------------------------------------
   360 // CVPbkGroupCardHandler::BuildContactGroupsHashMapL
   378 // CVPbkGroupCardHandler::BuildContactGroupsHashMapL
   361 // ----------------------------------------------------------------------------
   379 // ----------------------------------------------------------------------------
   362 void CVPbkGroupCardHandler::BuildContactGroupsHashMapL(MVPbkContactStore& aTargetContactStore)
   380 void CVPbkGroupCardHandler::BuildContactGroupsHashMapL(
       
   381         MVPbkContactStore& aTargetContactStore, 
       
   382         CVPbkGroupCardHandler** aSelfPointer)
   363     {
   383     {
   364     //Get all the Group Links in current VPBK
   384     //Get all the Group Links in current VPBK
   365     MVPbkContactLinkArray* groupLinks = aTargetContactStore.ContactGroupsLC();
   385     MVPbkContactLinkArray* groupLinks = aTargetContactStore.ContactGroupsLC();
   366 
   386 
   367     if( (groupLinks == NULL || iContactsGroupsMap != NULL))
   387     if( (groupLinks == NULL || iContactsGroupsMap != NULL))
   368         return;
   388         return;
   369 
   389 
       
   390     iSelfPtr = aSelfPointer;
       
   391     
   370     iContactsGroupsMap = new(ELeave) RPtrHashMap<TDesC16, MVPbkStoreContact>();
   392     iContactsGroupsMap = new(ELeave) RPtrHashMap<TDesC16, MVPbkStoreContact>();
   371 
   393 
   372     TInt groupCount = 0;
   394     TInt groupCount = 0;
   373     groupCount = groupLinks->Count();
   395     groupCount = groupLinks->Count();
   374 
   396 
   375     // Get Group Name and Group store, store these values into Hash Table.
   397     // Get Group Name and Group store, store these values into Hash Table.
   376     for(TInt i = 0;i < groupCount;i++)
   398     TBool thisDestroyed = EFalse;
       
   399     for(TInt i = 0;i < groupCount && !thisDestroyed;i++)
   377         {
   400         {
   378         const MVPbkContactLink& groupLink = (*groupLinks)[i];
   401         const MVPbkContactLink& groupLink = (*groupLinks)[i];
   379         iData.GetContactManager().RetrieveContactL( groupLink, *this );
   402         thisDestroyed = RetrieveContactL( groupLink, *this );
   380         //wait for Retrieve operation to complete, to get storecontact for the current group link
   403         if (!thisDestroyed && iTargetStore)
   381         if( ! (iWait->IsStarted()) )
       
   382             {
       
   383             iWait->Start();
       
   384             }
       
   385 
       
   386         if (iTargetStore)
       
   387             {
   404             {
   388             MVPbkContactGroup* contactGrp = iTargetStore->Group();
   405             MVPbkContactGroup* contactGrp = iTargetStore->Group();
   389             if(contactGrp)
   406             if(contactGrp)
   390                 {   
   407                 {   
   391                 TPtrC16 contactGroupName;
   408                 TPtrC16 contactGroupName;
   392                 contactGroupName.Set( contactGrp->GroupLabel());
   409                 contactGroupName.Set( contactGrp->GroupLabel());
   393                 HBufC16* grpName = contactGroupName.AllocLC();
   410                 HBufC16* grpName = contactGroupName.AllocLC();
   394                 iContactsGroupsMap->Insert(grpName, iTargetStore);
   411                 // iContactsGroupsMap takes the ownership of iTargetStore
       
   412                 TInt res = iContactsGroupsMap->Insert(grpName, iTargetStore);
       
   413                 if ( res != KErrNone )
       
   414                     {
       
   415                     delete iTargetStore;
       
   416                     }
       
   417                 iTargetStore = NULL;
   395                 CleanupStack::Pop();  //grpName
   418                 CleanupStack::Pop();  //grpName
   396                 }       
   419                 }       
   397             }    
   420             }    
   398         }// End of for loop
   421         }// End of for loop
   399 
   422 
   400     CleanupStack::PopAndDestroy(); //for groupLinks
   423     CleanupStack::PopAndDestroy(); //for groupLinks
       
   424     
       
   425     if ( !thisDestroyed )
       
   426         {
       
   427         iSelfPtr = NULL;
       
   428         }
       
   429     }
       
   430 
       
   431 // ----------------------------------------------------------------------------
       
   432 // CVPbkGroupCardHandler::RetrieveContactL
       
   433 // ----------------------------------------------------------------------------
       
   434 TBool CVPbkGroupCardHandler::RetrieveContactL( const MVPbkContactLink& aLink,
       
   435             MVPbkSingleContactOperationObserver& aObserver)
       
   436     {
       
   437     delete iTargetStore;
       
   438     iTargetStore = NULL;
       
   439     iRetrieveOp = iData.GetContactManager().RetrieveContactL( aLink, aObserver );
       
   440     
       
   441     TBool destroyed = EFalse;
       
   442     // Set iDestroyed to point to stack variable that is accessed from
       
   443     // destructor.
       
   444     iDestroyed = &destroyed; 
       
   445     //wait for GetContactGroupStoreL operation to complete,to get storecontact
       
   446     if( ! (iWait->IsStarted()) )
       
   447         {
       
   448         iWait->Start();
       
   449         }
       
   450     
       
   451     if ( !destroyed )
       
   452         {
       
   453         // Reset pointer to stack variable.
       
   454         iDestroyed = NULL;
       
   455         delete iRetrieveOp;
       
   456         iRetrieveOp = NULL;
       
   457         }
       
   458     return destroyed; 
   401     }
   459     }
   402 
   460 
   403 // ----------------------------------------------------------------------------
   461 // ----------------------------------------------------------------------------
   404 // CVPbkGroupCardHandler::VPbkSingleContactOperationComplete
   462 // CVPbkGroupCardHandler::VPbkSingleContactOperationComplete
   405 // ----------------------------------------------------------------------------
   463 // ----------------------------------------------------------------------------
   406 void CVPbkGroupCardHandler::VPbkSingleContactOperationComplete(
   464 void CVPbkGroupCardHandler::VPbkSingleContactOperationComplete(
   407         MVPbkContactOperationBase& aOperation,
   465         MVPbkContactOperationBase& /*aOperation*/,
   408         MVPbkStoreContact* aContact)
   466         MVPbkStoreContact* aContact)
   409     {
   467     {
   410     iTargetStore = aContact;
   468     iTargetStore = aContact;
   411 
       
   412     MVPbkContactOperationBase* operation = &aOperation;
       
   413     if ( operation )
       
   414         {
       
   415         delete operation;
       
   416         operation = NULL;
       
   417         }
       
   418 
   469 
   419     if( iWait->IsStarted() )
   470     if( iWait->IsStarted() )
   420         iWait->AsyncStop();
   471         iWait->AsyncStop();
   421 
   472 
   422     }
   473     }
   423 
   474 
   424 // ----------------------------------------------------------------------------
   475 // ----------------------------------------------------------------------------
   425 // CVPbkGroupCardHandler::VPbkSingleContactOperationFailed
   476 // CVPbkGroupCardHandler::VPbkSingleContactOperationFailed
   426 // ----------------------------------------------------------------------------
   477 // ----------------------------------------------------------------------------
   427 void CVPbkGroupCardHandler::VPbkSingleContactOperationFailed
   478 void CVPbkGroupCardHandler::VPbkSingleContactOperationFailed
   428 (MVPbkContactOperationBase& aOperation, TInt /*aError*/)
   479 (MVPbkContactOperationBase& /*aOperation*/, TInt /*aError*/)
   429     {
   480     {
   430     iTargetStore = NULL; //To Handle in Error case
       
   431     MVPbkContactOperationBase* operation = &aOperation;
       
   432     if ( operation )
       
   433         {
       
   434         delete operation;
       
   435         operation = NULL;
       
   436         }
       
   437 
       
   438     if( iWait->IsStarted() )
   481     if( iWait->IsStarted() )
   439         iWait->AsyncStop();
   482         iWait->AsyncStop();
   440     }
   483     }
   441 
   484 
   442 // ----------------------------------------------------------------------------
   485 // ----------------------------------------------------------------------------
   443 // CVPbkGroupCardHandler::GetContactGroupStoreL
   486 // CVPbkGroupCardHandler::GetContactGroupStoreL
   444 // Retrieve the contact store of group contact
   487 // Retrieve the contact store of group contact
   445 // ----------------------------------------------------------------------------
   488 // ----------------------------------------------------------------------------
   446 void CVPbkGroupCardHandler::GetContactGroupStoreL(const MVPbkContactLink& aContactLink)
   489 void CVPbkGroupCardHandler::GetContactGroupStoreL(
   447     {
   490         const MVPbkContactLink& aContactLink, 
   448     iData.GetContactManager().RetrieveContactL( aContactLink, *this );
   491         CVPbkGroupCardHandler** aSelfPointer)
   449     //wait for GetContactGroupStoreL operation to complete,to get storecontact
   492     {
   450     if( ! (iWait->IsStarted()) )
   493     iSelfPtr = aSelfPointer;
   451         {
   494     delete iStore;
   452         iWait->Start();
   495     iStore = NULL;
   453         }
   496     if ( !RetrieveContactL( aContactLink, *this ) )
   454     iStore = iTargetStore;
   497         {
       
   498         iSelfPtr = NULL;
       
   499         // iStore takes the ownership.
       
   500         iStore = iTargetStore;
       
   501         iTargetStore = NULL;
       
   502         }        
   455     }
   503     }
   456 
   504 
   457 // ----------------------------------------------------------------------------
   505 // ----------------------------------------------------------------------------
   458 // CVPbkGroupCardHandler::ContactOperationCompleted
   506 // CVPbkGroupCardHandler::ContactOperationCompleted
   459 // ----------------------------------------------------------------------------
   507 // ----------------------------------------------------------------------------