phonebookengines/VirtualPhonebook/VPbkEng/src/CVPbkxSPContactsOperation.cpp
changeset 0 e686773b3f54
child 68 9da50d567e3c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2005-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:  xSP Contact operation
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "CVPbkxSPContactsOperation.h"
       
    21 
       
    22 
       
    23 #include <barsc.h>
       
    24 #include <barsread.h>
       
    25 
       
    26 
       
    27 #include <TVPbkWordParserCallbackParam.h>
       
    28 #include <VPbkEng.rsg>
       
    29 #include <CVPbkContactFieldIterator.h>
       
    30 #include <CVPbkContactManager.h>
       
    31 #include <CVPbkFieldTypeRefsList.h>
       
    32 #include <MVPbkStoreContact.h>
       
    33 #include <MVPbkContactFieldUriData.h>
       
    34 #include <MVPbkContactStore.h>
       
    35 #include <MVPbkContactLink.h>
       
    36 #include <MVPbkContactStoreProperties.h>
       
    37 #include <VPbkContactStoreUris.h>
       
    38 #include "VPbkDebug.h"
       
    39 
       
    40 #include <CVPbkEComImplementationsList.h>
       
    41 #include <spsettings.h>
       
    42 #include <spproperty.h>
       
    43 namespace
       
    44     {
       
    45 
       
    46 #ifdef _DEBUG	
       
    47     enum TxSPErrors
       
    48         {
       
    49         ExSPErrorWrongLogic1,
       
    50         ExSPErrorWrongState,
       
    51         EInvalidOperation,
       
    52         ExSPErrorWrongStateRun
       
    53         };
       
    54 
       
    55     _LIT( KPanicStrxSPManagement, "VPbk_xSPManag");
       
    56 
       
    57     void Panic(TxSPErrors aReason)
       
    58         {
       
    59         User::Panic(KPanicStrxSPManagement, aReason);
       
    60         }
       
    61 #endif	
       
    62     }
       
    63 
       
    64 ///////////////////////////////////////////////////////////////////////////////
       
    65 
       
    66 CVPbkxSPContactsOperation* CVPbkxSPContactsOperation::NewLC(
       
    67         const MVPbkStoreContact& aContact,
       
    68         CVPbkContactManager& aContactManager,
       
    69         MVPbkOperationErrorObserver& aErrorObserver, TxSPOperation aOperation)
       
    70     {
       
    71     CVPbkxSPContactsOperation* self = new (ELeave) CVPbkxSPContactsOperation(
       
    72             aContact,
       
    73             aContactManager,
       
    74             aErrorObserver,
       
    75             aOperation );
       
    76     CleanupStack::PushL(self);
       
    77     self->ConstructL();
       
    78     return self;
       
    79     }
       
    80 
       
    81 void CVPbkxSPContactsOperation::ConstructL()
       
    82     {
       
    83     iContactLink = iContact.CreateLinkLC();
       
    84     CleanupStack::Pop();  //iContactLink
       
    85     iContactLinks = CVPbkContactLinkArray::NewL();
       
    86     CompleteOurself(); //start execution right away    
       
    87     }
       
    88 
       
    89 CVPbkxSPContactsOperation::CVPbkxSPContactsOperation(
       
    90     const MVPbkStoreContact& aContact,
       
    91     CVPbkContactManager& aContactManager,
       
    92     MVPbkOperationErrorObserver& aErrorObserver, 
       
    93     TxSPOperation /*aOperation*/) :
       
    94     CActive(EPriorityStandard), 
       
    95         iNextState(EStateNone), iContact(aContact),
       
    96         iContactManager(aContactManager), iErrorObserver(&aErrorObserver)
       
    97     {
       
    98     CActiveScheduler::Add(this);
       
    99     }
       
   100 
       
   101 CVPbkxSPContactsOperation::~CVPbkxSPContactsOperation()
       
   102     {
       
   103     Cancel();
       
   104     delete iContactOperation;
       
   105 
       
   106     if(iContactLinks) 
       
   107         {
       
   108         iContactLinks->ResetAndDestroy();
       
   109         }
       
   110     
       
   111     delete iContactLinks;
       
   112     delete iContactLink;
       
   113     delete iFieldTypeRefList;
       
   114     delete iXspIdArray;
       
   115     }
       
   116 
       
   117 MVPbkContactOperationBase* CVPbkxSPContactsOperation::NewGetxSPLinksOperationL(
       
   118         const MVPbkStoreContact& aContact,
       
   119         CVPbkContactManager& aContactManager,
       
   120         MVPbkOperationResultObserver<MVPbkContactLinkArray*>& aObserver,
       
   121         MVPbkOperationErrorObserver& aErrorObserver, TxSPOperation aOperation)
       
   122     {
       
   123     __ASSERT_DEBUG( aOperation == EGetxSPContactLinks,
       
   124             Panic(EInvalidOperation) );
       
   125 
       
   126     CVPbkxSPContactsOperation* self = NewLC(aContact, aContactManager,
       
   127             aErrorObserver, aOperation);
       
   128     self->iLinksObserver = &aObserver;
       
   129     self->iNextState = EStateFind;
       
   130 
       
   131     CleanupStack::Pop(self);
       
   132     return self;
       
   133     }
       
   134 
       
   135 void CVPbkxSPContactsOperation::DoCancel()
       
   136     {
       
   137     delete iContactOperation;
       
   138     iContactOperation = NULL;
       
   139     }
       
   140 
       
   141 void CVPbkxSPContactsOperation::RunL()
       
   142     {
       
   143     switch(iNextState)
       
   144         {
       
   145         case EStateFind:
       
   146             {
       
   147             GetIMPPDataL();
       
   148             }
       
   149             break;
       
   150         
       
   151         case EStateAbortWithError:
       
   152             {
       
   153             NotifyError(iAbortError);
       
   154             }
       
   155             break;
       
   156             
       
   157         default:
       
   158             {
       
   159             __ASSERT_DEBUG( EFalse,Panic( ExSPErrorWrongStateRun ) );
       
   160             }
       
   161         }
       
   162     }
       
   163 
       
   164 TInt CVPbkxSPContactsOperation::RunError(TInt aError)
       
   165     {
       
   166     NotifyError(aError);
       
   167     return KErrNone;
       
   168     }
       
   169 
       
   170 //---------------------------------------------------------------
       
   171 // These are from MVPbkContactStoreObserver
       
   172 
       
   173 void CVPbkxSPContactsOperation::FindFromStoreSucceededL(
       
   174         MVPbkContactStore& /*aStore*/,
       
   175         MVPbkContactLinkArray* aResultsFromStore)
       
   176     {
       
   177     ProcessContactLinksL(aResultsFromStore);
       
   178     delete aResultsFromStore;
       
   179     }
       
   180 
       
   181 void CVPbkxSPContactsOperation::FindFromStoreFailed(
       
   182         MVPbkContactStore& /*aStore*/, TInt /*aError*/)
       
   183     {
       
   184     //do nothing
       
   185     }
       
   186 
       
   187 void CVPbkxSPContactsOperation::FindFromStoresOperationComplete()
       
   188     {
       
   189     TRAP_IGNORE(SubsearchCompleteL());
       
   190     }
       
   191 
       
   192 // ----------------------------------------------------------
       
   193 // CVPbkxSPContactsOperation::GetIMPPDataL
       
   194 // 
       
   195 // ----------------------------------------------------------
       
   196 //
       
   197 void CVPbkxSPContactsOperation::GetIMPPDataL()
       
   198     {
       
   199     //Check is contact valid.
       
   200     __ASSERT_ALWAYS ( NULL != &iContact, User::Leave(KErrArgument));
       
   201     
       
   202     TBool imppFound = EFalse;
       
   203     iXspIdArray = new ( ELeave ) CDesCArrayFlat( 5 );    
       
   204     const MVPbkFieldType* type = 
       
   205         iContactManager.FieldTypes().Find( R_VPBK_FIELD_TYPE_IMPP);
       
   206     CVPbkBaseContactFieldTypeIterator* itr =
       
   207             CVPbkBaseContactFieldTypeIterator::NewLC( *type,
       
   208                     iContact.Fields() );
       
   209     if (itr->HasNext() )
       
   210         {
       
   211         imppFound = ETrue;
       
   212         while (itr->HasNext() )
       
   213             {
       
   214             const MVPbkBaseContactField* field = itr->Next();
       
   215             const MVPbkContactFieldUriData& uri =
       
   216                     MVPbkContactFieldUriData::Cast(field->FieldData() );
       
   217             HBufC* data = uri.Uri().AllocL();
       
   218             CleanupStack::PushL(data);
       
   219             iXspIdArray->AppendL(data->Des() ); 
       
   220             CleanupStack::PopAndDestroy(data); 
       
   221             }
       
   222 
       
   223         iFieldTypeRefList = CVPbkFieldTypeRefsList::NewL();
       
   224         iFieldTypeRefList->AppendL( 
       
   225             *iContactManager.FieldTypes().Find( R_VPBK_FIELD_TYPE_IMPP) );
       
   226         FindUsingIdL();
       
   227         }
       
   228 
       
   229     CleanupStack::PopAndDestroy(itr);     
       
   230 
       
   231     //If there isn't any impp fields, notify empty contact link array. 
       
   232     if (!imppFound)
       
   233         {
       
   234         NotifyResult();
       
   235         }
       
   236     }
       
   237 
       
   238 // ---------------------------------------------------------------------------
       
   239 // CVPbkxSPContactsOperation::FindUsingIdL
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 TBool CVPbkxSPContactsOperation::FindUsingIdL()
       
   243     {
       
   244     TBool started(EFalse);
       
   245 
       
   246     if( iXspIdArray->MdcaCount() > iNextXspId )
       
   247         {
       
   248         if ( iContactOperation != NULL )
       
   249             {
       
   250             delete iContactOperation;
       
   251             iContactOperation = NULL;
       
   252             }
       
   253    
       
   254         started = ETrue;
       
   255         CDesCArrayFlat* idArray = new ( ELeave ) CDesCArrayFlat( 1 );
       
   256         CleanupStack::PushL(idArray);
       
   257         idArray->AppendL( iXspIdArray->MdcaPoint(iNextXspId) );
       
   258         iNextXspId++;
       
   259         TCallBack parser(CVPbkxSPContactsOperation::WordParserL, this);        
       
   260         CDesC16ArrayFlat* aXUriArray = new ( ELeave ) CDesCArrayFlat( 52 );
       
   261 		CleanupStack::PushL(aXUriArray);
       
   262 		RIdArray ids;
       
   263 		CleanupClosePushL( ids );
       
   264 		CSPSettings* settings = CSPSettings::NewL();
       
   265 		CleanupStack::PushL( settings ); 
       
   266 		TInt error = settings->FindServiceIdsL( ids );  
       
   267 		const TInt count = ids.Count();
       
   268        	TVPbkContactStoreUriPtr defaultUri = VPbkContactStoreUris::DefaultCntDbUri();
       
   269 
       
   270 		for( TInt i = 0; i < count; i++ )
       
   271 		    {
       
   272 		    CSPProperty* property = CSPProperty::NewLC();
       
   273 		    error = settings->FindPropertyL( ids[i], EPropertyContactStoreId,
       
   274 		        *property );
       
   275 		    if( KErrNone == error )
       
   276 		        {
       
   277 		        RBuf uri;
       
   278 		        uri.Create( KSPMaxDesLength );
       
   279 		        CleanupClosePushL( uri );  
       
   280 		        property->GetValue( uri );
       
   281 		        
       
   282 		        // Append the uri which is not equal to default store uri
       
   283 		       	if ( defaultUri.UriDes().CompareF(uri) != KErrNone )
       
   284 		       		{
       
   285 		       		aXUriArray->AppendL( uri );
       
   286 		       		}
       
   287 		        CleanupStack::PopAndDestroy();  //uri
       
   288 		        }
       
   289 		    CleanupStack::PopAndDestroy();  //property
       
   290 		    }
       
   291 		CleanupStack::PopAndDestroy( 2 ); // settings, ids
       
   292         
       
   293         iContactOperation = iContactManager.FindL( *idArray, *iFieldTypeRefList,
       
   294                 *this, parser,*aXUriArray);
       
   295                 
       
   296         CleanupStack::PopAndDestroy( 2 );
       
   297         }
       
   298     
       
   299     return started;
       
   300     }
       
   301 
       
   302 // ----------------------------------------------------------
       
   303 // CVPbkxSPContactsOperation::WordParserL
       
   304 // 
       
   305 // ----------------------------------------------------------
       
   306 //
       
   307 TInt CVPbkxSPContactsOperation::WordParserL(TAny* aWordParserParam)
       
   308     {
       
   309     //Get all data, no need to parse anything.. 
       
   310     TVPbkWordParserCallbackParam* param =
       
   311             static_cast<TVPbkWordParserCallbackParam*>(aWordParserParam );
       
   312     TPtrC data(param->iStringToParse->Ptr() );
       
   313     param->iWordArray->AppendL(data);
       
   314     return 0;
       
   315     }
       
   316 
       
   317 void CVPbkxSPContactsOperation::ProcessContactLinksL(
       
   318         MVPbkContactLinkArray* aResultsFromStore)
       
   319     {
       
   320     TInt count = aResultsFromStore->Count();
       
   321 
       
   322     for (TInt a = 0; a < count; a++)
       
   323         {
       
   324         const MVPbkContactLink& link = aResultsFromStore->At(a);
       
   325 
       
   326         //If not from original storage, add to array.
       
   327         if (!link.RefersTo(iContact))
       
   328             {
       
   329             if(iContactLinks->Find(link) == KErrNotFound)
       
   330                 {
       
   331                 iContactLinks->AppendL(link.CloneLC());
       
   332                 CleanupStack::Pop(); //Clone
       
   333                 }
       
   334             }
       
   335         }
       
   336     }
       
   337 
       
   338 // --------------------------------------------------------------------------
       
   339 // Completes this active object.
       
   340 // --------------------------------------------------------------------------
       
   341 void CVPbkxSPContactsOperation::CompleteOurself()
       
   342     {
       
   343     if ( !IsActive() )
       
   344         {
       
   345         TRequestStatus* status = &iStatus;
       
   346         User::RequestComplete(status, KErrNone);
       
   347         SetActive();
       
   348         }
       
   349     else
       
   350         {
       
   351         __ASSERT_DEBUG( EFalse, Panic(ExSPErrorWrongLogic1) );
       
   352         }
       
   353     }
       
   354 
       
   355 // --------------------------------------------------------------------------
       
   356 // Search completed 
       
   357 // --------------------------------------------------------------------------
       
   358 void CVPbkxSPContactsOperation::SubsearchCompleteL()
       
   359     {
       
   360     if(!FindUsingIdL())
       
   361         {
       
   362         NotifyResult();
       
   363         }
       
   364     }
       
   365 
       
   366 // --------------------------------------------------------------------------
       
   367 // Notifies operation observer of results
       
   368 // --------------------------------------------------------------------------
       
   369 void CVPbkxSPContactsOperation::NotifyResult()
       
   370     {
       
   371     iLinksObserver->VPbkOperationResultCompleted( this, iContactLinks);
       
   372     }
       
   373 
       
   374 // --------------------------------------------------------------------------
       
   375 // Sends error notification to the error observer
       
   376 // --------------------------------------------------------------------------
       
   377 inline void CVPbkxSPContactsOperation::NotifyError(TInt aErr)
       
   378     {
       
   379     iErrorObserver->VPbkOperationFailed( this, aErr);
       
   380     }
       
   381 
       
   382 // --------------------------------------------------------------------------
       
   383 // Notify error async
       
   384 // --------------------------------------------------------------------------
       
   385 void CVPbkxSPContactsOperation::AbortWithError(TInt aErr)
       
   386     {
       
   387     iAbortError = aErr;
       
   388     iNextState = EStateAbortWithError;
       
   389     CompleteOurself();
       
   390     }
       
   391 
       
   392 // end of file