omads/omadsextensions/adapters/contacts/src/NSmlContactsModsFetcher.cpp
changeset 19 2691f6aa1921
parent 4 e6e896426eac
child 20 e1de7d03f843
equal deleted inserted replaced
4:e6e896426eac 19:2691f6aa1921
     1 /*
       
     2 * Copyright (c) 2006 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:  DS contacts adapters 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32base.h>
       
    21 #include <s32strm.h>
       
    22 #include <cntdb.h>
       
    23 #include <e32cmn.h>
       
    24 #include <e32des16.h>
       
    25 #include <cntitem.h>
       
    26 #include <cntfield.h>
       
    27 #include <cntfldst.h>
       
    28 
       
    29 #include "nsmlcontactsdefs.h"
       
    30 #include "NSmlContactsModsFetcher.h"
       
    31 #include "nsmldebug.h"
       
    32 #include "nsmlsnapshotitem.h"
       
    33 #include "nsmlchangefinder.h"
       
    34 
       
    35 #include <CVPbkContactViewDefinition.h>
       
    36 #include <CVPbkContactIdConverter.h>
       
    37 #include <CVPbkContactFieldIterator.h>
       
    38 #include <MVPbkContactStoreProperties.h>
       
    39 #include <MVPbkStoreContactProperties.h>
       
    40 #include <MVPbkViewContact.h>
       
    41 #include <MVPbkFieldType.h>
       
    42 #include <MVPbkContactFieldTextData.h>
       
    43 #include <MVPbkStoreContact.h>
       
    44 #include <MVPbkStoreContact2.h>
       
    45 #include <MVPbkContactLink.h>
       
    46 #include <VPbkEng.rsg>
       
    47 
       
    48 
       
    49 // ------------------------------------------------------------------------------------------------
       
    50 // CNSmlContactsModsFetcher::CNSmlContactsModsFetcher
       
    51 // ------------------------------------------------------------------------------------------------
       
    52 //
       
    53 CNSmlContactsModsFetcher::CNSmlContactsModsFetcher( TBool& aSnapshotRegistered,CVPbkContactManager& aContactManager,MVPbkContactStore& aStore,TKeyArrayFix& aKey, CNSmlChangeFinder& aChangeFinder ) :
       
    54 	CActive( EPriorityLow ), 
       
    55 	iSnapshotRegistered( aSnapshotRegistered ), 
       
    56 	iKey( aKey ), 
       
    57 	iChangeFinder( aChangeFinder ),
       
    58 	iContactManager(aContactManager) ,
       
    59 	iStore(aStore) ,
       
    60 	iContactViewBase(NULL)
       
    61 	{
       
    62 	_DBG_FILE("CNSmlContactsModsFetcher::CNSmlContactsModsFetcher(): begin");
       
    63 	CActiveScheduler::Add( this );
       
    64 	_DBG_FILE("CNSmlContactsModsFetcher::CNSmlContactsModsFetcher(): end");
       
    65 	}
       
    66 
       
    67 // ------------------------------------------------------------------------------------------------
       
    68 // CNSmlContactsModsFetcher::~CNSmlContactsModsFetcher
       
    69 // ------------------------------------------------------------------------------------------------
       
    70 //
       
    71 CNSmlContactsModsFetcher::~CNSmlContactsModsFetcher()
       
    72 	{
       
    73 	_DBG_FILE("CNSmlContactsModsFetcher::~CNSmlContactsModsFetcher(): begin");
       
    74 	
       
    75 	Cancel();	
       
    76 	delete iSnapshot;
       
    77 	delete iContactViewBase;
       
    78 	
       
    79 	
       
    80 	_DBG_FILE("CNSmlContactsModsFetcher::~CNSmlContactsModsFetcher(): end");
       
    81 	}
       
    82 	
       
    83 // ------------------------------------------------------------------------------------------------
       
    84 // CNSmlContactsModsFetcher::FetchModificationsL
       
    85 // ------------------------------------------------------------------------------------------------
       
    86 //
       
    87 void CNSmlContactsModsFetcher::FetchModificationsL( TRequestStatus& aStatus )
       
    88 	{
       
    89 	_DBG_FILE("CNSmlContactsModsFetcher::FetchModificationsL(): begin");
       
    90 	
       
    91 	iCallerStatus = &aStatus;
       
    92 	*iCallerStatus = KRequestPending;
       
    93 	iContactCount = 0;
       
    94 	if( !iSnapshot )
       
    95 		{
       
    96 		iSnapshot = new (ELeave) CArrayFixSeg<TNSmlSnapshotItem>( KNSmlSnapshotSize );
       
    97 		}
       
    98 		
       
    99 	if(iContactViewBase)
       
   100 	{
       
   101 	delete iContactViewBase;
       
   102 	iContactViewBase = NULL;	
       
   103 	}
       
   104 	
       
   105 	CreateViewL();
       
   106 	
       
   107 	_DBG_FILE("CNSmlContactsModsFetcher::FetchModificationsL(): end");
       
   108 	
       
   109 	}
       
   110 	
       
   111 // ------------------------------------------------------------------------------------------------
       
   112 // CNSmlContactsModsFetcher::ConstructL
       
   113 // ------------------------------------------------------------------------------------------------
       
   114 //
       
   115 void CNSmlContactsModsFetcher::ConstructL()
       
   116 	{
       
   117 	iSnapshot = new (ELeave) CArrayFixSeg<TNSmlSnapshotItem>(KNSmlSnapshotSmallSize);
       
   118 	
       
   119 	}
       
   120 	
       
   121 // ------------------------------------------------------------------------------------------------
       
   122 // CNSmlContactsModsFetcher::RunL
       
   123 // ------------------------------------------------------------------------------------------------	
       
   124 //
       
   125 void CNSmlContactsModsFetcher::RunL()
       
   126 	{
       
   127 	_DBG_FILE("CNSmlContactsModsFetcher::RunL(): begin");
       
   128 	
       
   129 	if ( !iSnapshotRegistered )
       
   130 		{
       
   131 		FetchNextContactL();
       
   132 		}
       
   133 #ifdef __NSML_MODULETEST__
       
   134 	else
       
   135 		{
       
   136 		CActiveScheduler::Stop();
       
   137 		}
       
   138 #endif
       
   139 	
       
   140 	_DBG_FILE("CNSmlContactsModsFetcher::RunL(): end");
       
   141 	}
       
   142 
       
   143 // ------------------------------------------------------------------------------------------------
       
   144 // CNSmlContactsModsFetcher::FetchNextContactL
       
   145 // ------------------------------------------------------------------------------------------------	
       
   146 //
       
   147 void CNSmlContactsModsFetcher::FetchNextContactL()
       
   148 	{
       
   149 	_DBG_FILE("CNSmlContactsModsFetcher::FetchNextContactL(): begin");
       
   150 	if(iContactViewBase->ContactCountL() == 0)
       
   151 		{
       
   152 		User::RequestComplete( iCallerStatus, KErrNone );
       
   153 		return;	
       
   154 		}
       
   155 	if ( !iSnapshotRegistered )
       
   156 		{
       
   157 		const MVPbkViewContact& contact = iContactViewBase->ContactAtL(iContactCount);
       
   158 
       
   159 		MVPbkContactLink* contactLink = contact.CreateLinkLC();
       
   160 		iContactManager.RetrieveContactL( *contactLink, *this );
       
   161 		CleanupStack::PopAndDestroy();	
       
   162 		}
       
   163 
       
   164 	_DBG_FILE("CNSmlContactsModsFetcher::FetchNextContactL(): end");
       
   165 	}
       
   166 
       
   167 // ------------------------------------------------------------------------------------------------
       
   168 // CNSmlContactsModsFetcher::DoCancel
       
   169 // ------------------------------------------------------------------------------------------------	
       
   170 //
       
   171 void CNSmlContactsModsFetcher::DoCancel()
       
   172 	{
       
   173 	_DBG_FILE("CNSmlContactsModsFetcher::DoCancel(): begin");
       
   174 
       
   175 	User::RequestComplete( iCallerStatus, KErrCancel );
       
   176 	
       
   177 	_DBG_FILE("CNSmlContactsModsFetcher::DoCancel(): end");
       
   178 	}
       
   179 
       
   180 // ----------------------------------------------------------------------------
       
   181 // CNSmlContactsModsFetcher::IsConfidentialL
       
   182 // ----------------------------------------------------------------------------
       
   183 //	
       
   184 TBool CNSmlContactsModsFetcher::IsConfidentialL( MVPbkStoreContact& aItem )
       
   185     {
       
   186     _DBG_FILE("CNSmlContactsModsFetcher::IsConfidentialL(): begin");
       
   187     TBool ret( EFalse );
       
   188     
       
   189     //find X-CLASS field type
       
   190 	const MVPbkFieldType* syncclass = 
       
   191         iContactManager.FieldTypes().Find(R_VPBK_FIELD_TYPE_SYNCCLASS );
       
   192 	CVPbkBaseContactFieldTypeIterator* itr = 
       
   193         CVPbkBaseContactFieldTypeIterator::NewLC( *syncclass, 
       
   194             aItem.Fields() );
       
   195 	while ( itr->HasNext() )
       
   196         {
       
   197         const MVPbkBaseContactField* field = itr->Next();
       
   198         const MVPbkContactFieldTextData& data = 
       
   199         	MVPbkContactFieldTextData::Cast(field->FieldData());
       
   200         TPtrC ptr = data.Text();
       
   201         //compare the sync type
       
   202         if ( ptr.Compare( KNSmlContactSyncNoSync ) == 0 )
       
   203             {
       
   204             _DBG_FILE("CNSmlContactsModsFetcher::IsConfidentialL(): \
       
   205                        find confidential");
       
   206             ret = ETrue;
       
   207             }
       
   208          
       
   209         }
       
   210 	CleanupStack::PopAndDestroy( itr );
       
   211     
       
   212     _DBG_FILE("CNSmlContactsModsFetcher::IsConfidentialL(): end");
       
   213     return ret;
       
   214     }
       
   215 
       
   216 
       
   217 // ----------------------------------------------------------------------------
       
   218 // CNSmlContactsModsFetcher::RunError
       
   219 // ----------------------------------------------------------------------------
       
   220  TInt CNSmlContactsModsFetcher::RunError( TInt aError )
       
   221 	{
       
   222 	DBG_FILE_CODE(aError, _S8("CNSmlContactsModsFetcher::RunError() : error code received "));
       
   223 	User::RequestComplete( iCallerStatus, aError );
       
   224 	return KErrNone;
       
   225 	}
       
   226 	
       
   227 // -----------------------------------------------------------------------------
       
   228 // CNSmlContactsModsFetcher::CreateViewL()
       
   229 // Create a contact view of the store which is opened
       
   230 // -----------------------------------------------------------------------------
       
   231  
       
   232 void CNSmlContactsModsFetcher::CreateViewL()
       
   233 	{
       
   234 	CVPbkContactViewDefinition* viewDef = CVPbkContactViewDefinition::NewL();
       
   235 	CleanupStack::PushL( viewDef );
       
   236 	viewDef->SetType( EVPbkContactsView );
       
   237 
       
   238 	viewDef->SetUriL(iStore.StoreProperties().Uri().UriDes());
       
   239 
       
   240 	 iContactViewBase = iContactManager.CreateContactViewLC( 
       
   241 				                         *this, 
       
   242 				                         *viewDef, 
       
   243 				                         iContactManager.FieldTypes()  );
       
   244 	CleanupStack::Pop();
       
   245 	CleanupStack::PopAndDestroy(viewDef);
       
   246 
       
   247 	}
       
   248 // ---------------------------------------------------------------------------
       
   249 //  CNSmlContactsModsFetcher::UpdateSnapshotL
       
   250 //  Add the fetched contacts id and timestamp 
       
   251 //  to the snapshot.
       
   252 // ---------------------------------------------------------------------------
       
   253 //
       
   254  
       
   255  void CNSmlContactsModsFetcher::UpdateSnapshotL(MVPbkStoreContact* aContact)
       
   256 {
       
   257 	if(!IsConfidentialL(*aContact))
       
   258 		{
       
   259 		CVPbkContactIdConverter* idConverter = CVPbkContactIdConverter::NewL(iStore );
       
   260 		CleanupStack::PushL( idConverter );
       
   261 
       
   262 		TInt32 id = idConverter->LinkToIdentifier(
       
   263 			*iContactViewBase->ContactAtL(iContactCount).CreateLinkLC());
       
   264 		TNSmlSnapshotItem snap( id);
       
   265 		
       
   266 		MVPbkStoreContact2* tempContact = 
       
   267 		reinterpret_cast<MVPbkStoreContact2*> (aContact->StoreContactExtension (KMVPbkStoreContactExtension2Uid));  
       
   268               
       
   269 		MVPbkStoreContactProperties *contactProterties 
       
   270 									= tempContact ->PropertiesL();
       
   271 		CleanupDeletePushL(contactProterties);				
       
   272 		snap.SetLastChangedDate(contactProterties->LastModifiedL());
       
   273 		CleanupStack::PopAndDestroy();
       
   274 				
       
   275 		iSnapshot->InsertIsqL( snap, iKey );
       
   276 		iCurrentSnapshotCounter++;
       
   277 		if ( iCurrentSnapshotCounter > 20 )
       
   278 			{
       
   279 			iSnapshot->Compress();
       
   280 			iCurrentSnapshotCounter = 0;
       
   281 			}
       
   282 		CleanupStack::PopAndDestroy();
       
   283 		CleanupStack::PopAndDestroy(idConverter);	
       
   284 		}
       
   285 	
       
   286 	iContactCount++;
       
   287 	
       
   288 	if ( iContactCount == iContactViewBase->ContactCountL() )
       
   289 		{
       
   290 		iChangeFinder.SetNewSnapshot( iSnapshot ); // changefinder takes ownership
       
   291 		iSnapshot = NULL;
       
   292 		iSnapshotRegistered = ETrue;
       
   293 		User::RequestComplete( iCallerStatus, KErrNone );
       
   294 		}
       
   295 
       
   296 }	
       
   297 // ---------------------------------------------------------------------------
       
   298 // CNSmlContactsModsFetcher::ContactViewReady
       
   299 // Implements the view ready function of MVPbkContactViewObserver
       
   300 // --------------------------------------------------------------------------- 
       
   301 void CNSmlContactsModsFetcher::ContactViewReady(
       
   302                 MVPbkContactViewBase& /*aView*/ ) 
       
   303 	{
       
   304     _DBG_FILE("CNSmlContactsModsFetcher::ContactViewReady(): begin");
       
   305 	
       
   306 	SetActive();
       
   307 	TRequestStatus* status = &iStatus;
       
   308 	User::RequestComplete( status, KErrNone );	
       
   309 	
       
   310 	_DBG_FILE("CNSmlContactsModsFetcher::ContactViewReady(): end");
       
   311 	}
       
   312 
       
   313 // ---------------------------------------------------------------------------
       
   314 // CNSmlContactsModsFetcher::ContactViewUnavailable
       
   315 // Implements the view unavailable function of MVPbkContactViewObserver
       
   316 // --------------------------------------------------------------------------- 
       
   317 void CNSmlContactsModsFetcher::ContactViewUnavailable(
       
   318                 MVPbkContactViewBase& /*aView*/ )  
       
   319 {
       
   320    
       
   321 
       
   322 }
       
   323 
       
   324 // ---------------------------------------------------------------------------
       
   325 // CNSmlContactsModsFetcher::ContactAddedToView
       
   326 // Implements the add contact function of MVPbkContactViewObserver
       
   327 // --------------------------------------------------------------------------- 
       
   328 void CNSmlContactsModsFetcher::ContactAddedToView(
       
   329             MVPbkContactViewBase& /*aView*/, 
       
   330             TInt /*aIndex*/, 
       
   331             const MVPbkContactLink& /*aContactLink*/ ) 
       
   332 {
       
   333 }
       
   334 
       
   335 // ---------------------------------------------------------------------------
       
   336 // Implements the remove contact function of MVPbkContactViewObserver
       
   337 // --------------------------------------------------------------------------- 
       
   338 void CNSmlContactsModsFetcher::ContactRemovedFromView(
       
   339                 MVPbkContactViewBase& /*aView*/, 
       
   340                 TInt /*aIndex*/, 
       
   341                 const MVPbkContactLink& /*aContactLink*/ )  
       
   342 {
       
   343 }
       
   344 
       
   345 // ---------------------------------------------------------------------------
       
   346 // CNSmlContactsModsFetcher::ContactViewError
       
   347 // Implements the view error function of MVPbkContactViewObserver
       
   348 // --------------------------------------------------------------------------- 
       
   349 void CNSmlContactsModsFetcher::ContactViewError(
       
   350             MVPbkContactViewBase& /*aView*/, 
       
   351             TInt aError, 
       
   352             TBool /*aErrorNotified*/ )  
       
   353 {
       
   354 User::RequestComplete( iCallerStatus, aError );
       
   355 }
       
   356 
       
   357 // ---------------------------------------------------------------------------
       
   358 //  CNSmlContactsModsFetcher::VPbkSingleContactOperationComplete
       
   359 //  Called when the contact operation is completed.
       
   360 // ---------------------------------------------------------------------------
       
   361 //
       
   362  void CNSmlContactsModsFetcher::VPbkSingleContactOperationComplete(
       
   363 		MVPbkContactOperationBase& aOperation,
       
   364 		MVPbkStoreContact* aContact )
       
   365 {
       
   366 	MVPbkContactOperationBase* operation = &aOperation;
       
   367 	if ( operation )
       
   368 	{
       
   369 		delete operation;
       
   370 		operation = NULL;
       
   371 	}
       
   372 	
       
   373 	TRAPD(error,UpdateSnapshotL(aContact));
       
   374 	delete aContact;
       
   375 	if(error!= KErrNone)
       
   376 	{
       
   377 	User::RequestComplete( iCallerStatus, error );	
       
   378 	}
       
   379 	else
       
   380 	{
       
   381 	SetActive();
       
   382 	TRequestStatus* status = &iStatus;
       
   383 	User::RequestComplete( status, KErrNone );		
       
   384 	}
       
   385 	
       
   386 }
       
   387 
       
   388 // ---------------------------------------------------------------------------
       
   389 //  CNSmlContactsModsFetcher::VPbkSingleContactOperationFailed
       
   390 //  Called when the contact operation fails
       
   391 // ---------------------------------------------------------------------------
       
   392 //
       
   393 void CNSmlContactsModsFetcher::VPbkSingleContactOperationFailed(
       
   394 		MVPbkContactOperationBase& aOperation,
       
   395 		 TInt /*aError*/ )
       
   396  {
       
   397  	MVPbkContactOperationBase* operation = &aOperation;
       
   398 	if ( operation )
       
   399 	{
       
   400 		delete operation;
       
   401 		operation = NULL;
       
   402 	}
       
   403  }
       
   404 // End of File