omads/omadsextensions/adapters/notes/src/NSmlNotepadDataStore.cpp
branchRCL_3
changeset 25 4f0867e42d62
parent 24 8e7494275d3a
child 26 3e6957da2ff8
equal deleted inserted replaced
24:8e7494275d3a 25:4f0867e42d62
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  DS notepad data store
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <barsc.h>
       
    21 #include <bautils.h>
       
    22 #include <s32mem.h>
       
    23 #include <sysutil.h>
       
    24 #include <nsmlnotepaddatastoreformat.rsg>
       
    25 #include <data_caging_path_literals.hrh>
       
    26 #include <SmlDataProvider.h>
       
    27 #include <txtetext.h>
       
    28 #include <charconv.h>
       
    29 #include <nsmlchangefinder.h>
       
    30 
       
    31 #include "NSmlNotepadDatastore.h"
       
    32 #include "nsmlnotepaddefs.h"
       
    33 #include "nsmlnotepadDatabase.h"
       
    34 #include "nsmlsnapshotitem.h"
       
    35 #include "nsmlnotepadlogging.h"
       
    36 	
       
    37 // ----------------------------------------------------------------------------
       
    38 // CNSmlNotepadDataStore::NewL
       
    39 // ----------------------------------------------------------------------------
       
    40 CNSmlNotepadDataStore* CNSmlNotepadDataStore::NewL()
       
    41 	{
       
    42 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::NewL: BEGIN");
       
    43 	CNSmlNotepadDataStore* self = new ( ELeave ) CNSmlNotepadDataStore();
       
    44 	CleanupStack::PushL(self);
       
    45 	self->ConstructL();
       
    46 	CleanupStack::Pop(self); // self
       
    47 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::NewL: END");
       
    48 	return self;
       
    49 	}
       
    50 
       
    51 // ----------------------------------------------------------------------------
       
    52 // CNSmlNotepadDataStore::~CNSmlNotepadDataStore()
       
    53 // ----------------------------------------------------------------------------
       
    54 CNSmlNotepadDataStore::~CNSmlNotepadDataStore()
       
    55 	{
       
    56 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::~CNSmlNotepadDataStore(): begin");
       
    57 	if(iOwnStoreFormat)
       
    58 	    {
       
    59 		delete iOwnStoreFormat;
       
    60 		iOwnStoreFormat = NULL;
       
    61 	    }
       
    62 	if(iDefaultStoreName)
       
    63 	    {
       
    64 		delete iDefaultStoreName;
       
    65 		iDefaultStoreName = NULL;
       
    66 	    }
       
    67 	if(iNpdDb)
       
    68 	    {
       
    69 		delete iNpdDb;
       
    70 		iNpdDb = NULL;
       
    71 	    }
       
    72 	if(iReplacedUids)
       
    73 	    {
       
    74 		delete iReplacedUids;
       
    75 		iReplacedUids = NULL;
       
    76 	    }
       
    77 	if(iMovedUids)
       
    78 	    {
       
    79 		delete iMovedUids;
       
    80 		iMovedUids = NULL;
       
    81 	    }
       
    82 	if(iSoftDeletedUids)
       
    83 	    {
       
    84 		delete iSoftDeletedUids;
       
    85 		iSoftDeletedUids = NULL;
       
    86 	    }
       
    87 	if(iDeletedUids)
       
    88 	    {
       
    89 		delete iDeletedUids;
       
    90 		iDeletedUids = NULL;
       
    91 	    }
       
    92 	if(iNewUids)
       
    93 	    {
       
    94 		delete iNewUids;
       
    95 		iNewUids = NULL;
       
    96 	    }
       
    97 	iRfs.Close();
       
    98 	iStringPool.Close();
       
    99 	if(iOpenedStoreName)
       
   100 	    {
       
   101 		delete iOpenedStoreName;
       
   102 		iOpenedStoreName = NULL;
       
   103 	    }
       
   104 	if(iItemData)
       
   105 	    {
       
   106 		delete iItemData;
       
   107 		iItemData = NULL;	
       
   108 	    }
       
   109 	if ( iChangeFinder )
       
   110 		{
       
   111 		TRAP_IGNORE( iChangeFinder->CloseL() );
       
   112 		delete iChangeFinder;
       
   113 		iChangeFinder = NULL;
       
   114 		}
       
   115 	
       
   116 	if( iSnapshot )
       
   117 	    {
       
   118         delete iSnapshot;
       
   119         iSnapshot = NULL;
       
   120 	    }
       
   121 	
       
   122 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::~CNSmlNotepadDataStore(): end");		
       
   123 	}
       
   124 
       
   125 
       
   126 // ----------------------------------------------------------------------------
       
   127 // CNSmlNotepadDataStore::CNSmlNotepadDataStore() 
       
   128 // ----------------------------------------------------------------------------
       
   129 CNSmlNotepadDataStore::CNSmlNotepadDataStore() : iOpenedStoreId(KNSmlNotepadAdapterImplUid),iKey( TKeyArrayFix( _FOFF( TNSmlSnapshotItem,ItemId() ),ECmpTInt ))
       
   130 	{
       
   131 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::CNSmlNotepadDataStore(): begin");
       
   132 	iSyncHistory = EFalse;
       
   133 	iDataBaseOpened = EFalse;
       
   134 	iReaderPosition = -1;
       
   135 	iWriterPosition = -1;
       
   136 	iModificationCount = KNSmlCompactAfterChanges;
       
   137 	iState = ENSmlClosed;
       
   138 	iSnapshotRegistered = EFalse;
       
   139 	iDrive = -1;
       
   140 	iReplaceItemId =  -1;
       
   141 	iServerMaxObjectSize = 0; // Maximum size default value
       
   142 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::CNSmlNotepadDataStore(): end");
       
   143 	}
       
   144 	
       
   145 // ----------------------------------------------------------------------------
       
   146 // CNSmlNotepadDataStore::ConstructL()
       
   147 // ----------------------------------------------------------------------------
       
   148 void CNSmlNotepadDataStore::ConstructL()
       
   149 	{
       
   150 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::ConstructL(): begin");
       
   151 	User::LeaveIfError(iRfs.Connect());
       
   152 	iStringPool.OpenL();
       
   153 	iUsedMimeType.Set( KMimePlainText() );
       
   154 	iUsedMimeVersion.Set( KMimePlainTextVersion() );
       
   155 	// Uid Sets
       
   156 	if(iNewUids)
       
   157 	    {
       
   158 		delete iNewUids;
       
   159 		iNewUids = NULL;
       
   160 	    }
       
   161 	iNewUids = new (ELeave) CNSmlDataItemUidSet();
       
   162 	if(iDeletedUids)
       
   163 	    {
       
   164 		delete iDeletedUids;
       
   165 		iDeletedUids = NULL;
       
   166 	    }
       
   167 	iDeletedUids = new (ELeave) CNSmlDataItemUidSet();
       
   168 	if(iSoftDeletedUids)
       
   169 	    {
       
   170 		delete iSoftDeletedUids;
       
   171 		iSoftDeletedUids = NULL;
       
   172 	    }
       
   173 	iSoftDeletedUids = new (ELeave) CNSmlDataItemUidSet();
       
   174 	if(iMovedUids)
       
   175 	    {
       
   176 		delete iMovedUids;
       
   177 		iMovedUids = NULL;
       
   178 	    }
       
   179 	iMovedUids = new (ELeave) CNSmlDataItemUidSet();
       
   180 	if(iReplacedUids)
       
   181 	    {
       
   182 		delete iReplacedUids;
       
   183 		iReplacedUids = NULL;
       
   184 	    }
       
   185 	iReplacedUids = new (ELeave) CNSmlDataItemUidSet();
       
   186 	if(iNpdDb)
       
   187 	    {
       
   188 		delete iNpdDb;
       
   189 		iNpdDb = NULL;
       
   190 	    }
       
   191 	iNpdDb = CNSmlNotepadDatabase::NewL();
       
   192 	if(iDefaultStoreName)
       
   193 	    {
       
   194 		delete iDefaultStoreName;
       
   195 		iDefaultStoreName = NULL;
       
   196 	    }
       
   197 	iDefaultStoreName = HBufC::NewL( KNSmlDefaultStoreNameMaxSize );
       
   198 	*iDefaultStoreName = KNSmlNotepadStoreName; 
       
   199 	
       
   200 	if(iOwnStoreFormat)
       
   201 	    {
       
   202 		delete iOwnStoreFormat;
       
   203 		iOwnStoreFormat = NULL;
       
   204 	    }
       
   205 	iOwnStoreFormat = DoOwnStoreFormatL();
       
   206 	
       
   207 	iSnapshot = new (ELeave) CArrayFixSeg<TNSmlSnapshotItem>(KSnapshotGranularity);
       
   208 	
       
   209 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::ConstructL(): end");
       
   210 	}
       
   211 
       
   212 
       
   213 // ----------------------------------------------------------------------------
       
   214 // void CNSmlNotepadDataStore::DefaultStoreNameL()
       
   215 // ----------------------------------------------------------------------------
       
   216 const TDesC& CNSmlNotepadDataStore::DefaultStoreNameL() const
       
   217 	{
       
   218 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DefaultStoreNameL(): begin");
       
   219 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DefaultStoreNameL(): end");
       
   220 	if ( iDefaultStoreName )
       
   221         {
       
   222         return *iDefaultStoreName;
       
   223         }
       
   224 	return KNullDesC;
       
   225 	}
       
   226 
       
   227 
       
   228 // ----------------------------------------------------------------------------
       
   229 // CDesCArray* CNSmlNotepadDataStore::DoListStoresLC();
       
   230 // ----------------------------------------------------------------------------
       
   231 CDesCArray* CNSmlNotepadDataStore::DoListStoresLC()
       
   232 	{
       
   233 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoListStoresLC(): begin");
       
   234 	CDesCArrayFlat *npdStores = new (ELeave)CDesCArrayFlat(1);
       
   235     CleanupStack::PushL(npdStores);
       
   236     npdStores->AppendL(KNSmlNotepadStoreName);
       
   237     _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoListStoresLC(): end");
       
   238 	return npdStores;
       
   239 	}
       
   240 	
       
   241 // ----------------------------------------------------------------------------
       
   242 // void CNSmlNotepadDataStore::DoOpenL(const TDesC& 		  aStoreName, 
       
   243 //									    MSmlSyncRelationship& aContext,
       
   244 //									    TRequestStatus& 	  aStatus)
       
   245 // ----------------------------------------------------------------------------
       
   246 void CNSmlNotepadDataStore::DoOpenL( const TDesC& aStoreName, 
       
   247 									MSmlSyncRelationship& aContext, 
       
   248 									TRequestStatus& aStatus )
       
   249     {
       
   250     _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoOpenL(): begin");	
       
   251     iCallerStatus = &aStatus;
       
   252 	*iCallerStatus = KRequestPending;
       
   253 	if( iState != ENSmlClosed || iDataBaseOpened )
       
   254 	    {
       
   255         
       
   256 		User::RequestComplete( iCallerStatus, KErrInUse );	
       
   257 		return;	
       
   258 	    }
       
   259 	if(aStoreName.Length()>0)
       
   260 	    {
       
   261 		if( RFs::CharToDrive(aStoreName[0], iDrive) != KErrNone )
       
   262             {
       
   263             RFs::CharToDrive( KNSmlDriveC()[0], iDrive );
       
   264             }	
       
   265 	    }
       
   266 		
       
   267 	// Open the Database
       
   268 	TInt err( KErrNone );
       
   269 	TRAP(err,iNpdDb->OpenL( KNSmlNotepadDefaultStoreName ));
       
   270 	if ( err )
       
   271         {
       
   272         User::RequestComplete( iCallerStatus, err );
       
   273         return;
       
   274         }
       
   275 	
       
   276     if ( iOpenedStoreName )
       
   277         {
       
   278         delete iOpenedStoreName;
       
   279         iOpenedStoreName = NULL;
       
   280         }
       
   281     iOpenedStoreName = HBufC::NewL( KNSmlDefaultStoreNameMaxSize );
       
   282     *iOpenedStoreName = KNSmlNotepadStoreName;
       
   283     
       
   284     iDataBaseOpened = ETrue;
       
   285 		
       
   286 	if ( iChangeFinder )
       
   287 		{
       
   288 		iChangeFinder->CloseL();
       
   289 		delete iChangeFinder;
       
   290 		iChangeFinder = NULL;
       
   291 		}
       
   292 	iChangeFinder = CNSmlChangeFinder::NewL( aContext, iKey, iSyncHistory, KNSmlNotepadAdapterImplUid );
       
   293 	
       
   294 	err = FetchModificationsL();
       
   295 	
       
   296 	
       
   297 	iState = ENSmlOpenAndWaiting;	
       
   298 	User::RequestComplete( iCallerStatus, err );
       
   299 	
       
   300 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoOpenL(): end");
       
   301 }
       
   302 
       
   303 // ----------------------------------------------------------------------------
       
   304 // void CNSmlNotepadDataStore::DoCancelRequest()
       
   305 // ----------------------------------------------------------------------------
       
   306 void CNSmlNotepadDataStore::DoCancelRequest()
       
   307 	{
       
   308 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCancelRequest(): begin");
       
   309 	if ( iState == ENSmlOpenAndWaiting )
       
   310         {
       
   311         iState = ENSmlClosed;
       
   312         }
       
   313     else
       
   314         {
       
   315         iState = ENSmlOpenAndWaiting;
       
   316         }
       
   317 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCancelRequest(): NOT NEEDED end");
       
   318 	}
       
   319 
       
   320 // ----------------------------------------------------------------------------
       
   321 // void CNSmlNotepadDataStore::DoStoreName()
       
   322 // ----------------------------------------------------------------------------
       
   323 const TDesC& CNSmlNotepadDataStore::DoStoreName() const
       
   324 	{
       
   325 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoStoreName(): begin");
       
   326 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoStoreName(): end");
       
   327 	if ( iOpenedStoreName )
       
   328 		{
       
   329         return *iOpenedStoreName;
       
   330 		}
       
   331 	return KNullDesC;
       
   332 	}
       
   333 
       
   334 // ----------------------------------------------------------------------------
       
   335 // void CNSmlNotepadDataStore::DoBeginTransactionL()
       
   336 // ----------------------------------------------------------------------------
       
   337 void CNSmlNotepadDataStore::DoBeginTransactionL()
       
   338 	{
       
   339 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoBeginTransactionL(): begin");
       
   340 	User::Leave( KErrNotSupported );
       
   341 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoBeginTransactionL(): end");
       
   342 	}
       
   343 
       
   344 // ----------------------------------------------------------------------------
       
   345 // void CNSmlNotepadDataStore::DoCommitTransactionL()
       
   346 // ----------------------------------------------------------------------------
       
   347 void CNSmlNotepadDataStore::DoCommitTransactionL(TRequestStatus& aStatus)
       
   348 	{
       
   349 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitTransactionL(): begin");
       
   350 	iCallerStatus = &aStatus;
       
   351 	*iCallerStatus = KRequestPending;
       
   352 	User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   353 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitTransactionL(): end");
       
   354 	}
       
   355 
       
   356 // ----------------------------------------------------------------------------
       
   357 // void CNSmlNotepadDataStore::DoRevertTransaction()
       
   358 // ----------------------------------------------------------------------------
       
   359 void CNSmlNotepadDataStore::DoRevertTransaction(TRequestStatus& aStatus)
       
   360 	{
       
   361 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoRevertTransaction(): begin");
       
   362 	iCallerStatus = &aStatus;
       
   363 	*iCallerStatus = KRequestPending;
       
   364 	User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   365 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoRevertTransaction(): end");
       
   366 	}
       
   367 
       
   368 // ----------------------------------------------------------------------------
       
   369 // void CNSmlNotepadDataStore::DoBeginBatchL()
       
   370 // ----------------------------------------------------------------------------
       
   371 void CNSmlNotepadDataStore::DoBeginBatchL()
       
   372 	{
       
   373 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoBeginBatchL(): begin");
       
   374 	User::Leave( KErrNotSupported );
       
   375 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoBeginBatchL(): end");
       
   376 	}
       
   377 
       
   378 // ----------------------------------------------------------------------------
       
   379 // void CNSmlNotepadDataStore::DoCommitBatchL()
       
   380 // ----------------------------------------------------------------------------
       
   381 void CNSmlNotepadDataStore::DoCommitBatchL( RArray<TInt>& /*aResultArray*/, TRequestStatus& aStatus )
       
   382 	{
       
   383 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitBatchL(): begin");
       
   384 	iCallerStatus = &aStatus;
       
   385 	*iCallerStatus = KRequestPending;
       
   386 	User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   387 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitBatchL(): end");
       
   388 	}
       
   389 // ----------------------------------------------------------------------------
       
   390 // void CNSmlNotepadDataStore::DoCancelBatch()
       
   391 // ----------------------------------------------------------------------------
       
   392 void CNSmlNotepadDataStore::DoCancelBatch()
       
   393 	{
       
   394 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCancelBatch(): begin");
       
   395 	// Nothing to do
       
   396 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCancelBatch(): end");
       
   397 	}
       
   398 // ----------------------------------------------------------------------------
       
   399 // void CNSmlNotepadDataStore::DoSetRemoteStoreFormatL()
       
   400 // ----------------------------------------------------------------------------
       
   401 void CNSmlNotepadDataStore::DoSetRemoteStoreFormatL( const CSmlDataStoreFormat& aServerDataStoreFormat )
       
   402 	{
       
   403 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSetRemoteStoreFormatL(): begin");
       
   404 	TBool foundMime = EFalse;
       
   405 	// KErrNotSupported is returned if server does not support same mime type 
       
   406 	// and same mime type version as Data Provider.
       
   407 	TInt i=0;
       
   408 	while( i< aServerDataStoreFormat.MimeFormatCount() && !foundMime )
       
   409 		{
       
   410 		if( iUsedMimeType.MatchF(aServerDataStoreFormat.MimeFormat(i).MimeType().DesC())      ==0 &&
       
   411 		    iUsedMimeVersion.MatchF(aServerDataStoreFormat.MimeFormat(i).MimeVersion().DesC())==0 )
       
   412 			{
       
   413 			foundMime = ETrue;
       
   414 			iServerMimeType    = aServerDataStoreFormat.MimeFormat(i).MimeType();
       
   415 			iServerMimeVersion = aServerDataStoreFormat.MimeFormat(i).MimeVersion();
       
   416 			}
       
   417 		i++;
       
   418 		}
       
   419 	if( !foundMime )
       
   420 		{
       
   421 		_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSetRemoteStoreFormatL(): SERVER MIMETYPE NOTSUPPORTED end");
       
   422 		
       
   423 		User::Leave( KErrNotSupported );
       
   424 		}
       
   425 	if( !iOwnStoreFormat )
       
   426 		{
       
   427         iOwnStoreFormat = DoOwnStoreFormatL();
       
   428 		}
       
   429 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSetRemoteStoreFormatL(): end");
       
   430 	return;
       
   431 	}
       
   432 	
       
   433 // ----------------------------------------------------------------------------
       
   434 // void CNSmlNotepadDataStore::DoSetRemoteMaxObjectSize()
       
   435 // ----------------------------------------------------------------------------
       
   436 void CNSmlNotepadDataStore::DoSetRemoteMaxObjectSize( TInt aServerMaxObjectSize )
       
   437 	{
       
   438 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSetRemoteMaxObjectSize(): begin");
       
   439 	iServerMaxObjectSize = aServerMaxObjectSize;
       
   440 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSetRemoteMaxObjectSize(): end");
       
   441 	}	
       
   442 // ----------------------------------------------------------------------------
       
   443 // void CNSmlNotepadDataStore::DoMaxObjectSize()
       
   444 // ----------------------------------------------------------------------------
       
   445 TInt CNSmlNotepadDataStore::DoMaxObjectSize() const
       
   446 	{
       
   447 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMaxObjectSize(): begin");
       
   448 	if ( iOwnStoreFormat )
       
   449 		{
       
   450 		if ( iOwnStoreFormat->HasMaxSize() )
       
   451 			{
       
   452 			_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMaxObjectSize - From resource: END");
       
   453 			
       
   454 			return iOwnStoreFormat->MaxSize();
       
   455 			}
       
   456 		}
       
   457 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMaxObjectSize() 100 k: end");
       
   458 	return KNSmlDataStoreMaxSize;  // 100 k
       
   459 	}
       
   460 		
       
   461 
       
   462 // ----------------------------------------------------------------------------
       
   463 // void CNSmlNotepadDataStore::DoOpenItemL()
       
   464 // ----------------------------------------------------------------------------
       
   465 void CNSmlNotepadDataStore::DoOpenItemL( TSmlDbItemUid aUid, 
       
   466 										  TBool& aFieldChange, 
       
   467 										  TInt& aSize, 
       
   468 										  TSmlDbItemUid& aParent, 
       
   469 										  TDes8& aMimeType, 
       
   470 										  TDes8& aMimeVer, 
       
   471 										  TRequestStatus& aStatus )
       
   472 	{
       
   473 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoOpenItemL(): begin");
       
   474 	iCallerStatus = &aStatus;
       
   475 	*iCallerStatus = KRequestPending;
       
   476 	if ( iState != ENSmlOpenAndWaiting )
       
   477 		{
       
   478 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
   479 		return;
       
   480 		}
       
   481 	aFieldChange = EFalse; // Whole item sent NOT SUPPORT ON FIELDCHANGE
       
   482 	aMimeType    = iOwnStoreFormat->MimeFormat( 0 ).MimeType().DesC();
       
   483 	aMimeVer	 = iOwnStoreFormat->MimeFormat( 0 ).MimeVersion().DesC();
       
   484 	aParent      = KNullDataItemId;
       
   485 	if( !iItemData )
       
   486 		{
       
   487 		iItemData= CBufFlat::NewL( KNSmlItemDataExpandSize );
       
   488 		}
       
   489 	else
       
   490 		{
       
   491 		iItemData->Reset();
       
   492 		}
       
   493 	TInt err = OpenItemL( aUid, *iItemData );
       
   494 	if( !err )
       
   495 		{
       
   496 		
       
   497 		iItemData->Compress();
       
   498 		iReaderPosition  = 0;
       
   499 		aSize = iItemData->Size();
       
   500 		iState = ENSmlItemOpen;
       
   501 		if ( iServerMaxObjectSize == 0 || aSize <= iServerMaxObjectSize )
       
   502 			{
       
   503 			User::RequestComplete( iCallerStatus, KErrNone );
       
   504 			}
       
   505 		else
       
   506 			{
       
   507 			User::RequestComplete( iCallerStatus, KErrTooBig );
       
   508 			}
       
   509 		}
       
   510 	else
       
   511 		{
       
   512 		User::RequestComplete( iCallerStatus, err );
       
   513 		}
       
   514 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoOpenItemL(): end");
       
   515 	}
       
   516 
       
   517 // ----------------------------------------------------------------------------
       
   518 // void CNSmlNotepadDataStore::DoCreateItemL()
       
   519 // ----------------------------------------------------------------------------
       
   520 void CNSmlNotepadDataStore::DoCreateItemL( TSmlDbItemUid& aUid, 
       
   521 											TInt aSize, 
       
   522 											TSmlDbItemUid /*aParent*/, 
       
   523 											const TDesC8& aMimeType, 
       
   524 											const TDesC8& aMimeVer, 
       
   525 											TRequestStatus& aStatus )
       
   526 	{
       
   527 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCreateItemL(): begin");
       
   528 	TBuf8<255> lMimeType;
       
   529 	lMimeType.Copy(aMimeType);
       
   530 	TBuf8<255> lMimeVer;
       
   531 	lMimeVer.Copy(aMimeVer);
       
   532 	iCallerStatus = &aStatus;
       
   533 	*iCallerStatus = KRequestPending;
       
   534 	iAddItemId = &aUid;
       
   535 	if ( iState != ENSmlOpenAndWaiting )
       
   536 		{
       
   537 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
   538 		return;
       
   539 		}
       
   540 	if ( iOwnStoreFormat->HasMaxSize() )
       
   541 		{
       
   542 		if ( iOwnStoreFormat->MaxSize() < aSize )
       
   543 			{
       
   544 			User::RequestComplete( iCallerStatus, KErrTooBig );
       
   545 			return;
       
   546 			}
       
   547 		}
       
   548 	if( SysUtil::DiskSpaceBelowCriticalLevelL( &iRfs, aSize, iDrive ) )
       
   549 		{
       
   550 		User::RequestComplete( iCallerStatus, KErrDiskFull );
       
   551 		return;
       
   552 		}
       
   553 	TBuf8<255> lOwnMimeType;
       
   554 	lOwnMimeType.Copy(iOwnStoreFormat->MimeFormat( 0 ).MimeType().DesC());
       
   555 	TBuf8<255> lOwnMimeVer;
       
   556 	lOwnMimeVer.Copy(iOwnStoreFormat->MimeFormat( 0 ).MimeVersion().DesC());	
       
   557 	if ( 	(iOwnStoreFormat->MimeFormat( 0 ).MimeType().DesC().Compare( aMimeType ) != 0 ) )
       
   558 		{
       
   559 		User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   560 		_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCreateItemL - KErrNotSupported: END");
       
   561 		return;
       
   562 		}	
       
   563 	if ( iItemData )
       
   564 		{
       
   565 		delete iItemData;
       
   566 		iItemData = NULL;
       
   567 		}
       
   568 	iItemData = CBufFlat::NewL( KNSmlItemDataExpandSize );
       
   569 	iWriterPosition  = 0;
       
   570 	iItemSize = aSize; // Size sent from server
       
   571 	iState = ENSmlItemCreating;
       
   572 	User::RequestComplete( iCallerStatus, KErrNone );
       
   573 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCreateItemL(): end");
       
   574 	}
       
   575 
       
   576 // ----------------------------------------------------------------------------
       
   577 // void CNSmlNotepadDataStore::DoReplaceItemL()
       
   578 // ----------------------------------------------------------------------------
       
   579 void CNSmlNotepadDataStore::DoReplaceItemL( TSmlDbItemUid aUid, 
       
   580 											 TInt aSize, 
       
   581 											 TSmlDbItemUid /*aParent*/, 
       
   582 											 TBool aFieldChange, 
       
   583 											 TRequestStatus& aStatus )
       
   584 	{
       
   585 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemL(): begin");
       
   586 	iCallerStatus = &aStatus;
       
   587 	*iCallerStatus = KRequestPending;
       
   588 	if ( iState != ENSmlOpenAndWaiting )
       
   589 		{
       
   590 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
   591 		return;
       
   592 		}
       
   593 	if ( iOwnStoreFormat->HasMaxSize() )
       
   594 		{
       
   595 		if ( iOwnStoreFormat->MaxSize() < aSize )
       
   596 			{
       
   597 			User::RequestComplete( iCallerStatus, KErrTooBig );
       
   598 			return;
       
   599 			}
       
   600 		}
       
   601 	if( aFieldChange )
       
   602 		{
       
   603 		User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   604 		return;
       
   605 		}
       
   606 	if( SysUtil::DiskSpaceBelowCriticalLevelL( &iRfs, aSize, iDrive ) )
       
   607 		{
       
   608 		User::RequestComplete(iCallerStatus, KErrDiskFull);
       
   609 		return;
       
   610 		}
       
   611 	TInt err = KErrNone;
       
   612 	iUid      = aUid;
       
   613 	iItemSize = aSize;
       
   614 	iWriterPosition  = 0;
       
   615 	if ( iItemData )
       
   616 		{
       
   617 		delete iItemData;
       
   618 		iItemData = NULL;
       
   619 		}
       
   620 	iItemData = CBufFlat::NewL( KNSmlItemDataExpandSize );	
       
   621 	CNpdItem *note;
       
   622     note = CNpdItem::NewLC();
       
   623     TInt itemFound;
       
   624     TRAP(itemFound,iNpdDb->GetNoteL(aUid, *note));	
       
   625 	CleanupStack::PopAndDestroy(note);//for note
       
   626 	if ( itemFound != KErrNone)
       
   627 		{
       
   628 		delete iItemData;
       
   629 		iItemData = NULL;
       
   630 		
       
   631 		User::RequestComplete( iCallerStatus, KErrNotFound );
       
   632 		return;
       
   633 		}
       
   634 	iReplaceItemId = aUid;
       
   635 	iState = ENSmlItemUpdating;
       
   636 	User::RequestComplete( iCallerStatus, err );
       
   637 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemL(): end");
       
   638 	}
       
   639 // ----------------------------------------------------------------------------
       
   640 // void CNSmlNotepadDataStore::DoReadItemL()
       
   641 // ----------------------------------------------------------------------------
       
   642 void CNSmlNotepadDataStore::DoReadItemL( TDes8& aBuffer )
       
   643 	{
       
   644 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReadItemL(): begin");
       
   645     if ( iState != ENSmlItemOpen || !iItemData )
       
   646         {
       
   647         iReaderPosition  = -1;
       
   648         User::Leave( KErrNotReady );
       
   649         }
       
   650     if ( iReaderPosition  == -1 )
       
   651         {
       
   652         User::Leave( KErrEof );
       
   653         }
       
   654 
       
   655 	if ( aBuffer.MaxSize() <= iItemData->Size() )
       
   656 		{
       
   657 		//iItemData->Read( 0, aBuffer );
       
   658 		//iReaderPosition  = -1;
       
   659 		if (iReaderPosition < (iItemData->Size() - iReaderPosition))
       
   660 		    {
       
   661 			iItemData->Read( iReaderPosition , aBuffer);
       
   662 			iReaderPosition  = iReaderPosition  + aBuffer.Size();
       
   663 		    }
       
   664 		else
       
   665 		    {
       
   666 			if (aBuffer.MaxLength() <= (iItemData->Size() - iReaderPosition))
       
   667 			{
       
   668             iItemData->Read( iReaderPosition , aBuffer, aBuffer.MaxLength() );
       
   669 			}
       
   670 			else
       
   671 			{
       
   672             iItemData->Read( iReaderPosition , aBuffer, (iItemData->Size() - iReaderPosition) );
       
   673 			}
       
   674 			iReaderPosition  = -1;
       
   675 		    }
       
   676 		}
       
   677 	else
       
   678 		{
       
   679 		TInt length = iItemData->Size() - iReaderPosition;
       
   680 		if ( aBuffer.Size() <= length )
       
   681 			{
       
   682 			iItemData->Read( iReaderPosition , aBuffer );
       
   683 			iReaderPosition  = -1;
       
   684 			}
       
   685 		else
       
   686 			{
       
   687 			iItemData->Read( iReaderPosition , aBuffer, aBuffer.Size() );
       
   688 			iReaderPosition  = iReaderPosition  + aBuffer.Size();
       
   689 			}
       
   690 		}
       
   691 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReadItemL(): end");
       
   692 	}
       
   693 
       
   694 // ----------------------------------------------------------------------------
       
   695 // void CNSmlNotepadDataStore::DoWriteItemL()
       
   696 // ----------------------------------------------------------------------------
       
   697 void CNSmlNotepadDataStore::DoWriteItemL( const TDesC8& aData )
       
   698 	{
       
   699 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoWriteItemL(): begin");
       
   700 	if ( iState == ENSmlItemCreating || iState == ENSmlItemUpdating )
       
   701 		{
       
   702 		TInt totalSize = aData.Size() + iItemData->Size();
       
   703 		if( SysUtil::DiskSpaceBelowCriticalLevelL( &iRfs, totalSize, iDrive ) )
       
   704 			{
       
   705 			User::RequestComplete(iCallerStatus, KErrDiskFull);
       
   706 			return;
       
   707 			}
       
   708 		if ( iItemData )
       
   709 			{
       
   710 			if ( iWriterPosition  == -1 )
       
   711 				{
       
   712 				User::Leave( KErrEof );
       
   713 				}
       
   714 			iItemData->InsertL( iWriterPosition , aData );
       
   715 			iWriterPosition  = aData.Size();
       
   716 			return;
       
   717 			}
       
   718 		}
       
   719 	User::Leave( KErrNotReady );
       
   720 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoWriteItemL(): end");
       
   721 	}
       
   722 
       
   723 // ----------------------------------------------------------------------------
       
   724 // void CNSmlNotepadDataStore::DoCommitItemL()
       
   725 // ----------------------------------------------------------------------------
       
   726 void CNSmlNotepadDataStore::DoCommitItemL( TRequestStatus& aStatus )
       
   727 	{
       
   728 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitItemL(): begin");	
       
   729 	iCallerStatus = &aStatus;
       
   730 	*iCallerStatus = KRequestPending;
       
   731 	if ( iState == ENSmlItemCreating || iState == ENSmlItemUpdating )
       
   732 		{
       
   733 		if ( !iItemData )
       
   734 			{
       
   735 			User::RequestComplete( iCallerStatus, KErrNotReady );
       
   736 			return;
       
   737 			}
       
   738 		}
       
   739 	else
       
   740 		{
       
   741 		User::RequestComplete(iCallerStatus, KErrNotReady );
       
   742 		return;
       
   743 		}
       
   744 	iItemData->Compress();
       
   745 	TInt err(KErrNone);
       
   746 	if ( iState == ENSmlItemCreating )
       
   747 		{
       
   748 		iState = ENSmlClosed; 
       
   749 		TSmlDbItemUid newUid; 
       
   750 		err = DoAddItemToDataBaseL( *iItemData,newUid);
       
   751 		*iAddItemId = newUid;
       
   752 		}
       
   753 	else // ENSmlItemUpdating
       
   754 		{
       
   755 		iState = ENSmlClosed; 
       
   756 		err = DoReplaceItemAtDataBaseL( *iItemData, iReplaceItemId );
       
   757 		}
       
   758 	iReplaceItemId = -1;
       
   759 	iWriterPosition  = -1;
       
   760 	iState = ENSmlOpenAndWaiting;
       
   761 	if ( iItemData )
       
   762 		{
       
   763 		delete iItemData;
       
   764 		iItemData = NULL;
       
   765 		}
       
   766 	User::RequestComplete( iCallerStatus, err);
       
   767 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitItemL(): end");
       
   768 	}
       
   769 
       
   770 // ----------------------------------------------------------------------------
       
   771 // void CNSmlNotepadDataStore::DoCloseItem()
       
   772 // ----------------------------------------------------------------------------
       
   773 void CNSmlNotepadDataStore::DoCloseItem()
       
   774 	{
       
   775 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCloseItem(): begin");
       
   776 	if ( iState == ENSmlItemOpen )
       
   777 		{
       
   778 		iReaderPosition  = -1;
       
   779 		if ( iItemData )
       
   780 			{
       
   781 			delete iItemData;
       
   782 			iItemData = NULL;
       
   783 			}
       
   784 		iState = ENSmlOpenAndWaiting;
       
   785 		}
       
   786 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCloseItem(): end");
       
   787 	}
       
   788 
       
   789 
       
   790 // ----------------------------------------------------------------------------
       
   791 // void CNSmlNotepadDataStore::DoMoveItemL()
       
   792 // ----------------------------------------------------------------------------
       
   793 void CNSmlNotepadDataStore::DoMoveItemL( TSmlDbItemUid /*aUid*/, 
       
   794 										TSmlDbItemUid /*aNewParent*/, 
       
   795 										TRequestStatus& aStatus )
       
   796 	{
       
   797 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMoveItemL(): begin");
       
   798 	iCallerStatus = &aStatus;
       
   799 	*iCallerStatus = KRequestPending;
       
   800 	if ( iState != ENSmlOpenAndWaiting )
       
   801 		{
       
   802 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
   803 		return;
       
   804 		}
       
   805 	User::RequestComplete( iCallerStatus, KErrNotSupported );
       
   806 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMoveItemL(): end");
       
   807 	}
       
   808 
       
   809 // ----------------------------------------------------------------------------
       
   810 // void CNSmlNotepadDataStore::DoDeleteItemL()
       
   811 // ----------------------------------------------------------------------------
       
   812 void CNSmlNotepadDataStore::DoDeleteItemL( TSmlDbItemUid aUid, TRequestStatus& aStatus )
       
   813 	{
       
   814 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoDeleteItemL(): begin");
       
   815 	iCallerStatus = &aStatus;
       
   816 	*iCallerStatus = KRequestPending;
       
   817 	if ( iState != ENSmlOpenAndWaiting ) 
       
   818 		{
       
   819 		// Items cannot be deleted if some of them is opened
       
   820 		User::RequestComplete( iCallerStatus, KErrNotReady );
       
   821 		return;
       
   822 		}
       
   823 	iItemData = CBufFlat::NewL( KNSmlItemDataExpandSize );
       
   824 	CNpdItem *note;
       
   825     note = CNpdItem::NewLC();
       
   826     TInt itemFound;
       
   827     TRAP(itemFound,iNpdDb->GetNoteL(aUid, *note));	
       
   828     CleanupStack::PopAndDestroy(note); // for note
       
   829 	if( itemFound != KErrNone)
       
   830 		{
       
   831 		delete iItemData;
       
   832 		iItemData = NULL;
       
   833 		
       
   834 		User::RequestComplete( iCallerStatus, KErrNotFound );
       
   835 		return;
       
   836 		}
       
   837 	TRAPD( err, iNpdDb->DeleteNoteL( aUid ) );
       
   838 	if( err == KErrNone )
       
   839 		{
       
   840 			if(iChangeFinder)
       
   841 			{
       
   842             iChangeFinder->ItemDeleted( TNSmlSnapshotItem( aUid ) );
       
   843 			}
       
   844 		}
       
   845 	User::RequestComplete(iCallerStatus, err);
       
   846 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoDeleteItemL(): end");
       
   847 	}
       
   848 
       
   849 // ----------------------------------------------------------------------------
       
   850 // void CNSmlNotepadDataStore::DoSoftDeleteItemL()
       
   851 // ----------------------------------------------------------------------------
       
   852 void CNSmlNotepadDataStore::DoSoftDeleteItemL( TSmlDbItemUid aUid, TRequestStatus& aStatus )
       
   853 	{
       
   854 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSoftDeleteItemL(): begin");
       
   855 	DoDeleteItemL( aUid,aStatus );
       
   856 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSoftDeleteItemL(): end");
       
   857 	}
       
   858 
       
   859 // ----------------------------------------------------------------------------
       
   860 // void CNSmlNotepadDataStore::DoDeleteAllItemsL()
       
   861 // ----------------------------------------------------------------------------
       
   862 void CNSmlNotepadDataStore::DoDeleteAllItemsL( TRequestStatus& aStatus )
       
   863 	{
       
   864 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoDeleteAllItemsL(): begin");
       
   865 	iCallerStatus = &aStatus;
       
   866 	*iCallerStatus = KRequestPending;
       
   867 	if ( iState != ENSmlOpenAndWaiting ) 
       
   868 		{
       
   869 		// Items cannot be deleted if they are opened.
       
   870 		User::RequestComplete(iCallerStatus, KErrNotReady);
       
   871 		return;
       
   872 		}
       
   873 	iNpdDb->ResetL();
       
   874 	if( iChangeFinder )
       
   875 		{
       
   876 		iChangeFinder->ResetL();		
       
   877 		}
       
   878 	iSnapshotRegistered = EFalse;
       
   879 	User::RequestComplete(iCallerStatus, KErrNone);
       
   880 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoDeleteAllItemsL(): end");
       
   881 	}
       
   882 
       
   883 // ----------------------------------------------------------------------------
       
   884 // TBool CNSmlNotepadDataStore::DoHasSyncHistory()
       
   885 // ----------------------------------------------------------------------------
       
   886 TBool CNSmlNotepadDataStore::DoHasSyncHistory() const
       
   887 	{
       
   888 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoHasSyncHistory(): begin");
       
   889 	TBool doHasSyncHistory = EFalse;
       
   890 	if( !iDataBaseOpened ) 
       
   891 		{
       
   892 		_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoHasSyncHistory(): NOT OPEN end");
       
   893 		return doHasSyncHistory;
       
   894 		}
       
   895 
       
   896 	if( iSyncHistory )
       
   897 		{
       
   898 		if ( iOpenedStoreId != iChangeFinder->DataStoreUid() )
       
   899 			{
       
   900 			iChangeFinder->SetDataStoreUid( iOpenedStoreId );
       
   901 			doHasSyncHistory = EFalse;
       
   902 			}
       
   903 		else
       
   904 			{
       
   905             doHasSyncHistory = ETrue;
       
   906 			}
       
   907 		}
       
   908 	else
       
   909 		{
       
   910 		iChangeFinder->SetDataStoreUid( iOpenedStoreId );
       
   911 		}
       
   912 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoHasSyncHistory(): end");
       
   913 	return doHasSyncHistory;
       
   914 	}
       
   915 
       
   916 // ----------------------------------------------------------------------------
       
   917 // void CNSmlNotepadDataStore::DoAddedItems()
       
   918 // ----------------------------------------------------------------------------
       
   919 const MSmlDataItemUidSet& CNSmlNotepadDataStore::DoAddedItems() const
       
   920 	{
       
   921 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddedItems(): begin");
       
   922 	if ( iState == ENSmlOpenAndWaiting )
       
   923 		{
       
   924 		iNewUids->Reset();
       
   925 		if( iChangeFinder )
       
   926 			{
       
   927 			TRAPD( err,iChangeFinder->FindNewItemsL( *iNewUids ) );
       
   928 			if( err != KErrNone )
       
   929 				{
       
   930 				_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddedItems(): LEAVE end");				
       
   931 				}
       
   932 			}
       
   933 		}	
       
   934 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddedItems(): end");
       
   935 	return *iNewUids;
       
   936 	}
       
   937 
       
   938 // ----------------------------------------------------------------------------
       
   939 // void CNSmlNotepadDataStore::DoDeletedItems()
       
   940 // ----------------------------------------------------------------------------
       
   941 const MSmlDataItemUidSet& CNSmlNotepadDataStore::DoDeletedItems() const
       
   942 	{
       
   943 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoDeletedItems(): begin");	
       
   944 	if ( iState == ENSmlOpenAndWaiting )
       
   945 		{
       
   946 		iDeletedUids->Reset();
       
   947 		if( iChangeFinder )
       
   948 			{
       
   949 			TRAPD(err,iChangeFinder->FindDeletedItemsL( *iDeletedUids ) );
       
   950 			if( err != KErrNone )
       
   951 				{
       
   952 				_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoDeletedItems(): LEAVE end");				
       
   953 				}
       
   954 			}
       
   955 		}
       
   956 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoDeletedItems(): end");
       
   957 	return *iDeletedUids;
       
   958 	}
       
   959 
       
   960 // ----------------------------------------------------------------------------
       
   961 // void CNSmlNotepadDataStore::DoSoftDeletedItems()
       
   962 // ----------------------------------------------------------------------------
       
   963 const MSmlDataItemUidSet& CNSmlNotepadDataStore::DoSoftDeletedItems() const
       
   964 	{
       
   965 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSoftDeletedItems(): begin");
       
   966 	if ( iState == ENSmlOpenAndWaiting )
       
   967 		{
       
   968 		iSoftDeletedUids->Reset();
       
   969 		if( iChangeFinder )
       
   970 			{
       
   971 			TRAPD( err, iChangeFinder->FindSoftDeletedItemsL( *iSoftDeletedUids ) );
       
   972 			if( err != KErrNone )
       
   973 				{
       
   974 				_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSoftDeletedItems(): LEAVE end");				
       
   975 				}
       
   976 			}
       
   977 		}
       
   978 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoSoftDeletedItems: END");
       
   979 	return *iSoftDeletedUids;
       
   980 	}
       
   981 
       
   982 // ----------------------------------------------------------------------------
       
   983 // void CNSmlNotepadDataStore::DoModifiedItems()
       
   984 // ----------------------------------------------------------------------------
       
   985 const MSmlDataItemUidSet& CNSmlNotepadDataStore::DoModifiedItems() const
       
   986 	{
       
   987 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoModifiedItems(): begin");
       
   988 	if ( iState == ENSmlOpenAndWaiting )
       
   989 		{
       
   990 		iReplacedUids->Reset();
       
   991 		if( iChangeFinder )
       
   992 			{
       
   993 			TRAPD( err, iChangeFinder->FindChangedItemsL( *iReplacedUids ) );
       
   994 			if( err != KErrNone )
       
   995 				{
       
   996                 _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoModifiedItems(): LEAVE end");				
       
   997 				}
       
   998 			}
       
   999 		}
       
  1000 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoModifiedItems: END");
       
  1001 	return *iReplacedUids;
       
  1002 	}
       
  1003 
       
  1004 // ----------------------------------------------------------------------------
       
  1005 // void CNSmlNotepadDataStore::DoMovedItems()
       
  1006 // ----------------------------------------------------------------------------
       
  1007 const MSmlDataItemUidSet& CNSmlNotepadDataStore::DoMovedItems() const
       
  1008 	{
       
  1009 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMovedItems(): begin");
       
  1010 	if ( iState == ENSmlOpenAndWaiting )
       
  1011 		{
       
  1012 		iMovedUids->Reset();
       
  1013 		if( iChangeFinder )
       
  1014 			{
       
  1015 			TRAPD( err, iChangeFinder->FindMovedItemsL( *iMovedUids ) );	
       
  1016 			if( err != KErrNone )
       
  1017 				{
       
  1018                 _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMovedItems(): LEAVE end");				
       
  1019 				}
       
  1020 			}
       
  1021 		}
       
  1022 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoMovedItems: END");
       
  1023 	return *iMovedUids;
       
  1024 	}
       
  1025 
       
  1026 // ----------------------------------------------------------------------------
       
  1027 // void CNSmlNotepadDataStore::DoResetChangeInfoL()
       
  1028 // ----------------------------------------------------------------------------
       
  1029 void CNSmlNotepadDataStore::DoResetChangeInfoL( TRequestStatus& aStatus )
       
  1030 	{
       
  1031 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoResetChangeInfoL(): begin");
       
  1032 	iCallerStatus = &aStatus;
       
  1033 	*iCallerStatus = KRequestPending;
       
  1034 	if( iChangeFinder )
       
  1035 		{
       
  1036 		iChangeFinder->ResetL();
       
  1037 		}
       
  1038 	iSnapshotRegistered = EFalse;
       
  1039 	TInt err  = FetchModificationsL();
       
  1040     User::RequestComplete( iCallerStatus, err );
       
  1041 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoResetChangeInfoL(): end");
       
  1042 	}
       
  1043 
       
  1044 // ----------------------------------------------------------------------------
       
  1045 // void CNSmlNotepadDataStore::DoCommitChangeInfoL()
       
  1046 // ----------------------------------------------------------------------------
       
  1047 void CNSmlNotepadDataStore::DoCommitChangeInfoL( TRequestStatus& aStatus, 
       
  1048 												  const MSmlDataItemUidSet& aItems )
       
  1049 	{
       
  1050 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitChangeInfoL(aItems): begin");
       
  1051 	iCallerStatus = &aStatus;
       
  1052 	*iCallerStatus = KRequestPending;
       
  1053 	if( iChangeFinder )
       
  1054 		{
       
  1055 		iChangeFinder->CommitChangesL( aItems );
       
  1056 		User::RequestComplete( iCallerStatus, KErrNone );
       
  1057 		_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitChangeInfoL(aItems):success end");
       
  1058 		return;
       
  1059 		}
       
  1060 	User::RequestComplete( iCallerStatus, KErrNotReady );
       
  1061 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitChangeInfoL(aItems): end");
       
  1062 	}
       
  1063 
       
  1064 // ----------------------------------------------------------------------------
       
  1065 // void CNSmlNotepadDataStore::DoCommitChangeInfoL()
       
  1066 // ----------------------------------------------------------------------------
       
  1067 void CNSmlNotepadDataStore::DoCommitChangeInfoL( TRequestStatus& aStatus )
       
  1068 	{
       
  1069 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitChangeInfoL(): begin");
       
  1070 	iCallerStatus = &aStatus;
       
  1071 	*iCallerStatus = KRequestPending;
       
  1072 	if( iChangeFinder )
       
  1073 		{
       
  1074 		iChangeFinder->CommitChangesL();
       
  1075 		User::RequestComplete( iCallerStatus, KErrNone );
       
  1076 		_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitChangeInfoL(): success end");
       
  1077 		return;
       
  1078 		}
       
  1079 	User::RequestComplete( iCallerStatus, KErrNotReady );
       
  1080 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoCommitChangeInfoL(): end");
       
  1081 	}
       
  1082 
       
  1083 // ----------------------------------------------------------------------------
       
  1084 // CSmlDataStoreFormat* CNSmlNotepadDataStore::DoOwnStoreFormatL()
       
  1085 // ----------------------------------------------------------------------------
       
  1086 CSmlDataStoreFormat* CNSmlNotepadDataStore::DoOwnStoreFormatL()
       
  1087     {
       
  1088 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoOwnStoreFormatL(): begin");
       
  1089 	TFileName fileName;
       
  1090 	TParse parse;
       
  1091 	
       
  1092 	// Locate the resource file
       
  1093     TFileName dllFileName;
       
  1094     Dll::FileName( dllFileName );
       
  1095     
       
  1096     TFileName resourceFileName;
       
  1097     resourceFileName.Copy( TParsePtrC( dllFileName ).Drive() );  
       
  1098     resourceFileName.Append(KNSmlNotepadStoreFormatRsc);
       
  1099     
       
  1100     parse.Set( resourceFileName, &KDC_RESOURCE_FILES_DIR, NULL );
       
  1101 	fileName = parse.FullName();
       
  1102 	RResourceFile resourceFile; 
       
  1103 	BaflUtils::NearestLanguageFile( iRfs, fileName );
       
  1104 	resourceFile.OpenL( iRfs, fileName );
       
  1105 	CleanupClosePushL( resourceFile );
       
  1106 	HBufC8* notepadDataFormat = resourceFile.AllocReadLC( NSML_NOTEPAD_DATA_STORE_FORMAT );
       
  1107 	TResourceReader reader;
       
  1108 	reader.SetBuffer( notepadDataFormat );
       
  1109 	CSmlDataStoreFormat* lDsFormat = CSmlDataStoreFormat::NewLC( iStringPool, reader );
       
  1110 	CleanupStack::Pop(lDsFormat); // lDsFormat
       
  1111 	CleanupStack::PopAndDestroy(notepadDataFormat); // notepadDataFormat
       
  1112 	CleanupStack::PopAndDestroy();// resourceFile 
       
  1113 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoOwnStoreFormatL(): end");	
       
  1114 	return lDsFormat; 
       
  1115 	}
       
  1116 
       
  1117 // ----------------------------------------------------------------------------
       
  1118 // TInt CNSmlNotepadDataStore::OpenItemL(TSmlDbItemUid& aUid,CBufBase& aItem)
       
  1119 // ----------------------------------------------------------------------------
       
  1120 TInt CNSmlNotepadDataStore::OpenItemL(TSmlDbItemUid& aUid,CBufBase& aItem)
       
  1121     {
       
  1122 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::OpenItemL(): begin");
       
  1123 	TInt err = KErrNone;
       
  1124 	if(iNpdDb->IsOpen())
       
  1125         {
       
  1126         CNpdItem *note;
       
  1127         note = CNpdItem::NewLC();
       
  1128         TRAP(err,iNpdDb->GetNoteL(aUid, *note));
       
  1129         if(err == KErrNone)
       
  1130             {
       
  1131             _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore:: FetchItemL():  begin");
       
  1132             
       
  1133             HBufC8* buf = ConvertNoteToSyncItemL(*note);
       
  1134             CleanupStack::PushL(buf);
       
  1135             aItem.Reset();
       
  1136             // Write the buffer to the passed writeStream (aItem)
       
  1137             TBufBuf bufBuf;
       
  1138             bufBuf.Set(aItem, 0, TBufBuf::EWrite);
       
  1139             RWriteStream writeStream( &bufBuf);
       
  1140             writeStream.PushL();
       
  1141             writeStream.WriteL(buf->Des().Ptr(), buf->Des().Length());
       
  1142             writeStream.CommitL();
       
  1143             CleanupStack::PopAndDestroy(1);//writeStream
       
  1144             CleanupStack::PopAndDestroy(buf);//buf
       
  1145             _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::FetchItemL(): end");
       
  1146             }
       
  1147         CleanupStack::PopAndDestroy(note); // note
       
  1148         }
       
  1149     else
       
  1150         {
       
  1151         err = KErrNotReady;
       
  1152         }
       
  1153         
       
  1154 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::OpenItemL(): end");
       
  1155 	return err;
       
  1156 }
       
  1157 
       
  1158 // ----------------------------------------------------------------------------
       
  1159 // TInt CNSmlNotepadDataStore::ConvertSyncItemToNoteL(const TDesC8& aSyncItem, CNpdItem& aNote)
       
  1160 // ----------------------------------------------------------------------------
       
  1161 TInt CNSmlNotepadDataStore::ConvertSyncItemToNoteL(const TDesC8& aSyncItem, CNpdItem& aNote)
       
  1162     {
       
  1163     TInt err(KErrNone);
       
  1164     TTime gmt;
       
  1165     gmt.UniversalTime();
       
  1166     gmt.RoundUpToNextMinute();
       
  1167     gmt -= TTimeIntervalMinutes(1);
       
  1168     HBufC *note = NULL;
       
  1169     if(&aSyncItem)
       
  1170         {
       
  1171         RDesReadStream stream(aSyncItem);
       
  1172         CPlainText *text;
       
  1173         text = CPlainText::NewL();
       
  1174         CleanupStack::PushL(text);
       
  1175         CPlainText::TImportExportParam param;
       
  1176         param.iForeignEncoding = KCharacterSetIdentifierUtf8;
       
  1177         param.iOrganisation = CPlainText::EOrganiseByParagraph;
       
  1178         param.iGuessForeignEncoding = EFalse;
       
  1179         CPlainText::TImportExportResult result;
       
  1180         text->ImportTextL(0,stream,param,result);
       
  1181         stream.Close();
       
  1182         note = HBufC::NewL(result.iOutputChars);
       
  1183         CleanupStack::PushL(note);
       
  1184         TPtr tmpdes = note->Des();
       
  1185         text->Extract(tmpdes,0);
       
  1186         CleanupStack::Pop(note); 
       
  1187         CleanupStack::PopAndDestroy(text); 
       
  1188         }
       
  1189     aNote.SetContent(note); 
       
  1190     aNote.SetModified(gmt);
       
  1191     return err;
       
  1192     }
       
  1193 
       
  1194 // ----------------------------------------------------------------------------
       
  1195 // HBufC8* CNSmlNotepadDataStore::ConvertNoteToSyncItemL(CNpdItem& aNote)
       
  1196 // ----------------------------------------------------------------------------
       
  1197 HBufC8* CNSmlNotepadDataStore::ConvertNoteToSyncItemL(CNpdItem& aNote)
       
  1198     {
       
  1199     if (!aNote.Content())
       
  1200         {
       
  1201         HBufC8* tempHBuf = HBufC8::NewL(KNullCharLen);//room for null character
       
  1202         return tempHBuf;
       
  1203         }
       
  1204     // Note: a single unicode character can be 4 bytes long in UTF8 format,
       
  1205     // hence the long length for 8-bit buffer.
       
  1206     TInt length = (aNote.Content()->Length()) * KUTF8Lenght;
       
  1207     HBufC8* tempHBuf = HBufC8::NewLC(length + KNullCharLen);
       
  1208     CPlainText *text = CPlainText::NewL();
       
  1209     CleanupStack::PushL(text);
       
  1210     text->InsertL(0,aNote.Content()->Des());
       
  1211     CPlainText::TImportExportResult result;
       
  1212     CPlainText::TImportExportParam param;
       
  1213     param.iForeignEncoding = KCharacterSetIdentifierUtf8;
       
  1214     param.iOrganisation = CPlainText::EOrganiseByParagraph;
       
  1215     param.iGuessForeignEncoding = EFalse;
       
  1216     param.iMaxOutputChars = length;
       
  1217     TPtr8 ptr = tempHBuf->Des();
       
  1218     RDesWriteStream stream(ptr);
       
  1219     text->ExportTextL(0,stream,param,result);
       
  1220     stream.Close();
       
  1221     CleanupStack::PopAndDestroy(text);
       
  1222     CleanupStack::Pop(tempHBuf);//pop the HBufC8 temp 
       
  1223     return (tempHBuf);
       
  1224     }
       
  1225 
       
  1226 // ----------------------------------------------------------------------------
       
  1227 // TInt CNSmlNotepadDataStore::DoAddItemToDataBaseL(CBufBase& aItem)
       
  1228 // ----------------------------------------------------------------------------
       
  1229 TInt CNSmlNotepadDataStore::DoAddItemToDataBaseL(CBufBase& aItem,TInt& aUid)
       
  1230 {
       
  1231 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddItemToDataBaseL: begin");
       
  1232 	TInt lKey = -1;
       
  1233 	TInt length = aItem.Size();
       
  1234     HBufC8* buf = HBufC8::NewLC(length*KUnicodeSize + KNullCharLen);
       
  1235     buf->Des().Copy(aItem.Ptr(0));
       
  1236     TPtr8 lPtr(buf->Des());
       
  1237     CNpdItem *note;
       
  1238     note = CNpdItem::NewLC();
       
  1239     TInt err(KErrNone);
       
  1240    	err = ConvertSyncItemToNoteL(*buf, *note);       
       
  1241     if(err != KErrNone)
       
  1242     {
       
  1243     	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddItemToDataBaseL: conversion error");
       
  1244     }
       
  1245     else
       
  1246     {
       
  1247 	    _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddItemToDataBaseL: before adding to notepad db");
       
  1248 	    TRAP(err,iNpdDb->AddNoteL(*note,lKey));
       
  1249 		if(err!=KErrNone)
       
  1250             {
       
  1251             _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddItemToDataBaseL: Addnote error");	
       
  1252             }
       
  1253 		else
       
  1254             {
       
  1255             TNSmlSnapshotItem item( lKey );
       
  1256             TTime lLastModified = note->LastModified();
       
  1257             item.SetLastChangedDate(lLastModified);
       
  1258             item.SetSoftDelete( EFalse );
       
  1259             iChangeFinder->ItemAddedL( item ); 
       
  1260             }
       
  1261     }
       
  1262     aUid = lKey;
       
  1263     _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoAddItemToDataBaseL: after adding to notepad db");
       
  1264 	CleanupStack::PopAndDestroy(note);//note
       
  1265 	CleanupStack::PopAndDestroy(buf);//buf
       
  1266 	return err;
       
  1267 }
       
  1268 // ----------------------------------------------------------------------------
       
  1269 // void CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL(CBufBase& aItem, TInt& aUid)
       
  1270 // ----------------------------------------------------------------------------
       
  1271 TInt CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL(CBufBase& aItem, TInt& aUid)
       
  1272 {
       
  1273 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL: begin");
       
  1274 	TInt length = aItem.Size();
       
  1275     HBufC8* buf = HBufC8::NewLC(length*KUnicodeSize + KNullCharLen);
       
  1276     buf->Des().Copy(aItem.Ptr(0));
       
  1277     TPtr8 lPtr(buf->Des());
       
  1278     CNpdItem *note;
       
  1279     note = CNpdItem::NewLC();
       
  1280     TInt err(KErrNone);
       
  1281     err = ConvertSyncItemToNoteL(*buf, *note);       
       
  1282     if(err != KErrNone)
       
  1283     {
       
  1284     	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL: conversion error");
       
  1285     }
       
  1286     else
       
  1287     {
       
  1288     	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL: before updating into notepad db");
       
  1289     	TRAP(err,iNpdDb->UpdateNoteL(aUid,*note));
       
  1290     	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL: after updating into notepad db");
       
  1291 		if(err!=KErrNone)
       
  1292 		{
       
  1293     		_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL: updating error");	
       
  1294     	}
       
  1295     	else
       
  1296     	{
       
  1297     		TNSmlSnapshotItem item( aUid );
       
  1298 			item.SetLastChangedDate( note->LastModified() );
       
  1299 			item.SetSoftDelete( EFalse );
       
  1300 			iChangeFinder->ItemUpdatedL( item );
       
  1301 		}
       
  1302     }
       
  1303     CleanupStack::PopAndDestroy(note);//note
       
  1304     CleanupStack::PopAndDestroy(buf); //buf
       
  1305 	_NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::DoReplaceItemAtDataBaseL: end");
       
  1306 	return err;
       
  1307 }
       
  1308 
       
  1309 // ------------------------------------------------------------------------------------------------
       
  1310 // CNSmlNotepadDataStore::DriveBelowCriticalLevelL()
       
  1311 // ------------------------------------------------------------------------------------------------
       
  1312 TBool CNSmlNotepadDataStore::DriveBelowCriticalLevelL( TInt aSize )
       
  1313 	{
       
  1314 		return SysUtil::DiskSpaceBelowCriticalLevelL( &iRfs, aSize, iDrive );
       
  1315 	}
       
  1316 
       
  1317 // ----------------------------------------------------------------------------
       
  1318 // TInt CNSmlNotepadDataStore::FetchModificationsL()
       
  1319 // ----------------------------------------------------------------------------
       
  1320 TInt CNSmlNotepadDataStore::FetchModificationsL()
       
  1321     {
       
  1322     _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::FetchModificationsL: begin");
       
  1323     TInt err( KErrNone );
       
  1324     CArrayPtr<CNpdItem>* arrNpdItem = NULL;
       
  1325     arrNpdItem = iNpdDb->FetchItemsLC();
       
  1326     
       
  1327     if( !iSnapshot )
       
  1328         {
       
  1329         _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::FetchModificationsL: inside if(!isnapshot)");
       
  1330         iSnapshot = new (ELeave) CArrayFixSeg<TNSmlSnapshotItem>( 8 );
       
  1331         }
       
  1332     
       
  1333     if( !iSnapshotRegistered )
       
  1334         {
       
  1335         _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::FetchModificationsL: inside if(!iSnapshotRegistered)");
       
  1336         for( TInt count = 0; count < arrNpdItem->Count(); count++ )
       
  1337             {
       
  1338             _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::FetchModificationsL: inside for before snap");
       
  1339             TNSmlSnapshotItem snap( arrNpdItem->At( count )->Key() );
       
  1340             snap.SetLastChangedDate( arrNpdItem->At( count )->LastModified() );
       
  1341             _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::FetchModificationsL: inside for before insertisql");
       
  1342             iSnapshot->InsertIsqL( snap, iKey );       
       
  1343             _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::FetchModificationsL: inside for after insertisql");
       
  1344             }
       
  1345         _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::FetchModificationsL: for loop ends");
       
  1346         iSnapshot->Compress();
       
  1347         iChangeFinder->SetNewSnapshot( iSnapshot ); // changefinder takes ownership
       
  1348         iSnapshot = NULL;
       
  1349         iSnapshotRegistered = ETrue;
       
  1350         }
       
  1351     _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::FetchModificationsL: before popanddestroy arrnpditem");
       
  1352     CleanupStack::PopAndDestroy(arrNpdItem);
       
  1353     _NOTEPAD_DBG_FILE("CNSmlNotepadDataStore::FetchModificationsL: end");
       
  1354     return err;
       
  1355     }
       
  1356 
       
  1357 // End of File