phonebookengines/VirtualPhonebook/VPbkVCardEng/src/CVPbkGroupCardHandler.cpp
changeset 0 e686773b3f54
child 17 2666d9724c76
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Handler to manage groupcard property during import and export
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CVPbkGroupCardHandler.h"
       
    20 
       
    21 #include <MVPbkContactStore.h>
       
    22 #include <MVPbkContactGroup.h>
       
    23 #include <MVPbkContactStore2.h>
       
    24 #include <MVPbkContactStoreProperties.h>
       
    25 #include <MVPbkContactStoreProperties2.h>
       
    26 #include <MVPbkContactOperationBase.h>
       
    27 #include <CVPbkContactManager.h>
       
    28 #include <CVPbkContactIdConverter.h>
       
    29 #include <MVPbkStoreContact2.h>
       
    30 
       
    31 _LIT8(KGroup,"X-CATEGORIES");
       
    32 _LIT16(KSemiColon,";");    
       
    33 _LIT16(KVersitTokencrlf,"\r\n");
       
    34 
       
    35 // ----------------------------------------------------------------------------
       
    36 // CVPbkGroupCardHandler::CVPbkGroupCardHandler
       
    37 // ----------------------------------------------------------------------------
       
    38 CVPbkGroupCardHandler::CVPbkGroupCardHandler(CVPbkVCardData& aData):iData(aData)
       
    39             {
       
    40             }
       
    41 
       
    42 // ----------------------------------------------------------------------------
       
    43 // CVPbkGroupCardHandler::NewL
       
    44 // ----------------------------------------------------------------------------
       
    45 CVPbkGroupCardHandler* CVPbkGroupCardHandler::NewL(CVPbkVCardData& aData)
       
    46     {
       
    47     CVPbkGroupCardHandler* self = new (ELeave) CVPbkGroupCardHandler(aData);
       
    48     CleanupStack::PushL( self );
       
    49     self->ConstructL();
       
    50     CleanupStack::Pop( self );
       
    51     return self;
       
    52     }
       
    53 
       
    54 // ----------------------------------------------------------------------------
       
    55 // CVPbkGroupCardHandler::ConstructL
       
    56 // ----------------------------------------------------------------------------
       
    57 void CVPbkGroupCardHandler::ConstructL()
       
    58     {
       
    59     iWait = new( ELeave )CActiveSchedulerWait();
       
    60     iContactsGroupsMap = NULL;
       
    61     iTargetStore = NULL;
       
    62     iStore = NULL;
       
    63     }
       
    64 
       
    65 // ----------------------------------------------------------------------------
       
    66 // CVPbkGroupCardHandler::~CVPbkGroupCardHandler
       
    67 // ----------------------------------------------------------------------------
       
    68 CVPbkGroupCardHandler::~CVPbkGroupCardHandler()
       
    69     {
       
    70     if ( iWait )
       
    71         {
       
    72         if( iWait->IsStarted() )
       
    73             iWait->AsyncStop();
       
    74         delete iWait;
       
    75         iWait = NULL;
       
    76         }
       
    77 
       
    78     if(iStore)
       
    79         {
       
    80         delete iStore;
       
    81         iStore = NULL;
       
    82         }
       
    83 
       
    84     if (iContactsGroupsMap)
       
    85         {   
       
    86         iContactsGroupsMap->ResetAndDestroy();
       
    87         iContactsGroupsMap->Close();
       
    88         delete iContactsGroupsMap;
       
    89         }
       
    90     }
       
    91 
       
    92 // ----------------------------------------------------------------------------
       
    93 // CVPbkGroupCardHandler::CreateXSelfPropertyL
       
    94 // ----------------------------------------------------------------------------
       
    95 CParserProperty* CVPbkGroupCardHandler::CreateXGroupPropertyL(const MVPbkStoreContact* aStore)
       
    96     {
       
    97     TPtrC8 text( KGroup);
       
    98     CParserProperty* property = NULL;
       
    99     CVPbkVCardParserParamArray* params = NULL;
       
   100     
       
   101     if ( aStore == NULL)
       
   102         return property;
       
   103 
       
   104     MVPbkContactLinkArray* groupIdArray = aStore->GroupsJoinedLC(); //Get Groupid list of contact.
       
   105 
       
   106     if ( groupIdArray == NULL)
       
   107         return property;
       
   108 
       
   109     TInt totalGroupsInContact = 0;
       
   110     totalGroupsInContact = groupIdArray->Count();
       
   111 
       
   112     if (totalGroupsInContact == 0)
       
   113         {
       
   114         //if contact has no groups,Append X-CATEGORIES field with NULL field Value. 
       
   115         CParserPropertyValueHBufC* tempValue = CParserPropertyValueHBufC::NewL( KNullDesC() );
       
   116         CParserPropertyValue* value = static_cast<CParserPropertyValue*>(tempValue);
       
   117         property = CParserProperty::NewL( *value,text, params);
       
   118         CleanupStack::PopAndDestroy();  //For GoupIdArray
       
   119         return property;
       
   120         }
       
   121 
       
   122     CDesCArrayFlat* desArray=new (ELeave)CDesCArrayFlat(totalGroupsInContact);
       
   123     CleanupStack::PushL(desArray);
       
   124 
       
   125     //Append all the Group Names of contact to X-CATEGORIES field value. 
       
   126     for ( TInt m = 0 ; m < totalGroupsInContact ; m++ )
       
   127         {                  
       
   128         const  MVPbkContactLink& groupLink = (*groupIdArray)[m];                                             
       
   129         iData.GetContactManager().RetrieveContactL( groupLink, *this );
       
   130         //wait for ContactRetrieve operation to complete, to get storecontact for the current group link
       
   131         if( ! (iWait->IsStarted()) )
       
   132             {
       
   133             iWait->Start();
       
   134             }
       
   135 
       
   136         if (iTargetStore)
       
   137             {
       
   138             MVPbkContactGroup* contactGrp = iTargetStore->Group();
       
   139             if(contactGrp)
       
   140                 {
       
   141                 CParserPropertyValueHBufC* tempValue = CParserPropertyValueHBufC::NewL( contactGrp->GroupLabel() );
       
   142                 CleanupStack::PushL(tempValue);
       
   143                 desArray->AppendL(tempValue->Value());
       
   144                 CleanupStack::PopAndDestroy(tempValue);
       
   145                 }
       
   146             delete iTargetStore;
       
   147             iTargetStore = NULL;
       
   148             }
       
   149         }
       
   150 
       
   151     CParserPropertyValue* value = new (ELeave) CParserPropertyValueCDesCArray(desArray);
       
   152     CleanupStack::PushL(value);
       
   153 
       
   154     property = CParserProperty::NewL( *value, text, params); //Create the Property with field and Value
       
   155 
       
   156     CleanupStack::Pop(value);
       
   157     CleanupStack::Pop(desArray);
       
   158     CleanupStack::PopAndDestroy();  //For GoupIdArray
       
   159 
       
   160     return property;
       
   161     }
       
   162 
       
   163 // ----------------------------------------------------------------------------
       
   164 // CVPbkGroupCardHandler::DeleteContactFromGroupsL
       
   165 // ----------------------------------------------------------------------------
       
   166 void CVPbkGroupCardHandler::DeleteContactFromGroupsL()
       
   167     {
       
   168     if(iStore == NULL)
       
   169         return;
       
   170     //Get Groupid list a contact belongs
       
   171     MVPbkContactLinkArray* groupIds = iStore->GroupsJoinedLC(); 
       
   172 
       
   173     if(groupIds == NULL)
       
   174         return; 
       
   175 
       
   176     TInt groupIdsCount = 0;
       
   177     groupIdsCount = groupIds->Count();    
       
   178     const MVPbkContactLink* contactLink = iStore->CreateLinkLC();
       
   179 
       
   180     //Remove the contact from all the Groups it belongs. 
       
   181     for(TInt i = 0;i < groupIdsCount;i++)
       
   182         {
       
   183         const MVPbkContactLink& groupLink = (*groupIds)[i];
       
   184         iData.GetContactManager().RetrieveContactL( groupLink, *this );
       
   185         //wait for Retrieve operation to complete,to get storecontact for the current group link
       
   186         if( ! (iWait->IsStarted()) )
       
   187             {
       
   188             iWait->Start();
       
   189             }
       
   190 
       
   191         if (iTargetStore)
       
   192             { 
       
   193             MVPbkContactGroup* contactGrp = iTargetStore->Group();
       
   194             if(contactGrp)
       
   195                 {   
       
   196                 iTargetStore->LockL(*this); //Lock the contact before editing the contact.
       
   197                 //wait for LockL operation to complete
       
   198                 if( ! (iWait->IsStarted()) )
       
   199                     {
       
   200                     iWait->Start();
       
   201                     }
       
   202 
       
   203                 contactGrp->RemoveContactL(*contactLink); //Removes a contact from a Group
       
   204 
       
   205                 iTargetStore->CommitL(*this);  //save the Changes to VPBK.
       
   206                 //wait for CommitL operation to complete
       
   207                 if( ! (iWait->IsStarted()) )
       
   208                     {
       
   209                     iWait->Start();
       
   210                     }
       
   211                 }  
       
   212 
       
   213             delete iTargetStore;
       
   214             iTargetStore = NULL;
       
   215             }
       
   216         }
       
   217 
       
   218     CleanupStack::PopAndDestroy();  // for contactLink
       
   219     CleanupStack::PopAndDestroy(); // for groupIds
       
   220     }
       
   221 
       
   222 // ----------------------------------------------------------------------------
       
   223 // CVPbkGroupCardHandler::CreateNewGroupFromContactL
       
   224 // ----------------------------------------------------------------------------
       
   225 void CVPbkGroupCardHandler::CreateNewGroupFromContactL(TPtr16& aGroupLabel)
       
   226     {
       
   227     if(iStore == NULL)
       
   228         return;
       
   229     //Creates New Group 
       
   230     MVPbkContactGroup* groupItem = iStore->ParentStore().CreateNewContactGroupLC(); 
       
   231 
       
   232     if(groupItem)
       
   233         {                    
       
   234         groupItem->SetGroupLabelL(aGroupLabel); // set the group name.
       
   235         
       
   236         //Retrieve group store
       
   237         const MVPbkContactLink* grpContactLink = groupItem->CreateLinkLC();
       
   238         iData.GetContactManager().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
       
   245 
       
   246         if (iTargetStore)
       
   247             {
       
   248             const MVPbkContactLink* contactLink = iStore->CreateLinkLC();
       
   249             groupItem->AddContactL(*contactLink);
       
   250             CleanupStack::PopAndDestroy(); //For aContactLink
       
   251             groupItem->CommitL(*this); 
       
   252             if( ! (iWait->IsStarted()) )
       
   253                 {
       
   254                 iWait->Start();
       
   255                 }//Commit to VPBK for new grp changes.
       
   256 
       
   257             //Add the New Group to Hash Table
       
   258             TPtrC16 contactGroupName;
       
   259             contactGroupName.Set( aGroupLabel );
       
   260             HBufC16* grpName = contactGroupName.AllocLC();
       
   261 
       
   262             if(iContactsGroupsMap == NULL)
       
   263                 {
       
   264                 iContactsGroupsMap = new(ELeave) RPtrHashMap<TDesC16, MVPbkStoreContact>();
       
   265                 }
       
   266             iContactsGroupsMap->Insert(grpName, iTargetStore);
       
   267             CleanupStack::Pop(); // To pop up grpName
       
   268             }
       
   269         }   
       
   270 
       
   271     CleanupStack::PopAndDestroy(); // To pop up and destroy groupItem  
       
   272     }
       
   273 
       
   274 // ----------------------------------------------------------------------------
       
   275 // CVPbkGroupCardHandler::DecodeContactGroupInVCardL
       
   276 // ----------------------------------------------------------------------------
       
   277 void CVPbkGroupCardHandler::DecodeContactGroupInVCardL(TPtr16 aValue)
       
   278     {        
       
   279     if(iStore == NULL)
       
   280         return; 
       
   281     const MVPbkContactLink* contactLink = iStore->CreateLinkLC();
       
   282 
       
   283     TInt newLinePos = aValue.Find(KVersitTokencrlf());
       
   284 
       
   285     if (newLinePos != KErrNotFound)
       
   286         aValue = aValue.Left(newLinePos); //remove CRLF from aValue
       
   287 
       
   288     TInt semiColonPos = 0;
       
   289     TBool endGrpValue = EFalse;    
       
   290 
       
   291     DeleteContactFromGroupsL(); //Delete the contact from the Groups it belongs
       
   292 
       
   293     while(!endGrpValue)
       
   294         {        
       
   295         semiColonPos = aValue.Find( KSemiColon() );
       
   296         if(semiColonPos == KErrNotFound)
       
   297             {
       
   298             semiColonPos = aValue.Length();
       
   299             endGrpValue = ETrue;  // all the Group card values are parsed
       
   300             }
       
   301 
       
   302         if(semiColonPos > 0)
       
   303             {          
       
   304             //Get each group card value to save OR add to Groups
       
   305             TPtrC16 desGrpValue = aValue.Left(semiColonPos); 
       
   306             HBufC16* unicodeGrpValue = desGrpValue.Alloc();
       
   307             CleanupStack::PushL( unicodeGrpValue );
       
   308             TPtr16 desGrpValueHash = unicodeGrpValue->Des();
       
   309 
       
   310             MVPbkContactGroup* groupContactItemId = NULL;
       
   311             MVPbkStoreContact* storeContact = NULL;
       
   312             if(iContactsGroupsMap)
       
   313                 {
       
   314                 // Find whether Group already present in VPBK, to get group store
       
   315                 storeContact = iContactsGroupsMap->Find(desGrpValueHash);
       
   316                 if (storeContact)
       
   317                     groupContactItemId = storeContact->Group();
       
   318                 }
       
   319 
       
   320             // if group already present add the contact to existing group otherwise 
       
   321             // add the contact to new group 
       
   322             if(groupContactItemId != NULL)
       
   323                 { 
       
   324                 storeContact->LockL(*this);
       
   325                 //wait for Lock operation to complete
       
   326                 if( ! (iWait->IsStarted()) )
       
   327                     {
       
   328                     iWait->Start();
       
   329                     }
       
   330                 groupContactItemId->AddContactL(*contactLink);
       
   331                 storeContact->CommitL(*this);  // save Group modifications.
       
   332                 if( ! (iWait->IsStarted()) )
       
   333                     {
       
   334                     iWait->Start();
       
   335                     }
       
   336                 }
       
   337             else
       
   338                 {                                
       
   339                 CreateNewGroupFromContactL(desGrpValueHash);
       
   340                 }// End of if(err = KErrNone)
       
   341 
       
   342             CleanupStack::PopAndDestroy(unicodeGrpValue);
       
   343             }// End of if(semiColonPos > 0)
       
   344 
       
   345         //Get Next Group card Value
       
   346         if(!endGrpValue)
       
   347             aValue = aValue.Right( aValue.Length() - semiColonPos - KSemiColon().Length());
       
   348         } //End Of While loop
       
   349 
       
   350     CleanupStack::PopAndDestroy(); // For contactLink 
       
   351 
       
   352     if(iStore)
       
   353         {
       
   354         delete iStore;
       
   355         iStore = NULL;
       
   356         }
       
   357 
       
   358     }
       
   359 // ----------------------------------------------------------------------------
       
   360 // CVPbkGroupCardHandler::BuildContactGroupsHashMapL
       
   361 // ----------------------------------------------------------------------------
       
   362 void CVPbkGroupCardHandler::BuildContactGroupsHashMapL(MVPbkContactStore& aTargetContactStore)
       
   363     {
       
   364     //Get all the Group Links in current VPBK
       
   365     MVPbkContactLinkArray* groupLinks = aTargetContactStore.ContactGroupsLC();
       
   366 
       
   367     if( (groupLinks == NULL || iContactsGroupsMap != NULL))
       
   368         return;
       
   369 
       
   370     iContactsGroupsMap = new(ELeave) RPtrHashMap<TDesC16, MVPbkStoreContact>();
       
   371 
       
   372     TInt groupCount = 0;
       
   373     groupCount = groupLinks->Count();
       
   374 
       
   375     // Get Group Name and Group store, store these values into Hash Table.
       
   376     for(TInt i = 0;i < groupCount;i++)
       
   377         {
       
   378         const MVPbkContactLink& groupLink = (*groupLinks)[i];
       
   379         iData.GetContactManager().RetrieveContactL( groupLink, *this );
       
   380         //wait for Retrieve operation to complete, to get storecontact for the current group link
       
   381         if( ! (iWait->IsStarted()) )
       
   382             {
       
   383             iWait->Start();
       
   384             }
       
   385 
       
   386         if (iTargetStore)
       
   387             {
       
   388             MVPbkContactGroup* contactGrp = iTargetStore->Group();
       
   389             if(contactGrp)
       
   390                 {   
       
   391                 TPtrC16 contactGroupName;
       
   392                 contactGroupName.Set( contactGrp->GroupLabel());
       
   393                 HBufC16* grpName = contactGroupName.AllocLC();
       
   394                 iContactsGroupsMap->Insert(grpName, iTargetStore);
       
   395                 CleanupStack::Pop();  //grpName
       
   396                 }       
       
   397             }    
       
   398         }// End of for loop
       
   399 
       
   400     CleanupStack::PopAndDestroy(); //for groupLinks
       
   401     }
       
   402 
       
   403 // ----------------------------------------------------------------------------
       
   404 // CVPbkGroupCardHandler::VPbkSingleContactOperationComplete
       
   405 // ----------------------------------------------------------------------------
       
   406 void CVPbkGroupCardHandler::VPbkSingleContactOperationComplete(
       
   407         MVPbkContactOperationBase& aOperation,
       
   408         MVPbkStoreContact* aContact)
       
   409     {
       
   410     iTargetStore = aContact;
       
   411 
       
   412     MVPbkContactOperationBase* operation = &aOperation;
       
   413     if ( operation )
       
   414         {
       
   415         delete operation;
       
   416         operation = NULL;
       
   417         }
       
   418 
       
   419     if( iWait->IsStarted() )
       
   420         iWait->AsyncStop();
       
   421 
       
   422     }
       
   423 
       
   424 // ----------------------------------------------------------------------------
       
   425 // CVPbkGroupCardHandler::VPbkSingleContactOperationFailed
       
   426 // ----------------------------------------------------------------------------
       
   427 void CVPbkGroupCardHandler::VPbkSingleContactOperationFailed
       
   428 (MVPbkContactOperationBase& aOperation, TInt /*aError*/)
       
   429     {
       
   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() )
       
   439         iWait->AsyncStop();
       
   440     }
       
   441 
       
   442 // ----------------------------------------------------------------------------
       
   443 // CVPbkGroupCardHandler::GetContactGroupStoreL
       
   444 // Retrieve the contact store of group contact
       
   445 // ----------------------------------------------------------------------------
       
   446 void CVPbkGroupCardHandler::GetContactGroupStoreL(const MVPbkContactLink& aContactLink)
       
   447     {
       
   448     iData.GetContactManager().RetrieveContactL( aContactLink, *this );
       
   449     //wait for GetContactGroupStoreL operation to complete,to get storecontact
       
   450     if( ! (iWait->IsStarted()) )
       
   451         {
       
   452         iWait->Start();
       
   453         }
       
   454     iStore = iTargetStore;
       
   455     }
       
   456 
       
   457 // ----------------------------------------------------------------------------
       
   458 // CVPbkGroupCardHandler::ContactOperationCompleted
       
   459 // ----------------------------------------------------------------------------
       
   460 void CVPbkGroupCardHandler::ContactOperationCompleted(TContactOpResult aResult)
       
   461     {
       
   462     delete aResult.iStoreContact;
       
   463     if( iWait->IsStarted() )
       
   464         iWait->AsyncStop();
       
   465     }
       
   466 
       
   467 // ----------------------------------------------------------------------------
       
   468 // CVPbkGroupCardHandler::ContactOperationFailed
       
   469 // ----------------------------------------------------------------------------
       
   470 void CVPbkGroupCardHandler::ContactOperationFailed
       
   471 (TContactOp /*aOpCode*/, TInt /*aErrorCode*/, TBool /*aErrorNotified*/)
       
   472     {
       
   473     if( iWait->IsStarted() )
       
   474         iWait->AsyncStop();
       
   475     }