ccservices/cmsservices/cmsengine/Server/src/cmsfindlinkedcontact.cpp
changeset 0 e686773b3f54
child 21 9da50d567e3c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2009 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: This class provides interface to find the First Contact in DefaultCntDbUri 
       
    15 *  which is linked to an XSPId.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDES
       
    21 #include <MVPbkContactStore.h>
       
    22 #include <CVPbkContactManager.h>
       
    23 #include <VPbkContactStoreUris.h>
       
    24 #include <CVPbkContactLinkArray.h>
       
    25 #include <MVPbkContactStoreList.h>
       
    26 #include <CVPbkFieldTypeRefsList.h>
       
    27 #include <VPbkEng.rsg>
       
    28 
       
    29 #include <MVPbkContactOperationBase.h>
       
    30 #include <MVPbkContactStoreProperties.h>
       
    31 #include <TVPbkWordParserCallbackParam.h>
       
    32 #include <CVPbkContactStoreUriArray.h>
       
    33 #include "cmsfindlinkedcontact.h"
       
    34 #include "cmsdebug.h"
       
    35 
       
    36 #include <MVPbkStoreContact.h>
       
    37 #include <MVPbkContactStore.h>
       
    38 #include <MVPbkContactStoreProperties.h> 
       
    39 #include <VPbkContactStoreUris.h>
       
    40 #include <MVPbkContactFieldUriData.h>
       
    41 #include <MVPbkContactLink.h>
       
    42 
       
    43 // CONSTANTS
       
    44 const TInt KArrayGranularity = 2;
       
    45 
       
    46 
       
    47 // ----------------------------------------------------------
       
    48 // CCmsLinkedContact::NewL
       
    49 //
       
    50 // ----------------------------------------------------------
       
    51 //
       
    52 CCmsFindLinkedContact* CCmsFindLinkedContact::NewL( CVPbkContactManager& aContactManager )
       
    53     {
       
    54     CCmsFindLinkedContact* self = new ( ELeave ) CCmsFindLinkedContact(
       
    55                                 aContactManager );
       
    56     return self;
       
    57     }
       
    58 
       
    59 
       
    60 // ----------------------------------------------------------
       
    61 // CCmsFindLinkedContact::CCmsFindLinkedContact
       
    62 //
       
    63 // ----------------------------------------------------------
       
    64 //
       
    65 CCmsFindLinkedContact::CCmsFindLinkedContact( CVPbkContactManager& aContactManager )
       
    66 : CActive( CActive::EPriorityHigh ),
       
    67 iContactManager( aContactManager ),
       
    68 iState( ERetreiveXSPInfo )
       
    69 
       
    70     {
       
    71     CActiveScheduler::Add( this );
       
    72     }
       
    73 
       
    74 // ----------------------------------------------------------
       
    75 // CCmsFindLinkedContact::~CCmsFindLinkedContact
       
    76 //
       
    77 // ----------------------------------------------------------
       
    78 //
       
    79 CCmsFindLinkedContact::~CCmsFindLinkedContact()
       
    80     {
       
    81     if ( iWait.IsStarted() )
       
    82         {
       
    83         iWait.AsyncStop();
       
    84         }
       
    85     Cancel();
       
    86     delete iContactLink;
       
    87     iOperationArray.ResetAndDestroy();
       
    88     delete iRetreivedXSPContact;
       
    89     }
       
    90 
       
    91 // ----------------------------------------------------------
       
    92 // CCmsFindLinkedContact::FetchLinkedContactL
       
    93 //
       
    94 // ----------------------------------------------------------
       
    95 //
       
    96 const MVPbkContactLink& CCmsFindLinkedContact::FetchLinkedContactL( 
       
    97                             const MVPbkContactLink& aXSPContactLink )
       
    98     {
       
    99     __ASSERT_DEBUG (ERetreiveXSPInfo == iState,
       
   100                                 User::Leave( KErrGeneral ));
       
   101     
       
   102     //if the XSP contact is linked to the a contact in DefaultCntDbUri(), 
       
   103     //       then we return the contact in DefaultCntDbUri() 
       
   104     //else we return the same XSP Contact        
       
   105     if ( iContactLink )
       
   106         {
       
   107         delete iContactLink;
       
   108         iContactLink = NULL;
       
   109         }        
       
   110     iContactLink = aXSPContactLink.CloneLC();
       
   111     CleanupStack::Pop();    
       
   112     if ( !iWait.IsStarted() )
       
   113         {
       
   114         //Start the operation
       
   115         IssueRequest();
       
   116         
       
   117         //Start waiting here till the operation completes
       
   118         iWait.Start();    
       
   119         }
       
   120     
       
   121     __ASSERT_DEBUG (NULL != iContactLink,
       
   122                                     User::Leave( KErrGeneral ));
       
   123     //return the contact link
       
   124     return *iContactLink;
       
   125     }
       
   126 
       
   127 
       
   128 // --------------------------------------------------------------------------
       
   129 // CCmsFindLinkedContact::IssueRequest
       
   130 // --------------------------------------------------------------------------
       
   131 //
       
   132 void CCmsFindLinkedContact::IssueRequest()
       
   133     {
       
   134     TRequestStatus* status = &iStatus;
       
   135     User::RequestComplete( status, KErrNone );
       
   136     SetActive();
       
   137     }
       
   138 
       
   139     
       
   140 // --------------------------------------------------------------------------
       
   141 // CCmsFindLinkedContact::RunL
       
   142 // --------------------------------------------------------------------------
       
   143 //
       
   144 void CCmsFindLinkedContact::RunL()
       
   145     {
       
   146     PRINT1( _L( "CCmsFindLinkedContact::RunL(): %d" ), iState );
       
   147     switch( iState )
       
   148         {
       
   149         case ERetreiveXSPInfo:
       
   150             {  
       
   151             MVPbkContactOperationBase* operation = 
       
   152                                 iContactManager.RetrieveContactL( *iContactLink, *this );
       
   153             CleanupStack::PushL( operation );
       
   154             iOperationArray.AppendL( operation );
       
   155             CleanupStack::Pop( operation );                    
       
   156             break;
       
   157             }
       
   158         
       
   159         case EFindLinkedContact:
       
   160             {
       
   161             __ASSERT_DEBUG (NULL != iRetreivedXSPContact,
       
   162                                                 User::Leave( KErrGeneral ));
       
   163             RetreiveAndFindLinkedContactL( *iRetreivedXSPContact );            
       
   164             break;
       
   165             }       
       
   166             
       
   167         case EComplete:
       
   168             {
       
   169             if ( iWait.IsStarted() )
       
   170                 {
       
   171                 iWait.AsyncStop();
       
   172                 }            
       
   173             break;  
       
   174             }
       
   175         default:
       
   176             {
       
   177             // Do nothing
       
   178             break;
       
   179             }
       
   180         }
       
   181     }
       
   182 
       
   183 // --------------------------------------------------------------------------
       
   184 // CCmsFindLinkedContact::DoCancel
       
   185 // --------------------------------------------------------------------------
       
   186 //
       
   187 void CCmsFindLinkedContact::DoCancel()
       
   188     {
       
   189     }
       
   190 
       
   191 // --------------------------------------------------------------------------
       
   192 // CVIMPSTStorageVPbkStoreHandler::RunError
       
   193 // --------------------------------------------------------------------------
       
   194 //
       
   195 TInt CCmsFindLinkedContact::RunError( TInt /*aError*/ )
       
   196     {    
       
   197     return KErrNone;
       
   198     }
       
   199 
       
   200 
       
   201 // ----------------------------------------------------------
       
   202 // CCmsFindLinkedContact::RetreiveAndFindLinkedContactL
       
   203 //
       
   204 // ----------------------------------------------------------
       
   205 //
       
   206 void CCmsFindLinkedContact::RetreiveAndFindLinkedContactL( 
       
   207                         const MVPbkStoreContact& aXSPContact )
       
   208     {
       
   209     TInt fieldCount = aXSPContact.Fields().FieldCount();
       
   210     
       
   211     HBufC* xspId = NULL;
       
   212     //Retreive the XSP Id of the Contact
       
   213     for( TInt index = 0 ; index < fieldCount ; index++ )
       
   214         {
       
   215         const MVPbkStoreContactField& contactField = aXSPContact.Fields().FieldAt(index);
       
   216         const MVPbkFieldType* fieldType = contactField.BestMatchingFieldType();        
       
   217         
       
   218         if ( fieldType &&  
       
   219                 ( fieldType->FieldTypeResId() == R_VPBK_FIELD_TYPE_IMPP ))
       
   220             {
       
   221             //Found the XSP Id
       
   222             xspId = MVPbkContactFieldUriData::Cast(
       
   223                     contactField.FieldData()).Uri().AllocLC();
       
   224             break;
       
   225             }                       
       
   226         }
       
   227             
       
   228     if (xspId)
       
   229         {            
       
   230         //Find the Contact in DefaultCntDbUri() which is linked to the xspId
       
   231         
       
   232         CVPbkFieldTypeRefsList* fieldTypeRefList = CVPbkFieldTypeRefsList::NewL();
       
   233         TCallBack parser( CCmsFindLinkedContact::WordParserL, this );
       
   234         CleanupStack::PushL( fieldTypeRefList );
       
   235         fieldTypeRefList->AppendL( *iContactManager.FieldTypes().Find( R_VPBK_FIELD_TYPE_IMPP ) );
       
   236         
       
   237         //Append the XSP Id that needs to be searched
       
   238         CDesCArrayFlat* searchString = new( ELeave) CDesCArrayFlat( 1 );  
       
   239         CleanupStack::PushL( searchString );
       
   240         searchString->AppendL( *xspId );        
       
   241         
       
   242         //The Store to search is DefaultCntDbUri()
       
   243         CDesCArrayFlat* xSPStoresArray = new (ELeave)
       
   244                 CDesCArrayFlat( KArrayGranularity );
       
   245         CleanupStack::PushL( xSPStoresArray );
       
   246         xSPStoresArray->AppendL( VPbkContactStoreUris::DefaultCntDbUri() );              
       
   247         
       
   248         //Start the find operation of the XSP id from the stores mentioned in xSPStoresArray
       
   249         MVPbkContactOperationBase* xSPFindOperation = iContactManager.FindL(
       
   250                 *searchString, *fieldTypeRefList, *this, parser, *xSPStoresArray );
       
   251         
       
   252         CleanupStack::PopAndDestroy( 4 ); //xSPStoresArray, searchString, fieldTypeRefList, xspId
       
   253         
       
   254         CleanupStack::PushL( xSPFindOperation );
       
   255         iOperationArray.AppendL( xSPFindOperation );
       
   256         CleanupStack::Pop( xSPFindOperation );    
       
   257         
       
   258         }
       
   259     else
       
   260         {
       
   261         iState = EComplete;
       
   262         IssueRequest();
       
   263         }
       
   264     }
       
   265     
       
   266 
       
   267 // ----------------------------------------------------------
       
   268 // CCmsFindLinkedContact::WordParser
       
   269 //
       
   270 // ----------------------------------------------------------
       
   271 //
       
   272 TInt CCmsFindLinkedContact::WordParserL( TAny* aWordParserParam )
       
   273     {
       
   274     TVPbkWordParserCallbackParam* param = 
       
   275             static_cast<TVPbkWordParserCallbackParam*>( aWordParserParam );
       
   276     TPtrC data( param->iStringToParse->Ptr() );
       
   277 
       
   278     PRINT1( _L( "CCmsFindLinkedContact::WordParserL(): %S" ), &data );
       
   279 
       
   280     param->iWordArray->AppendL( data );
       
   281     return 0;
       
   282     }
       
   283 
       
   284 // ----------------------------------------------------------
       
   285 // CCmsFindLinkedContact::VPbkSingleContactOperationComplete
       
   286 //
       
   287 // ----------------------------------------------------------
       
   288 //
       
   289 void CCmsFindLinkedContact::VPbkSingleContactOperationComplete(
       
   290         MVPbkContactOperationBase& /*aOperation*/,
       
   291         MVPbkStoreContact* aContact )
       
   292     {
       
   293     PRINT( _L( "Start CCmsFindLinkedContact::VPbkSingleContactOperationComplete()" ) );    
       
   294    
       
   295     //We were successfull in finding the Contact
       
   296     //now its time to retreive the XSP ID from this contact
       
   297     //and initiate the find operation of the XSP Id
       
   298     iRetreivedXSPContact = aContact;
       
   299     iState = EFindLinkedContact;
       
   300     IssueRequest();    
       
   301     
       
   302     PRINT( _L( "End CCmsFindLinkedContact::VPbkSingleContactOperationComplete()" ) );
       
   303     }
       
   304 
       
   305 // ----------------------------------------------------------
       
   306 // CCmsFindLinkedContact::~CCmsFindLinkedContact
       
   307 //
       
   308 // ----------------------------------------------------------
       
   309 //
       
   310 void CCmsFindLinkedContact::VPbkSingleContactOperationFailed(
       
   311         MVPbkContactOperationBase& /*aOperation*/,
       
   312         TInt aError )
       
   313     {
       
   314     PRINT1( _L( "CCmsFindLinkedContact::VPbkSingleContactOperationFailed(): Error: %d" ), 
       
   315             aError );
       
   316     //We were not successfull in finding the XSP Contact
       
   317     //there is no point in proceeding so complete this operation  
       
   318     iState = EComplete;
       
   319     IssueRequest();
       
   320     }
       
   321 
       
   322 // ----------------------------------------------------------
       
   323 // CCmsFindLinkedContact::FindFromStoreSucceededL
       
   324 //
       
   325 // ----------------------------------------------------------
       
   326 //
       
   327 void CCmsFindLinkedContact::FindFromStoreSucceededL( MVPbkContactStore& /*aStore*/, 
       
   328     MVPbkContactLinkArray* aResults )
       
   329     {
       
   330     PRINT1( _L( "CCmsFindLinkedContact::FindCompleteL() - Count: %d" ), aResults->Count() );
       
   331     //callback from FindL operation success
       
   332     TInt count( aResults->Count() );
       
   333     if( count > 0 )
       
   334         {    
       
   335         //Found atleast one contact
       
   336         if ( iContactLink )
       
   337             {
       
   338             delete iContactLink;
       
   339             iContactLink = NULL;
       
   340             }
       
   341         //choose the First Found Item
       
   342         iContactLink = aResults->At( 0 ).CloneLC();
       
   343         CleanupStack::Pop();        
       
   344         }    
       
   345     delete aResults;
       
   346     aResults = NULL;
       
   347     PRINT( _L( "End CCmsFindLinkedContact::FindCompleteL()" ) );
       
   348     }
       
   349 
       
   350 // ----------------------------------------------------------
       
   351 // CCmsFindLinkedContact::FindFromStoreFailed
       
   352 //
       
   353 // ----------------------------------------------------------
       
   354 //
       
   355 void CCmsFindLinkedContact::FindFromStoreFailed( 
       
   356         MVPbkContactStore& /*aStore*/, TInt aError )
       
   357     {
       
   358     //callback from FindL operation failure
       
   359     PRINT1( _L( "CCmsFindLinkedContact::FindFailed() - Error: %d" ), aError );    
       
   360     }
       
   361 
       
   362 // ----------------------------------------------------------
       
   363 // CCmsFindLinkedContact::FindFromStoresOperationComplete
       
   364 //
       
   365 // ----------------------------------------------------------
       
   366 //
       
   367 void CCmsFindLinkedContact::FindFromStoresOperationComplete()
       
   368     {
       
   369     //Final callback from FindL operation that denotes the end
       
   370     //of the operation
       
   371     iState = EComplete;
       
   372     IssueRequest();
       
   373     PRINT( _L( "CCmsFindLinkedContact::FindFromStoresOperationComplete()" ) ); 
       
   374     }
       
   375 
       
   376 
       
   377 // End of File