omads/omadsextensions/adapters/contacts/src/NSmlContactsDataStore.cpp
branchRCL_3
changeset 52 4f0867e42d62
parent 51 8e7494275d3a
child 56 3e6957da2ff8
equal deleted inserted replaced
51:8e7494275d3a 52:4f0867e42d62
    32 #include <data_caging_path_literals.hrh>
    32 #include <data_caging_path_literals.hrh>
    33 #include <SmlDataFormat.h>
    33 #include <SmlDataFormat.h>
    34 #include <SmlDataProvider.h>
    34 #include <SmlDataProvider.h>
    35 #include <e32property.h>
    35 #include <e32property.h>
    36 #include <DataSyncInternalPSKeys.h>
    36 #include <DataSyncInternalPSKeys.h>
    37 #include <nsmlcontactsdatastoreextension.h>
    37 #include <e32hashtab.h>
    38 #include <NSmlDataModBase.h>
    38 
    39 #include <nsmlcontactsdatastore.h>
    39 #include <CVPbkContactStoreUriArray.h>
       
    40 #include <VPbkContactStoreUris.h>
       
    41 #include <TVPbkContactStoreUriPtr.h>
       
    42 #include <MVPbkContactStoreList.h>
       
    43 #include <MVPbkContactStoreProperties.h>
       
    44 #include <MVPbkStoreContactProperties.h>
       
    45 #include <MVPbkViewContact.h>
       
    46 #include <MVPbkStoreContact.h>
       
    47 #include <MVPbkStoreContact2.h>
       
    48 #include <MVPbkFieldType.h>
       
    49 #include <MVPbkContactFieldTextData.h>
       
    50 #include <CVPbkContactFieldIterator.h>
       
    51 #include <CVPbkContactViewDefinition.h>
       
    52 
       
    53 #include <MVPbkContactLink.h>
       
    54 #include <s32mem.h>
       
    55 
       
    56 #include <NSmlContactsDataStore.h>
       
    57 #include "NSmlContactsModsFetcher.h"
    40 #include "nsmldebug.h"
    58 #include "nsmldebug.h"
    41 #include "nsmlconstants.h"
    59 #include "nsmlconstants.h"
       
    60 #include <NSmlDataModBase.h>
       
    61 #include "nsmlcontactsdefs.h"
    42 #include "nsmldsimpluids.h"
    62 #include "nsmldsimpluids.h"
    43 #include "nsmlsnapshotitem.h"
    63 #include "nsmlsnapshotitem.h"
    44 #include "nsmlchangefinder.h"
    64 #include "nsmlchangefinder.h"
    45 
    65 #include <VPbkEng.rsg>
    46 
    66 
    47 #ifndef __WINS__
    67 #ifndef __WINS__
    48 // This lowers the unnecessary compiler warning (armv5) to remark.
    68 // This lowers the unnecessary compiler warning (armv5) to remark.
    49 // "Warning:  #174-D: expression has no effect..." is caused by 
    69 // "Warning:  #174-D: expression has no effect..." is caused by 
    50 // DBG_ARGS8 macro in no-debug builds.
    70 // DBG_ARGS8 macro in no-debug builds.
    70 	_DBG_FILE("CNSmlContactsDataStore::NewL: BEGIN");
    90 	_DBG_FILE("CNSmlContactsDataStore::NewL: BEGIN");
    71 	CNSmlContactsDataStore* self = new ( ELeave ) CNSmlContactsDataStore();
    91 	CNSmlContactsDataStore* self = new ( ELeave ) CNSmlContactsDataStore();
    72 	CleanupStack::PushL(self);
    92 	CleanupStack::PushL(self);
    73 		
    93 		
    74 	self->ConstructL();
    94 	self->ConstructL();
    75 	CleanupStack::Pop(); // self
    95 	CleanupStack::Pop(self); // self
    76 		
    96 		
    77 	_DBG_FILE("CNSmlContactsDataStore::NewL: END");
    97 	_DBG_FILE("CNSmlContactsDataStore::NewL: END");
    78 	return self;
    98 	return self;
    79 	}
    99 	}
    80 
   100 
    81 // ----------------------------------------------------------------------------
   101 // ----------------------------------------------------------------------------
    82 // CNSmlContactsDataStore::CNSmlContactsDataStore() 
   102 // CNSmlContactsDataStore::CNSmlContactsDataStore() 
    83 // ----------------------------------------------------------------------------
   103 // ----------------------------------------------------------------------------
    84 EXPORT_C CNSmlContactsDataStore::CNSmlContactsDataStore() : iKey( TKeyArrayFix( _FOFF( TNSmlSnapshotItem,ItemId() ),ECmpTInt ))
   104 EXPORT_C CNSmlContactsDataStore::CNSmlContactsDataStore() : 
       
   105 	iKey( TKeyArrayFix( _FOFF( TNSmlSnapshotItem,ItemId() ),ECmpTInt )),
       
   106 	iContactManager(NULL),
       
   107 	iStore(NULL),
       
   108 	iIdConverter(NULL),
       
   109 	iContactViewBase(NULL),
       
   110 	iSize(NULL),	
       
   111 	iBuf(NULL),
       
   112 	iVCardEngine(NULL),
       
   113 	iContactLnks(NULL)
    85 	{
   114 	{
    86 	_DBG_FILE("CNSmlContactsDataStore::CNSmlContactsDataStore(): begin");
   115 	_DBG_FILE("CNSmlContactsDataStore::CNSmlContactsDataStore(): begin");
       
   116 
    87 	iSyncHistory = EFalse;
   117 	iSyncHistory = EFalse;
    88 	iSnapshotRegistered = EFalse;
   118 	iSnapshotRegistered = EFalse;
    89 
   119 
    90 	iServerMaxObjectSize = 0; // Maximum size default value
   120 	iServerMaxObjectSize = 0; // Maximum size default value 
    91 	iItemPos = -1;
   121 	iItemPos = -1;
    92 	iModificationCount = KNSmlCompactAfterChanges;
   122 	iModificationCount = KNSmlCompactAfterChanges;
    93 	
   123 	
    94 	iState = ENSmlClosed;
   124 	iState = ENSmlClosed;
    95 	iStoreName = NULL;
   125 	iStoreName = NULL;
    96 	
   126 	iRetCommand = KErrNone;
    97 	iDrive = -1;
   127 	iLastOperation = ENSmlNone;
    98 	
   128 	if(iDeleteAllOperation)
       
   129 	    {
       
   130 	    delete iDeleteAllOperation;
       
   131 	    iDeleteAllOperation = NULL;
       
   132 	    }
    99 	_DBG_FILE("CNSmlContactsDataStore::CNSmlContactsDataStore(): end");
   133 	_DBG_FILE("CNSmlContactsDataStore::CNSmlContactsDataStore(): end");
   100 	}
   134 	}
   101 	
   135 	
   102 // ----------------------------------------------------------------------------
   136 // ----------------------------------------------------------------------------
   103 // CNSmlContactsDataStore::ConstructL()
   137 // CNSmlContactsDataStore::ConstructL()
   104 // ----------------------------------------------------------------------------
   138 // ----------------------------------------------------------------------------
   105 EXPORT_C void CNSmlContactsDataStore::ConstructL()
   139 EXPORT_C void CNSmlContactsDataStore::ConstructL()
   106 	{
   140 	{
   107 	_DBG_FILE("CNSmlContactsDataStore::CNSmlContactsDataStore(): begin");
   141 	_DBG_FILE("CNSmlContactsDataStore::ConstructL(): begin");
   108 	ConstructL( KNSmlContactStoreNameForDefaultDB );		
   142 	
       
   143 	ConstructL( ( VPbkContactStoreUris::DefaultCntDbUri() ), KLegacySymbianDatabase );
       
   144 		
   109 	_DBG_FILE("CNSmlContactsDataStore::ConstructL(): end");
   145 	_DBG_FILE("CNSmlContactsDataStore::ConstructL(): end");
   110 	}
   146 	}
   111 
   147 
   112 // ----------------------------------------------------------------------------
   148 // ----------------------------------------------------------------------------
   113 // CNSmlContactsDataStore::ConstructL()
   149 // CNSmlContactsDataStore::ConstructL()
   114 // ----------------------------------------------------------------------------
   150 // ----------------------------------------------------------------------------
   115 EXPORT_C void CNSmlContactsDataStore::ConstructL( const TDesC& aStoreName )
   151 EXPORT_C void CNSmlContactsDataStore::ConstructL(
       
   152     const TDesC& aStoreName, const TDesC& aLegacyStore )
   116     {
   153     {
   117     _DBG_FILE("CNSmlContactsDataStore::CNSmlContactsDataStore(): begin");
   154     _DBG_FILE("CNSmlContactsDataStore::ConstructL(): begin");
   118     
       
   119     //TODO: NOT RESPECTING THE aStoreName VALUE FOR 10.1
       
   120     
   155     
   121     User::LeaveIfError( iRfs.Connect() );
   156     User::LeaveIfError( iRfs.Connect() );
   122     
   157 
   123     iStringPool.OpenL();
   158     iStringPool.OpenL();
   124     
   159     
   125     // Uid Sets
   160     // Uid Sets
   126     iNewUids = new (ELeave) CNSmlDataItemUidSet();
   161     iNewUids = new (ELeave) CNSmlDataItemUidSet();
   127     iDeletedUids = new (ELeave) CNSmlDataItemUidSet();
   162     iDeletedUids = new (ELeave) CNSmlDataItemUidSet();
   130     iReplacedUids = new (ELeave) CNSmlDataItemUidSet();
   165     iReplacedUids = new (ELeave) CNSmlDataItemUidSet();
   131 
   166 
   132     // Create iDataMod
   167     // Create iDataMod
   133     // This should be done after OpenL if there are more than one database
   168     // This should be done after OpenL if there are more than one database
   134     iDataMod = new (ELeave) CNSmlVCardMod();
   169     iDataMod = new (ELeave) CNSmlVCardMod();
   135     iContactsDataStoreExtension = NULL;
       
   136 
   170 
   137     SetOwnStoreFormatL();
   171     SetOwnStoreFormatL();
       
   172     // open  contact database
       
   173     CVPbkContactStoreUriArray* uriArray = CVPbkContactStoreUriArray::NewLC();
       
   174     uriArray->AppendL( TVPbkContactStoreUriPtr( aStoreName ));
   138     
   175     
   139     // TODO: Determine the Default StoreName
   176     iContactManager = CVPbkContactManager::NewL(*uriArray);
   140     iDefaultStoreName = HBufC::NewL(KNSmlDefaultStoreNameMaxSize);
   177     CleanupStack::PopAndDestroy(uriArray);
   141     *iDefaultStoreName = KNSmlContactStoreNameForDefaultDB;
   178     //Default store name
       
   179     iDefaultStoreName = aStoreName.AllocL();
   142     
   180     
   143     iSnapshot = new (ELeave) CArrayFixSeg<TNSmlSnapshotItem>(KNSmlContactsGranularity);
   181     //Fill iPacketStoreName buffer with legacy store name
   144     iCntUidList = new(ELeave) CArrayFixFlat<TUid>(KNSmlContactsGranularity);
   182     TBuf<KOldSymbianDBLength> dataBase(aLegacyStore);
       
   183     iPacketStoreName = HBufC::NewL(dataBase.Length());
       
   184     TPtr pktStorePtr(iPacketStoreName->Des());
       
   185     pktStorePtr.Copy(dataBase);
       
   186     iDeleteAllOperation = NULL;
   145         
   187         
   146     _DBG_FILE("CNSmlContactsDataStore::ConstructL(): end");
   188     _DBG_FILE("CNSmlContactsDataStore::ConstructL(): end");    
   147     }
   189     }
   148 
   190 
   149 // ----------------------------------------------------------------------------
   191 // ----------------------------------------------------------------------------
   150 // CNSmlContactsDataStore::~CNSmlContactsDataStore()
   192 // CNSmlContactsDataStore::~CNSmlContactsDataStore()
   151 // ----------------------------------------------------------------------------
   193 // ----------------------------------------------------------------------------
   155 	
   197 	
   156 	delete iStoreFormat; // SetOwnStoreFormatL creates
   198 	delete iStoreFormat; // SetOwnStoreFormatL creates
   157 	iStringPool.Close();
   199 	iStringPool.Close();
   158 
   200 
   159 	iRfs.Close();
   201 	iRfs.Close();
       
   202 	iWriteStream.Close();
       
   203 	iReadStream.Close();
       
   204 	delete iBuf;
       
   205 	
   160 	if ( iChangeFinder )
   206 	if ( iChangeFinder )
   161 		{
   207 		{
   162 		TRAP_IGNORE( iChangeFinder->CloseL() );
   208 		TRAP_IGNORE( iChangeFinder->CloseL() );
   163 		delete iChangeFinder;
   209 		delete iChangeFinder;
   164 		}
   210 		iChangeFinder = NULL;
   165 	
   211 		}
   166 	if( iContactsDataStoreExtension )
   212 	
       
   213 	if(iIdConverter)
       
   214 		{
       
   215 		delete iIdConverter;
       
   216 		iIdConverter = NULL;
       
   217 		}
       
   218 	if(iContactLnks)
       
   219 		{
       
   220         delete iContactLnks;
       
   221 		iContactLnks = NULL;
       
   222 		}
       
   223 
       
   224 	if(iContactViewBase)
   167 	    {
   225 	    {
   168         delete iContactsDataStoreExtension;
   226 	    delete iContactViewBase;
   169         iContactsDataStoreExtension = NULL;
   227 	    iContactViewBase = NULL;
   170 	    }
   228 	    }
   171 	
   229 	if ( iContactsModsFetcher )
       
   230 		{
       
   231 		iContactsModsFetcher->CancelRequest(); 
       
   232 		delete iContactsModsFetcher;
       
   233 		iContactsModsFetcher = NULL;
       
   234 		}
       
   235 	if ( iContactManager )
       
   236 		{
       
   237 			if(iStore)
       
   238 			{
       
   239 			iStore->Close( *this );	
       
   240 			}
       
   241 	    delete iContactManager;
       
   242 	    iContactManager = NULL;
       
   243 		}
       
   244 	
       
   245 	
       
   246 	delete iVCardEngine;
       
   247 	//  Delete UidSets		
       
   248 	//
   172 	delete iNewUids;
   249 	delete iNewUids;
   173 	delete iDeletedUids;
   250 	delete iDeletedUids;
   174 	delete iSoftDeletedUids;
   251 	delete iSoftDeletedUids;
   175 	delete iMovedUids;
   252 	delete iMovedUids;
   176 	delete iReplacedUids;
   253 	delete iReplacedUids;
   177 	
   254 	
   178 	delete iSnapshot;
   255 	//
   179     delete iCntUidList;
   256 	//	Empty BufferItemList if any
   180 //
   257 	//
   181 //	Empty BufferItemList if any
   258 	if ( iContactsBufferItemList.Count() > 0 )
   182 //
   259 		{
   183   if ( iContactsBufferItemList.Count() > 0 )
   260 		if ( iContactsBufferItemList[iContactsBufferItemList.Count() - 1]->iItemData == iItemData )
   184   {
   261 			{
   185     if ( iContactsBufferItemList[iContactsBufferItemList.Count() - 1]->iItemData == iItemData )
   262 			iItemData = NULL;
   186       {
   263 			}
   187       iItemData = NULL;
   264 		}
   188       }
       
   189   }
       
   190 	iContactsBufferItemList.ResetAndDestroy();
   265 	iContactsBufferItemList.ResetAndDestroy();
   191 	
       
   192 	delete iDataMod;
   266 	delete iDataMod;
   193 	
   267 	delete iMergeItem;
       
   268 
   194 	if(iItemData)
   269 	if(iItemData)
   195     	{
   270     	{
   196 	    if(iItemData->Size() > 0 )
   271 	    if(iItemData->Size() > 0 )
   197 	        {
   272 	        {
   198 		    delete iItemData;       
   273 		    delete iItemData;       
   203 		{
   278 		{
   204 		delete iItemDataAddBatch;	
   279 		delete iItemDataAddBatch;	
   205 		}
   280 		}
   206 		
   281 		
   207 	delete iDefaultStoreName;
   282 	delete iDefaultStoreName;
       
   283 	delete iPacketStoreName;
   208 
   284 
   209 	delete iStoreName;
   285 	delete iStoreName;
   210 
   286 
   211 	iAddResultArray.Close();
   287 	iAddResultArray.Close();
       
   288 	
       
   289 	if(iDeleteAllOperation)
       
   290 	    {
       
   291 	    delete iDeleteAllOperation;
       
   292 	    iDeleteAllOperation = NULL;
       
   293 	    }
       
   294 	
   212 	}
   295 	}
   213 
   296 
   214 // ----------------------------------------------------------------------------
   297 // ----------------------------------------------------------------------------
   215 // void CNSmlContactsDataStore::DoOpenL(const TDesC& 		  aStoreName, 
   298 // void CNSmlContactsDataStore::DoOpenL(const TDesC& 		  aStoreName, 
   216 //									    MSmlSyncRelationship& aContext,
   299 //									    MSmlSyncRelationship& aContext,
   217 //									    TRequestStatus& 	  aStatus)
   300 //									    TRequestStatus& 	  aStatus)
   218 // ----------------------------------------------------------------------------
   301 // ----------------------------------------------------------------------------
   219 EXPORT_C void CNSmlContactsDataStore::DoOpenL( const TDesC& aStoreName, 
   302 EXPORT_C void CNSmlContactsDataStore::DoOpenL( const TDesC& aStoreName, 
   220                                                MSmlSyncRelationship& aContext, 
   303 									MSmlSyncRelationship& aContext, 
   221                                                TRequestStatus& aStatus )
   304 									TRequestStatus& aStatus )
   222 	{
   305 	{
   223 	DBG_ARGS(_S("CNSmlContactsDataStore::DoOpenL(): '%S' begin"), &aStoreName);
   306 	DBG_ARGS(_S("CNSmlContactsDataStore::DoOpenL(): '%S' begin"), &aStoreName);
   224 
   307 
   225 	iCallerStatus = &aStatus;
   308 	iCallerStatus = &aStatus;
   226 	*iCallerStatus = KRequestPending;
   309 	*iCallerStatus = KRequestPending;
   229 		{
   312 		{
   230 		User::RequestComplete( iCallerStatus, KErrInUse );	
   313 		User::RequestComplete( iCallerStatus, KErrInUse );	
   231 		return;	
   314 		return;	
   232 		}
   315 		}
   233 
   316 
   234 	RFs::CharToDrive( KNSmlDriveC()[0], iDrive );
   317 	if( RFs::CharToDrive(aStoreName[0], iDrive) != KErrNone )
   235 
       
   236 	/*if( RFs::CharToDrive(aStoreName[0], iDrive) != KErrNone )
       
   237 		{
   318 		{
   238 		RFs::CharToDrive( KNSmlDriveC()[0], iDrive );
   319 		RFs::CharToDrive( KNSmlDriveC()[0], iDrive );
   239 		}*/
   320 		}
   240 	iOpened = EFalse;
   321 	iOpened = EFalse;
   241 	
   322 	
   242 	if( iStoreName )
   323 	if( iStoreName )
   243 		{
   324 		{
   244 		delete iStoreName;
   325 		delete iStoreName;
   245 		iStoreName = NULL;
   326 		iStoreName = NULL;
   246 		}
   327 		}
   247 		
   328 		
   248 	if ( iContactsDataStoreExtension )
   329 		if ( iChangeFinder )
   249 		{
       
   250 		delete iContactsDataStoreExtension;
       
   251 		iContactsDataStoreExtension = NULL;
       
   252 		}
       
   253 		
       
   254 	TInt err( KErrNone );
       
   255 	
       
   256 	// TODO:Change the storename later	
       
   257 	TRAP( err, iContactsDataStoreExtension = CNsmlContactsDataStoreExtension::NewL( KNSmlContactStoreNameForDefaultDB() ) );
       
   258 	
       
   259 	if( err == KErrNone )
       
   260 		{
       
   261 		iOpened = ETrue;
       
   262 		iStoreName = aStoreName.AllocL();
       
   263 		iOpenedStoreId = iContactsDataStoreExtension->MachineIdL();
       
   264 		}
       
   265 	else
       
   266 		{
       
   267 		DBG_ARGS(_S("CNSmlContactsDataStore::DoOpenL(): result = %d"), err); // Cnt open = err
       
   268 		iOpened = EFalse;
       
   269 		delete iStoreName;
       
   270 		iStoreName = NULL;
       
   271 		iOpenedStoreId = 0;
       
   272 		User::RequestComplete( iCallerStatus, err );
       
   273 		_DBG_FILE("CNSmlContactsDataStore::DoOpenL(): CONTACTS FILE NOTFOUND end");
       
   274 		return;
       
   275 		}
       
   276 		
       
   277 	if ( iChangeFinder )
       
   278 		{
   330 		{
   279 		iChangeFinder->CloseL();
   331 		iChangeFinder->CloseL();
   280 		delete iChangeFinder;
   332 		delete iChangeFinder;
   281 		iChangeFinder = NULL;
   333 		iChangeFinder = NULL;
   282 		}
   334 		}
   283 	iChangeFinder = CNSmlChangeFinder::NewL( aContext, iKey, iSyncHistory, KNSmlContactsAdapterImplUid );
   335 	// The Change finder maintains a list of all changes to the database.
   284 
   336 	iChangeFinder = CNSmlChangeFinder::NewL(
   285 	iState = ENSmlOpenAndWaiting;		
   337 					 aContext, iKey, iSyncHistory, KNSmlContactsAdapterImplUid );
   286 	
   338 	
   287 	err = FetchModificationsL();
   339 	iState = ENSmlOpenAndWaiting;			
   288 	
   340 	
   289 	DBG_ARGS(_S("CNSmlContactsDataStore::DoOpenL(): Error: %d"), err);
   341 	//find and open the store
   290 	
   342 	if ( aStoreName == KNSmlContactStoreNameForDefaultDB )
   291 	User::RequestComplete( iCallerStatus, err );
   343 	    {
       
   344 	iStore = iContactManager->ContactStoresL().Find(
       
   345 	TVPbkContactStoreUriPtr(*iDefaultStoreName));
       
   346 	    }
       
   347 	else
       
   348 	   {
       
   349 	    iStore = iContactManager->ContactStoresL().Find(
       
   350 	TVPbkContactStoreUriPtr(aStoreName));
       
   351 	    }
       
   352 	iStore->OpenL(*this);
       
   353 
       
   354 	iStoreName = aStoreName.AllocL();
       
   355 	
       
   356 	iVCardEngine = CVPbkVCardEng::NewL( *iContactManager );
       
   357 	iIdConverter = CVPbkContactIdConverter::NewL(*iStore );
       
   358 	if ( iContactsModsFetcher )
       
   359 		{
       
   360 		iContactsModsFetcher->CancelRequest();
       
   361 		delete iContactsModsFetcher;
       
   362 		iContactsModsFetcher = NULL;
       
   363 		}
       
   364 		
       
   365 	iContactsModsFetcher = CreateModsFetcherL();
       
   366 	
       
   367 #ifdef __NSML_MODULETEST__
       
   368 	CActiveScheduler::Start();
       
   369 #endif
   292 	
   370 	
   293 	_DBG_FILE("CNSmlContactsDataStore::DoOpenL(): end");
   371 	_DBG_FILE("CNSmlContactsDataStore::DoOpenL(): end");
   294 	}
   372 	}
   295 
   373 
   296 // ----------------------------------------------------------------------------
   374 // ----------------------------------------------------------------------------
   297 // void CNSmlContactsDataStore::DoCancelRequest()
   375 // void CNSmlContactsDataStore::DoCancelRequest()
   298 // ----------------------------------------------------------------------------
   376 // ----------------------------------------------------------------------------
   299 EXPORT_C void CNSmlContactsDataStore::DoCancelRequest()
   377 EXPORT_C void CNSmlContactsDataStore::DoCancelRequest()
   300 	{
   378 	{
   301 	_DBG_FILE("CNSmlContactsDataStore::DoCancelRequest(): begin");
   379 	_DBG_FILE("CNSmlContactsDataStore::DoCancelRequest(): begin");
       
   380 	if(iLastOperation == ENSMLDeleteAllOp)
       
   381 	    {   
       
   382 	    if(iDeleteAllOperation)
       
   383 	        {
       
   384 	        delete iDeleteAllOperation;
       
   385 	        iDeleteAllOperation = NULL;
       
   386 	        }
       
   387 
       
   388 	    if( iChangeFinder )
       
   389 	        {
       
   390             TRAP_IGNORE(iChangeFinder->ResetL());
       
   391 	        }
       
   392 	    iSnapshotRegistered = EFalse;
       
   393 
       
   394 	    if(iContactLnks)
       
   395 	        {
       
   396 	        delete iContactLnks;
       
   397 	        iContactLnks = NULL;
       
   398 			iContactLink = NULL;
       
   399 	        }
       
   400 
       
   401 	    if(iContactViewBase)
       
   402 	        {
       
   403 	        delete iContactViewBase;
       
   404 	        iContactViewBase = NULL;
       
   405 	        }
       
   406 	    User::RequestComplete( iCallerStatus, KErrCancel );  
       
   407 	    }
       
   408 		if(iContactsModsFetcher)
       
   409 		{
       
   410 		iContactsModsFetcher->CancelRequest();
       
   411 		}
   302 	
   412 	
   303 	_DBG_FILE("CNSmlContactsDataStore::DoCancelRequest(): NOT NEEDED end");
   413 	_DBG_FILE("CNSmlContactsDataStore::DoCancelRequest(): NOT NEEDED end");
   304 	}
   414 	}
   305 
   415 
   306 // ----------------------------------------------------------------------------
   416 // ----------------------------------------------------------------------------
   317 // void CNSmlContactsDataStore::DefaultStoreNameL()
   427 // void CNSmlContactsDataStore::DefaultStoreNameL()
   318 // ----------------------------------------------------------------------------
   428 // ----------------------------------------------------------------------------
   319 EXPORT_C const TDesC& CNSmlContactsDataStore::DefaultStoreNameL() const
   429 EXPORT_C const TDesC& CNSmlContactsDataStore::DefaultStoreNameL() const
   320 	{
   430 	{
   321 	_DBG_FILE("CNSmlContactsDataStore::DefaultStoreNameL(): begin");
   431 	_DBG_FILE("CNSmlContactsDataStore::DefaultStoreNameL(): begin");
   322 
   432 	
   323 	if ( !iDefaultStoreName )
   433 	if ( !iDefaultStoreName )
   324 		{
   434 		{
   325 		return KNullDesC;
   435 		return KNullDesC;
   326 		}
   436 		}
   327 
   437 
   328 	_DBG_FILE("CNSmlContactsDataStore::DefaultStoreNameL(): end");
   438 	_DBG_FILE("CNSmlContactsDataStore::DefaultStoreNameL(): end");
   329 	return *iDefaultStoreName;
   439 	//The engine methods that create syncml packet, use this information in Database fields
       
   440 	return *iPacketStoreName;
   330 	}
   441 	}
   331 	
   442 	
   332 // ----------------------------------------------------------------------------
   443 // ----------------------------------------------------------------------------
   333 // void CNSmlContactsDataStore::DoBeginTransactionL()
   444 // void CNSmlContactsDataStore::DoBeginTransactionL()
   334 // ----------------------------------------------------------------------------
   445 // ----------------------------------------------------------------------------
   373 EXPORT_C void CNSmlContactsDataStore::DoBeginBatchL()
   484 EXPORT_C void CNSmlContactsDataStore::DoBeginBatchL()
   374 	{
   485 	{
   375 	_DBG_FILE("CNSmlContactsDataStore::DoBeginBatchL(): begin");
   486 	_DBG_FILE("CNSmlContactsDataStore::DoBeginBatchL(): begin");
   376 	
   487 	
   377 	iBatchMode = ETrue;			// SUPPORTED on Contacts
   488 	iBatchMode = ETrue;			// SUPPORTED on Contacts
   378 	
   489 
   379 	if( iItemDataAddBatch )
       
   380 		{
       
   381 		iItemDataAddBatch->Reset();
       
   382 		}
       
   383 	
       
   384 	_DBG_FILE("CNSmlContactsDataStore::DoBeginBatchL(): end");
   490 	_DBG_FILE("CNSmlContactsDataStore::DoBeginBatchL(): end");
   385 	}
   491 	}
   386 
   492 
   387 // ----------------------------------------------------------------------------
   493 // ----------------------------------------------------------------------------
   388 // void CNSmlContactsDataStore::DoCommitBatchL()
   494 // void CNSmlContactsDataStore::DoCommitBatchL()
   389 // ----------------------------------------------------------------------------
   495 // ----------------------------------------------------------------------------
   390 EXPORT_C void CNSmlContactsDataStore::DoCommitBatchL( RArray<TInt>& aResultArray, 
   496 EXPORT_C void CNSmlContactsDataStore::DoCommitBatchL( RArray<TInt>& aResultArray, TRequestStatus& aStatus )
   391                                                       TRequestStatus& aStatus )
       
   392 	{
   497 	{
   393 	_DBG_FILE("CNSmlContactsDataStore::DoCommitBatchL(): begin");
   498 	_DBG_FILE("CNSmlContactsDataStore::DoCommitBatchL(): begin");
   394 
   499 
   395 	iCallerStatus = &aStatus;
   500 	iCallerStatus = &aStatus;
   396 	*iCallerStatus = KRequestPending;
   501 	*iCallerStatus = KRequestPending;
   401 		_DBG_FILE("CNSmlContactsDataStore::DoCommitBatchL(): NOT ON BATCH MODE end");
   506 		_DBG_FILE("CNSmlContactsDataStore::DoCommitBatchL(): NOT ON BATCH MODE end");
   402 		return;
   507 		return;
   403 		}
   508 		}
   404 //		
   509 //		
   405 // Go through Buffer and execute all commands return aResultArray
   510 // Go through Buffer and execute all commands return aResultArray
   406 // If none of commands succeeds KErrGeneral returned as status otherwise KErrNone returned
       
   407 //
   511 //
   408 	TInt err = KErrNone;
   512 	
   409 
   513 	iResultArray = &aResultArray;
   410 	TRAPD( leavecode, err = ExecuteBufferL(aResultArray) );
   514 	
       
   515 	iIndex = 0;
       
   516 	TRAPD( leavecode,ExecuteBufferL() );
   411 	
   517 	
   412 	if( leavecode != KErrNone )
   518 	if( leavecode != KErrNone )
   413 		{
   519 		{
   414 		err = leavecode;
   520 		User::RequestComplete( iCallerStatus, leavecode );
   415 		}
   521 		}
   416 //
   522 
   417 // Empty Buffer
       
   418 //	
       
   419 	iContactsBufferItemList.ResetAndDestroy();
       
   420 	
       
   421 	if( iItemDataAddBatch )
       
   422 		{
       
   423 		iItemDataAddBatch->Reset();
       
   424 		}
       
   425 	
       
   426 	iBatchMode = EFalse; // All data handled
       
   427 	User::RequestComplete( iCallerStatus, err );
       
   428 		
       
   429 	_DBG_FILE("CNSmlContactsDataStore::DoCommitBatchL(): end");
   523 	_DBG_FILE("CNSmlContactsDataStore::DoCommitBatchL(): end");
   430 	}
   524 	}
   431 	
   525 	
   432 // ----------------------------------------------------------------------------
   526 // ----------------------------------------------------------------------------
   433 // void CNSmlContactsDataStore::ExecuteBufferL()
   527 // void CNSmlContactsDataStore::ExecuteBufferL()
   434 // ----------------------------------------------------------------------------
   528 // ----------------------------------------------------------------------------
   435 EXPORT_C TInt CNSmlContactsDataStore::ExecuteBufferL(RArray<TInt>& aResultArray) 
   529 EXPORT_C void CNSmlContactsDataStore::ExecuteBufferL() 
   436     {
   530 	{
   437     _DBG_FILE("CNSmlContactsDataStore::ExecuteBufferL()");
   531 	_DBG_FILE("CNSmlContactsDataStore::ExecuteBufferL(): begin");
   438     return DoExecuteBufferL( aResultArray );
   532 	
   439     }
   533 	TInt  retCommand( KErrNone );
   440 
   534 	
       
   535 	//
       
   536 	// Loop through buffered commands
       
   537 	//
       
   538 		if( iIndex < iContactsBufferItemList.Count())
       
   539 		{
       
   540 		
       
   541 		if( iContactsBufferItemList[iIndex]->iStatus!= KNSmlNoError )
       
   542 			{
       
   543 			retCommand = iContactsBufferItemList[iIndex]->iStatus;
       
   544 			}
       
   545 		else if( iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemAdd )
       
   546 			{
       
   547 			retCommand = ExecuteAddL();
       
   548 			}
       
   549 		else if( iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemReplace ||
       
   550             iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemFieldLevelReplace )
       
   551 			{
       
   552 			ExecuteUpdateL();		
       
   553 			}
       
   554 		else if( iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemMove )
       
   555 			{
       
   556 			ExecuteMoveL();
       
   557 			}
       
   558 		else if( iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemDelete ||
       
   559 				 iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemSoftDelete )
       
   560 			{
       
   561 			ExecuteDeleteL();
       
   562 			}
       
   563 		//
       
   564 		//	Set allready set commands from iContactsBufferItemList
       
   565 		//
       
   566 		if( ( iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemAdd     ||
       
   567 		      iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemReplace ||
       
   568 		      iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemFieldLevelReplace ||
       
   569 		      iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemDelete  ||
       
   570 		      iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemSoftDelete ) &&
       
   571 		      iContactsBufferItemList[iIndex]->iStatus  != KNSmlNoError )
       
   572 			{
       
   573 			retCommand = iContactsBufferItemList[iIndex]->iStatus;
       
   574 			}
       
   575 			
       
   576 		
       
   577 		//if an error occurs when performing any operation
       
   578 		//return the error status 
       
   579 			else if(retCommand!=KErrNone)
       
   580 			{
       
   581 			User::RequestComplete( iCallerStatus, retCommand );	
       
   582 			}
       
   583 			
       
   584 			iRetCommand = retCommand;
       
   585 		}
       
   586 	}
       
   587 	
       
   588 // ----------------------------------------------------------------------------
       
   589 // void CNSmlContactsDataStore::ExecuteAddL()
       
   590 // ----------------------------------------------------------------------------
       
   591 	
       
   592 EXPORT_C TInt CNSmlContactsDataStore::ExecuteAddL()
       
   593 	{
       
   594 	TInt  retCommand( KErrNone );
       
   595 	StripPropertyL( iItemDataAddBatch, KVersitTokenUID() ); // Remove UID's from data
       
   596 
       
   597 	TInt err( KErrNone );
       
   598 	if(iBatchMode)			
       
   599 		{
       
   600 		TRAP( err,LdoAddItemsL( iItemDataAddBatch,
       
   601 		                   iItemDataAddBatch->Ptr(0).Length()) );
       
   602 		if(err!=KErrNone)			
       
   603 			{
       
   604 			retCommand = err;
       
   605 			TInt j = iIndex;
       
   606 			while( j < iContactsBufferItemList.Count() )
       
   607 				{
       
   608 				if( iContactsBufferItemList[j]->iModType == ENSmlCntItemAdd &&
       
   609 				iContactsBufferItemList[j]->iStatus  == KNSmlNoError )
       
   610 					{
       
   611 					iContactsBufferItemList[j]->iStatus = KErrNotSupported;
       
   612 					iResultArray->AppendL(KErrNotSupported);
       
   613 					}
       
   614 				j++;
       
   615 				}
       
   616 			}			                          
       
   617 		}
       
   618 	else
       
   619 		{
       
   620 		const TPtr8 dataPtr = iItemDataAddBatch->Ptr(0);
       
   621 		TRAP( err, LdoAddItemL( dataPtr,
       
   622 					iContactsBufferItemList[iIndex]->iSize));
       
   623 		if(err!=KErrNone)			
       
   624 			{
       
   625 			retCommand = err;
       
   626 			}	
       
   627 		}
       
   628 	return retCommand; 
       
   629 	}
       
   630 	
       
   631 // ----------------------------------------------------------------------------
       
   632 // void CNSmlContactsDataStore::ExecuteDeleteL()
       
   633 // ----------------------------------------------------------------------------
       
   634 	
       
   635 EXPORT_C void CNSmlContactsDataStore:: ExecuteDeleteL()
       
   636 	{
       
   637 	
       
   638 	if(iBatchMode)			
       
   639 		{
       
   640 		iContactLnks = CVPbkContactLinkArray::NewL();
       
   641 		
       
   642 		TInt j=iIndex;
       
   643 		while( j < iContactsBufferItemList.Count() )
       
   644 			{
       
   645 			if( (iContactsBufferItemList[j]->iModType == ENSmlCntItemDelete ||
       
   646 			iContactsBufferItemList[j]->iModType == ENSmlCntItemSoftDelete) &&
       
   647 			iContactsBufferItemList[j]->iStatus  == KNSmlNoError )
       
   648 				{
       
   649 				TContactItemId id = iContactsBufferItemList[j]->iUid; 
       
   650 
       
   651 				if( id != 0 )
       
   652 					{
       
   653 					MVPbkContactLink* contactLink = iIdConverter->IdentifierToLinkLC(id);
       
   654 					//check if the contact link is valid
       
   655 					TInt index = iContactViewBase->IndexOfLinkL(*contactLink);
       
   656 					if(index != KErrNotFound)
       
   657 						{
       
   658 						iContactLnks->AppendL(contactLink);	
       
   659 						}
       
   660 						else
       
   661 						{
       
   662 						iContactsBufferItemList[j]->iStatus  = KErrNotFound;
       
   663 						}
       
   664 
       
   665 					CleanupStack::Pop();//contactLink				
       
   666 					}
       
   667 					else 
       
   668 					{
       
   669 					iContactsBufferItemList[j]->iStatus  = KErrNotFound;
       
   670 					}
       
   671 				}
       
   672 			j++;
       
   673 			}
       
   674 
       
   675 		delete iContactViewBase;
       
   676 		iContactViewBase = NULL;
       
   677 
       
   678 		iContactManager->DeleteContactsL( *iContactLnks, *this );
       
   679 		iLastOperation = ENSmlDeleteOp;
       
   680 		}
       
   681 
       
   682 	}
       
   683 	
       
   684 // ----------------------------------------------------------------------------
       
   685 // void CNSmlContactsDataStore::ExecuteUpdateL()
       
   686 // ----------------------------------------------------------------------------
       
   687 	
       
   688 EXPORT_C void CNSmlContactsDataStore::ExecuteUpdateL()
       
   689 	{
       
   690 	iUid  = iContactsBufferItemList[iIndex]->iUid;
       
   691 
       
   692 	iDataMod->SetUsedMimeType( iServerMimeType, iServerMimeVersion );
       
   693 
       
   694 	// Check whether merging is needed or not.
       
   695 	if( iDataMod->NeedsMerge() )
       
   696 		{
       
   697 		iMergeItem = CBufFlat::NewL(64);
       
   698 		iWriteStream.Open(*iMergeItem);
       
   699 
       
   700 		MVPbkContactLink* contactLink = iIdConverter->IdentifierToLinkLC(iUid);
       
   701 
       
   702 		iVCardEngine->ExportVCardForSyncL(iWriteStream,*contactLink,*this);
       
   703 		CleanupStack::PopAndDestroy();
       
   704 
       
   705 		iLastOperation = ENSMLUpdateExportOp;
       
   706 		}
       
   707 	else
       
   708 		{
       
   709 			LdoUpdateItemL();
       
   710 		}
       
   711 	}
       
   712 
       
   713 // ----------------------------------------------------------------------------
       
   714 // void CNSmlContactsDataStore::ExecuteMoveL
       
   715 // ----------------------------------------------------------------------------
       
   716 
       
   717 EXPORT_C void CNSmlContactsDataStore::ExecuteMoveL()
       
   718 	{
       
   719 	// move command is not supported
       
   720 	if(!iBatchMode)
       
   721 		{
       
   722 		ResetBuffer();
       
   723 		User::RequestComplete( iCallerStatus, KErrNotSupported );	
       
   724 		}
       
   725 	else
       
   726 		{
       
   727 		iResultArray->AppendL(KErrNotSupported);
       
   728 		iIndex++;
       
   729 		if(iIndex == iContactsBufferItemList.Count() )
       
   730 			{
       
   731 			ResetBuffer();
       
   732 			User::RequestComplete( iCallerStatus, KErrNone );	
       
   733 			}
       
   734 		else
       
   735 			{
       
   736 				ExecuteBufferL();
       
   737 			}		
       
   738 		}
       
   739 	}
   441 // ----------------------------------------------------------------------------
   740 // ----------------------------------------------------------------------------
   442 // void CNSmlContactsDataStore::DoCancelBatch()
   741 // void CNSmlContactsDataStore::DoCancelBatch()
   443 // ----------------------------------------------------------------------------
   742 // ----------------------------------------------------------------------------
   444 EXPORT_C void CNSmlContactsDataStore::DoCancelBatch()
   743 EXPORT_C void CNSmlContactsDataStore::DoCancelBatch()
   445 	{
   744 	{
   510 	
   809 	
   511 	if( !iStoreFormat )
   810 	if( !iStoreFormat )
   512 		{
   811 		{
   513 		TFileName fileName;
   812 		TFileName fileName;
   514 		TParse parse;
   813 		TParse parse;
   515 		
   814 
   516 		// Locate the resource file
   815 		parse.Set( GetStoreFormatResourceFileL(), &KDC_RESOURCE_FILES_DIR, NULL  );
   517         TFileName dllFileName;
       
   518         Dll::FileName( dllFileName );
       
   519         
       
   520         TFileName resourceFileName;
       
   521         resourceFileName.Copy( TParsePtrC( dllFileName ).Drive() );  
       
   522         
       
   523         resourceFileName.Append( GetStoreFormatResourceFileL() );
       
   524 
       
   525         parse.Set( resourceFileName, &KDC_RESOURCE_FILES_DIR, NULL );
       
   526 
   816 
   527 		fileName = parse.FullName();
   817 		fileName = parse.FullName();
   528 		
       
   529 		DBG_ARGS(_S("CNSmlContactsDataStore::SetOwnStoreFormatL(): '%S'"), &parse.FullName());
       
   530 
   818 
   531 		RResourceFile resourceFile; 
   819 		RResourceFile resourceFile; 
   532 		BaflUtils::NearestLanguageFile( iRfs, fileName );
   820 		BaflUtils::NearestLanguageFile( iRfs, fileName );
   533 
   821 
   534 		resourceFile.OpenL( iRfs, fileName );
   822 		resourceFile.OpenL( iRfs, fileName );
   538 
   826 
   539 		TResourceReader reader;
   827 		TResourceReader reader;
   540 		reader.SetBuffer( contactsDataFormat );
   828 		reader.SetBuffer( contactsDataFormat );
   541 
   829 
   542 		iStoreFormat = CSmlDataStoreFormat::NewLC( iStringPool, reader );
   830 		iStoreFormat = CSmlDataStoreFormat::NewLC( iStringPool, reader );
   543 		CleanupStack::Pop(); // iStoreFormat
   831 		CleanupStack::Pop(iStoreFormat); // iStoreFormat
   544 		
   832 		
   545 		CleanupStack::PopAndDestroy(2); // contactsDataFormat, resourceFile  
   833 		CleanupStack::PopAndDestroy(2); // contactsDataFormat, resourceFile  
   546 		}
   834 		}
   547 		
   835 		
   548 	iDataMod->SetOwnStoreFormat( *iStoreFormat );
   836 	iDataMod->SetOwnStoreFormat( *iStoreFormat );
   549 
   837 
   550 	// Set own MIME type based on store format resource definition. Allows inherited classes to 
   838 	// Set own MIME type based on store format resource definition. Allows inherited classes to 
   551 	// use non-standard MIME type by recource change. E.g. Operator specific MIME types can be used.
   839     // use non-standard MIME type by recource change. E.g. Operator specific MIME types can be used.
   552     iUsedMimeType.Set( iStoreFormat->MimeFormat(0).MimeType().DesC() );
   840     iUsedMimeType.Set( iStoreFormat->MimeFormat(0).MimeType().DesC() );
   553     iUsedMimeVersion.Set( iStoreFormat->MimeFormat(0).MimeVersion().DesC() );
   841     iUsedMimeVersion.Set( iStoreFormat->MimeFormat(0).MimeVersion().DesC() );
   554 	
   842 
   555 	_DBG_FILE("CNSmlContactsDataStore::SetOwnStoreFormatL(): end");
   843 	_DBG_FILE("CNSmlContactsDataStore::SetOwnStoreFormatL(): end");
   556 	return;
   844 	return;
   557 	}
   845 	}
   558 
   846 
   559 // ----------------------------------------------------------------------------
   847 // ----------------------------------------------------------------------------
   616 		}
   904 		}
   617 	else
   905 	else
   618 		{
   906 		{
   619 		iItemData->Reset();
   907 		iItemData->Reset();
   620 		}
   908 		}
   621 
   909 	
   622 	TInt err = LdoFetchItemL( aUid, *iItemData );
   910 	 LdoFetchItemL( aUid, *iItemData );
   623 	
   911 	 iSize = &aSize;
   624 	if( !err )
   912 	
   625 		{
       
   626 		iItemData->Compress();
       
   627 		
       
   628 		iDataMod->StripTxL( *iItemData );
       
   629 	
       
   630 //  	Get Item size from database after stripping
       
   631 		aSize = iItemData->Size();
       
   632 		iState = ENSmlItemOpen;
       
   633 		
       
   634 		if ( iServerMaxObjectSize == 0 || aSize <= iServerMaxObjectSize )
       
   635 			{
       
   636 			User::RequestComplete( iCallerStatus, KErrNone );
       
   637 			}
       
   638 		else
       
   639 			{
       
   640 			User::RequestComplete( iCallerStatus, KErrTooBig );
       
   641 			}
       
   642 		}
       
   643 	else
       
   644 		{
       
   645 		User::RequestComplete( iCallerStatus, err );
       
   646 		}
       
   647 
       
   648 	_DBG_FILE("CNSmlContactsDataStore::DoOpenItemL(): end");
   913 	_DBG_FILE("CNSmlContactsDataStore::DoOpenItemL(): end");
   649 	}
   914 	}
   650 
   915 
   651 // ----------------------------------------------------------------------------
   916 // ----------------------------------------------------------------------------
   652 // void CNSmlContactsDataStore::DoCreateItemL()
   917 // void CNSmlContactsDataStore::DoCreateItemL()
   665 	
   930 	
   666 	TInt err = KErrNone;
   931 	TInt err = KErrNone;
   667 	
   932 	
   668 	if ( iState != ENSmlOpenAndWaiting )
   933 	if ( iState != ENSmlOpenAndWaiting )
   669 		{
   934 		{
   670         _DBG_FILE("CNSmlContactsDataStore::DoCreateItemL - KErrNotReady: END");
       
   671 		User::RequestComplete( iCallerStatus, KErrNotReady );
   935 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
   936 		_DBG_FILE("CNSmlContactsDataStore::DoCreateItemL - KErrNotReady: END");
   672 		return;
   937 		return;
   673 		}
   938 		}
   674 		
   939 		
   675 	iItemSize = aSize; // Size sent from server
   940 	iItemSize = aSize; // Size sent from server
   676 	iModType  = ENSmlCntItemAdd;
   941 	iModType  = ENSmlCntItemAdd;
   698 	if( ( aMimeType.MatchF( KNSmlvCard21Name ) < 0 ) &&
   963 	if( ( aMimeType.MatchF( KNSmlvCard21Name ) < 0 ) &&
   699 	    ( aMimeType.MatchF( KNSmlvCard30Name ) < 0 ) &&
   964 	    ( aMimeType.MatchF( KNSmlvCard30Name ) < 0 ) &&
   700 	    // Allow using custom MIME type defined in store format resource file
   965 	    // Allow using custom MIME type defined in store format resource file
   701 	    ( aMimeType.MatchF( iUsedMimeType ) < 0 ) )
   966 	    ( aMimeType.MatchF( iUsedMimeType ) < 0 ) )
   702 		{
   967 		{
   703         _DBG_FILE("CNSmlContactsDataStore::DoCreateItemL - KErrNotSupported: END");
       
   704 		User::RequestComplete( iCallerStatus, KErrNotSupported );
   968 		User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   969 		_DBG_FILE("CNSmlContactsDataStore::DoCreateItemL - KErrNotSupported: END");
   705 		return;
   970 		return;
   706 		}
   971 		}
   707     
   972     
   708     // Is there enough space on drive ?
   973     // Is there enough space on drive 
   709 	if( DriveBelowCriticalLevelL( aSize ) )
   974 	if( DriveBelowCriticalLevelL( aSize ) )
   710 		{
   975 		{
   711 		err = KErrDiskFull;
   976 		err = KErrDiskFull;
   712 		}
   977 		}
   713 				
   978 				
   734 // ----------------------------------------------------------------------------
   999 // ----------------------------------------------------------------------------
   735 // void CNSmlContactsDataStore::DoReplaceItemL()
  1000 // void CNSmlContactsDataStore::DoReplaceItemL()
   736 // ----------------------------------------------------------------------------
  1001 // ----------------------------------------------------------------------------
   737 EXPORT_C void CNSmlContactsDataStore::DoReplaceItemL( TSmlDbItemUid aUid, 
  1002 EXPORT_C void CNSmlContactsDataStore::DoReplaceItemL( TSmlDbItemUid aUid, 
   738 											 TInt aSize, 
  1003 											 TInt aSize, 
   739 											 TSmlDbItemUid /*aParent*/, 
  1004 											 TSmlDbItemUid /*aParent*/,
   740 											 TBool aFieldChange, 
  1005 											 TBool aFieldChange, 
   741 											 TRequestStatus& aStatus )
  1006 											 TRequestStatus& aStatus )
   742 	{
  1007 	{
   743 	_DBG_FILE("CNSmlContactsDataStore::DoReplaceItemL(): begin");
  1008 	_DBG_FILE("CNSmlContactsDataStore::DoReplaceItemL(): begin");
   744 
  1009 
   747 	
  1012 	
   748 	TInt err = KErrNone;
  1013 	TInt err = KErrNone;
   749 			
  1014 			
   750 	iUid      = aUid;
  1015 	iUid      = aUid;
   751 	iItemSize = aSize;
  1016 	iItemSize = aSize;
   752 	iModType  = aFieldChange ? ENSmlCntItemFieldLevelReplace : ENSmlCntItemReplace;
  1017 	iModType  =  aFieldChange ? ENSmlCntItemFieldLevelReplace : ENSmlCntItemReplace;
   753 	
  1018 	
   754 	if ( iItemPos == -1 || !iBatchMode )
  1019 	if ( iItemPos == -1 || !iBatchMode )
   755 		{
  1020 		{
   756 		iItemPos = 0;
  1021 		iItemPos = 0;
   757 		}
  1022 		}
   758 	
  1023 	
   759 	if ( iItemData )
  1024 	if ( iItemData )
   760 		{
  1025 		{
   761 		iItemData->Reset();
  1026 		iItemData->Reset();
   762 		}
  1027 		}
   763 		
  1028 
   764 	iFieldLevelReplace = aFieldChange;
  1029 	iFieldLevelReplace = aFieldChange;
   765 	
  1030 
   766 	iItemData = AddBufferListL( aUid, aSize, err );
  1031 	iItemData = AddBufferListL( aUid, aSize, err );
   767 	
  1032 	
   768 	if( iBatchMode )
  1033 	if( iBatchMode )
   769 		{
  1034 		{
   770 		iState = ENSmlItemUpdating;
  1035 		iState = ENSmlItemUpdating;
   771 		_DBG_FILE("CNSmlContactsDataStore::DoReplaceItemL(): BATCHMODE end");
  1036 		_DBG_FILE("CNSmlContactsDataStore::DoReplaceItemL(): BATCHMODE end");
   772 		User::RequestComplete( iCallerStatus, KErrNone );
  1037 		User::RequestComplete( iCallerStatus, KErrNone );
   773 		return;
  1038 		return;
   774 		}
  1039 		}
   775 	
       
   776 	CBufBase* tempData = CBufFlat::NewL( KNSmlItemDataExpandSize );
       
   777 	CleanupStack::PushL( tempData );
       
   778 
       
   779 	_DBG_FILE("CNSmlContactsDataStore::DoReplaceItemL(): CContactDatabase::ExportSelectedContactsL() begin");
       
   780 	
       
   781 	TInt ret = KErrNone;
       
   782 	TRAP( err, ret = ExportContactsL( TUid::Uid(aUid), *tempData ) );
       
   783 	
       
   784 	_DBG_FILE("CNSmlContactsDataStore::DoReplaceItemL(): CContactDatabase::ExportSelectedContactsL() end");
       
   785 	CleanupStack::PopAndDestroy( tempData );
       
   786 	
       
   787 	if ( err || ret )
       
   788 	    {
       
   789 	    _DBG_FILE("CNSmlContactsDataStore::DoReplaceItemL(): CContactDatabase::ExportSelectedContactsL() ERROR");
       
   790 	    if ( err == KErrNone )
       
   791 	        {
       
   792             err = ret;
       
   793 	        }
       
   794 	    User::RequestComplete( iCallerStatus, err );
       
   795 		return;
       
   796 	    }
       
   797 	
       
   798 	if( !err && DriveBelowCriticalLevelL( aSize ) ) // Is drive getting full?
       
   799 		{
       
   800 		err = KErrDiskFull;
       
   801 		}
       
   802 	
       
   803 	if ( err == KErrNone )
  1040 	if ( err == KErrNone )
   804 	    {
  1041 	    {
   805 	    iState = ENSmlItemUpdating;
  1042 	    iState = ENSmlItemUpdating;
   806 	    }
  1043 	    }
   807 	
  1044 	
   814 // void CNSmlContactsDataStore::DoReadItemL()
  1051 // void CNSmlContactsDataStore::DoReadItemL()
   815 // ----------------------------------------------------------------------------
  1052 // ----------------------------------------------------------------------------
   816 EXPORT_C void CNSmlContactsDataStore::DoReadItemL( TDes8& aBuffer )
  1053 EXPORT_C void CNSmlContactsDataStore::DoReadItemL( TDes8& aBuffer )
   817 	{
  1054 	{
   818 	_DBG_FILE("CNSmlContactsDataStore::DoReadItemL(): begin");
  1055 	_DBG_FILE("CNSmlContactsDataStore::DoReadItemL(): begin");
       
  1056 
   819     if ( iState != ENSmlItemOpen || !iItemData )
  1057     if ( iState != ENSmlItemOpen || !iItemData )
   820         {
  1058         {
   821         iItemPos = -1;
  1059         iItemPos = -1;
   822         User::Leave( KErrNotReady );
  1060         User::Leave( KErrNotReady );
   823         }
  1061         }
   834         }
  1072         }
   835     else
  1073     else
   836         {
  1074         {
   837         iItemData->Read( iItemPos, aBuffer, iItemData->Size() - iItemPos );
  1075         iItemData->Read( iItemPos, aBuffer, iItemData->Size() - iItemPos );
   838         iItemPos = -1;
  1076         iItemPos = -1;
   839         }	
  1077         }
       
  1078 
   840 	_DBG_FILE("CNSmlContactsDataStore::DoReadItemL(): end");
  1079 	_DBG_FILE("CNSmlContactsDataStore::DoReadItemL(): end");
   841 	}
  1080 	}
   842 
  1081 
   843 // ----------------------------------------------------------------------------
  1082 // ----------------------------------------------------------------------------
   844 // void CNSmlContactsDataStore::DoWriteItemL()
  1083 // void CNSmlContactsDataStore::DoWriteItemL()
   918       	    {
  1157       	    {
   919             iContactsBufferItemList[iContactsBufferItemList.Count()-1]->iStatus  = KNSmlNoError; 
  1158             iContactsBufferItemList[iContactsBufferItemList.Count()-1]->iStatus  = KNSmlNoError; 
   920       	    }
  1159       	    }
   921 	    }
  1160 	    }
   922 	 else if ((iModType   == ENSmlCntItemAdd ||         // If some write problems
  1161 	 else if ((iModType   == ENSmlCntItemAdd ||         // If some write problems
   923       	       iModType   == ENSmlCntItemReplace ||
  1162          iModType   == ENSmlCntItemReplace ||
   924       	       iModType   == ENSmlCntItemFieldLevelReplace ) && 
  1163          iModType   == ENSmlCntItemFieldLevelReplace ) &&  
   925       	       iStateItem != KErrNone &&
  1164       	 iStateItem != KErrNone &&
   926       	       iContactsBufferItemList.Count() > 0) 
  1165       	 iContactsBufferItemList.Count() > 0) 
   927 	    {
  1166 	    {
   928         iContactsBufferItemList[iContactsBufferItemList.Count()-1]->iStatus  = iStateItem; 
  1167         iContactsBufferItemList[iContactsBufferItemList.Count()-1]->iStatus  = iStateItem; 
   929 	    }
  1168 	    }
   930 	
  1169 
   931 	if( iBatchMode )
  1170 	if( iBatchMode )
   932 		{
  1171 		{
   933 		iItemData = NULL;
  1172 		iItemData = NULL;
   934 		iState = ENSmlOpenAndWaiting;
  1173 		iState = ENSmlOpenAndWaiting;
   935 		User::RequestComplete( iCallerStatus, KErrNone );
  1174 		User::RequestComplete( iCallerStatus, KErrNone );
   936 		return;
  1175 		return;
   937 		}
  1176 		}
   938 		
  1177 		
   939 	RArray<TInt> noResultArray;
  1178 
   940 	CleanupClosePushL( noResultArray );
  1179 	iIndex = 0;
   941 	
  1180 	ExecuteBufferL();
   942 	TRAPD(err, ExecuteBufferL( noResultArray ));
       
   943 	if( noResultArray.Count() == 1 )	// Should be a single item commit
       
   944 		{
       
   945 		err = noResultArray[0];
       
   946 		}
       
   947 	CleanupStack::PopAndDestroy(); // noResultArray
       
   948 	
       
   949 	//
       
   950 	// Empty Buffer if not batch mode
       
   951 	//	
       
   952 	if ( !iBatchMode )
       
   953 		{
       
   954 		if(iItemData)
       
   955 		    {
       
   956 		    iItemData->Reset();    
       
   957 		    }
       
   958 		    
       
   959 		if(iItemDataAddBatch)
       
   960 		    {
       
   961 		    iItemDataAddBatch->Reset();    
       
   962 		    }
       
   963 		
       
   964 		iContactsBufferItemList.ResetAndDestroy();
       
   965 		}
       
   966 	
  1181 	
   967 	iItemData = NULL;
  1182 	iItemData = NULL;
   968 	iState = ENSmlOpenAndWaiting;
  1183 	iState = ENSmlOpenAndWaiting;
   969 	
       
   970 	User::RequestComplete( iCallerStatus, err );
       
   971 	
  1184 	
   972 	_DBG_FILE("CNSmlContactsDataStore::DoCommitItemL(): end");
  1185 	_DBG_FILE("CNSmlContactsDataStore::DoCommitItemL(): end");
   973 	}
  1186 	}
   974 
  1187 
   975 // ----------------------------------------------------------------------------
  1188 // ----------------------------------------------------------------------------
  1020 // void CNSmlContactsDataStore::DoDeleteItemL()
  1233 // void CNSmlContactsDataStore::DoDeleteItemL()
  1021 // ----------------------------------------------------------------------------
  1234 // ----------------------------------------------------------------------------
  1022 EXPORT_C void CNSmlContactsDataStore::DoDeleteItemL( TSmlDbItemUid aUid, TRequestStatus& aStatus )
  1235 EXPORT_C void CNSmlContactsDataStore::DoDeleteItemL( TSmlDbItemUid aUid, TRequestStatus& aStatus )
  1023 	{
  1236 	{
  1024 	_DBG_FILE("CNSmlContactsDataStore::DoDeleteItemL(): begin");
  1237 	_DBG_FILE("CNSmlContactsDataStore::DoDeleteItemL(): begin");
  1025 	TInt err = KErrNone;
       
  1026 	iCallerStatus = &aStatus;
  1238 	iCallerStatus = &aStatus;
  1027 	*iCallerStatus = KRequestPending;
  1239 	*iCallerStatus = KRequestPending;
  1028 		
  1240 		
  1029 	if( iBatchMode )
  1241 	if( iBatchMode )
  1030 		{
  1242 		{
  1031 		if( iModType != ENSmlCntItemSoftDelete ) // Treated like deletes
  1243 		if(iContactViewBase ==NULL)
  1032 			{
  1244 		{
  1033 			iModType  = ENSmlCntItemDelete;	
  1245 		CreateViewL();	
  1034 			}
  1246 		}
       
  1247 	if( iModType != ENSmlCntItemSoftDelete ) // Treated like deletes
       
  1248 		{
       
  1249 		iModType  = ENSmlCntItemDelete;	
       
  1250 		}
  1035 			
  1251 			
  1036 		TInt fSize = 0;
  1252 	TInt fSize = 0;
  1037 		AddBufferListL( aUid, fSize, KErrNone );
  1253 	AddBufferListL( aUid, fSize, KErrNone );
  1038 		User::RequestComplete( iCallerStatus, KErrNone );
  1254 	User::RequestComplete( iCallerStatus, KErrNone );
  1039 		return;
  1255 	return;
  1040 		}
  1256 	}
  1041 
  1257 	iUid = aUid;
  1042 	if( iContactsDataStoreExtension->DeleteContactL( TUid::Uid(aUid) ) )
  1258 	
  1043 		{
  1259 	//add the contactlink to CVPbkContactLinkArray
  1044         iChangeFinder->ItemDeleted( TNSmlSnapshotItem( aUid ) );
  1260 	MVPbkContactLink* contactLink  = iIdConverter->IdentifierToLinkLC(aUid);
  1045 		}
  1261 	CleanupStack::Pop();
  1046 	else
  1262 	CVPbkContactLinkArray* contactLnks = CVPbkContactLinkArray::NewLC();
  1047 	    {
  1263 	contactLnks->AppendL( contactLink );
  1048         err = KErrGeneral;
  1264 	
  1049 	    }
  1265  	iContactManager->DeleteContactsL( *contactLnks, *this );
  1050 		
  1266 	CleanupStack::PopAndDestroy(); 
  1051 	User::RequestComplete( iCallerStatus, err );
  1267 	iLastOperation = ENSmlDeleteOp;
  1052 	
  1268 	
  1053 	_DBG_FILE("CNSmlContactsDataStore::DoDeleteItemL(): end");
  1269 	_DBG_FILE("CNSmlContactsDataStore::DoDeleteItemL(): end");
  1054 	}
  1270 	}
  1055 
  1271 
  1056 // ----------------------------------------------------------------------------
  1272 // ----------------------------------------------------------------------------
  1069 // void CNSmlContactsDataStore::DoDeleteAllItemsL()
  1285 // void CNSmlContactsDataStore::DoDeleteAllItemsL()
  1070 // ----------------------------------------------------------------------------
  1286 // ----------------------------------------------------------------------------
  1071 EXPORT_C void CNSmlContactsDataStore::DoDeleteAllItemsL( TRequestStatus& aStatus )
  1287 EXPORT_C void CNSmlContactsDataStore::DoDeleteAllItemsL( TRequestStatus& aStatus )
  1072 	{
  1288 	{
  1073 	_DBG_FILE("CNSmlContactsDataStore::DoDeleteAllItemsL(): begin");
  1289 	_DBG_FILE("CNSmlContactsDataStore::DoDeleteAllItemsL(): begin");
       
  1290 	
  1074 	iCallerStatus = &aStatus;
  1291 	iCallerStatus = &aStatus;
  1075 	*iCallerStatus = KRequestPending;
  1292 	*iCallerStatus = KRequestPending;
  1076 
  1293 	
  1077 	if( iContactsDataStoreExtension->DeleteAllContactsL() )
  1294 	//create view to get all the contactlinks to delete
  1078 	    {
  1295 	if(iContactViewBase ==NULL)
  1079         if( iChangeFinder )
  1296 		{
  1080             {
  1297 		CreateViewL();	
  1081             iChangeFinder->ResetL();		
  1298 		}
  1082             }
  1299 		
  1083         iSnapshotRegistered = EFalse;
  1300 	iLastOperation = ENSMLDeleteAllOp;
  1084         User::RequestComplete( iCallerStatus, KErrNone );
       
  1085 	    }
       
  1086 	else
       
  1087 	    {
       
  1088          DBG_ARGS(_S("CNSmlContactsDataStore::DoDeleteAllItemsL(): Error"));
       
  1089          User::RequestComplete( iCallerStatus, KErrGeneral );
       
  1090 	    }
       
  1091 	
  1301 	
  1092 	_DBG_FILE("CNSmlContactsDataStore::DoDeleteAllItemsL(): end");
  1302 	_DBG_FILE("CNSmlContactsDataStore::DoDeleteAllItemsL(): end");
  1093 	}
  1303 	}
  1094 
  1304 
  1095 // ----------------------------------------------------------------------------
  1305 // ----------------------------------------------------------------------------
  1256 	{
  1466 	{
  1257 	_DBG_FILE("CNSmlContactsDataStore::DoResetChangeInfoL(): begin");
  1467 	_DBG_FILE("CNSmlContactsDataStore::DoResetChangeInfoL(): begin");
  1258 	iCallerStatus = &aStatus;
  1468 	iCallerStatus = &aStatus;
  1259 	*iCallerStatus = KRequestPending;
  1469 	*iCallerStatus = KRequestPending;
  1260 	
  1470 	
       
  1471 	if( iChangeFinder )
       
  1472 		{
       
  1473 		iChangeFinder->ResetL();
       
  1474 		}
       
  1475 		
  1261 	iSnapshotRegistered = EFalse;
  1476 	iSnapshotRegistered = EFalse;
  1262 	
  1477 	
  1263 	if( iChangeFinder )
  1478 		if ( iContactsModsFetcher )
  1264 		{
  1479 		{
  1265 		iChangeFinder->ResetL();
  1480 		iContactsModsFetcher->FetchModificationsL( aStatus );
  1266 		FetchModificationsL();
  1481 #ifdef __NSML_MODULETEST__
  1267 		}
  1482 		CActiveScheduler::Start();
  1268 	
  1483 #endif
  1269     User::RequestComplete( iCallerStatus, KErrNone );
  1484 		}
       
  1485 	else
       
  1486 		{
       
  1487 		User::RequestComplete( iCallerStatus, KErrNone );
       
  1488 		}
  1270 	
  1489 	
  1271 	_DBG_FILE("CNSmlContactsDataStore::DoResetChangeInfoL(): end");
  1490 	_DBG_FILE("CNSmlContactsDataStore::DoResetChangeInfoL(): end");
  1272 	}
  1491 	}
  1273 
  1492 
  1274 // ----------------------------------------------------------------------------
  1493 // ----------------------------------------------------------------------------
  1319 // ----------------------------------------------------------------------------
  1538 // ----------------------------------------------------------------------------
  1320 EXPORT_C CDesCArray* CNSmlContactsDataStore::DoListStoresLC()
  1539 EXPORT_C CDesCArray* CNSmlContactsDataStore::DoListStoresLC()
  1321 	{
  1540 	{
  1322 	_DBG_FILE("CNSmlContactsDataStore::DoListStoresLC(): begin");
  1541 	_DBG_FILE("CNSmlContactsDataStore::DoListStoresLC(): begin");
  1323 	
  1542 	
  1324 	 /*CDesCArray* cntStores = new (ELeave) CDesCArrayFlat(1);
  1543 	MVPbkContactStoreList& storeList = iContactManager->ContactStoresL();
  1325 	 iContactsDataStoreExtension->ListStoresL( cntStores );
  1544 	CDesCArrayFlat* stores = new (ELeave) CDesCArrayFlat(storeList.Count());
  1326      CleanupStack::PushL( cntStores );*/
  1545 	CleanupStack::PushL( stores );
  1327 	
  1546 	
  1328 	 CDesCArray* array = new (ELeave) CDesCArrayFlat(1);
  1547   TBuf<KVPbhkPrefixLength> phoneDataBasePrefix(KVPbhkSymbianDBPrefix);
  1329 	 array->AppendL(*iDefaultStoreName);
  1548 	
  1330 	 CleanupStack::PushL( array );
  1549 	for(TInt i=0 ; i< storeList.Count(); i++)
  1331      
  1550 	{
  1332      _DBG_FILE("CNSmlContactsDataStore::DoListStoresLC(): end");
  1551 	    TBuf<KNSmlDefaultStoreNameMaxSize>  currentStore(storeList.At(i).StoreProperties().Uri().UriDes());	    	   
  1333      
  1552 	    
  1334      return array;
  1553 	    if(currentStore.Find(phoneDataBasePrefix) == 0)//Check if Symbian Database
       
  1554 	    {	    
       
  1555 	    		//For backward compatability with local and remote servers
       
  1556         	    //The Symbian Db name remains unchanged	   
       
  1557         	    //The engine methods that create syncml packet, use this information in Database fields
       
  1558 	            stores->AppendL(*iPacketStoreName);
       
  1559 	    }
       
  1560 	    else//End Symbian Database check
       
  1561 	    {
       
  1562 		     stores->AppendL(storeList.At(i).StoreProperties().Uri().UriDes());
       
  1563 	    }
       
  1564 	}
       
  1565 	
       
  1566 	_DBG_FILE("CNSmlContactsDataStore::DoListStoresLC(): end");
       
  1567 
       
  1568 	return stores;
  1335 	}
  1569 	}
  1336 	
  1570 	
  1337 // ------------------------------------------------------------------------------------------------
  1571 // ------------------------------------------------------------------------------------------------
  1338 // TInt CNSmlContactsDataStore::LdoFetchItemL( TSmlDbItemUid& aUid, CBufBase& aItem )
  1572 // TInt CNSmlContactsDataStore::LdoFetchItemL( TSmlDbItemUid& aUid, CBufBase& aItem )
  1339 // ------------------------------------------------------------------------------------------------
  1573 // ------------------------------------------------------------------------------------------------
  1340 EXPORT_C TInt CNSmlContactsDataStore::LdoFetchItemL( TSmlDbItemUid& aUid, CBufBase& aItem )
  1574 EXPORT_C void CNSmlContactsDataStore::LdoFetchItemL( TSmlDbItemUid& aUid, CBufBase& aItem )
  1341 	{
  1575 	{
  1342 	_DBG_FILE("CNSmlContactsDataStore::LdoFetchItemL(): begin");
  1576 	_DBG_FILE("CNSmlContactsDataStore::LdoFetchItemL(): begin");
  1343 	
  1577 	
  1344 	_DBG_FILE("CNSmlContactsDataStore::FetchItemL(): CContactDatabase::ExportSelectedContactsL() begin");
  1578 	 iWriteStream.Open(aItem);
  1345 	
  1579 		
  1346 	TInt err = KErrNone;	
  1580 	 MVPbkContactLink* contactLink  = iIdConverter->IdentifierToLinkLC(aUid);
  1347 	TInt ret = KErrNone;
  1581 	 iVCardEngine->ExportVCardForSyncL(iWriteStream,*contactLink,*this);
  1348 	
  1582 	 CleanupStack::PopAndDestroy();
  1349 	TRAP( err, ret = ExportContactsL( TUid::Uid(aUid), aItem ) );
  1583 	 
  1350 	
  1584 	 iLastOperation = ENSMLFetchOp;
  1351 	if( ret != KErrNone )
       
  1352 	    {
       
  1353         DBG_ARGS(_S("CNSmlContactsDataStore::FetchItemL(): Error = %d"), ret);
       
  1354         err = ret;
       
  1355 	    }
       
  1356 	
       
  1357 	_DBG_FILE("CNSmlContactsDataStore::FetchItemL(): CContactDatabase::ExportSelectedContactsL() end");
       
  1358 	
  1585 	
  1359 	_DBG_FILE("CNSmlContactsDataStore::LdoFetchItemL(): end");
  1586 	_DBG_FILE("CNSmlContactsDataStore::LdoFetchItemL(): end");
  1360 	return err;
  1587 
  1361 	}
  1588 	}
  1362 
  1589 
  1363 // ------------------------------------------------------------------------------------------------
  1590 // ------------------------------------------------------------------------------------------------
  1364 // TInt CNSmlContactsDataStore::LdoAddItemL( TSmlDbItemUid& aUid,
  1591 // TInt CNSmlContactsDataStore::LdoAddItemL( TSmlDbItemUid& aUid,
  1365 //                                           const TDesC8& aItem,
  1592 //                                           const TDesC8& aItem,
  1366 //                                           TInt aSize,
  1593 //                                           TInt aSize,
  1367 //                                           TTime& aLastModified )
  1594 //                                           TTime& aLastModified,
       
  1595 //                                           TBool& aConfidential )
  1368 // ------------------------------------------------------------------------------------------------
  1596 // ------------------------------------------------------------------------------------------------
  1369 EXPORT_C TInt CNSmlContactsDataStore::LdoAddItemL( TSmlDbItemUid& aUid,
  1597 EXPORT_C void CNSmlContactsDataStore::LdoAddItemL( const TDesC8& aItem,
  1370                                                    const TDesC8& aItem,
  1598                                           TInt aSize)
  1371                                                    TInt aSize,
       
  1372                                                    TTime& aLastModified )
       
  1373 	{
  1599 	{
  1374 	_DBG_FILE("CNSmlContactsDataStore::LdoAddItemL(): begin");
  1600 	_DBG_FILE("CNSmlContactsDataStore::LdoAddItemL(): begin");
  1375 	
  1601 
  1376 	TInt ret( KErrNone );
       
  1377 	
       
  1378 	if( !iOpened )
  1602 	if( !iOpened )
  1379 		{
  1603 		{
  1380 		User::Leave( KErrNotReady );
  1604 		User::Leave( KErrNotReady );
  1381 		}
  1605 		}
  1382 		
  1606 		
  1383 	// Is there enough space on drive ?
  1607 	// Is there enough space on drive 
  1384 	if( DriveBelowCriticalLevelL( aSize ) )
  1608 	if( DriveBelowCriticalLevelL( aSize ) )
  1385 		{
  1609 		{
  1386 		return KErrDiskFull;
  1610 		User::Leave( KErrDiskFull );
  1387 		}
  1611 		}
  1388 
  1612 
  1389 	HBufC8* buf = aItem.AllocLC();
  1613 	 iBuf = aItem.AllocLC();
  1390 	StripPropertyL( buf, KVersitTokenUID() );
  1614 	StripPropertyL( iBuf, KVersitTokenUID() );
  1391 
  1615 
  1392 	CArrayFixFlat<TUid>* entryArray = NULL;
  1616 	 iReadStream.Open( *iBuf );
  1393 	TRAP( ret, entryArray = ImportContactsL( *buf ) );
  1617 	// import a vcard without checking for duplicates
  1394 
  1618 	iVCardEngine->ImportVCardForSyncL(*iStore,iReadStream,*this);
  1395     DBG_ARGS(_S("New item to database with return value: '%d'"), ret );
  1619 	iLastOperation = ENSmlAddOp;
  1396 
  1620 	CleanupStack::Pop(iBuf);
  1397 	if( ret == KErrNone && entryArray != NULL )
       
  1398 		{
       
  1399 		CleanupStack::PushL( entryArray );
       
  1400 		if( entryArray->Count() )
       
  1401 			{
       
  1402             TInt err(KErrNone);
       
  1403 			TUid tId = entryArray->At( 0 );	
       
  1404 			CNsmlContactItem* contactitem = CNsmlContactItem::NewL();
       
  1405 			CleanupStack::PushL( contactitem );
       
  1406 			err = iContactsDataStoreExtension->ReadContactL( tId, contactitem );
       
  1407 			if( err == KErrNone)
       
  1408 			    {
       
  1409                 aLastModified = contactitem->iLastModified;
       
  1410                 aUid = tId.iUid;
       
  1411 			    }			
       
  1412 			CleanupStack::PopAndDestroy( contactitem );
       
  1413 			}
       
  1414 		else
       
  1415 			{
       
  1416 			ret = KErrNotSupported;
       
  1417 			}
       
  1418 		CleanupStack::PopAndDestroy( entryArray );
       
  1419 		CleanupStack::PopAndDestroy( buf );
       
  1420 		} 
       
  1421 	else
       
  1422 		{
       
  1423 		ret = KErrNotSupported;
       
  1424         CleanupStack::PopAndDestroy( buf );
       
  1425 		}
       
  1426 
  1621 
  1427 	_DBG_FILE("CNSmlContactsDataStore::LdoAddItemL(): end");
  1622 	_DBG_FILE("CNSmlContactsDataStore::LdoAddItemL(): end");
  1428 	return ret;
  1623 	
  1429 	}
  1624 	}
  1430 
       
  1431 // ------------------------------------------------------------------------------------------------
  1625 // ------------------------------------------------------------------------------------------------
  1432 // TInt CNSmlContactsDataStore::LdoAddItemsL( RArray<TInt>& aUids,CBufBase*& aItems,
  1626 // TInt CNSmlContactsDataStore::LdoAddItemsL( CBufBase*& aItems,
  1433 //                                            TInt aSize, TTime& aLastModified )
  1627 //                                           TInt aSize)
  1434 // ------------------------------------------------------------------------------------------------
  1628 // ------------------------------------------------------------------------------------------------
  1435 EXPORT_C TInt CNSmlContactsDataStore::LdoAddItemsL( RArray<TInt>& aUids,
  1629 EXPORT_C void CNSmlContactsDataStore::LdoAddItemsL( CBufBase*& aItems,
  1436                                                     CBufBase*& aItems,
  1630                                           TInt aSize)
  1437                                                     TInt aSize,
  1631 	{
  1438                                                     TTime& aLastModified  )
  1632 	_DBG_FILE("CNSmlContactsDataStore::LdoAddItemL(): begin");
  1439 	{
  1633 
  1440 	_DBG_FILE("CNSmlContactsDataStore::LdoAddItemsL(): begin");
       
  1441 	
       
  1442 	TInt ret( KErrNone );
       
  1443 	
       
  1444 	if( !iOpened )
  1634 	if( !iOpened )
  1445 		{
  1635 		{
  1446 		User::Leave( KErrNotReady );
  1636 		User::Leave( KErrNotReady );
  1447 		}
  1637 		}
  1448 		
  1638 		
  1449 	// Is there enough space on drive ?
  1639 	// Is there enough space on drive 
  1450 	if( DriveBelowCriticalLevelL( aSize ) )
  1640 	if( DriveBelowCriticalLevelL( aSize ) )
  1451 		{
  1641 		{
  1452 		return KErrDiskFull;
  1642 		User::Leave( KErrDiskFull );
  1453 		}
  1643 		}
  1454 
  1644 
  1455 	CArrayFixFlat<TUid>* entryArray = NULL;
  1645 	iReadStream.Open( aItems->Ptr(0) );
  1456     TRAP( ret, entryArray = ImportContactsL( aItems->Ptr( 0 ) ) );
  1646 	 // import vcards without checking for duplicates
  1457 	
  1647 	iVCardEngine->ImportVCardForSyncL(*iStore,iReadStream,*this);
  1458     DBG_ARGS(_S("New items to database with return value: '%d'"), ret );
  1648 	iLastOperation = ENSmlAddOp;
  1459 
  1649 	_DBG_FILE("CNSmlContactsDataStore::LdoAddItemL(): end");
  1460 	if( ret == KErrNone && entryArray != NULL)
  1650 	
  1461 		{
  1651 	}
  1462 		CleanupStack::PushL( entryArray );
  1652 		
  1463 		if( entryArray->Count() )
       
  1464 			{
       
  1465 			TInt i = 0;
       
  1466 			while( i < entryArray->Count() )
       
  1467 				{
       
  1468 				TUid tId = entryArray->At(i);
       
  1469 				if (i == (entryArray->Count()-1) ) // Get last Modified date
       
  1470 					{
       
  1471                     TInt err(KErrNone);
       
  1472                     CNsmlContactItem* contactitem = CNsmlContactItem::NewL();
       
  1473                     CleanupStack::PushL( contactitem );
       
  1474                     err = iContactsDataStoreExtension->ReadContactL( tId, contactitem );    
       
  1475 				    if( err == KErrNone )
       
  1476                         {
       
  1477                         aLastModified = contactitem->iLastModified;
       
  1478                         }
       
  1479 					CleanupStack::PopAndDestroy( contactitem );
       
  1480 					}
       
  1481 				aUids.Append( tId.iUid );
       
  1482 				i++;
       
  1483 				}
       
  1484 			}
       
  1485 		else
       
  1486 			{
       
  1487 			ret = KErrNotSupported;
       
  1488 			}
       
  1489 		CleanupStack::PopAndDestroy( entryArray );
       
  1490 		} 
       
  1491 	else
       
  1492 		{
       
  1493 	    ret = KErrNotSupported;
       
  1494 		}
       
  1495 
       
  1496 	_DBG_FILE("CNSmlContactsDataStore::LdoAddItemsL(): end");
       
  1497 	return ret;
       
  1498 	}
       
  1499 	
       
  1500 // ------------------------------------------------------------------------------------------------
  1653 // ------------------------------------------------------------------------------------------------
  1501 // TInt CNSmlContactsDataStore::LdoUpdateItemL(TSmlDbItemUid aUid,
  1654 // TInt CNSmlContactsDataStore::LdoUpdateItemL(TSmlDbItemUid aUid,
  1502 //                                             const TDesC8& aItem,
  1655 //                                             const TDesC8& aItem,
  1503 //                                             TInt aSize,
  1656 //                                             TInt aSize,
  1504 //                                             TTime& aLastModified )
  1657 //                                             TTime& aLastModified )
  1505 // ------------------------------------------------------------------------------------------------
  1658 // ------------------------------------------------------------------------------------------------
  1506 EXPORT_C TInt CNSmlContactsDataStore::LdoUpdateItemL( TSmlDbItemUid aUid,
  1659 EXPORT_C void CNSmlContactsDataStore::LdoUpdateItemL()
  1507                                              const TDesC8& aItem,
       
  1508                                              TInt aSize,
       
  1509                                              TTime& aLastModified )
       
  1510 	{
  1660 	{
  1511 	_DBG_FILE("CNSmlContactsDataStore::LdoUpdateItemL(): begin");
  1661 	_DBG_FILE("CNSmlContactsDataStore::LdoUpdateItemL(): begin");
  1512 	TInt ret(KErrNone);
  1662 	
  1513 		
  1663 	iBuf = iContactsBufferItemList[iIndex]->iItemData->Ptr(0).AllocLC();
  1514 	// Is there enough space on drive ?
  1664 	// Remove UID's from data
  1515 	if( DriveBelowCriticalLevelL( aSize) )
  1665 	StripPropertyL( iBuf, KVersitTokenUID() );
  1516 		{
  1666 
  1517 		return KErrDiskFull;
  1667 	iReadStream.Open( *iBuf );
  1518 		}
  1668 	iContactLink = iIdConverter->IdentifierToLinkLC(iUid);
  1519 	
  1669 	iVCardEngine->ImportVCardMergeL(*iContactLink, *iStore, iReadStream, *this);
  1520 	CNsmlContactItem* contactitem = CNsmlContactItem::NewL();
  1670 
  1521 	CleanupStack::PushL( contactitem );
  1671 	CleanupStack::Pop();
  1522     ret = iContactsDataStoreExtension->ReadContactL( TUid::Uid( aUid ), contactitem );
  1672 	iLastOperation = ENSMLUpdateImportOp;
  1523     
  1673 	CleanupStack::Pop(iBuf);
  1524     if( ret != KErrNone )
  1674 	
  1525         {
       
  1526         CleanupStack::PopAndDestroy( contactitem );
       
  1527         DBG_ARGS(_S("CNSmlContactsDataStore::LdoUpdateItemL(): Error = %d"), ret);
       
  1528         return ret;
       
  1529         }
       
  1530     
       
  1531     // Set mimetype + version
       
  1532 	iDataMod->SetUsedMimeType( iServerMimeType, iServerMimeVersion );
       
  1533 
       
  1534 	DBG_DUMP((void*)aItem.Ptr(), aItem.Length(),
       
  1535 	        _S8("CNSmlContactsDataStore::LdoUpdateItemL(): ReceivedItem:"));    
       
  1536 	
       
  1537 	CBufBase* bItem = LdoMergeLC( aUid, aItem );
       
  1538 	HBufC8* hItemBuf = HBufC8::NewL( bItem->Size() + 80 );
       
  1539 	*hItemBuf = bItem->Ptr(0);
       
  1540 	CleanupStack::PopAndDestroy(); // bItem
       
  1541 	CleanupStack::PushL( hItemBuf );
       
  1542 	StripPropertyL( hItemBuf, KVersitTokenUID() );
       
  1543 	
       
  1544 	TPtr8 hItemPtr( hItemBuf->Des() );
       
  1545 	HBufC8* endPropBuf = HBufC8::NewLC( KVersitTokenEND().Length() + KVersitTokenCRLF().Length() + KVersitTokenColon().Length() );
       
  1546 	TPtr8 endPropPtr( endPropBuf->Des() );
       
  1547 	endPropPtr.Append( KVersitTokenCRLF() );
       
  1548 	endPropPtr.Append( KVersitTokenEND() ); 
       
  1549 	endPropPtr.Append( KVersitTokenColon() ); 
       
  1550 	TInt endPos = hItemPtr.Find( endPropPtr );
       
  1551 	if( endPos == KErrNotFound )
       
  1552 		{
       
  1553 		User::Leave( KErrNotSupported );
       
  1554 		}
       
  1555 	hItemPtr.Insert( endPos, KVersitTokenCRLF() );
       
  1556 	endPos += KVersitTokenCRLF().Length();
       
  1557 	hItemPtr.Insert( endPos, KVersitTokenUID() );
       
  1558 	endPos += KVersitTokenUID().Length();
       
  1559 	hItemPtr.Insert( endPos, KVersitTokenColon() );
       
  1560 	endPos += KVersitTokenColon().Length();
       
  1561 	hItemPtr.Insert( endPos, *contactitem->iGuid );
       
  1562 	
       
  1563 	DBG_DUMP((void*)hItemPtr.Ptr(), hItemPtr.Length(),
       
  1564 	            _S8("CNSmlContactsDataStore::LdoUpdateItemL(): MergedItem:"));
       
  1565 	
       
  1566 	CleanupStack::PopAndDestroy( endPropBuf );
       
  1567 
       
  1568 	// TODO: Get it reviewed
       
  1569 	CArrayFixFlat<TUid>* entryArray = NULL;
       
  1570     TRAP( ret, entryArray = ImportContactsL( hItemPtr ) );
       
  1571 	
       
  1572     if( ret == KErrNone && entryArray != NULL)
       
  1573 		{
       
  1574         CleanupStack::PushL( entryArray );
       
  1575 		if( !entryArray->Count() )
       
  1576 			{
       
  1577 			ret = KErrNotSupported;								
       
  1578 			}
       
  1579 		else
       
  1580 			{
       
  1581             TInt err(KErrNone);
       
  1582 			TUid tId = TUid::Uid( aUid );
       
  1583 			CNsmlContactItem* updatedContactitem = CNsmlContactItem::NewL();
       
  1584 			CleanupStack::PushL( updatedContactitem );
       
  1585 			err = iContactsDataStoreExtension->ReadContactL( tId, contactitem );
       
  1586             if( err == KErrNone)
       
  1587                 {
       
  1588                 aLastModified = updatedContactitem->iLastModified;
       
  1589                 }
       
  1590 			CleanupStack::PopAndDestroy( updatedContactitem ); // updatedContact
       
  1591 			}
       
  1592 		CleanupStack::PopAndDestroy(3); // contactitem,  hItemBuf, entryArray
       
  1593 		}
       
  1594 	else
       
  1595 		{
       
  1596 		ret = KErrNotSupported;
       
  1597 		CleanupStack::PopAndDestroy(2); // contactitem,  hItemBuf
       
  1598 		}
       
  1599 	
       
  1600 	iFieldLevelReplace = EFalse;
       
  1601 	_DBG_FILE("CNSmlContactsDataStore::LdoUpdateItemL(): end");
  1675 	_DBG_FILE("CNSmlContactsDataStore::LdoUpdateItemL(): end");
  1602 	return ret;
  1676 
  1603 	}
  1677 	}
  1604 
  1678 
  1605 // ------------------------------------------------------------------------------------------------
       
  1606 //  CBufBase* CNSmlContactsDataStore::LdoMergeLC( TSmlDbItemUid& aUid, const TDesC8& aItem )
       
  1607 // ------------------------------------------------------------------------------------------------
       
  1608 EXPORT_C CBufBase* CNSmlContactsDataStore::LdoMergeLC( TSmlDbItemUid& aUid, const TDesC8& aItem )
       
  1609 	{
       
  1610 	_DBG_FILE("CNSmlContactsDataStore::LdoMergeLC(): begin");
       
  1611 	CBufFlat* b = CBufFlat::NewL(64);
       
  1612 	CleanupStack::PushL( b );
       
  1613 	b->InsertL( 0, aItem );
       
  1614 
       
  1615 	if( iDataMod->NeedsMerge() )
       
  1616 		{
       
  1617 		CBufFlat* mergeItem = CBufFlat::NewL(64);
       
  1618 		CleanupStack::PushL( mergeItem );
       
  1619 		if( LdoFetchItemL( aUid, *mergeItem) == KErrNone )
       
  1620 			{
       
  1621             iDataMod->MergeRxL( *b, *mergeItem, iFieldLevelReplace );
       
  1622 			}
       
  1623 		CleanupStack::PopAndDestroy(); // mergeItem
       
  1624 		}
       
  1625 	
       
  1626 	_DBG_FILE("CNSmlContactsDataStore::LdoMergeLC(): end");
       
  1627 	return b;
       
  1628 	}
       
  1629 
  1679 
  1630 // ------------------------------------------------------------------------------------------------
  1680 // ------------------------------------------------------------------------------------------------
  1631 // CNSmlContactsDataStore::DriveBelowCriticalLevelL()
  1681 // CNSmlContactsDataStore::DriveBelowCriticalLevelL()
  1632 // ------------------------------------------------------------------------------------------------
  1682 // ------------------------------------------------------------------------------------------------
  1633 EXPORT_C TBool CNSmlContactsDataStore::DriveBelowCriticalLevelL( TInt aSize )
  1683 EXPORT_C TBool CNSmlContactsDataStore::DriveBelowCriticalLevelL( TInt aSize )
  1672 	iContactsBufferItemList.AppendL( newBufferItem );
  1722 	iContactsBufferItemList.AppendL( newBufferItem );
  1673 	
  1723 	
  1674 	iMimeTypeItem.Set( KNullDesC8 ); 
  1724 	iMimeTypeItem.Set( KNullDesC8 ); 
  1675 	iMimeVersionItem.Set( KNullDesC8 );
  1725 	iMimeVersionItem.Set( KNullDesC8 );
  1676 	
  1726 	
  1677 	iLastItem = ETrue;
       
  1678 	
  1727 	
  1679 	CleanupStack::Pop();	
  1728 	CleanupStack::Pop();	
  1680 	
  1729 	
  1681 	if( iModType == ENSmlCntItemAdd && 
  1730 	if( iModType == ENSmlCntItemAdd && 
  1682 	    aStatus  == KErrNotSupported )
  1731 	    aStatus  == KErrNotSupported )
  1756 		_DBG_FILE("CNSmlContactsDataStore::StripPropertyL(): property removed");
  1805 		_DBG_FILE("CNSmlContactsDataStore::StripPropertyL(): property removed");
  1757 		}
  1806 		}
  1758 	CleanupStack::PopAndDestroy(); // propBuf
  1807 	CleanupStack::PopAndDestroy(); // propBuf
  1759 	_DBG_FILE("CNSmlContactsDataStore::StripPropertyL(): end");
  1808 	_DBG_FILE("CNSmlContactsDataStore::StripPropertyL(): end");
  1760 	}
  1809 	}
       
  1810 	
       
  1811 // ----------------------------------------------------------------------------
       
  1812 // CNSmlContactsDataStore::IsConfidentialL
       
  1813 // ----------------------------------------------------------------------------
       
  1814 //	
       
  1815 EXPORT_C TBool CNSmlContactsDataStore::IsConfidentialL(  MVPbkStoreContact& aItem )
       
  1816     {
       
  1817     _DBG_FILE("CNSmlContactsDataStore::IsConfidentialL(): begin");
       
  1818     TBool ret( EFalse );
       
  1819     
       
  1820     //find X-CLASS field type
       
  1821 	const MVPbkFieldType* syncclass = 
       
  1822         iContactManager->FieldTypes().Find(R_VPBK_FIELD_TYPE_SYNCCLASS );
       
  1823 	CVPbkBaseContactFieldTypeIterator* itr = 
       
  1824         CVPbkBaseContactFieldTypeIterator::NewLC( *syncclass, 
       
  1825             aItem.Fields() );
       
  1826 	while ( itr->HasNext() )
       
  1827         {
       
  1828         const MVPbkBaseContactField* field = itr->Next();
       
  1829         const MVPbkContactFieldTextData& data = MVPbkContactFieldTextData::Cast(field->FieldData());
       
  1830         TPtrC ptr = data.Text();
       
  1831         //compare the sync type
       
  1832         if ( ptr.Compare( KNSmlContactSyncNoSync ) == 0 )
       
  1833             {
       
  1834             _DBG_FILE("CNSmlContactsDataStore::IsConfidentialL(): \
       
  1835                        find confidential");
       
  1836             ret = ETrue;
       
  1837             }
       
  1838          
       
  1839         }
       
  1840 	CleanupStack::PopAndDestroy( itr );
       
  1841     
       
  1842     _DBG_FILE("CNSmlContactsDataStore::IsConfidentialL(): end");
       
  1843     return ret;
       
  1844     }
       
  1845     
       
  1846  // ----------------------------------------------------------------------------
       
  1847 // CNSmlContactsDataStore::ResetBuffer
       
  1848 // ----------------------------------------------------------------------------
       
  1849 //	
       
  1850 EXPORT_C void CNSmlContactsDataStore:: ResetBuffer()
       
  1851 	{
       
  1852 	if( iItemDataAddBatch )
       
  1853 		{
       
  1854 		iItemDataAddBatch->Reset();
       
  1855 		}
       
  1856 	iContactsBufferItemList.ResetAndDestroy();
       
  1857 	}
       
  1858 // -----------------------------------------------------------------------------
       
  1859 // From MVPbkContactStoreListObserver  
       
  1860 // Called when a contact store is ready to use.
       
  1861 // -----------------------------------------------------------------------------
       
  1862  
       
  1863 EXPORT_C void CNSmlContactsDataStore::StoreReady( MVPbkContactStore& /*aContactStore*/ )
       
  1864 	{
       
  1865 	_DBG_FILE("CNSmlContactsDataStore::StoreReady(): begin");
       
  1866 	//The contact data base opened successfully
       
  1867 	iOpened = ETrue;
       
  1868 	//id to uniquely identify the opened store 
       
  1869 	iOpenedStoreId = DefaultHash::Des16(
       
  1870 						iStore->StoreProperties().Uri().UriDes());
       
  1871 	
       
  1872 	TRAPD(error, iContactsModsFetcher->FetchModificationsL( *iCallerStatus ));
       
  1873 	if(error != KErrNone)
       
  1874 		{
       
  1875 		User::RequestComplete( iCallerStatus, error );
       
  1876 		}
       
  1877 	_DBG_FILE("CNSmlContactsDataStore::StoreReady(): end");
       
  1878 	}
       
  1879 
       
  1880 
       
  1881 // ---------------------------------------------------------------------------
       
  1882 //  From MVPbkContactStoreListObserver  
       
  1883 //	Called when a contact store becomes unavailable.
       
  1884 // ---------------------------------------------------------------------------
       
  1885 //
       
  1886 EXPORT_C void CNSmlContactsDataStore::StoreUnavailable(
       
  1887         MVPbkContactStore& /*aContactStore*/,
       
  1888         TInt aReason )
       
  1889     {
       
  1890     _DBG_FILE("CNSmlContactsDataStore::StoreUnavailable(): begin");
       
  1891 
       
  1892     //Unable open the default contact data base
       
  1893 	iOpened = EFalse;
       
  1894 	delete iStoreName;
       
  1895 	iStoreName = NULL;
       
  1896 	iOpenedStoreId = 0;
       
  1897 	User::RequestComplete( iCallerStatus, aReason );
       
  1898 	_DBG_FILE("CNSmlContactsDataStore::StoreUnavailable(): end");		
       
  1899 	return;
       
  1900 	
       
  1901     }
       
  1902 
       
  1903 // ---------------------------------------------------------------------------
       
  1904 // From MVPbkContactStoreListObserver  
       
  1905 // ---------------------------------------------------------------------------
       
  1906 //
       
  1907 EXPORT_C void CNSmlContactsDataStore::HandleStoreEventL(
       
  1908         MVPbkContactStore& /*aContactStore*/,
       
  1909         TVPbkContactStoreEvent /*aStoreEvent*/ )
       
  1910 	{
       
  1911 	//Indication not needed.
       
  1912 	}
       
  1913     
       
  1914 // ---------------------------------------------------------------------------
       
  1915 //  CNSmlContactsDataStore::VPbkSingleContactOperationComplete
       
  1916 // ---------------------------------------------------------------------------
       
  1917 //
       
  1918 EXPORT_C void CNSmlContactsDataStore::VPbkSingleContactOperationComplete(
       
  1919 		MVPbkContactOperationBase& aOperation,
       
  1920 		MVPbkStoreContact* aContact )
       
  1921 	{
       
  1922 	MVPbkContactOperationBase* operation = &aOperation;
       
  1923 	if ( operation )
       
  1924 	{
       
  1925 		delete operation;
       
  1926 		operation = NULL;
       
  1927 	}
       
  1928 	TRAPD(error ,SingleContactOperationCompleteL(aContact));
       
  1929 	delete aContact;
       
  1930 	if(error!= KErrNone)
       
  1931 		{
       
  1932 			User::RequestComplete( iCallerStatus, error );
       
  1933 		}
       
  1934 	}
       
  1935 
       
  1936 // ---------------------------------------------------------------------------
       
  1937 //  CNSmlContactsDataStore::VPbkSingleContactOperationFailed
       
  1938 // ---------------------------------------------------------------------------
       
  1939 //
       
  1940 EXPORT_C void CNSmlContactsDataStore::VPbkSingleContactOperationFailed(
       
  1941     MVPbkContactOperationBase& aOperation,
       
  1942     TInt aError )
       
  1943     {
       
  1944 	MVPbkContactOperationBase* operation = &aOperation;
       
  1945 	if ( operation )
       
  1946 	    {
       
  1947 		delete operation;
       
  1948 		operation = NULL;
       
  1949 	    }
       
  1950 	
       
  1951 	 if(iLastOperation == ENSMLFetchOp)
       
  1952 		{
       
  1953 	 	iWriteStream.Close();
       
  1954 	 	User::RequestComplete( iCallerStatus, aError );
       
  1955 	 	}
       
  1956 	else if (iLastOperation == ENSMLUpdateExportOp)
       
  1957         {
       
  1958         iWriteStream.Close();
       
  1959         delete iMergeItem;
       
  1960         iMergeItem = NULL;
       
  1961         User::RequestComplete( iCallerStatus, aError );
       
  1962         }
       
  1963 	else if (iLastOperation == ENSMLUpdateImportOp)
       
  1964         {
       
  1965         if(iBatchMode)
       
  1966             {
       
  1967             iResultArray->Append( aError ); 
       
  1968             } 
       
  1969 		delete iContactLink;
       
  1970         iContactLink = NULL;	
       
  1971         delete iBuf;
       
  1972         iBuf = NULL;
       
  1973         iReadStream.Close();
       
  1974         iIndex++;
       
  1975         if(iIndex == iContactsBufferItemList.Count() )
       
  1976             {
       
  1977             ResetBuffer();
       
  1978             User::RequestComplete( iCallerStatus, aError );    
       
  1979             }
       
  1980         else
       
  1981             {
       
  1982             _DBG_FILE("VPbkSingleContactOperationFailed, Continuing to complete the Batch Operation");
       
  1983             ExecuteBufferL();
       
  1984             }
       
  1985         }
       
  1986 	else if(iLastOperation == ENSmlRetrieveOp)
       
  1987 	    {
       
  1988         iRetrieveCount--;	        
       
  1989         if(iBatchMode)
       
  1990             {
       
  1991             iResultArray->AppendL( aError ); 
       
  1992             }        
       
  1993         iIndex++;
       
  1994         if(iIndex == iContactsBufferItemList.Count() )
       
  1995             {
       
  1996             ResetBuffer();
       
  1997             User::RequestComplete( iCallerStatus, aError );    
       
  1998             }
       
  1999         else if(!iRetrieveCount)
       
  2000             {
       
  2001             _DBG_FILE("VPbkSingleContactOperationFailed, Continuing to complete the Batch Operation");
       
  2002             ExecuteBufferL();
       
  2003             }
       
  2004 	    }
       
  2005     else
       
  2006         {
       
  2007         _DBG_FILE("VPbkSingleContactOperationFailed, No Matching LastOperation Completing the Operation");
       
  2008         User::RequestComplete( iCallerStatus, aError );    
       
  2009         }	
       
  2010 	_DBG_FILE("CNSmlContactsDataStore::VPbkSingleContactOperationFailed(): end");
       
  2011 	}
       
  2012  // ---------------------------------------------------------------------------
       
  2013 //  CNSmlContactsDataStore::VPbkSingleContactOperationFailed
       
  2014 // ---------------------------------------------------------------------------
       
  2015 //
       
  2016 EXPORT_C void CNSmlContactsDataStore::SingleContactOperationCompleteL(
       
  2017  		MVPbkStoreContact* aContact)
       
  2018 	{
       
  2019 	if(iLastOperation == ENSMLFetchOp)
       
  2020 		{
       
  2021 		iWriteStream.Close();
       
  2022 
       
  2023 		iItemData->Compress();
       
  2024 		//strip the fields not supported by server
       
  2025 		iDataMod->StripTxL( *iItemData );
       
  2026 		*iSize = iItemData->Size();
       
  2027 		iState = ENSmlItemOpen;
       
  2028 		if ( iServerMaxObjectSize == 0 || *iSize <= iServerMaxObjectSize )
       
  2029 			{
       
  2030 			User::RequestComplete( iCallerStatus, KErrNone );
       
  2031 			}
       
  2032 		else
       
  2033 			{
       
  2034 			User::RequestComplete( iCallerStatus, KErrTooBig );
       
  2035 			}
       
  2036 		}
       
  2037 	else if (iLastOperation == ENSMLUpdateExportOp)
       
  2038 		{
       
  2039 		iDataMod->MergeRxL( *iContactsBufferItemList[iIndex]->iItemData, 
       
  2040 		    *iMergeItem, iFieldLevelReplace );
       
  2041 		iWriteStream.Close();
       
  2042 		delete iMergeItem;
       
  2043 		iMergeItem = NULL;
       
  2044 		LdoUpdateItemL();
       
  2045 		}
       
  2046 	else if (iLastOperation == ENSMLUpdateImportOp)
       
  2047 		{
       
  2048 		iFieldLevelReplace = EFalse;
       
  2049 		
       
  2050 		if(!IsConfidentialL(*aContact))
       
  2051 			{
       
  2052 			TInt32 id = iIdConverter->LinkToIdentifier(*aContact->CreateLinkLC());
       
  2053 			
       
  2054 			TNSmlSnapshotItem item(id);
       
  2055 			
       
  2056 			MVPbkStoreContact2* tempContact = 
       
  2057 					reinterpret_cast<MVPbkStoreContact2*> (aContact->StoreContactExtension (KMVPbkStoreContactExtension2Uid));
       
  2058        		
       
  2059 			MVPbkStoreContactProperties *contactProterties 
       
  2060 										= tempContact ->PropertiesL();;
       
  2061 			CleanupDeletePushL(contactProterties);				
       
  2062 			item.SetLastChangedDate(contactProterties->LastModifiedL());
       
  2063 			CleanupStack::PopAndDestroy();
       
  2064 			
       
  2065 			item.SetSoftDelete( EFalse );
       
  2066 			
       
  2067 			TRAP_IGNORE(iChangeFinder->ItemUpdatedL( item ));
       
  2068 			
       
  2069 			CleanupStack::PopAndDestroy();
       
  2070 			if(iBatchMode)
       
  2071 				{
       
  2072 				iResultArray->Append(KErrNone);	
       
  2073 				}
       
  2074 			
       
  2075 	        delete iContactLink;
       
  2076 	        iContactLink = NULL;
       
  2077 			
       
  2078 			delete iBuf;
       
  2079 			iBuf = NULL;
       
  2080 			iReadStream.Close();
       
  2081 			
       
  2082 			iIndex++;
       
  2083 			if(iIndex == iContactsBufferItemList.Count() )
       
  2084 				{
       
  2085 				ResetBuffer();
       
  2086 				User::RequestComplete( iCallerStatus, iRetCommand );	
       
  2087 				}
       
  2088 			else
       
  2089 				{
       
  2090 					ExecuteBufferL();
       
  2091 				}
       
  2092 			}
       
  2093 		
       
  2094 		}
       
  2095 	else if(iLastOperation == ENSmlRetrieveOp)
       
  2096 		{
       
  2097 		iRetrieveCount--;	
       
  2098 		if(!IsConfidentialL(*aContact))
       
  2099 			{
       
  2100 			TInt32 id = iIdConverter->LinkToIdentifier(*aContact->CreateLinkLC());
       
  2101 			*iContactsBufferItemList[iIndex]->iPUid = id;
       
  2102 			
       
  2103 			TNSmlSnapshotItem item(id);
       
  2104 			
       
  2105        		MVPbkStoreContact2* tempContact = 
       
  2106        		    reinterpret_cast<MVPbkStoreContact2*> (aContact->StoreContactExtension (KMVPbkStoreContactExtension2Uid));  
       
  2107        		       			
       
  2108 			MVPbkStoreContactProperties *contactProterties 
       
  2109 										= tempContact ->PropertiesL();;
       
  2110 			CleanupDeletePushL(contactProterties);				
       
  2111 			item.SetLastChangedDate(contactProterties->LastModifiedL());
       
  2112 			CleanupStack::PopAndDestroy();
       
  2113 			
       
  2114 			item.SetSoftDelete( EFalse );
       
  2115 			// Add the snapshot item to the Change Finder.
       
  2116 			TRAP_IGNORE(iChangeFinder->ItemAddedL( item ));
       
  2117 			
       
  2118 			CleanupStack::PopAndDestroy();
       
  2119 			}
       
  2120 			if(iBatchMode)
       
  2121 			{
       
  2122 			iResultArray->AppendL(iRetCommand);	
       
  2123 			}
       
  2124 			
       
  2125 			iIndex++;
       
  2126 			if(iIndex == iContactsBufferItemList.Count() )
       
  2127 				{
       
  2128 				ResetBuffer();
       
  2129 				User::RequestComplete( iCallerStatus, iRetCommand );	
       
  2130 				}
       
  2131 			else if(!iRetrieveCount)
       
  2132 				{
       
  2133 					ExecuteBufferL();
       
  2134 				}
       
  2135 			
       
  2136 			}
       
  2137  }
       
  2138  // ---------------------------------------------------------------------------
       
  2139 //  CNSmlContactsDataStore::ContactsSaved.
       
  2140 // ---------------------------------------------------------------------------
       
  2141 //
       
  2142 EXPORT_C void CNSmlContactsDataStore::ContactsSaved( 
       
  2143 		MVPbkContactOperationBase& aOperation,
       
  2144         MVPbkContactLinkArray* aResults ) 
       
  2145 	{
       
  2146 	MVPbkContactOperationBase* operation = &aOperation;
       
  2147 	if ( operation )
       
  2148 	{
       
  2149 		delete operation;
       
  2150 		operation = NULL;
       
  2151 	}
       
  2152 
       
  2153 	if(iLastOperation == ENSmlAddOp)
       
  2154 		{
       
  2155 		//Count of contacts to be added from server to client 
       
  2156 		TInt  cntAddCount( 0 );
       
  2157 		TInt i(iIndex);
       
  2158 		while( i<iContactsBufferItemList.Count() )
       
  2159 			{
       
  2160 			if ( iContactsBufferItemList[i]->iModType == ENSmlCntItemAdd &&
       
  2161 			     iContactsBufferItemList[i]->iStatus  == KNSmlNoError )
       
  2162 				{
       
  2163 				cntAddCount++;
       
  2164 				}
       
  2165 			i++;
       
  2166 			}
       
  2167 		
       
  2168 		//Count of contacts successfully added to client	
       
  2169 		iRetrieveCount = aResults->Count();
       
  2170 		
       
  2171 		//Count of contacts which could not be added
       
  2172 		TInt cntAddFailedCount(0);
       
  2173 		cntAddFailedCount = cntAddCount-iRetrieveCount;
       
  2174 		while(cntAddFailedCount > 0)
       
  2175 		{
       
  2176 			iContactsBufferItemList[iIndex++]->iStatus  = KErrGeneral;
       
  2177 			iResultArray->Append(KErrGeneral);
       
  2178 			cntAddFailedCount--;
       
  2179 		}
       
  2180 		
       
  2181 		if ( aResults->Count() > 0 )
       
  2182 			{
       
  2183 			for(TInt i=0;i<aResults->Count();i++)
       
  2184 				{
       
  2185 				//retrieve the contact
       
  2186 				TRAPD(error,iContactManager->RetrieveContactL( aResults->At(i), *this ));
       
  2187 				if(error != KErrNone)
       
  2188 					{
       
  2189 					User::RequestComplete( iCallerStatus, error );	
       
  2190 					}
       
  2191 			iLastOperation = ENSmlRetrieveOp;				 	
       
  2192 				}
       
  2193 			}
       
  2194 		delete iBuf;
       
  2195 		delete aResults;
       
  2196 		iBuf = NULL;
       
  2197 		iReadStream.Close();	
       
  2198 		}
       
  2199 	
       
  2200 	}
       
  2201         
       
  2202  // ---------------------------------------------------------------------------
       
  2203 //  CNSmlContactsDataStore::ContactsSavingFailed
       
  2204 // ---------------------------------------------------------------------------
       
  2205 //       
       
  2206 EXPORT_C void CNSmlContactsDataStore::ContactsSavingFailed( 
       
  2207         MVPbkContactOperationBase& aOperation, TInt aError )
       
  2208 	{
       
  2209 	MVPbkContactOperationBase* operation = &aOperation;
       
  2210 	if ( operation )
       
  2211 	{
       
  2212 		delete operation;
       
  2213 		operation = NULL;
       
  2214 	}
       
  2215 	
       
  2216 	if(iLastOperation == ENSmlAddOp)
       
  2217 	{
       
  2218 	if(iBatchMode)
       
  2219 		{
       
  2220 		while( iIndex < iContactsBufferItemList.Count() &&
       
  2221 		iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemAdd )
       
  2222 			{
       
  2223 			iContactsBufferItemList[iIndex++]->iStatus  = KErrGeneral;
       
  2224 			iResultArray->Append(KErrGeneral);
       
  2225 			}	
       
  2226 		}
       
  2227 	else
       
  2228 		{
       
  2229 		iIndex++;			
       
  2230 		}
       
  2231 
       
  2232 	if(iIndex == iContactsBufferItemList.Count())
       
  2233 		{
       
  2234 		User::RequestComplete( iCallerStatus, aError );
       
  2235 		ResetBuffer();
       
  2236 		delete iBuf;
       
  2237 	 	iBuf = NULL;
       
  2238 	 	iReadStream.Close();	
       
  2239 		}
       
  2240 		//Execute the remaining commands in batch
       
  2241 		else
       
  2242 		{
       
  2243 			TRAPD(error,ExecuteBufferL());
       
  2244 			if(error != KErrNone)
       
  2245 			{
       
  2246 			User::RequestComplete( iCallerStatus, error );	
       
  2247 			}
       
  2248 		}
       
  2249 		}
       
  2250 	}
       
  2251 // ---------------------------------------------------------------------------
       
  2252 //  CNSmlContactsDataStore::StepComplete
       
  2253 // ---------------------------------------------------------------------------
       
  2254 //
       
  2255 EXPORT_C void CNSmlContactsDataStore::StepComplete(
       
  2256 		 MVPbkContactOperationBase& /*aOperation*/, 
       
  2257         TInt aStepSize )
       
  2258 	{
       
  2259 	if(iLastOperation == ENSmlDeleteOp)
       
  2260 		{
       
  2261 		if(iBatchMode)
       
  2262 			{
       
  2263 			for(TInt i = 0 ; i<aStepSize && iIndex < iContactsBufferItemList.Count() ;i++)
       
  2264 				{
       
  2265 				iResultArray->Append(KErrNone);
       
  2266 				if( iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemDelete &&
       
  2267 			    iContactsBufferItemList[iIndex]->iStatus  == KNSmlNoError )
       
  2268 					{
       
  2269 					iContactsBufferItemList[iIndex]->iStatus  = KErrNone;
       
  2270 					iUid  = iContactsBufferItemList[iIndex]->iUid;
       
  2271 					iChangeFinder->ItemDeleted( TNSmlSnapshotItem( iUid ));
       
  2272 					}						
       
  2273 					else if( iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemSoftDelete &&
       
  2274 				   		 iContactsBufferItemList[iIndex]->iStatus  == KNSmlNoError )
       
  2275 					{
       
  2276 					
       
  2277 					iContactsBufferItemList[iIndex]->iStatus  = KErrNone;
       
  2278 					iUid  = iContactsBufferItemList[iIndex]->iUid;
       
  2279 					TRAP_IGNORE(iChangeFinder->ItemSoftDeletedL( TNSmlSnapshotItem( iUid ) ));
       
  2280 					}
       
  2281 					iIndex++;
       
  2282 				}
       
  2283 			}
       
  2284 			else
       
  2285 			{
       
  2286 			iChangeFinder->ItemDeleted( TNSmlSnapshotItem( iUid ) );	
       
  2287 			User::RequestComplete( iCallerStatus, KErrNone );
       
  2288 			}
       
  2289 		}
       
  2290 	}
       
  2291  // ---------------------------------------------------------------------------
       
  2292 //  CNSmlContactsDataStore::StepFailed
       
  2293 // ---------------------------------------------------------------------------
       
  2294 //
       
  2295 EXPORT_C TBool CNSmlContactsDataStore::StepFailed(
       
  2296         MVPbkContactOperationBase& /*aOperation*/,
       
  2297         TInt /*aStepSize*/,
       
  2298         TInt aError )
       
  2299  {
       
  2300  if(iLastOperation == ENSmlDeleteOp)
       
  2301 	{
       
  2302 	 	if(iBatchMode)
       
  2303 	 	{
       
  2304 			if( iIndex < iContactsBufferItemList.Count() &&
       
  2305 			iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemDelete )
       
  2306 			{
       
  2307 			iContactsBufferItemList[iIndex++]->iStatus  = KErrGeneral;
       
  2308 			iResultArray->Append(KErrGeneral);
       
  2309 			}
       
  2310 		 	
       
  2311 	 	}
       
  2312 	 	else
       
  2313 	 	{
       
  2314 	 		User::RequestComplete( iCallerStatus, aError );	
       
  2315 	 	}
       
  2316 	}
       
  2317 	return ETrue;
       
  2318  }
       
  2319 // ---------------------------------------------------------------------------
       
  2320 //  CNSmlContactsDataStore::OperationComplete
       
  2321 // ---------------------------------------------------------------------------
       
  2322 //
       
  2323 EXPORT_C void CNSmlContactsDataStore::OperationComplete
       
  2324 		( MVPbkContactOperationBase& aOperation )
       
  2325 	{
       
  2326 	MVPbkContactOperationBase* operation = &aOperation;
       
  2327 	if ( operation )
       
  2328 	{
       
  2329 		delete operation;
       
  2330 		operation = NULL;
       
  2331 	}
       
  2332 
       
  2333 	TRAPD(error ,OperationCompleteL());	
       
  2334 	if(error != KErrNone)
       
  2335 		{
       
  2336 		User::RequestComplete( iCallerStatus, error );
       
  2337 		}
       
  2338 	}
       
  2339 
       
  2340 // ---------------------------------------------------------------------------
       
  2341 //  CNSmlContactsDataStore::OperationCompleteL
       
  2342 // ---------------------------------------------------------------------------
       
  2343 //
       
  2344 EXPORT_C void CNSmlContactsDataStore::OperationCompleteL()
       
  2345 	{
       
  2346 	if(iLastOperation == ENSmlDeleteOp)
       
  2347 		{
       
  2348 		if(iBatchMode )
       
  2349 			{
       
  2350 			if(iResultArray->Count() != iContactsBufferItemList.Count())
       
  2351 				{
       
  2352 				while( iIndex < iContactsBufferItemList.Count() && 
       
  2353 				iContactsBufferItemList[iIndex]->iModType == ENSmlCntItemDelete 
       
  2354 				&& iContactsBufferItemList[iIndex]->iStatus  == KErrNotFound )
       
  2355 					{
       
  2356 					iResultArray->Append(KErrNotFound);
       
  2357 					iIndex++;
       
  2358 					}
       
  2359 				}
       
  2360 			
       
  2361 			if(iIndex == iContactsBufferItemList.Count())
       
  2362 				{
       
  2363 				if(iItemData)
       
  2364 					{
       
  2365 					iItemData->Reset();    
       
  2366 					}
       
  2367 
       
  2368 				ResetBuffer();
       
  2369 				User::RequestComplete( iCallerStatus, KErrNone );
       
  2370 				}
       
  2371 				else
       
  2372 				{
       
  2373 				ExecuteBufferL();
       
  2374 				}	
       
  2375 			}
       
  2376 			else
       
  2377 			{
       
  2378 			
       
  2379 			if(iItemData)
       
  2380 				{
       
  2381 				iItemData->Reset();    
       
  2382 				}
       
  2383 
       
  2384 			ResetBuffer();
       
  2385 			}
       
  2386 		}
       
  2387 		else
       
  2388 		{
       
  2389 		if(iLastOperation == ENSMLDeleteAllOp)
       
  2390 	        {
       
  2391 	        iDeleteAllOperation = NULL;
       
  2392 	        }
       
  2393 		if( iChangeFinder )
       
  2394 			{
       
  2395 			iChangeFinder->ResetL();		
       
  2396 			}
       
  2397 		iSnapshotRegistered = EFalse;
       
  2398 		
       
  2399 		delete iContactLnks;
       
  2400 		iContactLnks = NULL;
       
  2401 		iContactLink = NULL;
       
  2402 		
       
  2403 		delete iContactViewBase;
       
  2404 		iContactViewBase = NULL;
       
  2405 		User::RequestComplete( iCallerStatus, KErrNone );	
       
  2406 		
       
  2407 		}
       
  2408 		
       
  2409 	}
       
  2410 
       
  2411 // ---------------------------------------------------------------------------
       
  2412 // CNSmlContactsDataStore::ContactViewReady
       
  2413 // Implements the view ready function of MVPbkContactViewObserver
       
  2414 // --------------------------------------------------------------------------- 
       
  2415 EXPORT_C void CNSmlContactsDataStore::ContactViewReady(
       
  2416                 MVPbkContactViewBase& /*aView*/ ) 
       
  2417 	{
       
  2418 	if(iLastOperation == ENSMLDeleteAllOp)
       
  2419 		{
       
  2420 		TRAPD(error,DoDeleteAllContactsL());
       
  2421 		if(error != KErrNone)
       
  2422 			{
       
  2423 			User::RequestComplete( iCallerStatus, error );
       
  2424 			}
       
  2425 		}
       
  2426 	}
       
  2427 // ---------------------------------------------------------------------------
       
  2428 // CNSmlContactsDataStore::PrepareForContactsDeleteL()
       
  2429 // Delete all the contacts at client
       
  2430 // ---------------------------------------------------------------------------	
       
  2431 EXPORT_C void CNSmlContactsDataStore::DoDeleteAllContactsL()
       
  2432 	{
       
  2433 	iContactLnks = CVPbkContactLinkArray::NewL();
       
  2434 	TInt contactCount = iContactViewBase->ContactCountL();
       
  2435 	//add all the contact links to  ContactLinkArray
       
  2436 	for( TInt i = 0; i < contactCount; i++ )
       
  2437 		{
       
  2438 			const MVPbkViewContact& contact = 
       
  2439 						iContactViewBase->ContactAtL(i);
       
  2440 			iContactLink = contact.CreateLinkLC();
       
  2441 			iContactLnks->AppendL(iContactLink);
       
  2442 			CleanupStack::Pop();
       
  2443 		}
       
  2444 	
       
  2445 	iDeleteAllOperation = iContactManager->DeleteContactsL( *iContactLnks, *this );
       
  2446 	
       
  2447 	}
       
  2448 
       
  2449 // ---------------------------------------------------------------------------
       
  2450 // CNSmlContactsDataStore::ContactViewUnavailable
       
  2451 // Implements the view unavailable function of MVPbkContactViewObserver
       
  2452 // --------------------------------------------------------------------------- 
       
  2453 EXPORT_C void CNSmlContactsDataStore::ContactViewUnavailable(
       
  2454                 MVPbkContactViewBase& /*aView*/ )  
       
  2455 	{
       
  2456 	   
       
  2457 	}
       
  2458 
       
  2459 // ---------------------------------------------------------------------------
       
  2460 // CNSmlContactsDataStore::ContactAddedToView
       
  2461 // Implements the add contact function of MVPbkContactViewObserver
       
  2462 // --------------------------------------------------------------------------- 
       
  2463 EXPORT_C void CNSmlContactsDataStore::ContactAddedToView(
       
  2464             MVPbkContactViewBase& /*aView*/, 
       
  2465             TInt /*aIndex*/, 
       
  2466             const MVPbkContactLink& /*aContactLink*/ ) 
       
  2467 	{
       
  2468 	}
       
  2469 
       
  2470 // ---------------------------------------------------------------------------
       
  2471 // CNSmlContactsDataStore::ContactRemovedFromView
       
  2472 // Implements the remove contact function of MVPbkContactViewObserver
       
  2473 // --------------------------------------------------------------------------- 
       
  2474 EXPORT_C void CNSmlContactsDataStore::ContactRemovedFromView(
       
  2475                 MVPbkContactViewBase& /*aView*/, 
       
  2476                 TInt /*aIndex*/, 
       
  2477                 const MVPbkContactLink& /*aContactLink*/ )  
       
  2478 	{
       
  2479 	}
       
  2480 
       
  2481 // ---------------------------------------------------------------------------
       
  2482 // CNSmlContactsDataStore::ContactViewError
       
  2483 // Implements the view error function of MVPbkContactViewObserver
       
  2484 // --------------------------------------------------------------------------- 
       
  2485 EXPORT_C void CNSmlContactsDataStore::ContactViewError(
       
  2486             MVPbkContactViewBase& /*aView*/, 
       
  2487             TInt /*aError*/, 
       
  2488             TBool /*aErrorNotified*/ )  
       
  2489 	{
       
  2490 
       
  2491 	}
       
  2492 
       
  2493 // -----------------------------------------------------------------------------
       
  2494 // CNSmlContactsDataStore::CreateViewL()
       
  2495 // Create a contact view 
       
  2496 // -----------------------------------------------------------------------------
       
  2497 EXPORT_C void CNSmlContactsDataStore::CreateViewL()
       
  2498 	{
       
  2499 	
       
  2500 	CVPbkContactViewDefinition* viewDef = CVPbkContactViewDefinition::NewL();
       
  2501 	CleanupStack::PushL( viewDef );
       
  2502 	viewDef->SetType( EVPbkContactsView );
       
  2503 
       
  2504 	viewDef->SetUriL(iStore->StoreProperties().Uri().UriDes());
       
  2505 
       
  2506 	 iContactViewBase = iContactManager->CreateContactViewLC( 
       
  2507 				                         *this, 
       
  2508 				                         *viewDef, 
       
  2509 				                         iContactManager->FieldTypes()  );
       
  2510 	CleanupStack::Pop();
       
  2511 	CleanupStack::PopAndDestroy(viewDef);	
       
  2512 	}
  1761 
  2513 
  1762 // ------------------------------------------------------------------------------------------------
  2514 // ------------------------------------------------------------------------------------------------
  1763 // CNSmlContactsDataStore::GetStoreFormatResourceFileL
  2515 // CNSmlContactsDataStore::GetStoreFormatResourceFileL
  1764 // ------------------------------------------------------------------------------------------------
  2516 // ------------------------------------------------------------------------------------------------
  1765 EXPORT_C const TDesC& CNSmlContactsDataStore::GetStoreFormatResourceFileL() const
  2517 EXPORT_C const TDesC& CNSmlContactsDataStore::GetStoreFormatResourceFileL()
  1766     {
  2518     {
  1767     _DBG_FILE("CNSmlContactsDataStore::GetStoreFormatResourceFileL(): BEGIN");
  2519     _DBG_FILE("CNSmlContactsDataStore::GetStoreFormatResourceFileL(): begin");
  1768 
  2520     
  1769     // Check correct Data Sync protocol
  2521     // Check correct Data Sync protocol
  1770     TInt value( EDataSyncNotRunning );
  2522     TInt value( EDataSyncNotRunning );
  1771     TInt error = RProperty::Get( KPSUidDataSynchronizationInternalKeys,
  2523     TInt error = RProperty::Get( KPSUidDataSynchronizationInternalKeys,
  1772                                      KDataSyncStatus,
  2524         KDataSyncStatus, value );
  1773                                      value );
  2525     if ( error == KErrNone &&
  1774     if ( error == KErrNone && value == EDataSyncRunning )
  2526          value == EDataSyncRunning )
  1775         {
  2527         {
  1776         _DBG_FILE("CNSmlContactsDataStore::GetStoreFormatResourceFileL() 1.1.2: END");
       
  1777         return KNSmlContactsStoreFormatRsc_1_1_2;
  2528         return KNSmlContactsStoreFormatRsc_1_1_2;
  1778         }
  2529         }
  1779     else // error or protocol version 1.2 
  2530     else // error or protocol version 1.2 
  1780         {
  2531         {
  1781         _DBG_FILE("CNSmlContactsDataStore::GetStoreFormatResourceFileL() 1.2: END");
       
  1782         return KNSmlContactsStoreFormatRsc_1_2;
  2532         return KNSmlContactsStoreFormatRsc_1_2;
  1783         }
  2533         }
       
  2534     _DBG_FILE("CNSmlContactsDataStore::GetStoreFormatResourceFileL(): end");
  1784     }
  2535     }
  1785 
  2536 
  1786 
  2537 // ------------------------------------------------------------------------------------------------
  1787 // ----------------------------------------------------------------------------
  2538 // CNSmlContactsDataStore::CreateModsFetcherL
  1788 // CNSmlContactsDataStore::DoExecuteBufferL
  2539 // ------------------------------------------------------------------------------------------------
  1789 // ----------------------------------------------------------------------------
  2540 EXPORT_C MContactsModsFetcher* CNSmlContactsDataStore::CreateModsFetcherL()
  1790 EXPORT_C TInt CNSmlContactsDataStore::DoExecuteBufferL(RArray<TInt>& aResultArray)
       
  1791     {
  2541     {
  1792     _DBG_FILE("CNSmlContactsDataStore::DoExecuteBufferL(): begin");
  2542     _DBG_FILE("CNSmlContactsDataStore::CreateModsFetcherL(): begin");
  1793 
  2543     CNSmlContactsModsFetcher* modsFetcher = 
  1794     TBool batchOK = EFalse;
  2544         new ( ELeave ) CNSmlContactsModsFetcher( iSnapshotRegistered, 
  1795     TInt  ret( KErrNone );
  2545         *iContactManager,*iStore, iKey, *iChangeFinder );
  1796     TInt  retCommand( KErrNone );
  2546     CleanupStack::PushL( modsFetcher );
  1797     TInt  itemAdds( 0 );
  2547     modsFetcher->ConstructL();
  1798 
  2548     CleanupStack::Pop( modsFetcher );
  1799     aResultArray.Reset();
  2549     _DBG_FILE("CNSmlContactsDataStore::CreateModsFetcherL(): end");
  1800 
  2550     return modsFetcher;
  1801     if( iContactsBufferItemList.Count() == 0 )
       
  1802         {
       
  1803         return retCommand;
       
  1804         }
       
  1805 
       
  1806     TInt i = 0;
       
  1807     //
       
  1808     // ResultArray initialized
       
  1809     //
       
  1810     while( i<iContactsBufferItemList.Count() )
       
  1811         {
       
  1812         if ( iContactsBufferItemList[i]->iModType == ENSmlCntItemAdd &&
       
  1813              iContactsBufferItemList[i]->iStatus  == KNSmlNoError )
       
  1814             {
       
  1815             itemAdds++;
       
  1816             }
       
  1817         aResultArray.Append( KErrGeneral ); 
       
  1818         i++;
       
  1819         }
       
  1820 
       
  1821     i = 0;
       
  1822     //
       
  1823     // Loop through buffered commands
       
  1824     //
       
  1825     while( i<iContactsBufferItemList.Count() )
       
  1826         {       
       
  1827         if( iContactsBufferItemList[i]->iStatus!= KNSmlNoError )
       
  1828             {
       
  1829             retCommand = iContactsBufferItemList[i]->iStatus;
       
  1830             }
       
  1831         else if( iContactsBufferItemList[i]->iModType == ENSmlCntItemAdd )
       
  1832             {
       
  1833             ret =0 ;
       
  1834             iPUid  = 0;
       
  1835 
       
  1836             StripPropertyL( iItemDataAddBatch, KVersitTokenUID() ); // Remove UID's from data
       
  1837 
       
  1838             TInt err( KErrNone );
       
  1839             TTime lastModified;
       
  1840             if( iBatchMode )    // All adds handled when first add on iContactsBufferItemList
       
  1841                 {
       
  1842                 iAddResultArray.Reset();
       
  1843                 
       
  1844                 TRAP( err, ret = LdoAddItemsL( iAddResultArray, iItemDataAddBatch,
       
  1845                                                iItemDataAddBatch->Ptr(0).Length(),
       
  1846                                                lastModified ) );
       
  1847                 if( err == KErrNone && ret == KErrNone &&
       
  1848                     itemAdds == iAddResultArray.Count() )
       
  1849                     {
       
  1850                     TInt j = i;
       
  1851                     TInt k = 0;
       
  1852                     while( j < iContactsBufferItemList.Count() )
       
  1853                         {
       
  1854                         if( iContactsBufferItemList[j]->iModType == ENSmlCntItemAdd &&
       
  1855                             iContactsBufferItemList[j]->iStatus  == KNSmlNoError &&
       
  1856                             k < iAddResultArray.Count() )
       
  1857                             {
       
  1858                             iContactsBufferItemList[j]->iStatus = KErrNone;
       
  1859                             *iContactsBufferItemList[j]->iPUid = iAddResultArray[k];
       
  1860 
       
  1861                             TNSmlSnapshotItem item( iAddResultArray[k]);
       
  1862                             item.SetLastChangedDate( lastModified );
       
  1863                             item.SetSoftDelete( EFalse );
       
  1864                             iChangeFinder->ItemAddedL( item );
       
  1865                            
       
  1866                             k++;
       
  1867                             }
       
  1868                         j++;
       
  1869                         }
       
  1870                     }
       
  1871                 else
       
  1872                     {
       
  1873                     TInt j = i;
       
  1874                     while( j < iContactsBufferItemList.Count() )
       
  1875                         {
       
  1876                         if( iContactsBufferItemList[j]->iModType == ENSmlCntItemAdd &&
       
  1877                             iContactsBufferItemList[j]->iStatus  == KNSmlNoError )
       
  1878                             {
       
  1879                             iContactsBufferItemList[j]->iStatus = KErrNotSupported;
       
  1880                             }
       
  1881                         j++;
       
  1882                         }
       
  1883                     }
       
  1884                 }
       
  1885             else
       
  1886                 {
       
  1887                 TTime lastModified;
       
  1888                 iPUid  =iContactsBufferItemList[i]->iPUid;
       
  1889                 const TPtr8 dataPtr = iItemDataAddBatch->Ptr(0);
       
  1890                 TRAP( err, ret = LdoAddItemL(*iPUid, dataPtr,
       
  1891                                              iContactsBufferItemList[i]->iSize,
       
  1892                                              lastModified));
       
  1893                 if( (err == KErrNone) && (ret == KErrNone))
       
  1894                     {
       
  1895                     // Add the snapshot item to the Change Finder.
       
  1896                     // The Change finder maintains a list of all changes to the database.
       
  1897 
       
  1898                     TNSmlSnapshotItem item( *iPUid );
       
  1899                     item.SetLastChangedDate( lastModified );
       
  1900                     item.SetSoftDelete( EFalse );
       
  1901 
       
  1902                     iChangeFinder->ItemAddedL( item );
       
  1903                     retCommand = KErrNone;
       
  1904                     }
       
  1905                 else if( err == KErrNone )
       
  1906                     {
       
  1907                     retCommand = ret;
       
  1908                     }
       
  1909                 else
       
  1910                     {
       
  1911                     retCommand = err;
       
  1912                     }
       
  1913                 }
       
  1914             }
       
  1915         else if( iContactsBufferItemList[i]->iModType == ENSmlCntItemReplace  ||
       
  1916             iContactsBufferItemList[i]->iModType == ENSmlCntItemFieldLevelReplace )
       
  1917             {
       
  1918             iItemData = iContactsBufferItemList[i]->iItemData;
       
  1919             iUid  = iContactsBufferItemList[i]->iUid;
       
  1920             const TPtr8 dataPtr = iItemData->Ptr(0);
       
  1921             ret = 0;
       
  1922             TTime lastModified;
       
  1923            
       
  1924             TRAPD( err, ret = LdoUpdateItemL(iUid, dataPtr,
       
  1925                                              iContactsBufferItemList[i]->iSize,
       
  1926                                              lastModified ));
       
  1927             if( (err == KErrNone) && (ret == KErrNone) )
       
  1928                 {
       
  1929                 retCommand = KErrNone;
       
  1930                 iContactsBufferItemList[i]->iStatus = KErrNone;
       
  1931 
       
  1932                 TNSmlSnapshotItem item( iUid );
       
  1933                 item.SetLastChangedDate( lastModified );
       
  1934                 item.SetSoftDelete( EFalse );
       
  1935 
       
  1936                 iChangeFinder->ItemUpdatedL( item );
       
  1937                 }
       
  1938             else if( err == KErrNone )
       
  1939                 {
       
  1940                 retCommand = ret;
       
  1941                 iContactsBufferItemList[i]->iStatus = ret;
       
  1942                 }
       
  1943             else
       
  1944                 {
       
  1945                 retCommand = err;
       
  1946                 iContactsBufferItemList[i]->iStatus = err;
       
  1947                 }
       
  1948             iItemData = NULL;
       
  1949             }
       
  1950         else if( iContactsBufferItemList[i]->iModType == ENSmlCntItemMove )
       
  1951             {
       
  1952             retCommand = KErrNotSupported;
       
  1953             }
       
  1954         else if( iContactsBufferItemList[i]->iModType == ENSmlCntItemDelete ||
       
  1955                  iContactsBufferItemList[i]->iModType == ENSmlCntItemSoftDelete )
       
  1956             {
       
  1957             if( iBatchMode )    // All deletes handled when 1'st occurrence on list
       
  1958                 {
       
  1959                 CArrayFixFlat<TUid>* arrDelete = new(ELeave) CArrayFixFlat<TUid>(4);
       
  1960                 CleanupStack::PushL( arrDelete );
       
  1961                 TInt j=i;
       
  1962                 while( j < iContactsBufferItemList.Count() )
       
  1963                     {
       
  1964                     if( (iContactsBufferItemList[j]->iModType == ENSmlCntItemDelete ||
       
  1965                          iContactsBufferItemList[j]->iModType == ENSmlCntItemSoftDelete) &&
       
  1966                          iContactsBufferItemList[j]->iStatus  == KNSmlNoError )
       
  1967                         {
       
  1968                         TUid id = TUid::Uid( iContactsBufferItemList[j]->iUid ); 
       
  1969                         CNsmlContactItem* contactitem = CNsmlContactItem::NewL();
       
  1970                         CleanupStack::PushL( contactitem );
       
  1971                         TInt err = KErrNone;
       
  1972                         TInt ret = KErrNone;
       
  1973                         TRAP( err, ret = iContactsDataStoreExtension->ReadContactL( id, contactitem ) ); 
       
  1974                         if( err == KErrNone && ret == KErrNone && id.iUid != 0 )
       
  1975                             {
       
  1976                             arrDelete->AppendL( id );
       
  1977                             }
       
  1978                         else if( err == KErrNone && ret == KErrNone )
       
  1979                             {
       
  1980                             iContactsBufferItemList[j]->iStatus  = KErrNotFound;
       
  1981                             }
       
  1982                         else
       
  1983                             {
       
  1984                             iContactsBufferItemList[j]->iStatus  = err;
       
  1985                             }
       
  1986                         CleanupStack::PopAndDestroy( contactitem ); // item
       
  1987                         }
       
  1988                     j++;
       
  1989                     }
       
  1990                 if( arrDelete->Count() > 0 )
       
  1991                     {
       
  1992                     TInt err2 = KErrInUse;
       
  1993                     TRAP( err2, iContactsDataStoreExtension->DeleteContactsL( arrDelete ) );
       
  1994                     CleanupStack::PopAndDestroy( arrDelete );
       
  1995 
       
  1996                     if( err2 == KErrInUse )
       
  1997                         {
       
  1998                         err2 = KErrGeneral;
       
  1999                         }
       
  2000 
       
  2001                     j= i;
       
  2002                     while( j < iContactsBufferItemList.Count() ) // Mark deleted items
       
  2003                         {
       
  2004                         if( iContactsBufferItemList[j]->iModType == ENSmlCntItemDelete &&
       
  2005                             iContactsBufferItemList[j]->iStatus  == KNSmlNoError )
       
  2006                             {
       
  2007 
       
  2008                             iContactsBufferItemList[j]->iStatus  = err2;
       
  2009                             if( err2 == KErrNone )
       
  2010                                 {
       
  2011                                 iUid  = iContactsBufferItemList[j]->iUid;
       
  2012                                 iChangeFinder->ItemDeleted( TNSmlSnapshotItem( iUid ));
       
  2013                                 }
       
  2014                             }
       
  2015                         else if( iContactsBufferItemList[j]->iModType == ENSmlCntItemSoftDelete &&
       
  2016                                  iContactsBufferItemList[j]->iStatus  == KNSmlNoError )
       
  2017                             {
       
  2018 
       
  2019                             iContactsBufferItemList[j]->iStatus  = err2;
       
  2020                             if( err2 == KErrNone )
       
  2021                                 {
       
  2022                                 iUid  = iContactsBufferItemList[j]->iUid;
       
  2023                                 iChangeFinder->ItemSoftDeletedL( TNSmlSnapshotItem( iUid ) );
       
  2024                                 }
       
  2025                             }
       
  2026                         j++;
       
  2027                         }
       
  2028                     }
       
  2029                 else
       
  2030                     {
       
  2031                     CleanupStack::PopAndDestroy( arrDelete );
       
  2032                     }
       
  2033                 }
       
  2034             else
       
  2035                 {
       
  2036                 iUid  = iContactsBufferItemList[i]->iUid;
       
  2037                 TInt err = KErrNone;
       
  2038                 TRAP( err, iContactsDataStoreExtension->DeleteContactL( TUid::Uid( iUid ) ) );
       
  2039                 if( err == KErrNone )
       
  2040                     {
       
  2041                     iChangeFinder->ItemDeleted( TNSmlSnapshotItem( iUid ) );
       
  2042                     }
       
  2043 
       
  2044                 iContactsBufferItemList[i]->iStatus  = err;
       
  2045                 retCommand = err;
       
  2046                 }
       
  2047             }
       
  2048         //
       
  2049         //  Set allready set commands from iContactsBufferItemList
       
  2050         //
       
  2051         if( ( iContactsBufferItemList[i]->iModType == ENSmlCntItemAdd     ||
       
  2052               iContactsBufferItemList[i]->iModType == ENSmlCntItemReplace ||
       
  2053               iContactsBufferItemList[i]->iModType == ENSmlCntItemFieldLevelReplace ||
       
  2054               iContactsBufferItemList[i]->iModType == ENSmlCntItemDelete  ||
       
  2055               iContactsBufferItemList[i]->iModType == ENSmlCntItemSoftDelete ) &&
       
  2056               iContactsBufferItemList[i]->iStatus  != KNSmlNoError )
       
  2057             {
       
  2058             retCommand = iContactsBufferItemList[i]->iStatus;
       
  2059             }
       
  2060 
       
  2061         
       
  2062         if( !batchOK && retCommand == KErrNone ) // If one command is OK => batchOK
       
  2063             {
       
  2064             batchOK = ETrue;
       
  2065             }
       
  2066 
       
  2067         aResultArray[i++] = retCommand;
       
  2068         }
       
  2069 
       
  2070     if ( batchOK )
       
  2071         {
       
  2072         _DBG_FILE("CNSmlContactsDataStore::DoExecuteBufferL(): end");
       
  2073         return KErrNone;
       
  2074         }
       
  2075 
       
  2076     _DBG_FILE("CNSmlContactsDataStore::DoExecuteBufferL(): ERROR end");
       
  2077     return KErrGeneral; // All commands failed    
       
  2078     }
  2551     }
  2079 
  2552 
  2080 // ------------------------------------------------------------------------------------------------
  2553 // ------------------------------------------------------------------------------------------------
  2081 // CNSmlContactsDataStore::FetchModificationsL
  2554 // CNSmlContactsDataStore::GetDataMod
  2082 // ------------------------------------------------------------------------------------------------
  2555 // ------------------------------------------------------------------------------------------------
  2083 //
       
  2084 TInt CNSmlContactsDataStore::FetchModificationsL()
       
  2085     {
       
  2086     _DBG_FILE("CNSmlContactsDataStore::FetchModificationsL(): begin");
       
  2087   
       
  2088     TInt error = KErrNone;
       
  2089     
       
  2090     if( !iSnapshot )
       
  2091         {
       
  2092         iSnapshot = new (ELeave) CArrayFixSeg<TNSmlSnapshotItem>( KNSmlContactsGranularity );
       
  2093         }
       
  2094     
       
  2095     iCntUidList->Reset();
       
  2096     iContactsDataStoreExtension->ListContactsL( iCntUidList );
       
  2097     
       
  2098     if( !iSnapshotRegistered )
       
  2099         {
       
  2100         DBG_ARGS(_S("CNSmlContactsDataStore::FetchModificationsL(): Count = %d"), iCntUidList->Count());
       
  2101         for ( TInt i = 0; i < iCntUidList->Count(); i++ )
       
  2102             {
       
  2103             CNsmlContactItem* contactitem = CNsmlContactItem::NewL();
       
  2104             CleanupStack::PushL( contactitem );
       
  2105             error = iContactsDataStoreExtension->ReadContactL( iCntUidList->At(i), contactitem ); 
       
  2106             if(error != KErrNone)
       
  2107                 {
       
  2108                 DBG_ARGS(_S("CNSmlContactsDataStore::FetchModificationsL(): Error = %d"), error);
       
  2109                 CleanupStack::PopAndDestroy(contactitem); // item
       
  2110                 return error;
       
  2111                 }           
       
  2112             TNSmlSnapshotItem snap( iCntUidList->At(i).iUid  );
       
  2113             snap.SetLastChangedDate( contactitem->iLastModified );
       
  2114             iSnapshot->InsertIsqL( snap, iKey );       
       
  2115                 
       
  2116             CleanupStack::PopAndDestroy(contactitem); // item
       
  2117             }
       
  2118         iSnapshot->Compress();
       
  2119           
       
  2120         iChangeFinder->SetNewSnapshot( iSnapshot ); // changefinder takes ownership
       
  2121         iSnapshot = NULL;
       
  2122         iSnapshotRegistered = ETrue;
       
  2123         }
       
  2124     
       
  2125     _DBG_FILE("CNSmlContactsDataStore::FetchModificationsL(): end");
       
  2126     return KErrNone;
       
  2127     }
       
  2128 
       
  2129 // ----------------------------------------------------------------------------»
       
  2130 // CNSmlContactsDataStore::GetDataMod
       
  2131 // return reference to datamod instance.
       
  2132 // ----------------------------------------------------------------------------
       
  2133 EXPORT_C CNSmlDataModBase& CNSmlContactsDataStore::GetDataMod()
  2556 EXPORT_C CNSmlDataModBase& CNSmlContactsDataStore::GetDataMod()
  2134     {
  2557     {
       
  2558     _DBG_FILE("CNSmlContactsDataStore::GetDataMod()");
  2135     return *iDataMod;
  2559     return *iDataMod;
  2136     }
  2560     }
  2137 
  2561 
  2138 // ----------------------------------------------------------------------------
       
  2139 // CNSmlContactsDataStore::ExportContactsL
       
  2140 // ----------------------------------------------------------------------------
       
  2141 EXPORT_C TInt CNSmlContactsDataStore::ExportContactsL( 
       
  2142     const TUid& aUid, CBufBase& aContactBufBase )
       
  2143     {
       
  2144     return iContactsDataStoreExtension->ExportContactsL( aUid, aContactBufBase ); 
       
  2145     }
       
  2146 
       
  2147 // ----------------------------------------------------------------------------
       
  2148 // CNSmlContactsDataStore::ImportContactsL
       
  2149 // ----------------------------------------------------------------------------
       
  2150 EXPORT_C CArrayFixFlat<TUid>* CNSmlContactsDataStore::ImportContactsL( 
       
  2151     const TDesC8& aContactBufBase )
       
  2152     {
       
  2153     return iContactsDataStoreExtension->ImportContactsL( aContactBufBase ); 
       
  2154     }
       
  2155 
       
  2156 // End of File  
  2562 // End of File