phonebookengines/VirtualPhonebook/VPbkVCardEng/src/CVPbkImportToStoreOperation.cpp
changeset 0 e686773b3f54
child 13 a6539d1e8e43
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  An operation for importing and saving a contact to the store
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CVPbkImportToStoreOperation.h"
       
    20 
       
    21 #include "CVPbkVCardCompactBCardImporter.h"
       
    22 #include "CVPbkVCardImporter.h"
       
    23 #include "CVPbkVCardData.h"
       
    24 #include "VPbkVCardEngError.h"
       
    25 
       
    26 // From Virtual Phonebook
       
    27 #include <MVPbkStoreContact.h>
       
    28 #include <MVPbkContactStore.h>
       
    29 #include <MVPbkContactStoreProperties.h>
       
    30 #include <CVPbkContactLinkArray.h>
       
    31 #include <MVPbkContactCopyPolicy.h>
       
    32 #include <MVPbkContactLink.h>
       
    33 #include <MVPbkContactDuplicatePolicy.h>
       
    34 
       
    35 /// Unnamed namespace for local definitions
       
    36 namespace 
       
    37     {
       
    38     // Max number of duplicate contacts to find
       
    39     const TInt KDuplicatesToFind = 1;
       
    40     }
       
    41 
       
    42 // ======== MEMBER FUNCTIONS ========
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // CVPbkImportToStoreOperation::CVPbkImportToStoreOperation
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CVPbkImportToStoreOperation::CVPbkImportToStoreOperation(
       
    49         MVPbkContactCopyObserver& aObserver,
       
    50         MVPbkContactStore& aTargetStore )
       
    51         :   iTargetStore( aTargetStore ),
       
    52             iObserver( aObserver )
       
    53     {
       
    54     iOwncardHandler = NULL;
       
    55     iGroupcardHandler = NULL;
       
    56     }
       
    57 
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // CVPbkImportToStoreOperation::ConstructL
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 void CVPbkImportToStoreOperation::ConstructL( TVPbkImportCardType aType,
       
    64         RReadStream &aSourceStream, CVPbkVCardData& aData, TBool aSync )
       
    65     {
       
    66     iCopyPolicy = &aData.CopyPolicyL( iTargetStore.StoreProperties().Uri() );
       
    67     if ( !iCopyPolicy )
       
    68         {
       
    69         User::Leave( KErrNotSupported );
       
    70         }
       
    71 
       
    72     // If contact data merge is supported then use duplicate checking
       
    73     // before copying.
       
    74     // During sync duplicate policy is ignored to improve performance
       
    75     if ( iCopyPolicy->SupportsContactMerge() && ! aSync )
       
    76         {
       
    77            iDuplicatePolicy = aData.DuplicatePolicy();
       
    78         }
       
    79 
       
    80     if ( aType == ECompactBusinessCard )
       
    81         {
       
    82         iOperationImpl = CVPbkVCardCompactBCardImporter::NewL(
       
    83             iImportedContacts, iTargetStore, aSourceStream, aData );
       
    84         }
       
    85     else if ( aType == EVCard )
       
    86         {
       
    87         iOperationImpl = CVPbkVCardImporter::NewL(
       
    88             iImportedContacts, aSourceStream, iTargetStore, aData );
       
    89         }
       
    90     else
       
    91         {
       
    92         User::Leave( KErrArgument );
       
    93         }
       
    94 
       
    95     iSavedContacts = CVPbkContactLinkArray::NewL();
       
    96    // Handle Owncard during Sync
       
    97     if(aSync)
       
    98 	    {
       
    99 	    iOwncardHandler = CVPbkOwnCardHandler::NewL(aData);	
       
   100 	    iGroupcardHandler = CVPbkGroupCardHandler::NewL(aData);
       
   101 	    }
       
   102     }
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // CVPbkImportToStoreOperation::NewL
       
   106 // ---------------------------------------------------------------------------
       
   107 //
       
   108 CVPbkImportToStoreOperation* CVPbkImportToStoreOperation::NewL(
       
   109         TVPbkImportCardType aType,
       
   110         MVPbkContactStore& aTargetStore,
       
   111         RReadStream& aSourceStream,
       
   112         CVPbkVCardData& aData,
       
   113         MVPbkContactCopyObserver& aObserver,
       
   114         TBool aSync )
       
   115     {
       
   116     CVPbkImportToStoreOperation* self =
       
   117         new( ELeave ) CVPbkImportToStoreOperation( aObserver, aTargetStore );
       
   118     CleanupStack::PushL( self );
       
   119     self->ConstructL( aType, aSourceStream, aData, aSync );
       
   120     CleanupStack::Pop( self );
       
   121     return self;
       
   122     }
       
   123 
       
   124 
       
   125 // ---------------------------------------------------------------------------
       
   126 // CVPbkImportToStoreOperation::~CVPbkImportToStoreOperation
       
   127 // ---------------------------------------------------------------------------
       
   128 //
       
   129 CVPbkImportToStoreOperation::~CVPbkImportToStoreOperation()
       
   130     {
       
   131     delete iVPbkOperation;
       
   132     delete iSavedContacts;
       
   133     delete iOperationImpl;
       
   134     iImportedContacts.ResetAndDestroy();
       
   135     iDuplicates.ResetAndDestroy();
       
   136     delete iOwncardHandler;
       
   137     delete iGroupcardHandler;
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // From class MVPbkContactOperation
       
   142 // CVPbkImportToStoreOperation::StartL
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 void CVPbkImportToStoreOperation::StartL()
       
   146     {
       
   147     __ASSERT_DEBUG( iOperationImpl, VPbkVCardEngError::Panic(
       
   148         VPbkVCardEngError::EPreCond_CVPbkImportToStoreOperation_StartL ) );
       
   149 
       
   150     iOperationImpl->SetObserver( *this );
       
   151     iOperationImpl->StartL();
       
   152     }
       
   153 
       
   154 // ---------------------------------------------------------------------------
       
   155 // From class MVPbkContactOperation
       
   156 // CVPbkImportToStoreOperation::Cancel
       
   157 // ---------------------------------------------------------------------------
       
   158 //
       
   159 void CVPbkImportToStoreOperation::Cancel()
       
   160     {
       
   161     delete iOperationImpl;
       
   162     iOperationImpl = NULL;
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // From class MVPbkImportOperationObserver
       
   167 // CVPbkImportToStoreOperation::ContactsImported
       
   168 // ---------------------------------------------------------------------------
       
   169 //
       
   170 void CVPbkImportToStoreOperation::ContactsImported()
       
   171     {
       
   172     TInt err( KErrNone );
       
   173     if ( iImportedContacts.Count() == 0 )
       
   174         {
       
   175         err = KErrNotFound;
       
   176         }
       
   177     else if ( iDuplicatePolicy )
       
   178         {
       
   179         ResetOperation();
       
   180         iDuplicates.ResetAndDestroy();
       
   181         TRAP( err, iVPbkOperation = iDuplicatePolicy->FindDuplicatesL(
       
   182             *iImportedContacts[0], iTargetStore, iDuplicates, *this,
       
   183             KDuplicatesToFind ) );
       
   184         }
       
   185     else
       
   186         {
       
   187         TRAP( err, CommitNextContactL() );
       
   188         }
       
   189     // delegate errors
       
   190     if ( err != KErrNone )
       
   191         {
       
   192         ContactImportingFailed( err );
       
   193         }
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // From class MVPbkImportOperationObserver
       
   198 // CVPbkImportToStoreOperation::ContactsImportedCompleted
       
   199 // ---------------------------------------------------------------------------
       
   200 //
       
   201 void CVPbkImportToStoreOperation::ContactsImportingCompleted()
       
   202 {
       
   203     MVPbkContactLinkArray* results = iSavedContacts;
       
   204     iSavedContacts = NULL;
       
   205     
       
   206     // Notice that client can delete this operation in
       
   207     // ContactsSaved so there must be no usage of member data
       
   208     // after calling this.
       
   209     if (results->Count())
       
   210         {
       
   211         // One or more successfully saved contacts
       
   212         iObserver.ContactsSaved( *this, results );
       
   213         }
       
   214     else
       
   215         {
       
   216         // No imported and saved contacts available
       
   217         ContactImportingFailed(KErrNotFound);
       
   218         }
       
   219     
       
   220 };
       
   221 
       
   222 // ---------------------------------------------------------------------------
       
   223 // From class MVPbkImportOperationObserver
       
   224 // CVPbkImportToStoreOperation::ContactImportingFailed
       
   225 // ---------------------------------------------------------------------------
       
   226 //
       
   227 void CVPbkImportToStoreOperation::ContactImportingFailed( TInt aError )
       
   228     {
       
   229     iObserver.ContactsSavingFailed( *this, aError );
       
   230     }
       
   231 
       
   232 // ---------------------------------------------------------------------------
       
   233 // From class MVPbkImportOperationObserver
       
   234 // CVPbkImportToStoreOperation::ContactsSaved
       
   235 // ---------------------------------------------------------------------------
       
   236 //
       
   237 void CVPbkImportToStoreOperation::ContactsSaved(
       
   238         MVPbkContactOperationBase& /*aOperation*/,
       
   239         MVPbkContactLinkArray* aResults )
       
   240     {
       
   241     ResetOperation();
       
   242     TRAPD( res, HandleContactSavedL( aResults ) );
       
   243     if ( res != KErrNone )
       
   244         {
       
   245         ContactImportingFailed( res );
       
   246         }
       
   247     }
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // From class MVPbkImportOperationObserver
       
   251 // CVPbkImportToStoreOperation::ContactsSavingFailed
       
   252 // ---------------------------------------------------------------------------
       
   253 //
       
   254 void CVPbkImportToStoreOperation::ContactsSavingFailed(
       
   255         MVPbkContactOperationBase& /*aOperation*/,
       
   256         TInt aError)
       
   257     {
       
   258     ContactImportingFailed( aError );
       
   259     }
       
   260 
       
   261 // ---------------------------------------------------------------------------
       
   262 // From class MVPbkContactFindObserver
       
   263 // CVPbkImportToStoreOperation::FindCompleteL
       
   264 // ---------------------------------------------------------------------------
       
   265 //
       
   266 void CVPbkImportToStoreOperation::FindCompleteL(
       
   267         MVPbkContactLinkArray* aResults )
       
   268     {
       
   269     // Links not needed
       
   270     delete aResults;
       
   271 
       
   272     if ( iDuplicates.Count() > 0 )
       
   273         {
       
   274         // Merge and commit, let policy decide how to handle merge and
       
   275         // what to do with imported contacts. Policy takes the contacts.
       
   276         ResetOperation();
       
   277         iVPbkOperation = iCopyPolicy->MergeAndSaveContactsL(
       
   278             iImportedContacts, *iDuplicates[0], *this );
       
   279         }
       
   280     else
       
   281         {
       
   282         // No duplicates, save imported contacts.
       
   283         CommitNextContactL();
       
   284         }
       
   285     }
       
   286 
       
   287 // ---------------------------------------------------------------------------
       
   288 // From class MVPbkContactFindObserver
       
   289 // CVPbkImportToStoreOperation::FindFailed
       
   290 // ---------------------------------------------------------------------------
       
   291 //
       
   292 void CVPbkImportToStoreOperation::FindFailed( TInt aError )
       
   293     {
       
   294     ContactImportingFailed( aError );
       
   295     }
       
   296 
       
   297 // ---------------------------------------------------------------------------
       
   298 // CVPbkImportToStoreOperation::CommitNextContactL
       
   299 // ---------------------------------------------------------------------------
       
   300 //
       
   301 void CVPbkImportToStoreOperation::CommitNextContactL()
       
   302     {
       
   303     __ASSERT_DEBUG( iImportedContacts.Count() > 0,
       
   304         VPbkVCardEngError::Panic(
       
   305             VPbkVCardEngError::EPreCond_CommitNextContactL ) );
       
   306     const TInt firstContact = 0;
       
   307     MVPbkStoreContact* contact = iImportedContacts[firstContact];
       
   308     /// contact ownership is given
       
   309     ResetOperation();
       
   310     iVPbkOperation = iCopyPolicy->CommitContactL( contact, *this );
       
   311     iImportedContacts.Remove( firstContact );
       
   312     }
       
   313 
       
   314 // ---------------------------------------------------------------------------
       
   315 // CVPbkImportToStoreOperation::HandleContactSavedL
       
   316 // ---------------------------------------------------------------------------
       
   317 //
       
   318 void CVPbkImportToStoreOperation::HandleContactSavedL(
       
   319         MVPbkContactLinkArray* aResults )
       
   320     {
       
   321     CleanupDeletePushL( aResults );
       
   322     const TInt count = aResults->Count();
       
   323     
       
   324     TBool added = EFalse;
       
   325     for ( TInt i = 0; i < count; ++i )
       
   326         {
       
   327         iSavedContacts->AppendL( (*aResults)[i].CloneLC() );
       
   328         added = ETrue;
       
   329         CleanupStack::Pop();
       
   330         }
       
   331 
       
   332     const TInt duplicateCount = iDuplicates.Count();
       
   333     const TInt curUnsavedCount = iImportedContacts.Count();
       
   334     // Duplicates are returned to the client if following conditions apply
       
   335     // 1) There are duplicates (iDuplicates.Count() > 0)
       
   336     // 2) All imported contacts have been saved (iImportedContacts.Count() == 0)
       
   337     // 3) Copy policy didn't actually save anything
       
   338     //    (iSavedContacts->Count() == 0 )
       
   339     //if ( ( duplicateCount > 0 ) &&
       
   340     //     ( curUnsavedCount == 0 ) &&
       
   341     //     ( iSavedContacts->Count() == 0 ) )
       
   342          
       
   343      if ( ( duplicateCount > 0 ) &&
       
   344           ( curUnsavedCount == 0 ) &&
       
   345           ( added == EFalse ) )     
       
   346         {
       
   347         for ( TInt k = 0; k < duplicateCount; ++k )
       
   348             {
       
   349             iSavedContacts->AppendL( iDuplicates[k]->CreateLinkLC() );
       
   350             CleanupStack::Pop();
       
   351             }
       
   352         }
       
   353 		if(iOwncardHandler && iOperationImpl->IsOwncard())
       
   354 	        {
       
   355 	    	iOwncardHandler->SetAsOwnContactL(aResults->At(0));
       
   356 	    	}
       
   357      if(iGroupcardHandler && ((CVPbkVCardImporter *)iOperationImpl)->IsGroupcard())
       
   358          {
       
   359          iGroupcardHandler->BuildContactGroupsHashMapL(iTargetStore);
       
   360          iGroupcardHandler->GetContactGroupStoreL(aResults->At(0));
       
   361          iGroupcardHandler->DecodeContactGroupInVCardL(((CVPbkVCardImporter *)iOperationImpl)->GetGroupcardvalue());
       
   362          }
       
   363     CleanupStack::PopAndDestroy(); // aResults
       
   364 
       
   365     if ( curUnsavedCount == 0 )
       
   366         {
       
   367         // Try to read the next entry, if one existed
       
   368         static_cast<CVPbkVCardImporter*>(iOperationImpl)->StartL();
       
   369         }
       
   370     else
       
   371         {
       
   372         CommitNextContactL();
       
   373         static_cast<CVPbkVCardImporter*>(iOperationImpl)->StartL();
       
   374         }
       
   375 
       
   376     }
       
   377 
       
   378 // ---------------------------------------------------------------------------
       
   379 // CVPbkImportToStoreOperation::ResetOperation
       
   380 // ---------------------------------------------------------------------------
       
   381 //
       
   382 void CVPbkImportToStoreOperation::ResetOperation()
       
   383     {
       
   384     delete iVPbkOperation;
       
   385     iVPbkOperation = NULL;
       
   386     }
       
   387 // End of file